Skip to content

Commit

Permalink
fix(form): async properites (#935)
Browse files Browse the repository at this point in the history
  • Loading branch information
cipchk committed May 30, 2020
1 parent 0080efd commit aa6de23
Show file tree
Hide file tree
Showing 12 changed files with 335 additions and 48 deletions.
90 changes: 90 additions & 0 deletions packages/form/src/widgets/select/demo/custom-dropdown-menu.md
@@ -0,0 +1,90 @@
---
order: 2
title:
zh-CN: 扩展菜单
en-US: Custom dropdown
---

## zh-CN

使用 `dropdownRender` 对下拉菜单进行自由扩展。

## en-US

Customize the dropdown menu via `dropdownRender`.

```ts
import { Component, OnInit, TemplateRef, ViewChild } from '@angular/core';
import { SFComponent, SFSchema, SFSelectWidgetSchema } from '@delon/form';
import { NzMessageService } from 'ng-zorro-antd/message';

@Component({
selector: 'app-demo',
template: `
<sf #sf *ngIf="schema" [schema]="schema" (formSubmit)="submit($event)"></sf>
<ng-template #dropdownRender>
<nz-divider></nz-divider>
<div class="container">
<input type="text" nz-input #inputElement />
<a class="add-item" (click)="addItem(inputElement)"><i nz-icon nzType="plus"></i> Add item</a>
</div>
</ng-template>
`,
styles: [
`
nz-divider {
margin: 4px 0;
}
.container {
display: flex;
flex-wrap: nowrap;
padding: 8px;
}
input {
}
.add-item {
flex: 0 0 auto;
padding: 8px;
display: block;
}
`,
],
})
export class DemoComponent implements OnInit {
@ViewChild('sf', { static: false }) private sf: SFComponent;
@ViewChild('dropdownRender', { static: true }) private dropdownRender!: TemplateRef<void>;

schema: SFSchema;
statusList: string[] = ['1', '2', '3'];

constructor(private msg: NzMessageService) {}

submit(value: any) {
this.msg.success(JSON.stringify(value));
}

ngOnInit(): void {
this.schema = {
properties: {
status: {
type: 'string',
title: '状态',
enum: this.statusList,
default: '1',
ui: {
widget: 'select',
dropdownRender: this.dropdownRender,
} as SFSelectWidgetSchema,
},
},
};
}

addItem(input: HTMLInputElement): void {
this.statusList.push(input.value);
const statusProperty = this.sf.getProperty('/status')!;
statusProperty.schema.enum = this.statusList;
this.sf.setValue('/status', input.value);
}
}
```
64 changes: 36 additions & 28 deletions packages/form/src/widgets/select/index.md
Expand Up @@ -10,34 +10,42 @@ type: Widgets

### schema 属性

成员 | 说明 | 类型 | 默认值
----|------|-----|------
`[enum]` | 数据源 | `SFSchemaEnumType[]` | -
`[readOnly]` | 禁用状态 | `boolean` | -
| 成员 | 说明 | 类型 | 默认值 |
|----|----|----|-----|
| `[enum]` | 数据源 | `SFSchemaEnumType[]` | - |
| `[readOnly]` | 禁用状态 | `boolean` | - |

### ui 属性

成员 | 说明 | 类型 | 默认值
----|------|-----|------
`[asyncData]` | 异步数据源 | `() => Observable<SFSchemaEnumType[]>` | -
`[size]` | 大小,等同 `nzSize` | `string` | -
`[compareWith]` | 与 [SelectControlValueAccessor](https://angular.io/api/forms/SelectControlValueAccessor#caveat-option-selection) 相同 | `(o1: any, o2: any) => boolean` | `(o1: any, o2: any) => o1===o2`
`[placeholder]` | 在文字框中显示提示讯息 | `string` | -
`[autoClearSearchValue]` | 是否在选中项后清空搜索框,只在 `mode``multiple``tags` 时有效。 | `boolean` | `true`
`[allowClear]` | 支持清除 | `boolean` | `false`
`[borderless]` | 是否无边框 | `boolean` | `false`
`[autoFocus]` | 默认获取焦点 | `boolean` | `false`
`[dropdownClassName]` | 下拉菜单的 className 属性 | `string` | -
`[dropdownMatchSelectWidth]` | 下拉菜单和选择器同宽 | `boolean` | `true`
`[dropdownStyle]` | 下拉菜单的 style 属性 | `object` | -
`[serverSearch]` | 是否使用服务端搜索,当为 true 时,将不再在前端对 nz-option 进行过滤 | `boolean` | `false`
`[maxMultipleCount]` | 最多选中多少个标签| `number` | `Infinity`
`[mode]` | 设置 nz-select 的模式,`tags` 建议增加 `default: null`,否则可能会遇到初始化有一个空的标签。 | `multiple,tags,default` | `default`
`[notFoundContent]` | 当下拉列表为空时显示的内容 | `string` | -
`[showSearch]` | 使单选模式可搜索 | `boolean` | `false`
`[onSearch]` | 搜索内容变化回调函数,参数为搜索内容,必须返回 `Promise` 对象 | `(text: string) => Promise<SFSchemaEnum[]>` | -
`[tokenSeparators]` | 在 tags 和 multiple 模式下自动分词的分隔符 | `string[]` | `[]`
`[maxTagCount]` | 最多显示多少个 tag | `number` | -
`[change]` | 选中的 nz-option 发生变化时,调用此函数 | `(ngModel:any丨any[])=>void` | -
`[openChange]` | 下拉菜单打开关闭回调函数 | `(status: boolean) => void` | -
`[scrollToBottom]` | 下拉菜单滚动到底部回调,可用于作为动态加载的触发条件 | `() => void` | -
| 成员 | 说明 | 类型 | 默认值 |
|----|----|----|-----|
| `[asyncData]` | 异步数据源 | `() => Observable<SFSchemaEnumType[]>` | - |
| `[size]` | 大小,等同 `nzSize` | `string` | - |
| `[compareWith]` |[SelectControlValueAccessor](https://angular.io/api/forms/SelectControlValueAccessor#caveat-option-selection) 相同 | `(o1: any, o2: any) => boolean` | `(o1: any, o2: any) => o1===o2` |
| `[placeholder]` | 在文字框中显示提示讯息 | `string` | - |
| `[autoClearSearchValue]` | 是否在选中项后清空搜索框,只在 `mode``multiple``tags` 时有效。 | `boolean` | `true` |
| `[allowClear]` | 支持清除 | `boolean` | `false` |
| `[borderless]` | 是否无边框 | `boolean` | `false` |
| `[autoFocus]` | 默认获取焦点 | `boolean` | `false` |
| `[dropdownClassName]` | 下拉菜单的 className 属性 | `string` | - |
| `[dropdownMatchSelectWidth]` | 下拉菜单和选择器同宽 | `boolean` | `true` |
| `[dropdownStyle]` | 下拉菜单的 style 属性 | `object` | - |
| `[serverSearch]` | 是否使用服务端搜索,当为 true 时,将不再在前端对 nz-option 进行过滤 | `boolean` | `false` |
| `[maxMultipleCount]` | 最多选中多少个标签 | `number` | `Infinity` |
| `[mode]` | 设置 nz-select 的模式,`tags` 建议增加 `default: null`,否则可能会遇到初始化有一个空的标签。 | `multiple,tags,default` | `default` |
| `[notFoundContent]` | 当下拉列表为空时显示的内容 | `string` | - |
| `[showSearch]` | 使单选模式可搜索 | `boolean` | `false` |
| `[onSearch]` | 搜索内容变化回调函数,参数为搜索内容,必须返回 `Promise` 对象 | `(text: string) => Promise<SFSchemaEnum[]>` | - |
| `[tokenSeparators]` | 在 tags 和 multiple 模式下自动分词的分隔符 | `string[]` | `[]` |
| `[maxTagCount]` | 最多显示多少个 tag | `number` | - |
| `[change]` | 选中的 nz-option 发生变化时,调用此函数 | `(ngModel:any丨any[])=>void` | - |
| `[openChange]` | 下拉菜单打开关闭回调函数 | `(status: boolean) => void` | - |
| `[scrollToBottom]` | 下拉菜单滚动到底部回调,可用于作为动态加载的触发条件 | `() => void` | - |
| `[customTemplate]` | 自定义选择框的Template内容 | `TemplateRef<{ $implicit: NzOptionComponent }>` | - |
| `[suffixIcon]` | 自定义的选择框后缀图标 | `TemplateRef<any>, string` | - |
| `[removeIcon]` | 自定义的多选框清除图标 | `TemplateRef<any>` | - |
| `[clearIcon]` | 自定义的多选框清空图标 | `TemplateRef<any>` | - |
| `[menuItemSelectedIcon]` | 自定义当前选中的条目图标 | `TemplateRef<any>` | - |
| `[maxTagPlaceholder]` | 隐藏 tag 时显示的内容 | `TemplateRef<{ $implicit: any[] }>` | - |
| `[optionHeightPx]` | 下拉菜单中每个 Option 的高度 | `number` | `32` |
| `[optionOverflowSize]` | 下拉菜单中最多展示的 Option 个数,超出部分滚动 | `number` | `8` |
48 changes: 48 additions & 0 deletions packages/form/src/widgets/select/schema.ts
@@ -1,3 +1,6 @@
import { TemplateRef } from '@angular/core';
import { NzSafeAny } from 'ng-zorro-antd/core/types';
import { NzOptionComponent } from 'ng-zorro-antd/select';
import { Observable } from 'rxjs';
import { SFValue } from '../../interface';
import { SFSchemaEnum, SFSchemaEnumType } from '../../schema';
Expand Down Expand Up @@ -96,6 +99,51 @@ export interface SFSelectWidgetSchema extends SFUISchemaItem {
*/
maxTagCount?: number;

/**
* 自定义选择框的Template内容
*/
customTemplate?: TemplateRef<{ $implicit: NzOptionComponent }>;

/**
* 自定义的选择框后缀图标
*/
suffixIcon?: TemplateRef<any> | string;

/**
* 自定义的多选框清除图标
*/
removeIcon?: TemplateRef<any>;

/**
* 自定义的多选框清空图标
*/
clearIcon?: TemplateRef<any>;

/**
* 自定义当前选中的条目图标
*/
menuItemSelectedIcon?: TemplateRef<any>;

/**
* 隐藏 tag 时显示的内容
*/
maxTagPlaceholder?: TemplateRef<{ $implicit: any[] }>;

/**
* 下拉菜单中每个 Option 的高度,默认:`32`
*/
optionHeightPx?: number;

/**
* 下拉菜单中最多展示的 Option 个数,超出部分滚动,默认:`8`
*/
optionOverflowSize?: number;

/**
* 自由扩展
*/
dropdownRender?: TemplateRef<NzSafeAny>;

/**
* 选中的 `nz-option` 发生变化时,调用此函数
*/
Expand Down
18 changes: 14 additions & 4 deletions packages/form/src/widgets/select/select.widget.html
@@ -1,24 +1,34 @@
<sf-item-wrap [id]="id" [schema]="schema" [ui]="ui" [showError]="showError" [error]="error" [showTitle]="schema.title">
<nz-select
[nzDisabled]="disabled"
[nzSize]="ui.size"
[(ngModel)]="_value"
(ngModelChange)="change($event)"
[nzSize]="ui.size"
[nzPlaceHolder]="ui.placeholder"
[nzNotFoundContent]="ui.notFoundContent"
[nzDropdownClassName]="ui.dropdownClassName"
[nzAllowClear]="ui.allowClear"
[nzDropdownStyle]="ui.dropdownStyle"
[nzCustomTemplate]="ui.customTemplate"
[nzSuffixIcon]="ui.suffixIcon"
[nzRemoveIcon]="ui.removeIcon"
[nzClearIcon]="ui.clearIcon"
[nzMenuItemSelectedIcon]="ui.menuItemSelectedIcon"
[nzMaxTagPlaceholder]="ui.maxTagPlaceholder"
[nzDropdownRender]="ui.dropdownRender"
[nzAutoClearSearchValue]="i.autoClearSearchValue"
[nzAllowClear]="i.allowClear"
[nzBorderless]="i.borderless"
[nzAutoFocus]="i.autoFocus"
[nzDropdownClassName]="i.dropdownClassName"
[nzDropdownMatchSelectWidth]="i.dropdownMatchSelectWidth"
[nzServerSearch]="i.serverSearch"
[nzMaxMultipleCount]="i.maxMultipleCount"
[nzMode]="i.mode"
[nzNotFoundContent]="i.notFoundContent"
[nzShowSearch]="i.showSearch"
[nzTokenSeparators]="i.tokenSeparators"
[nzMaxTagCount]="i.maxTagCount"
[compareWith]="i.compareWith"
[nzOptionHeightPx]="i.optionHeightPx"
[nzOptionOverflowSize]="i.optionOverflowSize"
(nzOpenChange)="openChange($event)"
(nzOnSearch)="searchChange($event)"
(nzScrollToBottom)="scrollToBottom()"
Expand Down
15 changes: 7 additions & 8 deletions packages/form/src/widgets/select/select.widget.ts
@@ -1,4 +1,5 @@
import { Component, OnInit, ViewEncapsulation } from '@angular/core';
import { NzSafeAny } from 'ng-zorro-antd/core/types';
import { SFValue } from '../../interface';
import { SFSchemaEnum } from '../../schema';
import { getData, toBool } from '../../utils';
Expand All @@ -12,9 +13,9 @@ import { SFSelectWidgetSchema } from './schema';
encapsulation: ViewEncapsulation.None,
})
export class SelectWidget extends ControlUIWidget<SFSelectWidgetSchema> implements OnInit {
i: any;
i: SFSelectWidgetSchema;
data: SFSchemaEnum[];
_value: any;
_value: NzSafeAny;
hasGroup = false;

private checkGroup(list: SFSchemaEnum[]): void {
Expand All @@ -24,34 +25,32 @@ export class SelectWidget extends ControlUIWidget<SFSelectWidgetSchema> implemen
ngOnInit(): void {
const {
autoClearSearchValue,
allowClear,
borderless,
autoFocus,
dropdownClassName,
dropdownMatchSelectWidth,
serverSearch,
maxMultipleCount,
mode,
notFoundContent,
showSearch,
tokenSeparators,
maxTagCount,
compareWith,
optionHeightPx,
optionOverflowSize,
} = this.ui;
this.i = {
autoClearSearchValue: toBool(autoClearSearchValue, true),
allowClear,
borderless: toBool(borderless, false),
autoFocus: toBool(autoFocus, false),
dropdownClassName: dropdownClassName || null,
dropdownMatchSelectWidth: toBool(dropdownMatchSelectWidth, true),
serverSearch: toBool(serverSearch, false),
maxMultipleCount: maxMultipleCount || Infinity,
mode: mode || 'default',
notFoundContent,
showSearch: toBool(showSearch, true),
tokenSeparators: tokenSeparators || [],
maxTagCount: maxTagCount || undefined,
optionHeightPx: optionHeightPx || 32,
optionOverflowSize: optionOverflowSize || 8,
compareWith: compareWith || ((o1: any, o2: any) => o1 === o2),
};
}
Expand Down
66 changes: 66 additions & 0 deletions packages/form/src/widgets/tree-select/demo/customized-icon.md
@@ -0,0 +1,66 @@
---
order: 4
debug: true
title:
zh-CN: 自定义图标
en-US: Customize Icon
---

## zh-CN

可以针对不同节点采用样式覆盖的方式定制图标。

## en-US

You can customize icons for different nodes.

```ts
import { Component, OnInit, TemplateRef, ViewChild } from '@angular/core';
import { SFSchema, SFTreeSelectWidgetSchema } from '@delon/form';
import { NzTreeNode } from 'ng-zorro-antd/tree';

@Component({
selector: 'app-demo',
template: `
<sf *ngIf="schema" [schema]="schema"></sf>
<ng-template #customTpl let-node>
<span class="ant-tree-node-content-wrapper" [class.ant-tree-node-selected]="node.isSelected">
<span> <i nz-icon [nzType]="node.isExpanded ? 'folder-open' : 'folder'"></i> {{ node.title }} </span>
</span>
</ng-template>
`,
})
export class DemoComponent implements OnInit {
@ViewChild('customTpl', { static: true }) private customTpl!: TemplateRef<{ $implicit: NzTreeNode }>;

schema: SFSchema;

ngOnInit(): void {
this.schema = {
properties: {
status: {
type: 'string',
title: '基本',
enum: [
{
title: 'parent 1',
key: '100',
expanded: true,
icon: 'smile',
children: [
{ title: 'leaf 1-0-0', key: '10010', icon: 'meh', isLeaf: true },
{ title: 'leaf 1-0-1', key: '10011', icon: 'frown', isLeaf: true },
],
},
],
default: '10010',
ui: {
widget: 'tree-select',
treeTemplate: this.customTpl,
} as SFTreeSelectWidgetSchema,
},
},
};
}
}
```

0 comments on commit aa6de23

Please sign in to comment.