Skip to content

Commit 2ee261a

Browse files
authored
feat(module:select): support to customize attr.title of nz-option-item (#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
1 parent 0674f78 commit 2ee261a

File tree

8 files changed

+40
-1
lines changed

8 files changed

+40
-1
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ import { NzSelectModule } from 'ng-zorro-antd/select';
7676
| Property | Description | Type | Default |
7777
| -------- | ----------- | ---- | ------- |
7878
| `[nzDisabled]` | Disable this option | `boolean` | `false` |
79+
| `[nzTitle]` | Native title hint on this option | `string \| number` | - |
7980
| `[nzLabel]` | The text show in nz-select and dropdown menu | `string \| number` | - |
8081
| `[nzValue]` | The value passed to ngModel of nz-select | `any ` | - |
8182
| `[nzKey]` | Should be passed when typeof nzValue - Object. Key will be used for performance optimizations | `string \| number ` | - |

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ import { NzSelectModule } from 'ng-zorro-antd/select';
7777
| 参数 | 说明 | 类型 | 默认值 |
7878
| --- | --- | --- | --- |
7979
| `[nzDisabled]` | 是否禁用 | `boolean` | `false` |
80+
| `[nzTitle]` | 选项上的原生 title 提示 | `string \| number` | - |
8081
| `[nzLabel]` | 选中该 nz-option 后,nz-select 中显示的文字 | `string \| number` | - |
8182
| `[nzValue]` | nz-select 中 ngModel 的值 | `any` | - |
8283
| `[nzKey]` | nz-select 中 ngModel 的值 | `string \| number` | - |

components/select/option-container.component.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ import { NzSelectItemInterface, NzSelectModeType } from './select.types';
5959
[grouped]="!!item.groupLabel"
6060
[disabled]="item.nzDisabled"
6161
[showState]="mode === 'tags' || mode === 'multiple'"
62+
[title]="item.nzTitle"
6263
[label]="item.nzLabel"
6364
[compareWith]="compareWith"
6465
[activatedValue]="activatedValue"

components/select/option-item.component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import { NzSafeAny } from 'ng-zorro-antd/core/types';
4040
encapsulation: ViewEncapsulation.None,
4141
host: {
4242
class: 'ant-select-item ant-select-item-option',
43-
'[attr.title]': 'label',
43+
'[attr.title]': 'title',
4444
'[class.ant-select-item-option-grouped]': 'grouped',
4545
'[class.ant-select-item-option-selected]': 'selected && !disabled',
4646
'[class.ant-select-item-option-disabled]': 'disabled',
@@ -56,6 +56,7 @@ export class NzOptionItemComponent implements OnChanges, OnInit {
5656
@Input() template: TemplateRef<NzSafeAny> | null = null;
5757
@Input() disabled = false;
5858
@Input() showState = false;
59+
@Input() title?: string | number | null;
5960
@Input() label: string | number | null = null;
6061
@Input() value: NzSafeAny | null = null;
6162
@Input() activatedValue: NzSafeAny | null = null;

components/select/option.component.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export class NzOptionComponent implements OnChanges, OnInit {
4343
changes = new Subject<void>();
4444
groupLabel: string | number | TemplateRef<NzSafeAny> | null = null;
4545
@ViewChild(TemplateRef, { static: true }) template!: TemplateRef<NzSafeAny>;
46+
@Input() nzTitle?: string | number | null;
4647
@Input() nzLabel: string | number | null = null;
4748
@Input() nzValue: NzSafeAny | null = null;
4849
@Input() nzKey?: string | number;

components/select/select.component.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,7 @@ export class NzSelectComponent implements ControlValueAccessor, OnInit, AfterCon
623623
const listOfTransformedItem = listOfOptions.map(item => {
624624
return {
625625
template: item.label instanceof TemplateRef ? item.label : null,
626+
nzTitle: this.getTitle(item.title, item.label),
626627
nzLabel: typeof item.label === 'string' || typeof item.label === 'number' ? item.label : null,
627628
nzValue: item.value,
628629
nzDisabled: item.disabled || false,
@@ -766,6 +767,7 @@ export class NzSelectComponent implements ControlValueAccessor, OnInit, AfterCon
766767
nzHide,
767768
nzCustomContent,
768769
groupLabel,
770+
nzTitle: this.getTitle(item.nzTitle, item.nzLabel),
769771
type: 'item',
770772
key: nzKey === undefined ? nzValue : nzKey
771773
};
@@ -795,4 +797,17 @@ export class NzSelectComponent implements ControlValueAccessor, OnInit, AfterCon
795797
}
796798
});
797799
}
800+
801+
private getTitle(title: NzSelectOptionInterface['title'], label: NzSelectOptionInterface['label']): string {
802+
let rawTitle: string = undefined!;
803+
if (title === undefined) {
804+
if (typeof label === 'string' || typeof label === 'number') {
805+
rawTitle = label.toString();
806+
}
807+
} else if (typeof title === 'string' || typeof title === 'number') {
808+
rawTitle = title.toString();
809+
}
810+
811+
return rawTitle;
812+
}
798813
}

components/select/select.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,21 @@ describe('select', () => {
275275
expect(selectElement.classList).toContain('ant-select-disabled');
276276
expect(selectElement.querySelector('input')!.getAttribute('disabled')).toBe('');
277277
}));
278+
it('should nzTitle works', fakeAsync(() => {
279+
component.listOfOption = [
280+
{ nzValue: '1', nzLabel: '1' },
281+
{ nzValue: '2', nzLabel: '2', nzTitle: '-' },
282+
{ nzValue: '3', nzLabel: '3', nzTitle: null }
283+
];
284+
component.nzOpen = true;
285+
fixture.detectChanges();
286+
flush();
287+
fixture.detectChanges();
288+
console.log(document.querySelectorAll('nz-option-item'));
289+
expect((document.querySelectorAll('nz-option-item')[0] as HTMLElement)?.title).toBe('1');
290+
expect((document.querySelectorAll('nz-option-item')[1] as HTMLElement)?.title).toBe('-');
291+
expect((document.querySelectorAll('nz-option-item')[2] as HTMLElement)?.title).toBeFalsy();
292+
}));
278293

279294
it('should select option by enter', fakeAsync(() => {
280295
const flushChanges = (): void => {
@@ -1554,6 +1569,7 @@ describe('select', () => {
15541569
*ngFor="let o of listOfOption"
15551570
[nzValue]="o.nzValue"
15561571
[nzLabel]="o.nzLabel"
1572+
[nzTitle]="o.nzTitle"
15571573
[nzDisabled]="o.nzDisabled"
15581574
[nzHide]="o.nzHide"
15591575
></nz-option>
@@ -1562,6 +1578,7 @@ describe('select', () => {
15621578
*ngFor="let o of group.children"
15631579
[nzValue]="o.nzValue"
15641580
[nzLabel]="o.nzLabel"
1581+
[nzTitle]="o.nzTitle"
15651582
[nzDisabled]="o.nzDisabled"
15661583
[nzHide]="o.nzHide"
15671584
></nz-option>

components/select/select.types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export interface NzSelectItemInterface {
1212
template?: TemplateRef<NzSafeAny> | null;
1313
nzLabel: string | number | null;
1414
nzValue: NzSafeAny | null;
15+
nzTitle?: string | number | null;
1516
nzDisabled?: boolean;
1617
nzHide?: boolean;
1718
nzCustomContent?: boolean;
@@ -23,6 +24,7 @@ export interface NzSelectItemInterface {
2324
export interface NzSelectOptionInterface {
2425
label: string | number | null | TemplateRef<NzSafeAny>;
2526
value: NzSafeAny | null;
27+
title?: string | number | null;
2628
disabled?: boolean;
2729
hide?: boolean;
2830
groupLabel?: string | number | TemplateRef<NzSafeAny> | null;

0 commit comments

Comments
 (0)