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

fix(module:cascader): select and render leaf label & reset in reactive form (#335) (#336) #356

Merged
merged 1 commit into from
Sep 22, 2017
Merged
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
11 changes: 6 additions & 5 deletions src/components/cascader/nz-cascader.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function noop(): void { }

function toArray(value: any): any[] {
let ret = value;
if (value === undefined) {
if (value === undefined || value === null) {
ret = [];
} else if (!Array.isArray(value)) {
ret = [value];
Expand Down Expand Up @@ -762,6 +762,11 @@ export class NzCascaderComponent implements OnInit, OnDestroy, OnChanges, AfterV
}, (reason: any) => {
option.isLeaf = true;
});
} else {
// clicking leaf node will remove any children columns
if (index < this._nzColumns.length - 1) {
this._nzColumns = this._nzColumns.slice(0, index + 1);
}
}

// 生成显示
Expand Down Expand Up @@ -982,10 +987,6 @@ export class NzCascaderComponent implements OnInit, OnDestroy, OnChanges, AfterV
* @Override (From ControlValueAccessor interface)
*/
writeValue(value: any): void {
if (value == null) {
return;
}

const array: any[] = [];
toArray(value).forEach((v: any, index: number) => {
if (typeof v !== 'object') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ const init_options = [{
label: 'West Lake',
isLeaf: true
}],
}, {
value: 'ningbo',
label: 'Ningbo',
isLeaf: true
}],
}, {
value: 'jiangsu',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ const options = [{
label: 'West Lake',
isLeaf: true
}],
}, {
value: 'ningbo',
label: 'Ningbo',
isLeaf: true
}],
}, {
value: 'jiangsu',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ const options = [{
code: 752100,
isLeaf: true
}],
}, {
value: 'ningbo',
label: 'Ningbo',
isLeaf: true
}],
}, {
value: 'jiangsu',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ const options = [{
label: 'West Lake',
isLeaf: true
}],
}, {
value: 'ningbo',
label: 'Ningbo',
isLeaf: true
}],
}, {
value: 'jiangsu',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ const options = [{
label: 'West Lake',
isLeaf: true
}],
}, {
value: 'ningbo',
label: 'Ningbo',
isLeaf: true
}],
}, {
value: 'jiangsu',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ const options = [{
label: 'West Lake',
isLeaf: true
}],
}, {
value: 'ningbo',
label: 'Ningbo',
isLeaf: true
}],
}, {
value: 'jiangsu',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ const options = [{
label: 'West Lake',
isLeaf: true
}],
}, {
value: 'ningbo',
label: 'Ningbo',
isLeaf: true
}],
}, {
value: 'jiangsu',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ const cities = {
zhejiang: [{
value: 'hangzhou',
label: 'Hangzhou',
}, {
value: 'ningbo',
label: 'Ningbo',
isLeaf: true
}],
jiangsu: [{
value: 'nanjing',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import {Component, OnInit} from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';

const init_options = [{
value: 'zhejiang',
label: 'Zhejiang',
children: [{
value: 'hangzhou',
label: 'Hangzhou',
children: [{
value: 'xihu',
label: 'West Lake',
isLeaf: true
}],
}, {
value: 'ningbo',
label: 'Ningbo',
isLeaf: true
}],
}, {
value: 'jiangsu',
label: 'Jiangsu',
children: [{
value: 'nanjing',
label: 'Nanjing',
children: [{
value: 'zhonghuamen',
label: 'Zhong Hua Men',
isLeaf: true
}],
}],
}];

@Component({
selector: 'nz-demo-cascader-reactive-form',
template: `
<form [formGroup]="_form" novalidate>
<nz-cascader
[class.class123]="true"
[nzOptions]="_options"
(nzChange)="_console($event)"
[formControlName]="'name'">
</nz-cascader>
</form>
<br>
<button nz-button (click)="_reset()">Reset</button>
`,
styles : [
`

`
]
})
export class NzDemoCascaderReactiveFormComponent implements OnInit {
/** init data */
_options = null;

_value: any[] = null;

_form: FormGroup;

_console(value) {
console.log(value);
}

constructor(private _fb: FormBuilder) {
this._createForm();
}

ngOnInit() {
this._options = init_options;
}

_createForm() {
this._form = this._fb.group({
name: [null, Validators.required ]
});
}

_reset(): void {
this._form.reset();
}
}

14 changes: 10 additions & 4 deletions src/showcase/nz-demo-cascader/nz-demo-cascader-size.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ const options = [{
label: 'West Lake',
isLeaf: true
}],
}, {
value: 'ningbo',
label: 'Ningbo',
isLeaf: true
}],
}, {
value: 'jiangsu',
Expand All @@ -33,22 +37,22 @@ const options = [{
<nz-cascader
[nzSize]="'large'"
[nzOptions]="_options"
[(ngModel)]="_value"
[(ngModel)]="_value1"
(ngModelChange)="_console($event)"
(nzChange)="_console($event)">
</nz-cascader>
<br><br>
<nz-cascader
[nzOptions]="_options"
[(ngModel)]="_value"
[(ngModel)]="_value2"
(ngModelChange)="_console($event)"
(nzChange)="_console($event)">
</nz-cascader>
<br><br>
<nz-cascader
[nzSize]="'small'"
[nzOptions]="_options"
[(ngModel)]="_value"
[(ngModel)]="_value3"
(ngModelChange)="_console($event)"
(nzChange)="_console($event)">
</nz-cascader>
Expand All @@ -59,7 +63,9 @@ export class NzDemoCascaderSizeComponent implements OnInit {
/** init data */
_options = options;

_value: any[] = null;
_value1: any[] = null;
_value2: any[] = null;
_value3: any[] = null;

_console(value) {
console.log(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export class NzDemoCascaderComponent implements OnInit {
NzDemoCascaderChangeOnSelectCode = require('!!raw-loader!./nz-demo-cascader-change-on-select.component');
NzDemoCascaderCustomRenderCode = require('!!raw-loader!./nz-demo-cascader-custom-render.component');
NzDemoCascaderLazyCode = require('!!raw-loader!./nz-demo-cascader-lazy.component');
NzDemoCascaderReactiveFormCode = require('!!raw-loader!./nz-demo-cascader-reactive-form.component');

constructor() {
}
Expand Down
7 changes: 7 additions & 0 deletions src/showcase/nz-demo-cascader/nz-demo-cascader.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ <h2>代码演示<i class="code-box-expand-trigger anticon anticon-appstore" titl
</div>
</nz-code-box>

<nz-code-box [nzTitle]="'响应式表单'" id="components-cascader-demo-reactive-form" [nzCode]="NzDemoCascaderReactiveFormCode">
<nz-demo-cascader-reactive-form demo></nz-demo-cascader-reactive-form>
<div intro>
<p>创建响应式表单组件,并通过 <code>reset()</code> 方法重置数值。</p>
</div>
</nz-code-box>

</div>
</div>
<section class="markdown api-container">
Expand Down
8 changes: 5 additions & 3 deletions src/showcase/nz-demo-cascader/nz-demo-cascader.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';

import { NzDemoCascaderBasicComponent } from './nz-demo-cascader-basic.component';
import { NzDemoCascaderCustomTriggerComponent } from './nz-demo-cascader-custom-trigger.component';
Expand All @@ -11,14 +11,15 @@ import { NzDemoCascaderHoverComponent } from './nz-demo-cascader-hover.component
import { NzDemoCascaderChangeOnSelectComponent } from './nz-demo-cascader-change-on-select.component';
import { NzDemoCascaderCustomRenderComponent } from './nz-demo-cascader-custom-render.component';
import { NzDemoCascaderLazyComponent } from './nz-demo-cascader-lazy.component';
import { NzDemoCascaderReactiveFormComponent } from './nz-demo-cascader-reactive-form.component';

import { NzDemoCascaderComponent } from './nz-demo-cascader.component';
import { NzCodeBoxModule } from '../share/nz-codebox/nz-codebox.module';
import { NgZorroAntdModule } from '../../../index.showcase';
import { NzDemoCascaderRoutingModule } from './nz-demo-cascader.routing.module';

@NgModule({
imports : [ NzDemoCascaderRoutingModule, CommonModule, FormsModule, NzCodeBoxModule, NgZorroAntdModule ],
imports : [ NzDemoCascaderRoutingModule, CommonModule, FormsModule, ReactiveFormsModule, NzCodeBoxModule, NgZorroAntdModule ],
declarations: [
NzDemoCascaderComponent,
NzDemoCascaderBasicComponent,
Expand All @@ -29,7 +30,8 @@ import { NzDemoCascaderRoutingModule } from './nz-demo-cascader.routing.module';
NzDemoCascaderHoverComponent,
NzDemoCascaderChangeOnSelectComponent,
NzDemoCascaderCustomRenderComponent,
NzDemoCascaderLazyComponent
NzDemoCascaderLazyComponent,
NzDemoCascaderReactiveFormComponent
]
})

Expand Down