Skip to content

Commit

Permalink
Merge pull request #15738 from robGardiner01/fix-slider-keyboard-slid…
Browse files Browse the repository at this point in the history
…eend

fix - Slider: Change range slider by keys does not trigger the onSlideEnd event
  • Loading branch information
cetincakiroglu authored May 30, 2024
2 parents 0c97fbd + 82fc6d8 commit b08e5a2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/app/components/slider/slider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,31 @@ describe('Slider', () => {
expect(unbindDragListenersSpy).toHaveBeenCalled();
});

it('should change values with keyboard events', () => {
slider.updateValue(0);
fixture.detectChanges();

const incrementValueSpy = spyOn(slider, 'incrementValue').and.callThrough();
const spanEl = fixture.debugElement.query(By.css('.p-slider-handle'));
spanEl.nativeElement.dispatchEvent(new KeyboardEvent('keydown', { code: 'ArrowRight' }));
spanEl.nativeElement.dispatchEvent(new KeyboardEvent('keydown', { code: 'ArrowRight' }));
fixture.detectChanges();
expect(incrementValueSpy).toHaveBeenCalled();

const decrementValueSpy = spyOn(slider, 'decrementValue').and.callThrough();
spanEl.nativeElement.dispatchEvent(new KeyboardEvent('keydown', { code: 'ArrowLeft' }));
fixture.detectChanges();
expect(decrementValueSpy).toHaveBeenCalled();

const onDragEndSpy = spyOn(slider, 'onDragEnd').and.callThrough();
const onSlideEndEmitterSpy = spyOn(slider.onSlideEnd, 'emit').and.callThrough();
spanEl.nativeElement.dispatchEvent(new KeyboardEvent('keydown', { code: 'Tab' }));
fixture.detectChanges();
expect(onDragEndSpy).toHaveBeenCalled();
expect(onSlideEndEmitterSpy).toHaveBeenCalled();
expect(slider.value).toEqual(1);
});

it('should increment value with step', () => {
slider.value = 0;
slider.step = 2;
Expand Down
4 changes: 4 additions & 0 deletions src/app/components/slider/slider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,10 @@ export class Slider implements OnDestroy, ControlValueAccessor {
this.updateValue(this.max, event);
event.preventDefault();
break;
case 'Tab':
this.onDragEnd(event);
event.preventDefault();
break;

default:
break;
Expand Down

0 comments on commit b08e5a2

Please sign in to comment.