Skip to content

Commit 213bd30

Browse files
committed
fix(slider): two way databind
Closes #3449
1 parent 2294eb2 commit 213bd30

File tree

2 files changed

+116
-40
lines changed

2 files changed

+116
-40
lines changed

projects/igniteui-angular/src/lib/slider/slider.component.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
<div (keydown)="onKeyDown($event);" (keyup)="hideThumbsLabels()" (blur)="hideThumbLabelsOnBlur()" (focus)="onFocus($event);"
99
*ngIf="isRange" class="igx-slider__thumb-from" tabindex="1" [ngClass]="{ 'igx-slider__thumb-from--active': isActiveLabel }"
1010
#thumbFrom>
11-
<span class="label">{{ lowerLabel }}</span>
11+
<span class="label">{{ leftLabel }}</span>
1212
<span class="dot"></span>
1313
</div>
1414
<div (keydown)="onKeyDown($event);" (keyup)="hideThumbsLabels()" (blur)="hideThumbLabelsOnBlur()" (focus)="onFocus($event);"
1515
class="igx-slider__thumb-to" tabindex="1" [ngClass]="{ 'igx-slider__thumb-to--active': isActiveLabel }" #thumbTo>
16-
<span *ngIf="isRange" class="label">{{ upperLabel }}</span>
17-
<span *ngIf="!isRange" class="label">{{ valueLabel }}</span>
16+
<span *ngIf="isRange" class="label">{{ rightLabel }}</span>
17+
<span *ngIf="!isRange" class="label">{{ label }}</span>
1818
<span class="dot"></span>
1919
</div>
2020
</div>

projects/igniteui-angular/src/lib/slider/slider.component.ts

+113-37
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { CommonModule } from '@angular/common';
22
import {
33
AfterViewInit, Component, ElementRef, EventEmitter,
4-
forwardRef, HostBinding, Input, NgModule, OnInit, Output, Renderer2,
4+
HostBinding, Input, NgModule, OnInit, Output, Renderer2,
55
ViewChild,
6-
TemplateRef
76
} from '@angular/core';
87
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
98
import { EditorProvider } from '../core/edit-provider';
@@ -459,10 +458,7 @@ export class IgxSliderComponent implements ControlValueAccessor, EditorProvider,
459458
*}
460459
*```
461460
*/
462-
public get value(): number | string | IRangeSliderValue | IRangeSliderValueLabel {
463-
// if (this.labelsViewEnabled) {
464-
// return this.getLabelValues(this.lowerValue, this.upperValue);
465-
// }
461+
public get value(): number | IRangeSliderValue {
466462
if (this.isRange) {
467463
return {
468464
lower: this.snapValueToStep(this.lowerValue),
@@ -476,11 +472,9 @@ export class IgxSliderComponent implements ControlValueAccessor, EditorProvider,
476472

477473
/**
478474
* Sets the slider value.
479-
* If the slider is of type SLIDER the argument is number. By default if no value is set the default value is
480-
* same as lower upper bound.
481-
* If the slider type is RANGE the the argument is object containing lower and upper properties for the values.
482-
* By default if no value is set the default value is for lower value it is the same as lower bound and if no
483-
* value is set for the upper value it is the same as the upper bound.
475+
* If the slider is of type SLIDER the argument is number. By default the @value gets the @lowerBound value.
476+
* If the slider type is RANGE the argument represents an object of lower and upper properties.
477+
* By default the object lower and upper properties are associated with the @lowerBound and @upperBound property values.
484478
* ```typescript
485479
*rangeValue = {
486480
* lower: 30,
@@ -492,11 +486,7 @@ export class IgxSliderComponent implements ControlValueAccessor, EditorProvider,
492486
* ```
493487
*/
494488
@Input()
495-
public set value(value: number | string | IRangeSliderValue | IRangeSliderValueLabel) {
496-
if (this.labelsViewEnabled) {
497-
value = this.getLabelIndexes(value);
498-
}
499-
489+
public set value(value: number | IRangeSliderValue) {
500490
if (!this.isRange) {
501491
this.upperValue = this.snapValueToStep(value as number);
502492
} else {
@@ -506,33 +496,115 @@ export class IgxSliderComponent implements ControlValueAccessor, EditorProvider,
506496
this.snapValueToStep((value as IRangeSliderValue) == null ? null : (value as IRangeSliderValue).lower);
507497
}
508498

509-
this._onChangeCallback(this.value);
499+
this.labelsViewEnabled ?
500+
this._onChangeCallback(this.getLabelValues(this.value)):
501+
this._onChangeCallback(this.value);
510502

511503
if (this.hasViewInit) {
512504
this.positionHandlesAndUpdateTrack();
513505
}
514506
}
515507

516-
public get labelsViewEnabled() {
517-
return !!this.stepLabels.length;
518-
}
519-
520-
public get lowerLabel() {
508+
/**
509+
*Returns the left label value of the `IgxSliderComponent`.
510+
*```typescript
511+
* @ViewChild("slider")
512+
* public slider: IgxSliderComponent;
513+
* public leftLabel(){
514+
* let sliderLeftLabel = this.slider.leftLabel;
515+
* }
516+
*```
517+
*/
518+
public get leftLabel() {
521519
return this.labelsViewEnabled ?
522520
this.stepLabels[this.lowerValue] :
523521
this.lowerValue;
524522
}
525523

526-
public get upperLabel() {
524+
/**
525+
*Returns the right label value of the `IgxSliderComponent`.
526+
*```typescript
527+
* @ViewChild("slider")
528+
* public slider: IgxSliderComponent;
529+
* public rightLabel(){
530+
* let sliderRightLabel = this.slider.rightLabel;
531+
* }
532+
*```
533+
*/
534+
public get rightLabel() {
527535
return this.labelsViewEnabled ?
528536
this.stepLabels[this.upperValue] :
529537
this.upperValue;
530538
}
531539

532-
public get valueLabel() {
533-
return this.labelsViewEnabled ?
534-
this.stepLabels[this.upperValue] :
535-
this.value;
540+
/**
541+
* Sets the slider label.
542+
* If the slider is of type SLIDER the argument is number or string. By default the value takes to @lowerValue
543+
* or the first element of the @stepLabels collection.
544+
* If the slider type is RANGE the value represents an object of lower/upper or left/right properties.
545+
* By default the @value takes the @lowerValue / @upperValue valeus or first and last element of the @stepLabels collection.
546+
* ```typescript
547+
* rangeValue = {
548+
* lower: 30,
549+
* upper: 60
550+
*};
551+
*
552+
* labels = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
553+
* labelsRange = {
554+
* left: 'Tuesday',
555+
* right: 'Friday'
556+
* }
557+
* ```
558+
* ```html
559+
* <igx-slider [type]="sliderType" [(ngModel)]="rangeValue" [minValue]="56" [maxValue]="256">
560+
*
561+
* ```html
562+
* <igx-slider [type]="sliderType" [label]="labelsRange" [stepLabels]="labels">
563+
* ```
564+
*/
565+
@Input()
566+
public set label(value: number | string | IRangeSliderValue | IRangeSliderValueLabel) {
567+
if (!value) {
568+
return;
569+
}
570+
571+
this.value = this.getLabelIndexes(value);
572+
}
573+
574+
/**
575+
* Returns the slider label value. If the slider is of type SLIDER the returned value is number or string.
576+
* If the slider type is RANGE the returned value represents an object of lower/upper or left/right properties as values.
577+
*```typescript
578+
*@ViewChild("slider2")
579+
*public slider: IgxSliderComponent;
580+
*public sliderValue(){
581+
* let sliderLabel = this.slider.label;
582+
*}
583+
*```
584+
*/
585+
public get label(): number | string | IRangeSliderValue | IRangeSliderValueLabel {
586+
if (this.labelsViewEnabled) {
587+
if (this.isRange) {
588+
const lower = (this.value as IRangeSliderValue).lower;
589+
const upper = (this.value as IRangeSliderValue).upper;
590+
return {
591+
left: this.stepLabels[lower],
592+
right: this.stepLabels[upper]
593+
};
594+
}
595+
596+
return this.stepLabels[this.upperValue];
597+
}
598+
599+
return this.value;
600+
}
601+
602+
603+
/**
604+
* Returns if the label view is enabled. This is determined by the length of the @stepLabels collection.;
605+
*/
606+
public get labelsViewEnabled() {
607+
return !!this.stepLabels.length;
536608
}
537609

538610
/**
@@ -586,7 +658,11 @@ export class IgxSliderComponent implements ControlValueAccessor, EditorProvider,
586658
* @hidden
587659
*/
588660
public writeValue(value: any): void {
589-
this.value = value;
661+
if (!this.labelsViewEnabled && typeof value === 'string') {
662+
value = parseInt(value);
663+
}
664+
665+
this.label = value;
590666
}
591667

592668
/**
@@ -992,24 +1068,24 @@ export class IgxSliderComponent implements ControlValueAccessor, EditorProvider,
9921068
}
9931069
}
9941070

995-
private getLabelIndexes(value: number | string | IRangeSliderValue | IRangeSliderValueLabel) {
1071+
private getLabelIndexes(value: number | string | IRangeSliderValue | IRangeSliderValueLabel): number | IRangeSliderValue {
9961072
if (typeof value === 'string' && !this.isRange) {
9971073
return this.stepLabels.findIndex(e => e === value);
9981074
}
9991075

10001076
const left = (value as IRangeSliderValueLabel).left;
10011077
const right = (value as IRangeSliderValueLabel).right;
1002-
if (left || right) {
1078+
if (this.isRange && (left || right)) {
10031079
return {
10041080
lower: left ? this.stepLabels.findIndex(e => e === left) : 0,
10051081
upper: right ? this.stepLabels.findIndex(e => e === right) : this.stepLabels.length - 1
10061082
}
10071083
}
10081084

1009-
return value;
1085+
return this.isRange ? (value as IRangeSliderValue) : +value;
10101086
}
10111087

1012-
private getLabelValues(value) {
1088+
private getLabelValues(value): string | IRangeSliderValueLabel {
10131089
if (this.isRange) {
10141090
return {
10151091
left: this.stepLabels[value.lower],
@@ -1030,14 +1106,14 @@ export class IgxSliderComponent implements ControlValueAccessor, EditorProvider,
10301106
}
10311107

10321108
private emitValueChanged(oldValueRef: number | string | IRangeSliderValue | IRangeSliderValueLabel) {
1033-
let oldVal = oldValueRef, newVal = this.value;
1034-
10351109
if (this.labelsViewEnabled) {
1036-
oldVal = this.getLabelValues(oldValueRef);
1037-
newVal = this.getLabelValues(this.value);
1110+
const oldVal = this.getLabelValues(oldValueRef),
1111+
newVal = this.getLabelValues(this.value);
1112+
this.onValueChange.emit({oldValue: oldVal, value: newVal});
1113+
} else {
1114+
this.onValueChange.emit({ oldValue: oldValueRef, value: this.value });
10381115
}
10391116

1040-
this.onValueChange.emit({ oldValue: oldVal, value: newVal });
10411117
}
10421118
}
10431119

0 commit comments

Comments
 (0)