Skip to content

Commit 9f6e596

Browse files
authored
Merge pull request #10324 from IgniteUI/normalize-slider-initialval
fix(slider): normalize initial value set up
2 parents 1a5b043 + b40f8e3 commit 9f6e596

File tree

6 files changed

+42
-17
lines changed

6 files changed

+42
-17
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,24 @@ describe('IgxSlider', () => {
236236
expect(slider.value.upper).toBe(30);
237237
});
238238

239+
it('should position correctly lower and upper value based on the step', () => {
240+
slider.step = 10;
241+
242+
slider.type = IgxSliderType.RANGE;
243+
244+
fixture.detectChanges();
245+
246+
slider.value = {
247+
lower: 23,
248+
upper: 56
249+
};
250+
251+
fixture.detectChanges();
252+
253+
expect((slider.value as IRangeSliderValue).lower).toBe(20);
254+
expect((slider.value as IRangeSliderValue).upper).toBe(50);
255+
});
256+
239257
it('should not set lower value outside bounds slider when slider is RANGE', () => {
240258
slider.lowerBound = 10;
241259
slider.upperBound = 40;

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

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import {
1212
ChangeDetectorRef,
1313
OnChanges,
1414
NgZone,
15-
AfterContentInit
15+
AfterContentInit,
16+
SimpleChanges
1617
} from '@angular/core';
1718
import { ControlValueAccessor, NG_VALUE_ACCESSOR, FormsModule } from '@angular/forms';
1819
import { EditorProvider } from '../core/edit-provider';
@@ -274,7 +275,9 @@ export class IgxSliderComponent implements
274275

275276
if (this._hasViewInit) {
276277
this.stepDistance = this.calculateStepDistance();
277-
this.normalizeByStep(this.value);
278+
this.normalizeByStep(this._value);
279+
this.setValue(this._value, true);
280+
this.positionHandlersAndUpdateTrack();
278281
this.setTickInterval();
279282
}
280283
}
@@ -564,11 +567,11 @@ export class IgxSliderComponent implements
564567
*/
565568
@Input()
566569
public set value(value: number | IRangeSliderValue) {
570+
this.normalizeByStep(value);
571+
567572
if (this._hasViewInit) {
568-
this.setValue(value, true);
573+
this.setValue(this._value, true);
569574
this.positionHandlersAndUpdateTrack();
570-
} else {
571-
this._value = value;
572575
}
573576
}
574577

@@ -960,12 +963,16 @@ export class IgxSliderComponent implements
960963
/**
961964
* @hidden
962965
*/
963-
public ngOnChanges(changes) {
966+
public ngOnChanges(changes: SimpleChanges) {
964967
if (changes.minValue && changes.maxValue &&
965968
changes.minValue.currentValue < changes.maxValue.currentValue) {
966969
this._maxValue = changes.maxValue.currentValue;
967970
this._minValue = changes.minValue.currentValue;
968971
}
972+
973+
if (changes.step && changes.step.isFirstChange()) {
974+
this.normalizeByStep(this._value);
975+
}
969976
}
970977

971978
/**
@@ -1037,6 +1044,8 @@ export class IgxSliderComponent implements
10371044
}
10381045

10391046
this.normalizeByStep(value);
1047+
this.setValue(this._value, false);
1048+
this.positionHandlersAndUpdateTrack();
10401049
}
10411050

10421051
/**
@@ -1349,15 +1358,13 @@ export class IgxSliderComponent implements
13491358
*/
13501359
private normalizeByStep(value: IRangeSliderValue | number) {
13511360
if (this.isRange) {
1352-
this.value = {
1361+
this._value = {
13531362
lower: (value as IRangeSliderValue).lower - ((value as IRangeSliderValue).lower % this.step),
13541363
upper: (value as IRangeSliderValue).upper - ((value as IRangeSliderValue).upper % this.step)
13551364
};
13561365
} else {
1357-
this.value = (value as number) - ((value as number) % this.step);
1366+
this._value = (value as number) - ((value as number) % this.step);
13581367
}
1359-
1360-
this._cdr.detectChanges();
13611368
}
13621369

13631370
private updateTrack() {

src/app/app.module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { SharedModule } from './shared/shared.module';
1717
import { routing } from './routing';
1818
import { ActionStripSampleComponent } from './action-strip/action-strip.sample';
1919
import { AppComponent } from './app.component';
20-
import { AvatartSampleComponent } from './avatar/avatar.sample';
20+
import { AvatarSampleComponent } from './avatar/avatar.sample';
2121
import { PageHeaderComponent } from './pageHeading/pageHeading.component';
2222
import { BadgeSampleComponent } from './badge/badge.sample';
2323
import { ButtonSampleComponent } from './button/button.sample';
@@ -162,7 +162,7 @@ const components = [
162162
AutocompletePipeContains,
163163
AutocompleteGroupPipeContains,
164164
AutocompleteSampleComponent,
165-
AvatartSampleComponent,
165+
AvatarSampleComponent,
166166
BadgeSampleComponent,
167167
BannerSampleComponent,
168168
ButtonSampleComponent,

src/app/app.routing.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { TreeGridAddRowSampleComponent } from './tree-grid-add-row/tree-grid-add-row.sample';
22
import { RouterModule } from '@angular/router';
3-
import { AvatartSampleComponent } from './avatar/avatar.sample';
3+
import { AvatarSampleComponent } from './avatar/avatar.sample';
44
import { BadgeSampleComponent } from './badge/badge.sample';
55
import { ButtonSampleComponent } from './button/button.sample';
66
import { CalendarSampleComponent } from './calendar/calendar.sample';
@@ -116,7 +116,7 @@ const appRoutes = [
116116
},
117117
{
118118
path: 'avatar',
119-
component: AvatartSampleComponent
119+
component: AvatarSampleComponent
120120
},
121121
{
122122
path: 'badge',

src/app/avatar/avatar.sample.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ import { Component } from '@angular/core';
55
styleUrls: ['avatar.sample.css'],
66
templateUrl: `avatar.sample.html`
77
})
8-
export class AvatartSampleComponent {
8+
export class AvatarSampleComponent {
99
}

src/app/routing.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { TreeGridAddRowSampleComponent } from './tree-grid-add-row/tree-grid-add-row.sample';
22
import { RouterModule } from '@angular/router';
3-
import { AvatartSampleComponent } from './avatar/avatar.sample';
3+
import { AvatarSampleComponent } from './avatar/avatar.sample';
44
import { BadgeSampleComponent } from './badge/badge.sample';
55
import { ButtonSampleComponent } from './button/button.sample';
66
import { CalendarSampleComponent } from './calendar/calendar.sample';
@@ -146,7 +146,7 @@ const appRoutes = [
146146
},
147147
{
148148
path: 'avatar',
149-
component: AvatartSampleComponent
149+
component: AvatarSampleComponent
150150
},
151151
{
152152
path: 'badge',

0 commit comments

Comments
 (0)