Skip to content

Commit f3d9344

Browse files
authored
fix(date-range-picker): focus on arrow-up/escape for single input (#1783)
1 parent 6f49c26 commit f3d9344

File tree

3 files changed

+86
-3
lines changed

3 files changed

+86
-3
lines changed

src/components/date-range-picker/date-range-picker-single.spec.ts

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ import {
88
import { spy } from 'sinon';
99
import IgcCalendarComponent from '../calendar/calendar.js';
1010
import { CalendarDay } from '../calendar/model.js';
11-
import { escapeKey } from '../common/controllers/key-bindings.js';
11+
import {
12+
altKey,
13+
arrowDown,
14+
arrowUp,
15+
escapeKey,
16+
} from '../common/controllers/key-bindings.js';
1217
import { defineComponents } from '../common/definitions/defineComponents.js';
1318
import {
1419
isFocused,
@@ -134,7 +139,7 @@ describe('Date range picker - single input', () => {
134139
await elementUpdated(picker);
135140

136141
input.focus();
137-
simulateKeyboard(input, 'ArrowDown');
142+
simulateKeyboard(input, arrowDown);
138143
await elementUpdated(picker);
139144

140145
expect(isFocused(input)).to.be.true;
@@ -588,6 +593,47 @@ describe('Date range picker - single input', () => {
588593
expect(picker.value).to.deep.equal(null);
589594
expect(input.value).to.equal('');
590595
});
596+
597+
it('should toggle the calendar with keyboard combinations and keep focus', async () => {
598+
const eventSpy = spy(picker, 'emitEvent');
599+
const input = picker.renderRoot!.querySelector(
600+
IgcInputComponent.tagName
601+
)!;
602+
input.focus();
603+
604+
expect(isFocused(input)).to.be.true;
605+
606+
simulateKeyboard(input, [altKey, arrowDown]);
607+
await elementUpdated(picker);
608+
609+
expect(picker.open).to.be.true;
610+
expect(isFocused(input)).to.be.false;
611+
expect(eventSpy.firstCall).calledWith('igcOpening');
612+
expect(eventSpy.lastCall).calledWith('igcOpened');
613+
eventSpy.resetHistory();
614+
615+
simulateKeyboard(input, [altKey, arrowUp]);
616+
await elementUpdated(picker);
617+
618+
expect(picker.open).to.be.false;
619+
expect(isFocused(input)).to.be.true;
620+
expect(eventSpy.firstCall).calledWith('igcClosing');
621+
expect(eventSpy.lastCall).calledWith('igcClosed');
622+
eventSpy.resetHistory();
623+
624+
simulateKeyboard(input, [altKey, arrowDown]);
625+
await elementUpdated(picker);
626+
eventSpy.resetHistory();
627+
628+
simulateKeyboard(picker, escapeKey);
629+
await elementUpdated(picker);
630+
await elementUpdated(input);
631+
632+
expect(picker.open).to.be.false;
633+
expect(isFocused(input)).to.be.true;
634+
expect(eventSpy.firstCall).calledWith('igcClosing');
635+
expect(eventSpy.lastCall).calledWith('igcClosed');
636+
});
591637
});
592638

593639
describe('Readonly state', () => {

src/components/date-range-picker/date-range-picker-two-inputs.spec.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { spy } from 'sinon';
99
import IgcCalendarComponent from '../calendar/calendar.js';
1010
import { CalendarDay } from '../calendar/model.js';
1111
import {
12+
altKey,
1213
arrowDown,
1314
arrowUp,
1415
escapeKey,
@@ -799,6 +800,42 @@ describe('Date range picker - two inputs', () => {
799800

800801
checkDatesEqual(calendar.activeDate, june3rd2025);
801802
});
803+
it('should toggle the calendar with keyboard combinations and keep focus', async () => {
804+
const eventSpy = spy(picker, 'emitEvent');
805+
dateTimeInputs[0].focus();
806+
807+
expect(isFocused(dateTimeInputs[0])).to.be.true;
808+
809+
simulateKeyboard(dateTimeInputs[0], [altKey, arrowDown]);
810+
await elementUpdated(picker);
811+
812+
expect(picker.open).to.be.true;
813+
expect(isFocused(dateTimeInputs[0])).to.be.false;
814+
expect(eventSpy.firstCall).calledWith('igcOpening');
815+
expect(eventSpy.lastCall).calledWith('igcOpened');
816+
eventSpy.resetHistory();
817+
818+
simulateKeyboard(dateTimeInputs[0], [altKey, arrowUp]);
819+
await elementUpdated(picker);
820+
821+
expect(picker.open).to.be.false;
822+
expect(isFocused(dateTimeInputs[0])).to.be.true;
823+
expect(eventSpy.firstCall).calledWith('igcClosing');
824+
expect(eventSpy.lastCall).calledWith('igcClosed');
825+
826+
simulateKeyboard(dateTimeInputs[0], [altKey, arrowDown]);
827+
await elementUpdated(picker);
828+
eventSpy.resetHistory();
829+
830+
simulateKeyboard(dateTimeInputs[0], escapeKey);
831+
await elementUpdated(picker);
832+
await elementUpdated(dateTimeInputs[0]);
833+
834+
expect(picker.open).to.be.false;
835+
expect(isFocused(dateTimeInputs[0])).to.be.true;
836+
expect(eventSpy.firstCall).calledWith('igcClosing');
837+
expect(eventSpy.lastCall).calledWith('igcClosed');
838+
});
802839
});
803840
describe('Readonly state', () => {
804841
beforeEach(async () => {

src/components/date-range-picker/date-range-picker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ export default class IgcDateRangePickerComponent extends FormAssociatedRequiredM
761761
if (!this._isDropDown) {
762762
this._revertValue();
763763
}
764-
this._inputs[0]?.focus();
764+
this.useTwoInputs ? this._inputs[0].focus() : this._input.focus();
765765
}
766766
}
767767

0 commit comments

Comments
 (0)