Skip to content

Commit

Permalink
feat(module:select): support to customize attr.title of nz-option-item (
Browse files Browse the repository at this point in the history
#8097)

* feat(module:select): support to customize attr.title of nz-option-item

* feat(module:select): support to customize attr.title of nz-option-item
  • Loading branch information
Laffery committed Oct 19, 2023
1 parent 0674f78 commit 2ee261a
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 1 deletion.
1 change: 1 addition & 0 deletions components/select/doc/index.en-US.md
Expand Up @@ -76,6 +76,7 @@ import { NzSelectModule } from 'ng-zorro-antd/select';
| Property | Description | Type | Default |
| -------- | ----------- | ---- | ------- |
| `[nzDisabled]` | Disable this option | `boolean` | `false` |
| `[nzTitle]` | Native title hint on this option | `string \| number` | - |
| `[nzLabel]` | The text show in nz-select and dropdown menu | `string \| number` | - |
| `[nzValue]` | The value passed to ngModel of nz-select | `any ` | - |
| `[nzKey]` | Should be passed when typeof nzValue - Object. Key will be used for performance optimizations | `string \| number ` | - |
Expand Down
1 change: 1 addition & 0 deletions components/select/doc/index.zh-CN.md
Expand Up @@ -77,6 +77,7 @@ import { NzSelectModule } from 'ng-zorro-antd/select';
| 参数 | 说明 | 类型 | 默认值 |
| --- | --- | --- | --- |
| `[nzDisabled]` | 是否禁用 | `boolean` | `false` |
| `[nzTitle]` | 选项上的原生 title 提示 | `string \| number` | - |
| `[nzLabel]` | 选中该 nz-option 后,nz-select 中显示的文字 | `string \| number` | - |
| `[nzValue]` | nz-select 中 ngModel 的值 | `any` | - |
| `[nzKey]` | nz-select 中 ngModel 的值 | `string \| number` | - |
Expand Down
1 change: 1 addition & 0 deletions components/select/option-container.component.ts
Expand Up @@ -59,6 +59,7 @@ import { NzSelectItemInterface, NzSelectModeType } from './select.types';
[grouped]="!!item.groupLabel"
[disabled]="item.nzDisabled"
[showState]="mode === 'tags' || mode === 'multiple'"
[title]="item.nzTitle"
[label]="item.nzLabel"
[compareWith]="compareWith"
[activatedValue]="activatedValue"
Expand Down
3 changes: 2 additions & 1 deletion components/select/option-item.component.ts
Expand Up @@ -40,7 +40,7 @@ import { NzSafeAny } from 'ng-zorro-antd/core/types';
encapsulation: ViewEncapsulation.None,
host: {
class: 'ant-select-item ant-select-item-option',
'[attr.title]': 'label',
'[attr.title]': 'title',
'[class.ant-select-item-option-grouped]': 'grouped',
'[class.ant-select-item-option-selected]': 'selected && !disabled',
'[class.ant-select-item-option-disabled]': 'disabled',
Expand All @@ -56,6 +56,7 @@ export class NzOptionItemComponent implements OnChanges, OnInit {
@Input() template: TemplateRef<NzSafeAny> | null = null;
@Input() disabled = false;
@Input() showState = false;
@Input() title?: string | number | null;
@Input() label: string | number | null = null;
@Input() value: NzSafeAny | null = null;
@Input() activatedValue: NzSafeAny | null = null;
Expand Down
1 change: 1 addition & 0 deletions components/select/option.component.ts
Expand Up @@ -43,6 +43,7 @@ export class NzOptionComponent implements OnChanges, OnInit {
changes = new Subject<void>();
groupLabel: string | number | TemplateRef<NzSafeAny> | null = null;
@ViewChild(TemplateRef, { static: true }) template!: TemplateRef<NzSafeAny>;
@Input() nzTitle?: string | number | null;
@Input() nzLabel: string | number | null = null;
@Input() nzValue: NzSafeAny | null = null;
@Input() nzKey?: string | number;
Expand Down
15 changes: 15 additions & 0 deletions components/select/select.component.ts
Expand Up @@ -623,6 +623,7 @@ export class NzSelectComponent implements ControlValueAccessor, OnInit, AfterCon
const listOfTransformedItem = listOfOptions.map(item => {
return {
template: item.label instanceof TemplateRef ? item.label : null,
nzTitle: this.getTitle(item.title, item.label),
nzLabel: typeof item.label === 'string' || typeof item.label === 'number' ? item.label : null,
nzValue: item.value,
nzDisabled: item.disabled || false,
Expand Down Expand Up @@ -766,6 +767,7 @@ export class NzSelectComponent implements ControlValueAccessor, OnInit, AfterCon
nzHide,
nzCustomContent,
groupLabel,
nzTitle: this.getTitle(item.nzTitle, item.nzLabel),
type: 'item',
key: nzKey === undefined ? nzValue : nzKey
};
Expand Down Expand Up @@ -795,4 +797,17 @@ export class NzSelectComponent implements ControlValueAccessor, OnInit, AfterCon
}
});
}

private getTitle(title: NzSelectOptionInterface['title'], label: NzSelectOptionInterface['label']): string {
let rawTitle: string = undefined!;
if (title === undefined) {
if (typeof label === 'string' || typeof label === 'number') {
rawTitle = label.toString();
}
} else if (typeof title === 'string' || typeof title === 'number') {
rawTitle = title.toString();
}

return rawTitle;
}
}
17 changes: 17 additions & 0 deletions components/select/select.spec.ts
Expand Up @@ -275,6 +275,21 @@ describe('select', () => {
expect(selectElement.classList).toContain('ant-select-disabled');
expect(selectElement.querySelector('input')!.getAttribute('disabled')).toBe('');
}));
it('should nzTitle works', fakeAsync(() => {
component.listOfOption = [
{ nzValue: '1', nzLabel: '1' },
{ nzValue: '2', nzLabel: '2', nzTitle: '-' },
{ nzValue: '3', nzLabel: '3', nzTitle: null }
];
component.nzOpen = true;
fixture.detectChanges();
flush();
fixture.detectChanges();
console.log(document.querySelectorAll('nz-option-item'));
expect((document.querySelectorAll('nz-option-item')[0] as HTMLElement)?.title).toBe('1');
expect((document.querySelectorAll('nz-option-item')[1] as HTMLElement)?.title).toBe('-');
expect((document.querySelectorAll('nz-option-item')[2] as HTMLElement)?.title).toBeFalsy();
}));

it('should select option by enter', fakeAsync(() => {
const flushChanges = (): void => {
Expand Down Expand Up @@ -1554,6 +1569,7 @@ describe('select', () => {
*ngFor="let o of listOfOption"
[nzValue]="o.nzValue"
[nzLabel]="o.nzLabel"
[nzTitle]="o.nzTitle"
[nzDisabled]="o.nzDisabled"
[nzHide]="o.nzHide"
></nz-option>
Expand All @@ -1562,6 +1578,7 @@ describe('select', () => {
*ngFor="let o of group.children"
[nzValue]="o.nzValue"
[nzLabel]="o.nzLabel"
[nzTitle]="o.nzTitle"
[nzDisabled]="o.nzDisabled"
[nzHide]="o.nzHide"
></nz-option>
Expand Down
2 changes: 2 additions & 0 deletions components/select/select.types.ts
Expand Up @@ -12,6 +12,7 @@ export interface NzSelectItemInterface {
template?: TemplateRef<NzSafeAny> | null;
nzLabel: string | number | null;
nzValue: NzSafeAny | null;
nzTitle?: string | number | null;
nzDisabled?: boolean;
nzHide?: boolean;
nzCustomContent?: boolean;
Expand All @@ -23,6 +24,7 @@ export interface NzSelectItemInterface {
export interface NzSelectOptionInterface {
label: string | number | null | TemplateRef<NzSafeAny>;
value: NzSafeAny | null;
title?: string | number | null;
disabled?: boolean;
hide?: boolean;
groupLabel?: string | number | TemplateRef<NzSafeAny> | null;
Expand Down

0 comments on commit 2ee261a

Please sign in to comment.