Skip to content

Commit aea80c4

Browse files
wilsoncookvthinkxie
authored andcommitted
feat(module:slider): support dynamic update nzMarks from outside (#636)
close #624
1 parent d2fc97d commit aea80c4

4 files changed

Lines changed: 37 additions & 19 deletions

File tree

src/components/slider/nz-slider-marks.component.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,34 @@ export class NzSliderMarksComponent implements OnInit, OnChanges {
1414
// Dynamic properties
1515
@Input() nzLowerBound: number = null;
1616
@Input() nzUpperBound: number = null;
17+
@Input() nzMarksArray: MarksArray;
1718

1819
// Static properties
1920
@Input() nzClassName: string;
2021
@Input() nzVertical: boolean; // Required
21-
@Input() nzMarksArray: MarksArray; // Required
2222
@Input() nzMin: number; // Required
2323
@Input() nzMax: number; // Required
2424
@Input() nzIncluded: boolean;
2525

2626
attrs; // points for inner use
2727

2828
ngOnChanges(changes: SimpleChanges) {
29-
if (changes.nzLowerBound || changes.nzUpperBound) {
29+
if (changes.nzMarksArray) {
30+
this.buildAttrs();
31+
}
32+
if (changes.nzMarksArray || changes.nzLowerBound || changes.nzUpperBound) {
3033
this.togglePointActive();
3134
}
3235
}
3336

34-
ngOnInit() {
35-
const { nzVertical, nzClassName, nzMarksArray, nzMin, nzMax, nzLowerBound, nzUpperBound } = this;
37+
ngOnInit() {}
38+
39+
trackById(index: number, attr) {
40+
return attr.id;
41+
}
42+
43+
buildAttrs() {
44+
const { nzVertical, nzClassName, nzMarksArray, nzMin, nzMax } = this;
3645
const range = nzMax - nzMin;
3746
this.attrs = nzMarksArray.map(mark => {
3847
const { value, offset, config } = mark;
@@ -72,11 +81,6 @@ export class NzSliderMarksComponent implements OnInit, OnChanges {
7281
label : label
7382
};
7483
}); // END - map
75-
this.togglePointActive();
76-
}
77-
78-
trackById(index: number, attr) {
79-
return attr.id;
8084
}
8185

8286
togglePointActive() {

src/components/slider/nz-slider-step.component.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,31 @@ export class NzSliderStepComponent implements OnInit, OnChanges {
1515
// Dynamic properties
1616
@Input() nzLowerBound: number = null;
1717
@Input() nzUpperBound: number = null;
18+
@Input() nzMarksArray: any[];
1819

1920
// Static properties
2021
@Input() nzPrefixCls: string;
2122
@Input() nzVertical: boolean;
22-
@Input() nzMarksArray: any[];
2323
@Input() nzIncluded: boolean;
2424

2525
attrs;
2626

2727
ngOnChanges(changes: SimpleChanges) {
28-
if (changes.nzLowerBound || changes.nzUpperBound) {
28+
if (changes.nzMarksArray) {
29+
this.buildAttrs();
30+
}
31+
if (changes.nzMarksArray || changes.nzLowerBound || changes.nzUpperBound) {
2932
this.togglePointActive();
3033
}
3134
}
3235

33-
ngOnInit() {
36+
ngOnInit() {}
37+
38+
trackById(index: number, attr) {
39+
return attr.id;
40+
}
41+
42+
buildAttrs() {
3443
const orient = this.nzVertical ? 'bottom' : 'left', prefixCls = this.nzPrefixCls;
3544
this.attrs = this.nzMarksArray.map(mark => {
3645
const { value, offset } = mark;
@@ -47,11 +56,6 @@ export class NzSliderStepComponent implements OnInit, OnChanges {
4756
}
4857
};
4958
});
50-
this.togglePointActive();
51-
}
52-
53-
trackById(index: number, attr) {
54-
return attr.id;
5559
}
5660

5761
togglePointActive() {

src/components/slider/nz-slider.component.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,12 @@ export class NzSliderComponent implements ControlValueAccessor, OnInit, OnChange
250250
}
251251

252252
ngOnChanges(changes: SimpleChanges) {
253-
const { nzDisabled } = changes;
253+
const { nzDisabled, nzMarks } = changes;
254254
if (nzDisabled && !nzDisabled.firstChange) {
255255
this.toggleDragDisabled(nzDisabled.currentValue);
256256
this.setClassMap();
257+
} else if (nzMarks && !nzMarks.firstChange) {
258+
this.marksArray = this.nzMarks ? this.toMarksArray(this.nzMarks) : null;
257259
}
258260
}
259261

src/showcase/nz-demo-slider/nz-demo-slider-mark.component.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { Component } from '@angular/core';
1414
<h4>step=null || dots=true</h4>
1515
<nz-slider [nzMarks]="marks" [nzStep]="null" [nzDefaultValue]="37"></nz-slider>
1616
<nz-slider [nzMarks]="marks" [nzDots]="true" [nzDefaultValue]="37"></nz-slider>
17+
Change nzMarks dynamically: <button nz-button (click)="changeMarks()">Change nzMarks</button>
1718
</div>
1819
`,
1920
styles : [ `
@@ -28,7 +29,7 @@ import { Component } from '@angular/core';
2829
})
2930
export class NzDemoSliderMarkComponent {
3031

31-
marks = {
32+
marks: any = {
3233
0 : '0°C',
3334
26 : '26°C',
3435
37 : '37°C',
@@ -40,4 +41,11 @@ export class NzDemoSliderMarkComponent {
4041
}
4142
};
4243

44+
changeMarks() {
45+
this.marks = {
46+
20: '20%',
47+
99: '99%',
48+
};
49+
}
50+
4351
}

0 commit comments

Comments
 (0)