Skip to content

Commit 40da33f

Browse files
Merge pull request #14288 from IgniteUI/bpachilova/fix-14135
fix(slider): respect ngModelOptions.updateOn: blur - master
2 parents c774857 + 9da7a87 commit 40da33f

File tree

3 files changed

+55
-8
lines changed

3 files changed

+55
-8
lines changed

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

Lines changed: 42 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';
@@ -1753,6 +1753,10 @@ describe('IgxSlider', () => {
17531753
fakeDoc.documentElement.dir = 'rtl';
17541754
});
17551755

1756+
afterEach(() => {
1757+
fakeDoc.documentElement.dir = 'ltr';
1758+
});
1759+
17561760
it('should reflect on the right instead of the left css property of the slider handlers', () => {
17571761
const fix = TestBed.createComponent(SliderRtlComponent);
17581762
fix.detectChanges();
@@ -1809,6 +1813,40 @@ describe('IgxSlider', () => {
18091813
fixture.detectChanges();
18101814
expect(slider.value).toBe(formControl.value);
18111815
});
1816+
1817+
it('Should respect the ngModelOptions updateOn: blur', fakeAsync(() => {
1818+
const fixture = TestBed.createComponent(SliderTemplateFormComponent);
1819+
fixture.componentInstance.updateOn = 'blur';
1820+
fixture.componentInstance.value = 0;
1821+
fixture.detectChanges();
1822+
1823+
const slider = fixture.componentInstance.slider;
1824+
1825+
const thumbEl = fixture.debugElement.query(By.css(THUMB_TAG)).nativeElement;
1826+
const { x: sliderX, width: sliderWidth } = thumbEl.getBoundingClientRect();
1827+
const startX = sliderX + sliderWidth / 2;
1828+
1829+
thumbEl.dispatchEvent(new Event('focus'));
1830+
fixture.detectChanges();
1831+
1832+
(slider as any).onPointerDown(new PointerEvent('pointerdown', { pointerId: 1, clientX: startX }));
1833+
fixture.detectChanges();
1834+
tick();
1835+
1836+
(slider as any).onPointerMove(new PointerEvent('pointermove', { pointerId: 1, clientX: startX + 150 }));
1837+
fixture.detectChanges();
1838+
tick();
1839+
1840+
const activeThumb = fixture.debugElement.query(By.css(THUMB_TO_PRESSED_CLASS));
1841+
expect(activeThumb).not.toBeNull();
1842+
expect(fixture.componentInstance.value).not.toBeGreaterThan(0);
1843+
1844+
thumbEl.dispatchEvent(new Event('blur'));
1845+
fixture.detectChanges();
1846+
tick();
1847+
1848+
expect(fixture.componentInstance.value).toBeGreaterThan(0);
1849+
}));
18121850
});
18131851

18141852

@@ -1984,7 +2022,7 @@ class RangeSliderWithCustomTemplateComponent {
19842022
@Component({
19852023
template: `
19862024
<form #form="ngForm">
1987-
<igx-slider [(ngModel)]="value" name="amount"></igx-slider>
2025+
<igx-slider [(ngModel)]="value" name="amount" [ngModelOptions]="{ updateOn: updateOn}"></igx-slider>
19882026
</form>
19892027
`,
19902028
standalone: true,
@@ -1993,6 +2031,8 @@ class RangeSliderWithCustomTemplateComponent {
19932031
export class SliderTemplateFormComponent {
19942032
@ViewChild(IgxSliderComponent, { read: IgxSliderComponent, static: true }) public slider: IgxSliderComponent;
19952033

2034+
@Input() public updateOn: 'change' | 'blur' | 'submit' = 'change';
2035+
19962036
public value = 10;
19972037
}
19982038

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -991,13 +991,13 @@ export class IgxSliderComponent implements
991991
this.setTickInterval();
992992
this.changeThumbFocusableState(this.disabled);
993993

994-
this.subscribeTo(this.thumbFrom, this.thumbChanged.bind(this));
995-
this.subscribeTo(this.thumbTo, this.thumbChanged.bind(this));
994+
this.subscribeToEvents(this.thumbFrom);
995+
this.subscribeToEvents(this.thumbTo);
996996

997997
this.thumbs.changes.pipe(takeUntil(this._destroyer$)).subscribe(change => {
998998
const thumbFrom = change.find((thumb: IgxSliderThumbComponent) => thumb.type === SliderHandle.FROM);
999999
this.positionHandler(thumbFrom, null, this.lowerValue);
1000-
this.subscribeTo(thumbFrom, this.thumbChanged.bind(this));
1000+
this.subscribeToEvents(thumbFrom);
10011001
this.changeThumbFocusableState(this.disabled);
10021002
});
10031003

@@ -1078,7 +1078,6 @@ export class IgxSliderComponent implements
10781078
// Finally do positionHandlersAndUpdateTrack the DOM
10791079
// based on data values
10801080
this.positionHandlersAndUpdateTrack();
1081-
this._onTouchedCallback();
10821081
}
10831082

10841083
/**
@@ -1411,14 +1410,18 @@ export class IgxSliderComponent implements
14111410
}
14121411
}
14131412

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

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

14241427
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
@@ -66,6 +66,9 @@ export class IgxSliderThumbComponent implements OnInit, OnDestroy {
6666
@Output()
6767
public thumbChange = new EventEmitter<any>();
6868

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

@@ -189,6 +192,7 @@ export class IgxSliderThumbComponent implements OnInit, OnDestroy {
189192
this.isActive = false;
190193
this.zIndex = 0;
191194
this.focused = false;
195+
this.thumbBlur.emit();
192196
}
193197

194198
@HostListener('focus')

0 commit comments

Comments
 (0)