forked from NG-ZORRO/ng-zorro-antd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(module:cascader): refactor cascader
close NG-ZORRO#2935, NG-ZORRO#2198
- Loading branch information
Wendell
committed
Nov 27, 2018
1 parent
f6361b6
commit e7428f8
Showing
17 changed files
with
866 additions
and
1,162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--- | ||
order: 16 | ||
title: | ||
zh-CN: 自定义字段名 | ||
en-US: Custom Field Names | ||
--- | ||
|
||
## zh-CN | ||
|
||
自定义字段名。 | ||
|
||
## en-US | ||
|
||
Custom field names. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// tslint:disable:no-any | ||
import { Component } from '@angular/core'; | ||
|
||
const options = [{ | ||
code: 'zhejiang', | ||
name: 'Zhejiang', | ||
children: [{ | ||
code: 'hangzhou', | ||
name: 'Hangzhou', | ||
children: [{ | ||
code: 'xihu', | ||
name: 'West Lake', | ||
isLeaf: true | ||
}] | ||
}, { | ||
code: 'ningbo', | ||
name: 'Ningbo', | ||
children: [{ | ||
code: 'dongqianlake', | ||
name: 'Dongqian Lake', | ||
isLeaf: true | ||
}] | ||
}] | ||
}, { | ||
code: 'jiangsu', | ||
name: 'Jiangsu', | ||
children: [{ | ||
code: 'nanjing', | ||
name: 'Nanjing', | ||
children: [{ | ||
code: 'zhonghuamen', | ||
name: 'Zhong Hua Men', | ||
isLeaf: true | ||
}] | ||
}] | ||
}]; | ||
|
||
@Component({ | ||
selector: 'nz-demo-cascader-custom-field-names', | ||
template: ` | ||
<nz-cascader | ||
[nzChangeOn]="validate" | ||
[nzOptions]="nzOptions" | ||
[nzLabelProperty]="'name'" | ||
[nzValueProperty]="'code'" | ||
[nzShowSearch]="true" | ||
[(ngModel)]="values" | ||
(ngModelChange)="onChanges($event)"> | ||
</nz-cascader>`, | ||
styles : [ | ||
` | ||
.ant-cascader-picker { | ||
width: 300px; | ||
} | ||
` | ||
] | ||
}) | ||
export class NzDemoCascaderCustomFieldNamesComponent { | ||
/** init data */ | ||
nzOptions = options; | ||
|
||
/** ngModel value */ | ||
public values: any[] = null; | ||
|
||
public onChanges(values: any): void { | ||
console.log(values, this.values); | ||
} | ||
|
||
public validate(option: any, index: number): boolean { | ||
const value = option.value; | ||
return ['hangzhou', 'xihu', 'nanjing', 'zhonghuamen'].indexOf(value) >= 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<ng-container *ngIf="highlightText"><span [innerHTML]="renderHighlightString(getOptionLabel())"></span></ng-container> | ||
<ng-container *ngIf="!highlightText">{{ getOptionLabel() }}</ng-container> | ||
<span *ngIf="!option.isLeaf || option.children && option.children.length || option.loading" class="ant-cascader-menu-item-expand-icon"> | ||
<i nz-icon [type]="option.loading ? 'loading' : 'right'"></i> | ||
</span> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { ChangeDetectionStrategy, Component, Input, SecurityContext, ViewEncapsulation } from '@angular/core'; | ||
import { DomSanitizer } from '@angular/platform-browser'; | ||
import { CascaderOption } from './types'; | ||
|
||
@Component({ | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
encapsulation : ViewEncapsulation.None, | ||
selector : '[nz-cascader-option]', | ||
templateUrl : './nz-cascader-li.component.html', | ||
host : { | ||
'[attr.title]' : 'option.title || getOptionLabel()', | ||
'[class.ant-cascader-menu-item]' : 'true', | ||
'[class.ant-cascader-menu-item-active]' : 'activated', | ||
'[class.ant-cascader-menu-item-expand]' : '!option.isLeaf', | ||
'[class.ant-cascader-menu-item-disabled]': 'option.disabled' | ||
} | ||
}) | ||
export class NzCascaderOptionComponent { | ||
@Input() option: CascaderOption; | ||
@Input() activated = false; | ||
@Input() highlightText: string; | ||
@Input() nzLabelProperty = 'label'; | ||
|
||
constructor(private sanitizer: DomSanitizer) {} | ||
|
||
getOptionLabel(): string { | ||
return this.option ? this.option[ this.nzLabelProperty ] : ''; | ||
} | ||
|
||
renderHighlightString(str: string): string { | ||
const safeHtml = this.sanitizer.sanitize(SecurityContext.HTML, `<span class="ant-cascader-menu-item-keyword">${this.highlightText}</span>`); | ||
if (!safeHtml) { | ||
throw new Error(`[NG-ZORRO] Input value "${this.highlightText}" is not considered security.`); | ||
} | ||
return str.replace(new RegExp(this.highlightText, 'g'), safeHtml); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.