Skip to content

Commit 9f69c85

Browse files
Merge pull request #14292 from IgniteUI/bpachilova/fix-14135-16.1.x
fix(slider): respect ngModelOptions.updateOn: blur - 16.1.x
2 parents 3208f2c + 67843d2 commit 9f69c85

File tree

3 files changed

+46
-8
lines changed

3 files changed

+46
-8
lines changed

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

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, ViewChild } from '@angular/core';
1+
import { Component, Input, ViewChild } from '@angular/core';
22
import { ComponentFixture, fakeAsync, TestBed, tick, waitForAsync } from '@angular/core/testing';
33
import { FormsModule, ReactiveFormsModule, UntypedFormControl } from '@angular/forms';
44
import { By, HammerModule } from '@angular/platform-browser';
@@ -1745,6 +1745,10 @@ describe('IgxSlider', () => {
17451745
fakeDoc.documentElement.dir = 'rtl';
17461746
});
17471747

1748+
afterEach(() => {
1749+
fakeDoc.documentElement.dir = 'ltr';
1750+
});
1751+
17481752
it('should reflect on the right instead of the left css property of the slider handlers', () => {
17491753
const fix = TestBed.createComponent(SliderRtlComponent);
17501754
fix.detectChanges();
@@ -1801,6 +1805,31 @@ describe('IgxSlider', () => {
18011805
fixture.detectChanges();
18021806
expect(slider.value).toBe(formControl.value);
18031807
});
1808+
1809+
it('Should respect the ngModelOptions updateOn: blur', fakeAsync(() => {
1810+
const fixture = TestBed.createComponent(SliderTemplateFormComponent);
1811+
fixture.componentInstance.updateOn = 'blur';
1812+
fixture.detectChanges();
1813+
tick();
1814+
1815+
const slider = fixture.componentInstance.slider;
1816+
const thumb = fixture.nativeElement.querySelector('igx-thumb');
1817+
1818+
expect(slider.value).toBe(fixture.componentInstance.value);
1819+
1820+
thumb.dispatchEvent(new Event('focus'));
1821+
fixture.detectChanges();
1822+
1823+
slider.value = 30;
1824+
fixture.detectChanges();
1825+
tick();
1826+
expect(slider.value).not.toBe(fixture.componentInstance.value);
1827+
1828+
thumb.dispatchEvent(new Event('blur'));
1829+
fixture.detectChanges();
1830+
tick();
1831+
expect(slider.value).toBe(fixture.componentInstance.value);
1832+
}));
18041833
});
18051834

18061835
const panRight = (element, elementHeight, elementWidth, duration) => {
@@ -1991,7 +2020,7 @@ class RangeSliderWithCustomTemplateComponent {
19912020
@Component({
19922021
template: `
19932022
<form #form="ngForm">
1994-
<igx-slider [(ngModel)]="value" name="amount"></igx-slider>
2023+
<igx-slider [(ngModel)]="value" name="amount" [ngModelOptions]="{ updateOn: updateOn }"></igx-slider>
19952024
</form>
19962025
`,
19972026
standalone: true,
@@ -2000,6 +2029,8 @@ class RangeSliderWithCustomTemplateComponent {
20002029
export class SliderTemplateFormComponent {
20012030
@ViewChild(IgxSliderComponent, { read: IgxSliderComponent, static: true }) public slider: IgxSliderComponent;
20022031

2032+
@Input() public updateOn: 'change' | 'blur' | 'submit' = 'change';
2033+
20032034
public value = 10;
20042035
}
20052036

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,13 +1033,13 @@ export class IgxSliderComponent implements
10331033
this.setTickInterval();
10341034
this.changeThumbFocusableState(this.disabled);
10351035

1036-
this.subscribeTo(this.thumbFrom, this.thumbChanged.bind(this));
1037-
this.subscribeTo(this.thumbTo, this.thumbChanged.bind(this));
1036+
this.subscribeToEvents(this.thumbFrom);
1037+
this.subscribeToEvents(this.thumbTo);
10381038

10391039
this.thumbs.changes.pipe(takeUntil(this._destroyer$)).subscribe(change => {
10401040
const thumbFrom = change.find((thumb: IgxSliderThumbComponent) => thumb.type === SliderHandle.FROM);
10411041
this.positionHandler(thumbFrom, null, this.lowerValue);
1042-
this.subscribeTo(thumbFrom, this.thumbChanged.bind(this));
1042+
this.subscribeToEvents(thumbFrom);
10431043
this.changeThumbFocusableState(this.disabled);
10441044
});
10451045

@@ -1115,7 +1115,6 @@ export class IgxSliderComponent implements
11151115
// Finally do positionHandlersAndUpdateTrack the DOM
11161116
// based on data values
11171117
this.positionHandlersAndUpdateTrack();
1118-
this._onTouchedCallback();
11191118
}
11201119

11211120
/**
@@ -1412,14 +1411,18 @@ export class IgxSliderComponent implements
14121411
}
14131412
}
14141413

1415-
private subscribeTo(thumb: IgxSliderThumbComponent, callback: (a: number, b: string) => void) {
1414+
private subscribeToEvents(thumb: IgxSliderThumbComponent) {
14161415
if (!thumb) {
14171416
return;
14181417
}
14191418

14201419
thumb.thumbValueChange
14211420
.pipe(takeUntil(this.unsubscriber(thumb)))
1422-
.subscribe(value => callback(value, thumb.type));
1421+
.subscribe(value => this.thumbChanged(value, thumb.type));
1422+
1423+
thumb.thumbBlur
1424+
.pipe(takeUntil(this.unsubscriber(thumb)))
1425+
.subscribe(() => this._onTouchedCallback());
14231426
}
14241427

14251428
private unsubscriber(thumb: IgxSliderThumbComponent) {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ export class IgxSliderThumbComponent implements OnInit, OnDestroy {
6565
@Output()
6666
public thumbChange = new EventEmitter<any>();
6767

68+
@Output()
69+
public thumbBlur = new EventEmitter<void>();
70+
6871
@Output()
6972
public hoverChange = new EventEmitter<boolean>();
7073

@@ -188,6 +191,7 @@ export class IgxSliderThumbComponent implements OnInit, OnDestroy {
188191
this.isActive = false;
189192
this.zIndex = 0;
190193
this.focused = false;
194+
this.thumbBlur.emit();
191195
}
192196

193197
@HostListener('focus')

0 commit comments

Comments
 (0)