Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(module:select): support custom template in select component #3071

Merged
merged 13 commits into from
Jun 22, 2019
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions components/select/demo/custom-template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
order: 18
title:
zh-CN: 自定义选择器内容
en-US: Custom Select Template
---

## zh-CN

通过 `nzCustomTemplate` 自定义 nz-select 显示的内容。

## en-US

Custom the content of nz-select via `nzCustomTemplate`.

24 changes: 24 additions & 0 deletions components/select/demo/custom-template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Component } from '@angular/core';

@Component({
selector: 'nz-demo-select-custom-template',
template: `
<nz-select
style="width: 200px;"
nzAllowClear
nzPlaceHolder="Select OS"
[(ngModel)]="selectedOS"
[nzCustomTemplate]="custom"
>
<nz-option nzCustomContent nzLabel="Windows" nzValue="windows"><i nz-icon type="windows"></i> Windows</nz-option>
<nz-option nzCustomContent nzLabel="Mac" nzValue="mac"><i nz-icon type="apple"></i> Mac</nz-option>
<nz-option nzCustomContent nzLabel="Android" nzValue="android"><i nz-icon type="android"></i> Android</nz-option>
</nz-select>
<ng-template #custom let-selected>
<span>Label: {{ selected.nzLabel }} Value: {{ selected.nzValue }}</span>
</ng-template>
`
})
export class NzDemoSelectCustomTemplateComponent {
selectedOS = null;
}
1 change: 1 addition & 0 deletions components/select/doc/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import { NzSelectModule } from 'ng-zorro-antd';
| `[nzDropdownClassName]` | className of dropdown menu | `string` | - |
| `[nzDropdownMatchSelectWidth]` | Whether dropdown's with is same with select. | `boolean` | `true` |
| `[nzDropdownStyle]` | style of dropdown menu | `object` | - |
| `[nzCustomTemplate]` | The custom template of select | `TemplateRef<{ $implicit: NzOptionComponent }>` | - |
| `[nzServerSearch]` | nz-option will not be filtered when set to true | `boolean` | `false` |
| `[nzFilterOption]` | Filter options against it. The function will receive two arguments, `inputValue` and `option`, if the function returns `true`, the option will be included in the filtered set; Otherwise, it will be excluded. | `(input?: string, option?: NzOptionComponent) => boolean;` | - |
| `[nzMaxMultipleCount]` | Max selected option can be selected | `number` | `Infinity` |
Expand Down
1 change: 1 addition & 0 deletions components/select/doc/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import { NzSelectModule } from 'ng-zorro-antd';
| `[nzDropdownClassName]` | 下拉菜单的 className 属性 | `string` | - |
| `[nzDropdownMatchSelectWidth]` | 下拉菜单和选择器同宽 | `boolean` | `true` |
| `[nzDropdownStyle]` | 下拉菜单的 style 属性 | `object` | - |
| `[nzCustomTemplate]` | 自定义选择框的Template内容 | `TemplateRef<{ $implicit: NzOptionComponent }>` | - |
| `[nzServerSearch]` | 是否使用服务端搜索,当为 true 时,将不再在前端对 nz-option 进行过滤 | `boolean` | `false` |
| `[nzFilterOption]` | 是否根据输入项进行筛选。当其为一个函数时,会接收 `inputValue` `option` 两个参数,当 `option` 符合筛选条件时,应返回 `true`,反之则返回 `false`。 | `(input?: string, option?: NzOptionComponent) => boolean;` | - |
| `[nzMaxMultipleCount]` | 最多选中多少个标签| `number` | `Infinity` |
Expand Down
10 changes: 7 additions & 3 deletions components/select/nz-select-top-control.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
class="ant-select-selection-selected-value"
[attr.title]="nzSelectService.listOfCachedSelectedOption[0]?.nzLabel"
[ngStyle]="selectedValueStyle">
{{ nzSelectService.listOfCachedSelectedOption[0]?.nzLabel }}
<ng-container *nzStringTemplateOutlet="nzCustomTemplate; context: { $implicit: nzSelectService.listOfCachedSelectedOption[0] }">
<div class="ant-select-selection__choice__content">{{ nzSelectService.listOfCachedSelectedOption[0]?.nzLabel }}</div>
</ng-container>
</div>
<!--show search-->
<div *ngIf="nzShowSearch"
Expand All @@ -34,13 +36,15 @@
</ng-container>
<!--multiple or tags mode-->
<ul *ngIf="nzSelectService.isMultipleOrTags">
<ng-container *ngFor="let option of nzSelectService.listOfCachedSelectedOption | slice: 0 : nzMaxTagCount;trackBy:trackValue;">
<ng-container *ngFor="let option of nzSelectService.listOfCachedSelectedOption | slice: 0 : nzMaxTagCount;trackBy:trackValue; let index = index">
<li [@zoomMotion]
[nzNoAnimation]="noAnimation?.nzNoAnimation"
[attr.title]="option.nzLabel"
[class.ant-select-selection__choice__disabled]="option.nzDisabled"
class="ant-select-selection__choice">
<div class="ant-select-selection__choice__content">{{ option.nzLabel }}</div>
<ng-container *nzStringTemplateOutlet="nzCustomTemplate; context:{ $implicit: nzSelectService.listOfCachedSelectedOption[index] }">
<div class="ant-select-selection__choice__content">{{ option.nzLabel }}</div>
</ng-container>
<span *ngIf="!option.nzDisabled"
class="ant-select-selection__choice__remove"
(mousedown)="$event.preventDefault()"
Expand Down
8 changes: 7 additions & 1 deletion components/select/nz-select-top-control.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ import { NzSelectService } from './nz-select.service';
animations: [zoomMotion],
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
templateUrl: './nz-select-top-control.component.html'
templateUrl: './nz-select-top-control.component.html',
host: {
'[class.ant-select-selection--single]': 'nzSelectService.isSingleMode',
Copy link
Member

@vthinkxie vthinkxie Jun 22, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this is because I moved these into the component's host https://github.com/NG-ZORRO/ng-zorro-antd/pull/3071/files#diff-290abbcec228b4006c68b790f2878cf5L18
I can change it back in order to be consistent.

'[class.ant-select-selection--multiple]': 'nzSelectService.isMultipleOrTags',
'[style.cursor]': 'nzShowSearch ? "inherit" : "pointer"'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason add this line?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm.. I can't recall it's been too long... let me try.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't replicate the issue I ran into... it might be fixed by other updates, so I just removed it.

}
})
export class NzSelectTopControlComponent implements OnInit, OnDestroy {
inputValue: string;
Expand All @@ -50,6 +55,7 @@ export class NzSelectTopControlComponent implements OnInit, OnDestroy {
@Input() nzAllowClear = false;
@Input() nzShowArrow = true;
@Input() nzLoading = false;
@Input() nzCustomTemplate: TemplateRef<{ $implicit: NzOptionComponent }>;
@Input() nzSuffixIcon: TemplateRef<void>;
@Input() nzClearIcon: TemplateRef<void>;
@Input() nzRemoveIcon: TemplateRef<void>;
Expand Down
3 changes: 1 addition & 2 deletions components/select/nz-select.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@
[nzMaxTagCount]="nzMaxTagCount"
[nzShowArrow]="nzShowArrow"
[nzLoading]="nzLoading"
[nzCustomTemplate]="nzCustomTemplate"
[nzSuffixIcon]="nzSuffixIcon"
[nzClearIcon]="nzClearIcon"
[nzRemoveIcon]="nzRemoveIcon"
[nzShowSearch]="nzShowSearch"
[nzTokenSeparators]="nzTokenSeparators"
[class.ant-select-selection--single]="nzSelectService.isSingleMode"
[class.ant-select-selection--multiple]="nzSelectService.isMultipleOrTags"
(keydown)="onKeyDown($event)">
</div>
<ng-template
Expand Down
30 changes: 29 additions & 1 deletion components/select/nz-select.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,16 @@ describe('nz-select component', () => {
expect(targetElement.style.width).toBe('');
expect(targetElement.style.minWidth).toBe('10px');
}));
it('should custom template work', () => {
andrew-yangy marked this conversation as resolved.
Show resolved Hide resolved
select.nativeElement.click();
fixture.detectChanges();
expect(testComponent.open).toBe(true);
fixture.detectChanges();
overlayContainerElement.querySelector('li')!.click();
fixture.detectChanges();
const selection = select.nativeElement.querySelector('.ant-select-selection') as HTMLElement;
expect(selection.innerText).toContain('Label: Jack\nValue: jack');
});
it('should click option close dropdown', () => {
testComponent.showSearch = true;
select.nativeElement.click();
Expand Down Expand Up @@ -279,6 +289,15 @@ describe('nz-select component', () => {
fixture.detectChanges();
expect(testComponent.selectedValue.length).toBe(0);
}));
it('should custom template work', fakeAsync(() => {
fixture.detectChanges();
selectComponent.nzSelectService.updateListOfSelectedValue(['jack'], true);
fixture.detectChanges();
flush();
fixture.detectChanges();
const selection = select.nativeElement.querySelector('.ant-select-selection') as HTMLElement;
expect(selection.innerText).toContain('Label: Jack\nValue: jack');
}));
});

describe('form', () => {
Expand Down Expand Up @@ -406,13 +425,18 @@ describe('nz-select component', () => {
[nzDropdownMatchSelectWidth]="dropdownMatchSelectWidth"
[nzDropdownStyle]="dropdownStyle"
[nzDropdownClassName]="'test-class'"
[nzCustomTemplate]="custom"
(nzOnSearch)="onSearch($event)"
[nzPlaceHolder]="placeholder"
>
<nz-option nzValue="jack" nzLabel="Jack"></nz-option>
<nz-option nzValue="lucy" nzLabel="Lucy"></nz-option>
<nz-option nzValue="disabled" nzLabel="Disabled" nzDisabled></nz-option>
</nz-select>
<ng-template #custom let-selected>
<div>Label: {{ selected.nzLabel }}</div>
<div>Value: {{ selected.nzValue }}</div>
</ng-template>
`
})
export class NzTestSelectDefaultComponent {
Expand Down Expand Up @@ -442,11 +466,15 @@ export class NzTestSelectDefaultComponent {

@Component({
template: `
<nz-select [(ngModel)]="selectedValue" [nzAllowClear]="true" [nzMode]="'tags'">
<nz-select [(ngModel)]="selectedValue" [nzAllowClear]="true" [nzMode]="'tags'" [nzCustomTemplate]="custom">
<nz-option nzValue="jack" nzLabel="Jack"></nz-option>
<nz-option nzValue="lucy" nzLabel="Lucy"></nz-option>
<nz-option nzValue="disabled" nzLabel="Disabled" nzDisabled nzCustomContent>Disabled</nz-option>
</nz-select>
<ng-template #custom let-selected>
<div>Label: {{ selected.nzLabel }}</div>
<div>Value: {{ selected.nzValue }}</div>
</ng-template>
`
})
export class NzTestSelectTagsComponent {
Expand Down
1 change: 1 addition & 0 deletions components/select/nz-select.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export class NzSelectComponent implements ControlValueAccessor, OnInit, AfterVie
@Input() nzPlaceHolder: string;
@Input() nzMaxTagCount: number;
@Input() nzDropdownRender: TemplateRef<void>;
@Input() nzCustomTemplate: TemplateRef<{ $implicit: NzOptionComponent }>;
@Input() nzSuffixIcon: TemplateRef<void>;
@Input() nzClearIcon: TemplateRef<void>;
@Input() nzRemoveIcon: TemplateRef<void>;
Expand Down