Skip to content

Commit

Permalink
feat(form:select,cascader,tree-select): default value when cleared (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
cipchk authored Nov 5, 2024
1 parent 8f9e5c0 commit 655806d
Show file tree
Hide file tree
Showing 13 changed files with 28 additions and 7 deletions.
1 change: 1 addition & 0 deletions packages/form/src/widgets/select/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Select.
| `[placeholder]` | Placeholder | `string` | - |
| `[autoClearSearchValue]` | Whether clear search box when an option is selected, only valid when `mode` is `multiple` or `tags`. | `boolean` | `true` |
| `[allowClear]` | Allow clear | `boolean` | `false` |
| `[clearValue]` | Default value when cleared | `any` | `undefined` |
| `[borderless]` | Whether has border | `boolean` | `false` |
| `[autoFocus]` | Focused by default | `boolean` | `false` |
| `[dropdownClassName]` | className property of dropdown list | `string` | - |
Expand Down
1 change: 1 addition & 0 deletions packages/form/src/widgets/select/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ order: 6
| `[placeholder]` | 在文字框中显示提示讯息 | `string` | - |
| `[autoClearSearchValue]` | 是否在选中项后清空搜索框,只在 `mode``multiple``tags` 时有效。 | `boolean` | `true` |
| `[allowClear]` | 支持清除 | `boolean` | `false` |
| `[clearValue]` | 清空时默认值 | `any` | `undefined` |
| `[borderless]` | 是否无边框 | `boolean` | `false` |
| `[autoFocus]` | 默认获取焦点 | `boolean` | `false` |
| `[dropdownClassName]` | 下拉菜单的 className 属性 | `string` | - |
Expand Down
5 changes: 5 additions & 0 deletions packages/form/src/widgets/select/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ export interface SFSelectWidgetSchema extends SFUISchemaItem {
*/
clearIcon?: TemplateRef<NzSafeAny>;

/**
* 清空时默认值,默认:`undefined`
*/
clearValue?: NzSafeAny;

/**
* 自定义当前选中的条目图标
*/
Expand Down
7 changes: 4 additions & 3 deletions packages/form/src/widgets/select/select.widget.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,9 @@ describe('form: widget: select', () => {
default: 1,
ui: {
widget,
allowClear: true
}
allowClear: true,
clearValue: 2
} as SFSelectWidgetSchema
}
}
};
Expand All @@ -151,7 +152,7 @@ describe('form: widget: select', () => {
.time()
.typeEvent('click', '.ant-select-close-icon')
.time()
.checkValue('/a', undefined)
.checkValue('/a', 2)
.asyncEnd();
}));

Expand Down
2 changes: 1 addition & 1 deletion packages/form/src/widgets/select/select.widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export class SelectWidget extends ControlUIWidget<SFSelectWidgetSchema> implemen
if (this.ui.change) {
this.ui.change(values, this.getOrgData(values));
}
this.setValue(values == null ? undefined : values);
this.setValue(values == null ? this.ui.clearValue : values);
}

private getOrgData(values: SFValue): SFSchemaEnum | SFSchemaEnum[] {
Expand Down
1 change: 1 addition & 0 deletions packages/form/widgets/cascader/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Every select triggers a HTTP request, data source is from `asyncData`; includes
| `[placeholder]` | Placeholder | `string` | - |
| `[showSearch]` | Whether support search | `bool` | `false` |
| `[allowClear]` | Whether show clear button | `bool` | `true` |
| `[clearValue]` | Default value when cleared | `any` | `undefined` |
| `[clearText]` | Title of clear button | `string` | `清除` |
| `[showArrow]` | Whether show arrow | `bool` | `true` |
| `[showInput]` | Whether show input | `bool` | `true` |
Expand Down
1 change: 1 addition & 0 deletions packages/form/widgets/cascader/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type: Non-built-in widgets
| `[placeholder]` | 在文字框中显示提示讯息 | `string` | - |
| `[showSearch]` | 是否支持搜索 | `bool` | `false` |
| `[allowClear]` | 是否显示清除按钮 | `bool` | `true` |
| `[clearValue]` | 清空时默认值 | `any` | `undefined` |
| `[clearText]` | 清除按钮的标题 | `string` | `清除` |
| `[showArrow]` | 是否显示箭头 | `bool` | `true` |
| `[showInput]` | 是否显示输入框 | `bool` | `true` |
Expand Down
5 changes: 5 additions & 0 deletions packages/form/widgets/cascader/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ export interface SFCascaderWidgetSchema extends SFUISchemaItem {
*/
allowClear?: boolean;

/**
* 清空时默认值,默认:`undefined`
*/
clearValue?: NzSafeAny;

/**
* 清除按钮的标题,默认:`清除`
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/form/widgets/cascader/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class CascaderWidget extends ControlUIWidget<SFCascaderWidgetSchema> impl
}

_change(value: NzSafeAny[] | null): void {
this.setValue(value);
this.setValue(value == null ? this.ui.clearValue : value);
if (this.ui.change) {
this.ui.change(value);
}
Expand Down
1 change: 1 addition & 0 deletions packages/form/widgets/tree-select/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Non-built-in modules need to additionally register `withTreeSelectWidget` in [js
| `[placeholder]` | Placeholder | `string` | - |
| `[notFoundContent]` | Text to display when a column is empty | `string` | - |
| `[allowClear]` | Whether show clear button | `boolean` | `false` |
| `[clearValue]` | Default value when cleared | `any` | `undefined` |
| `[dropdownMatchSelectWidth]` | Determine whether the dropdown menu and the select input are the same width | `boolean` | `true` |
| `[dropdownStyle]` | Set the style of the dropdown menu | `object` | - |
| `[dropdownClassName]` | Set className of the dropdown menu | `string` | - |
Expand Down
1 change: 1 addition & 0 deletions packages/form/widgets/tree-select/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type: Non-built-in widgets
| `[placeholder]` | 在文字框中显示提示讯息 | `string` | - |
| `[notFoundContent]` | 当下拉列表为空时显示的内容 | `string` | - |
| `[allowClear]` | 支持清除 | `boolean` | `false` |
| `[clearValue]` | 清空时默认值 | `any` | `undefined` |
| `[dropdownMatchSelectWidth]` | 下拉菜单和选择器同宽 | `boolean` | `true` |
| `[dropdownStyle]` | 下拉菜单的 style 属性 | `object` | - |
| `[dropdownClassName]` | 下拉菜单的 className 属性 | `string` | - |
Expand Down
5 changes: 5 additions & 0 deletions packages/form/widgets/tree-select/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export interface SFTreeSelectWidgetSchema extends SFUISchemaItem {
*/
allowClear?: boolean;

/**
* 清空时默认值,默认:`undefined`
*/
clearValue?: NzSafeAny;

/**
* 下拉菜单和选择器同宽,默认:`true`
*/
Expand Down
3 changes: 1 addition & 2 deletions packages/form/widgets/tree-select/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,8 @@ export class TreeSelectWidget extends ControlUIWidget<SFTreeSelectWidgetSchema>
}

change(value: NzSafeAny[] | NzSafeAny): void {
console.log(value);
if (this.ui.change) this.ui.change(value);
this.setValue(value);
this.setValue(value == null ? this.ui.clearValue : value);
}

expandChange(e: NzFormatEmitEvent): void {
Expand Down

0 comments on commit 655806d

Please sign in to comment.