Skip to content

Commit

Permalink
feat:ConditionBuilder字段配置支持defaultOp
Browse files Browse the repository at this point in the history
  • Loading branch information
hsm-lv committed Nov 27, 2023
1 parent 9c92c4b commit d2e0f78
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 19 deletions.
6 changes: 5 additions & 1 deletion docs/zh-CN/components/form/condition-builder.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ type Value = ValueGroup;
- `label` 字段名称。
- `placeholder` 占位符
- `operators` 默认为 `[ 'select_equals', 'select_not_equals', 'select_any_in', 'select_not_any_in' ]` 如果不要那么多,可以配置覆盖。
- `defaultOp`
- `defaultOp` 默认为 `"select_equals"`
- `options` 选项列表,`Array<{label: string, value: any}>`
- `source` 动态选项,请配置 api。
- `searchable` 是否可以搜索
Expand Down Expand Up @@ -392,7 +392,9 @@ type Value = ValueGroup;
- `label` 字段名称
- `placeholder` 占位符
- `operators` 默认为空,需配置自定义判断条件,支持字符串或 key-value 格式
- `defaultOp` 默认操作符
- `value` 字段配置右边值需要渲染的组件,支持 amis 输入类组件或自定义输入类组件
- `defaultValue` 右边值的默认值

```schema: scope="body"
{
Expand All @@ -412,6 +414,8 @@ type Value = ValueGroup;
"value": {
"type": "input-color"
},
"defaultOp": "equal",
"defaultValue": "#ff0000",
"operators": [
"equal",
{
Expand Down
5 changes: 4 additions & 1 deletion packages/amis-ui/src/components/condition-builder/Item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,13 @@ export class ConditionItem extends React.Component<ConditionItemProps> {

@autobind
handleLeftChange(leftValue: any) {
const {fields, config} = this.props;
// 获取默认Op
const field: any = findTree(fields, (f: any) => f.name === leftValue.field);
const value = {
...this.props.value,
left: leftValue,
op: undefined,
op: field?.defaultOp || config.types[field?.type]?.defaultOp || undefined,
right: undefined
};
const onChange = this.props.onChange;
Expand Down
6 changes: 6 additions & 0 deletions packages/amis-ui/src/components/condition-builder/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const defaultConfig: ConditionBuilderConfig = {
]
},
number: {
defaultOp: 'equal',
operators: [
'equal',
'not_equal',
Expand All @@ -75,6 +76,7 @@ const defaultConfig: ConditionBuilderConfig = {
]
},
date: {
defaultOp: 'equal',
operators: [
'equal',
'not_equal',
Expand All @@ -90,6 +92,7 @@ const defaultConfig: ConditionBuilderConfig = {
},

time: {
defaultOp: 'equal',
operators: [
'equal',
'not_equal',
Expand All @@ -105,6 +108,7 @@ const defaultConfig: ConditionBuilderConfig = {
},

datetime: {
defaultOp: 'equal',
operators: [
'equal',
'not_equal',
Expand All @@ -120,6 +124,7 @@ const defaultConfig: ConditionBuilderConfig = {
},

select: {
defaultOp: 'select_equals',
operators: [
'select_equals',
'select_not_equals',
Expand All @@ -130,6 +135,7 @@ const defaultConfig: ConditionBuilderConfig = {
},

boolean: {
defaultOp: 'equal',
operators: ['equal', 'not_equal']
}
}
Expand Down
18 changes: 1 addition & 17 deletions packages/amis/__tests__/renderers/Form/conditionBuilder.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,6 @@ test('Renderer:condition-builder add', async () => {

fireEvent.click(textType);

const textOpType = await findByText('请选择操作');

fireEvent.click(textOpType);

const qualOpType = await findByText('等于');

fireEvent.click(qualOpType);

const textRightInput = await findByPlaceholderText('请输入文本');

fireEvent.change(textRightInput, {target: {value: 'amis'}});
Expand Down Expand Up @@ -251,8 +243,6 @@ test('Renderer:condition-builder with number type', async () => {
fireEvent.click(await findByText('添加条件'));
fireEvent.click(await findByText('请选择字段'));
fireEvent.click(await findByText('数字'));
fireEvent.click(await findByText('请选择操作'));
fireEvent.click(await findByText('等于'));

fireEvent.change(await findByPlaceholderText('请输入数字'), {
target: {value: 81192}
Expand Down Expand Up @@ -396,8 +386,6 @@ test('Renderer:condition-builder with select type & source & searchable', async
fireEvent.click(await findByText('添加条件'));
fireEvent.click(await findByText('请选择字段'));
fireEvent.click(await findByText('动态选项'));
fireEvent.click(await findByText('请选择操作'));
fireEvent.click(await findByText('等于'));

await wait(200);
expect(fetcher).toHaveBeenCalled();
Expand Down Expand Up @@ -573,8 +561,6 @@ test('Renderer:condition-builder with source fields', async () => {
fireEvent.click(await findByText('添加条件'));
fireEvent.click(await findByText('请选择字段'));
fireEvent.click(await findByText('布尔'));
fireEvent.click(await findByText('请选择操作'));
fireEvent.click(await findByText('等于'));
fireEvent.click(container.querySelector('.cxd-Switch')!);

await wait(200);
Expand Down Expand Up @@ -706,8 +692,6 @@ test('Renderer:condition-builder with builderMode & showANDOR & showNot', async

fireEvent.click(await findByText('请选择字段'));
fireEvent.click(await findByText('布尔'));
fireEvent.click(await findByText('请选择操作'));
fireEvent.click(await findByText('等于'));
fireEvent.click(container.querySelector('.cxd-Switch')!);

await wait(200);
Expand Down Expand Up @@ -810,7 +794,7 @@ test('Renderer:condition-builder with not embed', async () => {
fireEvent.click(await findByText('添加条件'));
fireEvent.click(await findByText('请选择字段'));
fireEvent.click(await findByText('日期测试'));
fireEvent.click(await findByText('请选择操作'));
fireEvent.click(await findByText('等于'));
fireEvent.click(await findByText('不属于范围'));

expect(
Expand Down

0 comments on commit d2e0f78

Please sign in to comment.