Skip to content

Commit e94da4e

Browse files
perf(module:select): ability to pass nzKey to nz-option (#8033)
When object is used as key for select option re-rendering occurs, to solve this added new 'key' param | 'nzKey' Input
1 parent 7f7c155 commit e94da4e

File tree

5 files changed

+9
-5
lines changed

5 files changed

+9
-5
lines changed

components/select/doc/index.en-US.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ import { NzSelectModule } from 'ng-zorro-antd/select';
5959
| `[nzTokenSeparators]` | Separator used to tokenize on tag/multiple mode | `string[]` | `[]` |
6060
| `[nzLoading]` | indicate loading state | `boolean` | false |
6161
| `[nzMaxTagCount]` | Max tag count to show| `number` | - |
62-
| `[nzOptions]` | use nzOptions or `nz-option` to pass options to the select | `Array<{ label: string \| number \| TemplateRef<any>; value: any; disabled?: boolean; hide?: boolean; groupLabel?: string \| TemplateRef<any>;}>` | - |
62+
| `[nzOptions]` | use nzOptions or `nz-option` to pass options to the select | `Array<{ label: string \| number \| TemplateRef<any>; value: any; key?: string \| number; disabled?: boolean; hide?: boolean; groupLabel?: string \| TemplateRef<any>;}>` | - |
6363
| `[nzMaxTagPlaceholder]` | Placeholder for not showing tags | `TemplateRef<{ $implicit: any[] }>` | - |
6464
| `[nzOptionHeightPx]` | Each option height inside the dropdown | `number` | `32` |
6565
| `[nzOptionOverflowSize]` | Max option size inside the dropdown, overflow when exceed the size | `number` | `8` |
@@ -78,6 +78,7 @@ import { NzSelectModule } from 'ng-zorro-antd/select';
7878
| `[nzDisabled]` | Disable this option | `boolean` | `false` |
7979
| `[nzLabel]` | The text show in nz-select and dropdown menu | `string \| number` | - |
8080
| `[nzValue]` | The value passed to ngModel of nz-select | `any ` | - |
81+
| `[nzKey]` | Should be passed when typeof nzValue - Object. Key will be used for performance optimizations | `string \| number ` | - |
8182
| `[nzHide]` | Whether hide the option in the option list | `boolean` | `false` |
8283
| `[nzCustomContent]` | Whether custom nz-option content in drodown menu, the ng-content in nz-option will replace nzLabel when it was set to true | `boolean` | `false` |
8384

components/select/doc/index.zh-CN.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ import { NzSelectModule } from 'ng-zorro-antd/select';
6161
| `[nzLoading]` | 加载中状态 | `boolean` | `false` |
6262
| `[nzMaxTagCount]` | 最多显示多少个 tag | `number` | - |
6363
| `[nzMaxTagPlaceholder]` | 隐藏 tag 时显示的内容 | `TemplateRef<{ $implicit: any[] }>` | - |
64-
| `[nzOptions]` | option 列表,可以取代 nz-option,用法参见例子 | `Array<{ label: string \| number \| TemplateRef<any>; value: any; disabled?: boolean; hide?: boolean; groupLabel?: string \| TemplateRef<any>;}>` | - |
64+
| `[nzOptions]` | option 列表,可以取代 nz-option,用法参见例子 | `Array<{ label: string \| number \| TemplateRef<any>; value: any; key?: string \| number; disabled?: boolean; hide?: boolean; groupLabel?: string \| TemplateRef<any>;}>` | - |
6565
| `[nzOptionHeightPx]` | 下拉菜单中每个 Option 的高度 | `number` | `32` |
6666
| `[nzOptionOverflowSize]` | 下拉菜单中最多展示的 Option 个数,超出部分滚动 | `number` | `8` |
6767
| `[nzSelectOnTab]` | 允许使用 TAB 键选择项目 | `boolean` | `false` |
@@ -79,6 +79,7 @@ import { NzSelectModule } from 'ng-zorro-antd/select';
7979
| `[nzDisabled]` | 是否禁用 | `boolean` | `false` |
8080
| `[nzLabel]` | 选中该 nz-option 后,nz-select 中显示的文字 | `string \| number` | - |
8181
| `[nzValue]` | nz-select 中 ngModel 的值 | `any` | - |
82+
| `[nzKey]` | nz-select 中 ngModel 的值 | `string \| number` | - |
8283
| `[nzHide]` | 是否在选项列表中隐藏该选项 | `boolean` | `false` |
8384
| `[nzCustomContent]` | 是否自定义在下拉菜单中的 Template 内容,当为 true 时,nz-option 包裹的内容将直接渲染在下拉菜单中 | `boolean` | `false` |
8485

components/select/option.component.ts

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export class NzOptionComponent implements OnChanges, OnInit {
4545
@ViewChild(TemplateRef, { static: true }) template!: TemplateRef<NzSafeAny>;
4646
@Input() nzLabel: string | number | null = null;
4747
@Input() nzValue: NzSafeAny | null = null;
48+
@Input() nzKey?: string | number;
4849
@Input() @InputBoolean() nzDisabled = false;
4950
@Input() @InputBoolean() nzHide = false;
5051
@Input() @InputBoolean() nzCustomContent = false;

components/select/select.component.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ export class NzSelectComponent implements ControlValueAccessor, OnInit, AfterCon
630630
nzCustomContent: item.label instanceof TemplateRef,
631631
groupLabel: item.groupLabel || null,
632632
type: 'item',
633-
key: item.value
633+
key: item.key === undefined ? item.value : item.key
634634
};
635635
});
636636
this.listOfTemplateItem$.next(listOfTransformedItem);
@@ -757,7 +757,7 @@ export class NzSelectComponent implements ControlValueAccessor, OnInit, AfterCon
757757
)
758758
.subscribe(() => {
759759
const listOfOptionInterface = this.listOfNzOptionComponent.toArray().map(item => {
760-
const { template, nzLabel, nzValue, nzDisabled, nzHide, nzCustomContent, groupLabel } = item;
760+
const { template, nzLabel, nzValue, nzKey, nzDisabled, nzHide, nzCustomContent, groupLabel } = item;
761761
return {
762762
template,
763763
nzLabel,
@@ -767,7 +767,7 @@ export class NzSelectComponent implements ControlValueAccessor, OnInit, AfterCon
767767
nzCustomContent,
768768
groupLabel,
769769
type: 'item',
770-
key: nzValue
770+
key: nzKey === undefined ? nzValue : nzKey
771771
};
772772
});
773773
this.listOfTemplateItem$.next(listOfOptionInterface);

components/select/select.types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export interface NzSelectOptionInterface {
2626
disabled?: boolean;
2727
hide?: boolean;
2828
groupLabel?: string | number | TemplateRef<NzSafeAny> | null;
29+
key?: string | number;
2930
}
3031

3132
export type NzSelectTopControlItemType = Partial<NzSelectItemInterface> & {

0 commit comments

Comments
 (0)