Skip to content
This repository was archived by the owner on Jan 13, 2025. It is now read-only.

Commit 476c81f

Browse files
anton-kachurinkfranqueiro
authored andcommitted
fix(slider): Properly handle arrow key events in IE (#1613)
1 parent 39dd593 commit 476c81f

File tree

2 files changed

+104
-23
lines changed

2 files changed

+104
-23
lines changed

packages/mdc-slider/foundation.js

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -312,35 +312,32 @@ export default class MDCSliderFoundation extends MDCFoundation {
312312
}
313313

314314
getKeyId_(kbdEvt) {
315-
switch (kbdEvt.key || kbdEvt.keyCode) {
316-
case KEY_IDS.ARROW_LEFT:
317-
case 37:
315+
if (kbdEvt.key === KEY_IDS.ARROW_LEFT || kbdEvt.keyCode === 37) {
318316
return KEY_IDS.ARROW_LEFT;
319-
case KEY_IDS.ARROW_RIGHT:
320-
case 39:
317+
}
318+
if (kbdEvt.key === KEY_IDS.ARROW_RIGHT || kbdEvt.keyCode === 39) {
321319
return KEY_IDS.ARROW_RIGHT;
322-
case KEY_IDS.ARROW_UP:
323-
case 38:
320+
}
321+
if (kbdEvt.key === KEY_IDS.ARROW_UP || kbdEvt.keyCode === 38) {
324322
return KEY_IDS.ARROW_UP;
325-
case KEY_IDS.ARROW_DOWN:
326-
case 40:
323+
}
324+
if (kbdEvt.key === KEY_IDS.ARROW_DOWN || kbdEvt.keyCode === 40) {
327325
return KEY_IDS.ARROW_DOWN;
328-
case KEY_IDS.HOME:
329-
case 36:
326+
}
327+
if (kbdEvt.key === KEY_IDS.HOME || kbdEvt.keyCode === 36) {
330328
return KEY_IDS.HOME;
331-
case KEY_IDS.END:
332-
case 35:
329+
}
330+
if (kbdEvt.key === KEY_IDS.END || kbdEvt.keyCode === 35) {
333331
return KEY_IDS.END;
334-
case KEY_IDS.PAGE_UP:
335-
case 33:
332+
}
333+
if (kbdEvt.key === KEY_IDS.PAGE_UP || kbdEvt.keyCode === 33) {
336334
return KEY_IDS.PAGE_UP;
337-
case KEY_IDS.PAGE_DOWN:
338-
case 34:
335+
}
336+
if (kbdEvt.key === KEY_IDS.PAGE_DOWN || kbdEvt.keyCode === 34) {
339337
return KEY_IDS.PAGE_DOWN;
340-
default:
341-
// Doesn't matter
342-
return '';
343338
}
339+
340+
return '';
344341
}
345342

346343
getValueForKeyId_(keyId) {

test/unit/mdc-slider/foundation-keyboard-events.test.js

Lines changed: 87 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,27 @@ test('on arrow left keydown works with keyCode', () => {
125125
raf.restore();
126126
});
127127

128+
test('on arrow left keydown works with non-standard IE key propery backed with proper keyCode', () => {
129+
const {foundation, mockAdapter, raf, rootHandlers} = setupTest();
130+
131+
td.when(mockAdapter.computeBoundingRect()).thenReturn({left: 0, width: 100});
132+
foundation.init();
133+
raf.flush();
134+
135+
foundation.setValue(50);
136+
raf.flush();
137+
138+
rootHandlers.keydown(createFakeEvent({key: 'Left', keyCode: 37}));
139+
raf.flush();
140+
141+
assert.equal(foundation.getValue(), 49);
142+
td.verify(mockAdapter.setThumbContainerStyleProperty(TRANSFORM_PROP, 'translateX(49px) translateX(-50%)'));
143+
td.verify(mockAdapter.setTrackStyleProperty(TRANSFORM_PROP, 'scaleX(0.49)'));
144+
145+
raf.restore();
146+
});
147+
148+
128149
test('on arrow up keydown prevents the default behavior', () => {
129150
const {foundation, mockAdapter, raf, rootHandlers} = setupTest();
130151
const evt = createFakeEvent({key: 'ArrowUp'});
@@ -202,6 +223,27 @@ test('on arrow up keydown works with keyCode', () => {
202223
raf.restore();
203224
});
204225

226+
test('on arrow up keydown works with non-standard IE key propery backed with proper keyCode', () => {
227+
const {foundation, mockAdapter, raf, rootHandlers} = setupTest();
228+
229+
td.when(mockAdapter.computeBoundingRect()).thenReturn({left: 0, width: 100});
230+
foundation.init();
231+
raf.flush();
232+
233+
foundation.setValue(50);
234+
raf.flush();
235+
236+
rootHandlers.keydown(createFakeEvent({key: 'Up', keyCode: 38}));
237+
raf.flush();
238+
239+
assert.equal(foundation.getValue(), 51);
240+
td.verify(mockAdapter.setThumbContainerStyleProperty(TRANSFORM_PROP, 'translateX(51px) translateX(-50%)'));
241+
td.verify(mockAdapter.setTrackStyleProperty(TRANSFORM_PROP, 'scaleX(0.51)'));
242+
243+
raf.restore();
244+
});
245+
246+
205247
test('on arrow right keydown prevents the default behavior', () => {
206248
const {foundation, mockAdapter, raf, rootHandlers} = setupTest();
207249
const evt = createFakeEvent({key: 'ArrowRight'});
@@ -300,6 +342,27 @@ test('on arrow right keydown works with keyCode', () => {
300342
raf.restore();
301343
});
302344

345+
test('on arrow right keydown works with non-standard IE key propery backed with proper keyCode', () => {
346+
const {foundation, mockAdapter, raf, rootHandlers} = setupTest();
347+
348+
td.when(mockAdapter.computeBoundingRect()).thenReturn({left: 0, width: 100});
349+
foundation.init();
350+
raf.flush();
351+
352+
foundation.setValue(50);
353+
raf.flush();
354+
355+
rootHandlers.keydown(createFakeEvent({key: 'Right', keyCode: 39}));
356+
raf.flush();
357+
358+
assert.equal(foundation.getValue(), 51);
359+
td.verify(mockAdapter.setThumbContainerStyleProperty(TRANSFORM_PROP, 'translateX(51px) translateX(-50%)'));
360+
td.verify(mockAdapter.setTrackStyleProperty(TRANSFORM_PROP, 'scaleX(0.51)'));
361+
362+
raf.restore();
363+
});
364+
365+
303366
test('on arrow down keydown prevents the default behavior', () => {
304367
const {foundation, mockAdapter, raf, rootHandlers} = setupTest();
305368
const evt = createFakeEvent({key: 'ArrowDown'});
@@ -367,7 +430,7 @@ test('on arrow down keydown works with keyCode', () => {
367430
foundation.setValue(50);
368431
raf.flush();
369432

370-
rootHandlers.keydown(createFakeEvent({keyCode: 37}));
433+
rootHandlers.keydown(createFakeEvent({keyCode: 40}));
371434
raf.flush();
372435

373436
assert.equal(foundation.getValue(), 49);
@@ -377,6 +440,27 @@ test('on arrow down keydown works with keyCode', () => {
377440
raf.restore();
378441
});
379442

443+
test('on arrow down keydown works with non-standard IE key propery backed with proper keyCode', () => {
444+
const {foundation, mockAdapter, raf, rootHandlers} = setupTest();
445+
446+
td.when(mockAdapter.computeBoundingRect()).thenReturn({left: 0, width: 100});
447+
foundation.init();
448+
raf.flush();
449+
450+
foundation.setValue(50);
451+
raf.flush();
452+
453+
rootHandlers.keydown(createFakeEvent({key: 'Down', keyCode: 40}));
454+
raf.flush();
455+
456+
assert.equal(foundation.getValue(), 49);
457+
td.verify(mockAdapter.setThumbContainerStyleProperty(TRANSFORM_PROP, 'translateX(49px) translateX(-50%)'));
458+
td.verify(mockAdapter.setTrackStyleProperty(TRANSFORM_PROP, 'scaleX(0.49)'));
459+
460+
raf.restore();
461+
});
462+
463+
380464
test('on home keydown prevents default', () => {
381465
const {foundation, mockAdapter, raf, rootHandlers} = setupTest();
382466
const evt = createFakeEvent({key: 'Home'});
@@ -546,7 +630,7 @@ test('on page up keydown increases the slider by 4 * the step value when a step
546630
raf.restore();
547631
});
548632

549-
test('on page up keydown works with keycode', () => {
633+
test('on page up keydown works with keyCode', () => {
550634
const {foundation, mockAdapter, raf, rootHandlers} = setupTest();
551635

552636
td.when(mockAdapter.computeBoundingRect()).thenReturn({left: 0, width: 100});
@@ -623,7 +707,7 @@ test('on page down keydown increases the slider by 4 * the step value when a ste
623707
raf.restore();
624708
});
625709

626-
test('on page down keydown works with keycode', () => {
710+
test('on page down keydown works with keyCode', () => {
627711
const {foundation, mockAdapter, raf, rootHandlers} = setupTest();
628712

629713
td.when(mockAdapter.computeBoundingRect()).thenReturn({left: 0, width: 100});

0 commit comments

Comments
 (0)