﻿# nf-conditions {#conditions}

Condition groups use OR between groups and AND between their rules. Supports add/remove, condition/rule limits, nested validation paths and all nf-rules options.

## Examples {#examples}

### Add conditions {#example-1}

### Add conditions
```vue
<template>
  <NfConfigProvider theme="chalk" locale="en-US">
    <div>
      <NfButton @click='getCondition'>Get condition data</NfButton>
      <NfButton @click='validate'>Validate conditions</NfButton>
      <ElDivider>AND + all rule types</ElDivider>
       <NfConditions
          ref='condition1'

          @ruleChange='ruleChange'
          @reduceRule='reduceRule'
          @addRule='addRule'
          @reduceCondition='reduceCondition'
          @addCondition='addCondition'

          :conditions='plan1.conditions'
        />

      <ElDivider>OR + available rule types</ElDivider>
       <NfConditions
          @ruleChange='ruleChange'
          @reduceRule='reduceRule'
          @addRule='addRule'
          @reduceCondition='reduceCondition'
          @addCondition='addCondition'

          logic-merge-desc='or'
          :show-rule-types="['意向标签', '个性标签']"
        />

      <ElDivider>Default type for new rules</ElDivider>
       <NfConditions
        :default-add-type='规则类型.外呼次数'
        @addRule='addRule'
        @reduceRule='reduceRule'
      />

      <ElDivider>Default data for new rules</ElDivider>
       <NfConditions
        :default-add-data='addData'
         @addRule='addRule'
        @reduceRule='reduceRule'
      />
      <output aria-live="polite">{{ validation }}</output>
      <pre v-if="result">{{ result }}</pre>
    </div>
  </NfConfigProvider>
</template>

<script lang='ts' setup>
import { ref } from 'vue'
import { NfConfigProvider, NfButton } from '@94ai-plus/common-ui'
import { ElDivider } from 'element-plus'
import { NfConditions } from '@94ai-plus/nf-conditions'
import { 规则类型, 操作类型, 计算类型, 日期类型, 外呼状态, type RuleItem } from '@94ai-plus/nf-rules'
interface ConditionItem {
  rules: RuleItem[],
  key?: string
}
const rulesData1 = ref<RuleItem[]>([
    {
        "type": 规则类型.意向标签,
        "operator": 操作类型.等于,
        "intentionTags": []
    },
    {
        "type": 规则类型.个性标签,
        "tags": "",
        "calculationMethod": 计算类型.累计,
         "number": undefined,
        "operator": 操作类型.大于等于
    },
    {
        "type": 规则类型.回复关键词,
        "keywords": "",
        "calculationMethod": 计算类型.累计,
        "operator": 操作类型.大于等于,
        "number": undefined
    },
    {
        "type": 规则类型.自定义标签,
        "operator": 操作类型.等于,
        "tags": ""
    },
    {
        "type": 规则类型.导入时间,
        "timeType": 日期类型.当天,
        "time": undefined
    },
    {
        "type": 规则类型.外呼时间,
        "timeType": 日期类型.非当天,
         "date": undefined
    },
    {
        "type": 规则类型.当前时间,
        "time": undefined
    },
    {
        "type": 规则类型.通话时长,
        "operator": 操作类型.大于等于,
         "bill": undefined
    },
    {
        "type": 规则类型.对话轮次,
        "operator": 操作类型.大于等于,
         "number": undefined
    },
    {
        "type": 规则类型.外呼次数,
        "operator": 操作类型.等于,
         "number": undefined
    },
    {
        "type": 规则类型.参数,
        "paramName": "",
        "operator": 操作类型.等于,
        "tags": "",
    },
    {
        "type": 规则类型.外呼状态,
        "operator": 操作类型.等于,
        "states": [],
    }
]);

const plan1 = ref({
  conditions: [
    {
      rules: rulesData1.value
    }
  ]
});

const condition1 = ref();

const addData = ref<RuleItem>({
    "type": 规则类型.个性标签,
    "calculationMethod": 计算类型.累计,
    "operator": 操作类型.大于等于,
    "tags": "xxx#666",
    "number": 12,
});

const addRule = (data) => {
  console.log(data, 'addRule');
}
const reduceRule = (data) => {
  console.log(data, 'reduceRule');
}
const ruleChange = (data) => {
  console.log(data, 'ruleChange1');
}
const reduceCondition = (data) => {
  console.log(data, 'reduceCondition');
}
const addCondition = (data) => {
  console.log(data, 'addCondition');
}

const getCondition = () => {
    result.value = JSON.stringify(condition1.value.plan, null, 2)
}
const validate = () => {
  condition1.value.nfConditionsForm.validate((valid) => {
    validation.value = valid ? 'Validation passed' : 'Validation failed'
  })
}
const result = ref('')
const validation = ref('')
</script>
```

### Restore conditions {#example-2}

### Restore conditions
```vue
<template>
  <NfConfigProvider theme="chalk" locale="en-US">
    <div>
      <NfConditions
        :conditions="plan2.conditions"
      />
    </div>
  </NfConfigProvider>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { NfConfigProvider } from '@94ai-plus/common-ui'
import { NfConditions } from '@94ai-plus/nf-conditions'
import { 规则类型, 操作类型, 计算类型, 日期类型, 外呼状态, type RuleItem } from '@94ai-plus/nf-rules'
const deepClone = (source: RuleItem[]) => source.map(rule => ({
  ...rule,
  ...(Array.isArray(rule.time) ? { time: rule.time.map(date => new Date(date.getTime())) } : {}),
  ...(Array.isArray(rule.date) ? { date: rule.date.map(date => new Date(date.getTime())) } : {}),
  ...(rule.intentionTags ? { intentionTags: [...rule.intentionTags] } : {}),
  ...(rule.states ? { states: [...rule.states] } : {})
}))

const rulesData2 = ref<RuleItem[]>([
  {
    "type": 规则类型.意向标签,
    "operator": 操作类型.等于,
    "intentionTags": ['A', 'D', 'E'],
  },
  {
    "type": 规则类型.个性标签,
    "tags": "xxx#www",
    "number": 1,
    "calculationMethod": 计算类型.累计,
    "operator": 操作类型.大于等于,
  },
  {
    "type": 规则类型.回复关键词,
    "keywords": "xxx#www",
    "number": 1,
    "calculationMethod": 计算类型.累计,
    "operator": 操作类型.大于等于,
  },
  {
    "type": 规则类型.自定义标签,
    "operator": 操作类型.等于,
    "tags": "xxx#www",
  },
  {
    "type": 规则类型.导入时间,
    "timeType": 日期类型.当天,
    "time": [
      new Date("2023-10-13T10:36:17.000Z"),
      new Date("2023-10-13T11:36:17.000Z")
    ],
  },
  {
    "type": 规则类型.外呼时间,
    "timeType": 日期类型.非当天,
    "date": [
      new Date("2023-10-13T10:36:17.000Z"),
      new Date("2023-10-13T11:36:17.000Z")
    ],
  },
  {
    "type": 规则类型.当前时间,
    "time": [
      new Date("2023-10-13T10:36:17.000Z"),
      new Date("2023-10-13T11:36:17.000Z")
    ],
  },
  {
    "type": 规则类型.通话时长,
    "operator": 操作类型.大于等于,
    "bill": 2,
  },
  {
    "type": 规则类型.对话轮次,
    "operator": 操作类型.大于等于,
    "number": 2,
  },
  {
    "type": 规则类型.外呼次数,
    "operator": 操作类型.等于,
    "number": 3,
  },
  {
    "type": 规则类型.参数,
    "paramName": "xxxx",
    "operator": 操作类型.等于,
    "tags": "xxxx",
  },
  {
    "type": 规则类型.外呼状态,
    "operator": 操作类型.等于,
    "states": ['已接听'],
  }
]);

const plan2 = ref({
  conditions: [
    {
      rules: rulesData2.value
    },
    {
      rules: deepClone(rulesData2.value)
    }
  ]
});

</script>
```

### Custom external form {#example-3}

### Custom external form
```vue
<template>
  <NfConfigProvider theme="chalk" locale="en-US">
  <div>
    <NfButton @click='valid' >Validate external form</NfButton>

    <ElDivider></ElDivider>

    <ElForm :model='plan3' :size='size' ref='outForm'>
      <NfConditions
          tag='div'
          valid-prefix='xxx.'
          :conditions='plan3.xxx.conditions'
      />
    </ElForm>
    <output aria-live="polite">{{ validation }}</output>
  </div>
  </NfConfigProvider>
</template>

<script lang='ts' setup>
import { ref } from 'vue'
import { NfConfigProvider, NfButton } from '@94ai-plus/common-ui'
import { ElDivider, ElForm } from 'element-plus'
import { NfConditions } from '@94ai-plus/nf-conditions'
const size = ref<'small'>('small')
const outForm = ref()

const valid = () => {
  outForm.value.validate((valid) => {
    validation.value = valid ? 'Validation passed' : 'Validation failed'
  })
}
const plan3 = ref({
  xxx: {
    conditions: [
      {
        rules: []
      }
    ]
  }
})
const validation = ref('')
</script>
```

## Props {#props}

Text props follow the library language when omitted; defaults below use this language.

| Parameter | Description | Type | Options | Default |
| --- | --- | --- | --- | --- |
| `conditions` | Condition data for display and retrieval; each entry contains a rules array. | `{ key?: string; rules?: RuleItem[] }[]` | — | `undefined` |
| `conditionLimit` | Maximum number of entries; 0 means unlimited. | `number` | — | `0` |
| `hiddenConditionAdd` | Hide the add action. | `boolean` | — | `false` |
| `hiddenConditionReduce` | Hide the remove action. | `boolean` | — | `false` |
| `conditionsKey` | Custom data field name. | `string` | — | `'conditions'` |
| `initAddRuleData` | Initialize a rule when a condition group is added. | `boolean` | — | `true` |
| `conditionDesc` | Customize the “Condition” text. | `string` | — | `Condition` |
| `addConditionDesc` | Customize the “+ Add condition” text. | `string` | — | `+ Add condition` |
| `deleteConditionDesc` | Customize the “- Delete condition” text. | `string` | — | `- Delete condition` |
| `outLogicMergeDesc` | Customize the “or” text. | `string` | — | `or` |
| `rules` | Compatibility prop; place each group’s rules in its rulesKey field under conditions. | `RuleItem[]` | — | `undefined` |
| `showRuleTypes` | Allowed rule types: enum values, original Chinese names or localized labels; an empty array allows all. | `(string \| number)[]` | — | `[]` |
| `defaultAddData` | Default data object when adding a rule. | `RuleItem` | — | `undefined` |
| `defaultAddType` | Default rule type when adding a rule. | `string \| number` | — | `undefined` |
| `initAddData` | Initialize an entry when data is absent or empty. | `boolean` | — | `true` |
| `size` | Control size; mini maps to small and medium to default. | `string` | mini / small / medium / default / large | `'mini'` |
| `tag` | Custom form container tag; use div inside an external form to avoid nested forms. | `string` | — | `'el-form'` |
| `validPrefix` | Custom prefix for form validation paths. | `string` | — | `''` |
| `rulesKey` | Custom data field name. | `string` | — | `'rules'` |
| `limit` | Maximum number of entries; 0 means unlimited. | `number` | — | `0` |
| `disabled` | Whether to disable the component. | `boolean` | — | `false` |
| `conditionDisabled` | Disable the rule-type selector. | `boolean` | — | `false` |
| `hiddenAdd` | Hide the add action. | `boolean` | — | `false` |
| `hiddenReduce` | Hide the remove action. | `boolean` | — | `false` |
| `disables` | Disable controls by rule type; array entries follow the controls in that rule. | `Record<string, boolean[]>` | — | `undefined` |
| `showMessage` | Whether to show validation error messages. | `boolean` | — | `true` |
| `inlineMessage` | Display validation messages inline. | `boolean` | — | `false` |
| `statusIcon` | Whether to show validation status icons. | `boolean` | — | `false` |
| `规则类型` | Customize enum values for 规则类型; defaults to the exported enum with the same name. | `Record<string, string \| number>` | — | `规则类型` |
| `规则类型下拉` | Customize 规则类型下拉 data for localization or custom dropdown options. | `DropDownItem[]` | — | `undefined` |
| `规则类型下拉合并策略` | Merge strategy for 规则类型下拉: income uses incoming items; incomePatch patches matching incoming values with defaults; defaultPatch patches matching defaults with incoming values. | `MergeStrategy` | income / incomePatch / defaultPatch | `'defaultPatch'` |
| `操作类型` | Customize enum values for 操作类型; defaults to the exported enum with the same name. | `Record<string, string \| number>` | — | `操作类型` |
| `操作类型下拉` | Customize 操作类型下拉 data for localization or custom dropdown options. | `DropDownItem[]` | — | `undefined` |
| `操作类型下拉合并策略` | Merge strategy for 操作类型下拉: income uses incoming items; incomePatch patches matching incoming values with defaults; defaultPatch patches matching defaults with incoming values. | `MergeStrategy` | income / incomePatch / defaultPatch | `'defaultPatch'` |
| `计算类型` | Customize enum values for 计算类型; defaults to the exported enum with the same name. | `Record<string, string \| number>` | — | `计算类型` |
| `计算类型下拉` | Customize 计算类型下拉 data for localization or custom dropdown options. | `DropDownItem[]` | — | `undefined` |
| `计算类型下拉合并策略` | Merge strategy for 计算类型下拉: income uses incoming items; incomePatch patches matching incoming values with defaults; defaultPatch patches matching defaults with incoming values. | `MergeStrategy` | income / incomePatch / defaultPatch | `'defaultPatch'` |
| `日期类型` | Customize enum values for 日期类型; defaults to the exported enum with the same name. | `Record<string, string \| number>` | — | `日期类型` |
| `日期类型下拉` | Customize 日期类型下拉 data for localization or custom dropdown options. | `DropDownItem[]` | — | `undefined` |
| `日期类型下拉合并策略` | Merge strategy for 日期类型下拉: income uses incoming items; incomePatch patches matching incoming values with defaults; defaultPatch patches matching defaults with incoming values. | `MergeStrategy` | income / incomePatch / defaultPatch | `'defaultPatch'` |
| `外呼状态` | Customize enum values for 外呼状态; defaults to the exported enum with the same name. | `Record<string, string \| number>` | — | `外呼状态` |
| `外呼状态下拉` | Customize 外呼状态下拉 data for localization or custom dropdown options. | `DropDownItem[]` | — | `undefined` |
| `外呼状态下拉合并策略` | Merge strategy for 外呼状态下拉: income uses incoming items; incomePatch patches matching incoming values with defaults; defaultPatch patches matching defaults with incoming values. | `MergeStrategy` | income / incomePatch / defaultPatch | `'defaultPatch'` |
| `意向标签集合` | Customize 意向标签集合 data for localization or custom dropdown options. | `DropDownItem[]` | — | `undefined` |
| `意向标签集合合并策略` | Merge strategy for 意向标签集合: income uses incoming items; incomePatch patches matching incoming values with defaults; defaultPatch patches matching defaults with incoming values. | `MergeStrategy` | income / incomePatch / defaultPatch | `'defaultPatch'` |
| `logicMergeDesc` | Customize the “and” text. | `string` | — | `and` |
| `placeholder` | Customize the “Please select” text. | `string` | — | `Please select` |
| `numberRequireDesc` | Customize the “Please enter a number” text. | `string` | — | `Please enter a number` |
| `commonLenLimitDesc` | Customize the “Total length cannot exceed 2000 Chinese characters” text. | `string` | — | `Total length cannot exceed 2000 Chinese characters` |
| `placeEnter` | Customize the “Please enter” text. | `string` | — | `Please enter` |
| `prohibitEmptyDesc` | Customize the “Cannot be empty” text. | `string` | — | `Cannot be empty` |
| `paramsLenLimitDesc` | Customize the “Up to 50 English characters” text. | `string` | — | `Up to 50 English characters` |
| `splitDesc` | Customize the “Separate multiple entries with #” text. | `string` | — | `Separate multiple entries with #` |
| `times` | Customize the “times” text. | `string` | — | `times` |
| `second` | Customize the “s” text. | `string` | — | `s` |
| `startPlaceholderDate` | Customize the “Start date” text. | `string` | — | `Start date` |
| `endPlaceholderDate` | Customize the “End date” text. | `string` | — | `End date` |
| `startPlaceholderTime` | Customize the “Start time” text. | `string` | — | `Start time` |
| `endPlaceholderTime` | Customize the “End time” text. | `string` | — | `End time` |
| `timePickerPlaceholder` | Customize the “Select time range” text. | `string` | — | `Select time range` |
| `datePickerPlaceholder` | Customize the “Select date range” text. | `string` | — | `Select date range` |
| `rangeSeparatorDesc` | Customize the “→” text. | `string` | — | `→` |

## Events {#events}

| Event | Description | Payload |
| --- | --- | --- |
| `onRuleChange` | Fires when collected condition data changes. | `(conditions: ConditionItem[])` |
| `ruleChange` | Fires when collected condition data changes. | `(conditions: ConditionItem[])` |
| `addRule` | Fires when a rule is added; returns the added object. | `(rule: RuleItem)` |
| `reduceRule` | Fires when a rule is removed; returns the removed object. | `(rule: RuleItem)` |
| `addCondition` | Fires when a condition is added; returns the added object. | `(condition: ConditionItem)` |
| `reduceCondition` | Fires when a condition is removed; returns the removed object. | `(condition: ConditionItem)` |

## Slots {#slots}

No custom slots.

## Exposed methods and state {#methods}

- `plan`
- `nfConditionsForm`
- `conditionContainer`
- `nfRules`
- `addCondition()`
- `removeCondition(index)`
- `initPlan()`
- `getCondition()`

## Migration notes {#migration}

Vue 3 uses `modelValue` / `update:modelValue`; legacy `value`, `update:value` and `input` remain available on selection components. The implementation uses public Element Plus APIs, not Element UI internals. `mini` maps to `small`; `medium` maps to `default`.
