Skip to content

Commit 40815ad

Browse files
JackYumgwethin
andauthored
feat(module:tree-select): add status (#7477)
* feat(module:tree-select): add status add 'nzStatus' , display diffrent status of tree-select * docs update 'nzStatus' docs * test add 'nzStatus' tests * docs(module:tree-select): update status docs * update 'status' demo * fix(module:tree-select): add 'NzSpaceModule' * fix module of demo need to depend on NzSpaceModule Co-authored-by: wethin <806769481@qq.com>
1 parent 999215e commit 40815ad

7 files changed

Lines changed: 146 additions & 5 deletions

File tree

components/tree-select/demo/module

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { NzTreeSelectModule } from 'ng-zorro-antd/tree-select';
22
import { NzIconModule } from 'ng-zorro-antd/icon';
33
import { FormsModule } from '@angular/forms';
4+
import { NzSpaceModule } from 'ng-zorro-antd/space';
45

5-
export const moduleList = [ FormsModule, NzTreeSelectModule, NzIconModule ];
6+
7+
export const moduleList = [ FormsModule, NzTreeSelectModule, NzIconModule , NzSpaceModule];
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
order: 8
3+
title:
4+
zh-CN: 自定义状态
5+
en-US: Status
6+
---
7+
8+
## zh-CN
9+
10+
使用 `nzStatus``TreeSelect` 添加状态,可选 `error` 或者 `warning`
11+
12+
## en-US
13+
14+
Add status to TreeSelect with `nzStatus`, which could be `error` or `warning`.
15+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { Component } from '@angular/core';
2+
3+
@Component({
4+
selector: 'nz-demo-tree-select-status',
5+
template: `
6+
<nz-space nzDirection="vertical" style="width:100%;">
7+
<nz-tree-select
8+
*nzSpaceItem
9+
[nzNodes]="nodes"
10+
nzStatus="error"
11+
nzPlaceHolder="Error"
12+
[(ngModel)]="value"
13+
style="width:100%;"
14+
></nz-tree-select>
15+
<nz-tree-select
16+
*nzSpaceItem
17+
nzMultiple
18+
[nzNodes]="nodes"
19+
nzShowSearch
20+
nzStatus="warning"
21+
nzPlaceHolder="Warning multiple"
22+
[(ngModel)]="value"
23+
style="width:100%;"
24+
></nz-tree-select>
25+
</nz-space>
26+
`
27+
})
28+
export class NzDemoTreeSelectStatusComponent {
29+
value?: string;
30+
nodes = [];
31+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import { NzTreeSelectModule } from 'ng-zorro-antd/tree-select';
3535
| `[nzMultiple]` | Support multiple or not, will be `true` when enable `nzCheckable`. | `boolean` | `false` |
3636
| `[nzHideUnMatched]` | Hide unmatched nodes while searching | `boolean` | `false` ||
3737
| `[nzSize]` | To set the size of the select input | `'large' \| 'small' \| 'default'` | `'default'` ||
38+
| `[nzStatus]` | Set validation status | `'error' \| 'warning'` | - | |
3839
| `[nzCheckable]` | Whether to show checkbox on the treeNodes | `boolean` | `false` |
3940
| `[nzCheckStrictly]` | Check treeNode precisely; parent treeNode and children treeNodes are not associated | `boolean` | `false` |
4041
| `[nzShowExpand]` | Show a Expand Icon before the treeNodes | `boolean` | `true` | |

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import { NzTreeSelectModule } from 'ng-zorro-antd/tree-select';
3535
| `[nzMultiple]` | 支持多选(当设置 nzCheckable 时自动变为true) | `boolean` | `false` |
3636
| `[nzHideUnMatched]` | 搜索隐藏未匹配的节点 | `boolean` | `false` ||
3737
| `[nzSize]` | 选择框大小 | `'large' \| 'small' \| 'default'` | `'default'` ||
38+
| `[nzStatus]` | 设置校验状态 | `'error' \| 'warning'` | - | |
3839
| `[nzCheckable]` | 节点前添加 Checkbox 复选框 | `boolean` | `false` |
3940
| `[nzCheckStrictly]` | checkable 状态下节点选择完全受控(父子节点选中状态不再关联) | `boolean` | `false` |
4041
| `[nzShowExpand]` | 节点前添加展开图标 | `boolean` | `true` | |

components/tree-select/tree-select.component.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,16 @@ import {
4444
NzTreeNode,
4545
NzTreeNodeOptions
4646
} from 'ng-zorro-antd/core/tree';
47-
import { BooleanInput, NgStyleInterface, NzSizeLDSType, OnChangeType, OnTouchedType } from 'ng-zorro-antd/core/types';
48-
import { InputBoolean, isNotNil } from 'ng-zorro-antd/core/util';
47+
import {
48+
BooleanInput,
49+
NgClassInterface,
50+
NgStyleInterface,
51+
NzSizeLDSType,
52+
NzStatus,
53+
OnChangeType,
54+
OnTouchedType
55+
} from 'ng-zorro-antd/core/types';
56+
import { getStatusClassNames, InputBoolean, isNotNil } from 'ng-zorro-antd/core/util';
4957
import { NzSelectSearchComponent } from 'ng-zorro-antd/select';
5058
import { NzTreeComponent } from 'ng-zorro-antd/tree';
5159

@@ -253,6 +261,7 @@ export class NzTreeSelectComponent extends NzTreeBase implements ControlValueAcc
253261
@Input() nzDropdownStyle: NgStyleInterface | null = null;
254262
@Input() nzDropdownClassName?: string;
255263
@Input() @WithConfig() nzBackdrop = false;
264+
@Input() nzStatus: NzStatus = '';
256265
@Input()
257266
set nzExpandedKeys(value: string[]) {
258267
this.expandedKeys = value;
@@ -285,6 +294,10 @@ export class NzTreeSelectComponent extends NzTreeBase implements ControlValueAcc
285294
return this.nzTreeTemplate || this.nzTreeTemplateChild;
286295
}
287296

297+
prefixCls: string = 'ant-select';
298+
statusCls: NgClassInterface = {};
299+
nzHasFeedback: boolean = false;
300+
288301
dropdownClassName = TREE_SELECT_DEFAULT_CLASS;
289302
triggerWidth?: number;
290303
isComposing = false;
@@ -370,15 +383,30 @@ export class NzTreeSelectComponent extends NzTreeBase implements ControlValueAcc
370383
this.closeDropDown();
371384
}
372385

386+
private setStatusStyles(): void {
387+
// render status if nzStatus is set
388+
this.statusCls = getStatusClassNames(this.prefixCls, this.nzStatus, this.nzHasFeedback);
389+
Object.keys(this.statusCls).forEach(status => {
390+
if (this.statusCls[status]) {
391+
this.renderer.addClass(this.elementRef.nativeElement, status);
392+
} else {
393+
this.renderer.removeClass(this.elementRef.nativeElement, status);
394+
}
395+
});
396+
}
397+
373398
ngOnChanges(changes: SimpleChanges): void {
374-
const { nzNodes, nzDropdownClassName } = changes;
399+
const { nzNodes, nzDropdownClassName, nzStatus } = changes;
375400
if (nzNodes) {
376401
this.updateSelectedNodes(true);
377402
}
378403
if (nzDropdownClassName) {
379404
const className = this.nzDropdownClassName && this.nzDropdownClassName.trim();
380405
this.dropdownClassName = className ? `${TREE_SELECT_DEFAULT_CLASS} ${className}` : TREE_SELECT_DEFAULT_CLASS;
381406
}
407+
if (nzStatus) {
408+
this.setStatusStyles();
409+
}
382410
}
383411

384412
writeValue(value: string[] | string): void {

components/tree-select/tree-select.spec.ts

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
typeInElement
1717
} from 'ng-zorro-antd/core/testing';
1818
import { NzTreeNode, NzTreeNodeOptions } from 'ng-zorro-antd/core/tree';
19+
import { NzStatus } from 'ng-zorro-antd/core/types';
1920

2021
import { NzTreeSelectComponent } from './tree-select.component';
2122
import { NzTreeSelectModule } from './tree-select.module';
@@ -33,7 +34,8 @@ describe('tree-select component', () => {
3334
NzTestTreeSelectBasicComponent,
3435
NzTestTreeSelectCheckableComponent,
3536
NzTestTreeSelectFormComponent,
36-
NzTestTreeSelectCustomizedIconComponent
37+
NzTestTreeSelectCustomizedIconComponent,
38+
NzTestTreeSelectStatusComponent
3739
],
3840
providers: [
3941
{
@@ -606,6 +608,29 @@ describe('tree-select component', () => {
606608
expect(overlayContainerElement.querySelector('i.anticon.anticon-frown-o')).toBeTruthy();
607609
}));
608610
});
611+
612+
describe('Status', () => {
613+
let fixture: ComponentFixture<NzTestTreeSelectStatusComponent>;
614+
let treeSelect: DebugElement;
615+
616+
beforeEach(() => {
617+
fixture = TestBed.createComponent(NzTestTreeSelectStatusComponent);
618+
treeSelect = fixture.debugElement.query(By.directive(NzTreeSelectComponent));
619+
});
620+
621+
it('should className correct', () => {
622+
fixture.detectChanges();
623+
expect(treeSelect.nativeElement.className).toContain('ant-select-status-error');
624+
625+
fixture.componentInstance.status = 'warning';
626+
fixture.detectChanges();
627+
expect(treeSelect.nativeElement.className).toContain('ant-select-status-warning');
628+
629+
fixture.componentInstance.status = '';
630+
fixture.detectChanges();
631+
expect(treeSelect.nativeElement.className).not.toContain('ant-select-status-warning');
632+
});
633+
});
609634
});
610635

611636
@Component({
@@ -863,3 +888,41 @@ export class NzTestTreeSelectCustomizedIconComponent {
863888
})
864889
];
865890
}
891+
892+
@Component({
893+
template: `
894+
<nz-tree-select
895+
style="width:100%;margin:20px 0;"
896+
[nzNodes]="nodes"
897+
nzShowSearch
898+
[nzStatus]="status"
899+
nzPlaceHolder="Please select"
900+
[(ngModel)]="value"
901+
></nz-tree-select>
902+
`
903+
})
904+
export class NzTestTreeSelectStatusComponent {
905+
status: NzStatus = 'error';
906+
value?: string = '1001';
907+
nodes = [
908+
{
909+
title: 'parent 1',
910+
key: '100',
911+
children: [
912+
{
913+
title: 'parent 1-0',
914+
key: '1001',
915+
children: [
916+
{ title: 'leaf 1-0-0', key: '10010', isLeaf: true },
917+
{ title: 'leaf 1-0-1', key: '10011', isLeaf: true }
918+
]
919+
},
920+
{
921+
title: 'parent 1-1',
922+
key: '1002',
923+
children: [{ title: 'leaf 1-1-0', key: '10020', isLeaf: true }]
924+
}
925+
]
926+
}
927+
];
928+
}

0 commit comments

Comments
 (0)