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:tree-select): support customized icon #2055

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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/tree-select/demo/customized-icon.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
order: 4
debug: true
title:
zh-CN: 自定义图标
en-US: customize
---

## zh-CN

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

## en-US

You can customize icons for different nodes by styles override.
68 changes: 68 additions & 0 deletions components/tree-select/demo/customized-icon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { Component } from '@angular/core';

@Component({
selector: 'nz-demo-tree-select-customized-icon',
template: `
<nz-tree-select
style="width: 250px"
[nzNodes]="nodes"
[(ngModel)]="value"
nzPlaceHolder="Please select"
>
<ng-template #nzTreeTemplate let-node>
<span class="ant-tree-node-content-wrapper" [class.ant-tree-node-selected]="node.isSelected">
<span>
<i class="anticon anticon-smile-o" *ngIf="node.isExpanded"></i>
<i class="anticon anticon-frown-o" *ngIf="!node.isExpanded"></i> {{node.title}}
</span>
</span>
</ng-template>
</nz-tree-select>`,
styles : [ `
.anticon {
padding-right: 4px;
}
` ]
})
export class NzDemoTreeSelectCustomizedIconComponent {
value: string;
nodes = [
{
title : 'root1',
key : '1001',
expanded: true,
children: [
{
title : 'child1',
key : '10001',
children: [
{
title : 'child1.1',
key : '100011',
selected: true,
children: []
},
{
title : 'child1.2',
key : '100012',
children: [
{
title : 'grandchild1.2.1',
key : '1000121',
isLeaf : true,
checked : true,
disabled: true
},
{
title : 'grandchild1.2.2',
key : '1000122',
isLeaf: true
}
]
}
]
}
]
}
];
}
7 changes: 7 additions & 0 deletions components/tree-select/nz-tree-select.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
AfterViewInit,
ChangeDetectorRef,
Component,
ContentChild,
ElementRef,
EventEmitter,
HostListener,
Expand Down Expand Up @@ -102,6 +103,8 @@ export class NzTreeSelectComponent implements ControlValueAccessor, OnInit, Afte
@Output() readonly nzTreeClick = new EventEmitter<NzFormatEmitEvent>();
@Output() readonly nzTreeCheckBoxChange = new EventEmitter<NzFormatEmitEvent>();

@ContentChild('nzTreeTemplate') nzTreeSelectTemplate: TemplateRef<{}>;

@ViewChild('inputElement') inputElement: ElementRef;
@ViewChild('treeSelect') treeSelect: ElementRef;
@ViewChild('dropdownTemplate', { read: TemplateRef }) dropdownTemplate;
Expand Down Expand Up @@ -182,6 +185,10 @@ export class NzTreeSelectComponent implements ControlValueAccessor, OnInit, Afte

ngAfterViewInit(): void {
this.attachOverlay();
if (this.nzTreeSelectTemplate) {
// Throw ExpressionChangedAfterItHasBeenCheckedError without setTimeout
setTimeout(() => this.treeRef.nzTreeTemplate = this.nzTreeSelectTemplate, 0);
}
}

setDisabledState(isDisabled: boolean): void {
Expand Down
59 changes: 58 additions & 1 deletion components/tree-select/nz-tree-select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('tree-select component', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports : [ NzTreeSelectModule, NoopAnimationsModule, FormsModule, ReactiveFormsModule ],
declarations: [ NzTestTreeSelectBasicComponent, NzTestTreeSelectCheckableComponent, NzTestTreeSelectFormComponent ]
declarations: [ NzTestTreeSelectBasicComponent, NzTestTreeSelectCheckableComponent, NzTestTreeSelectFormComponent, NzTestTreeSelectCustomizedIconComponent ]
});
TestBed.compileComponents();
inject([ OverlayContainer ], (oc: OverlayContainer) => {
Expand Down Expand Up @@ -400,6 +400,28 @@ describe('tree-select component', () => {
}));
});

describe('customized icon', () => {
let fixture;
let testComponent;
let treeSelect;
let treeSelectComponent;
beforeEach(() => {
fixture = TestBed.createComponent(NzTestTreeSelectCustomizedIconComponent);
testComponent = fixture.debugElement.componentInstance;
treeSelect = fixture.debugElement.query(By.directive(NzTreeSelectComponent));
treeSelectComponent = treeSelect.componentInstance;
});
it('should display', fakeAsync(() => {
treeSelectComponent.ngAfterViewInit();
flush();
fixture.detectChanges();
treeSelect.nativeElement.click();
fixture.detectChanges();
flush();
expect(overlayContainerElement.querySelector('i.anticon.anticon-frown-o')).toBeTruthy();
}));
});

});

@Component({
Expand Down Expand Up @@ -623,3 +645,38 @@ export class NzTestTreeSelectFormComponent {
this.formGroup.get('select').reset(null);
}
}

@Component({
selector: 'nz-test-tree-select-customized-icon',
template: `
<nz-tree-select
[nzNodes]="nodes"
[(ngModel)]="value"
>
<ng-template #nzTreeTemplate let-node>
<span>
<i class="anticon anticon-frown-o"></i> {{node.title}}
</span>
</ng-template>
</nz-tree-select>
`
})
export class NzTestTreeSelectCustomizedIconComponent {
value: string;
nodes = [
new NzTreeNode({
title : 'root3',
key : '1003',
children: [
{
title: 'child3.1',
key : '10031'
},
{
title: 'child3.2',
key : '10032'
}
]
})
];
}