Skip to content

Commit b00e42a

Browse files
committed
some fixes for calendar selection
1 parent e21c658 commit b00e42a

2 files changed

Lines changed: 75 additions & 19 deletions

File tree

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

Lines changed: 70 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,8 @@ describe('Date range picker', () => {
516516
) as any;
517517
expect(separator?.innerText).to.equal('Separator - localized');
518518
});
519+
// TODO
520+
it('should set the default placeholder of the single input to the input format (like dd/MM/yyyy - dd/MM/yyyy)', async () => {});
519521
});
520522
describe('Methods', () => {
521523
it('should open/close the picker on invoking show/hide/toggle and not emit events', async () => {
@@ -607,8 +609,37 @@ describe('Date range picker', () => {
607609
expect(popover?.hasAttribute('open')).to.equal(false);
608610
});
609611

610-
// TODO
611-
it('should swap the selected dates if the end is earlier than the start', async () => {});
612+
it('should swap the selected dates after input if the end is earlier than the start', async () => {
613+
const eventSpy = spy(picker, 'emitEvent');
614+
const aMonthAgo = today.add('month', -1);
615+
picker.value = null;
616+
await elementUpdated(picker);
617+
618+
dateTimeInputs[0].focus();
619+
await elementUpdated(dateTimeInputs[0]);
620+
expect(isFocused(dateTimeInputs[0])).to.be.true;
621+
622+
simulateKeyboard(dateTimeInputs[0], arrowUp);
623+
await elementUpdated(picker);
624+
625+
expect(eventSpy).calledWith('igcInput');
626+
eventSpy.resetHistory();
627+
628+
dateTimeInputs[1].focus();
629+
// first arrow sets today, second sets aMonthAgo
630+
simulateKeyboard(dateTimeInputs[1], arrowDown);
631+
simulateKeyboard(dateTimeInputs[1], arrowDown);
632+
await elementUpdated(picker);
633+
634+
dateTimeInputs[1].blur();
635+
await elementUpdated(picker);
636+
637+
expect(eventSpy).calledWith('igcChange');
638+
checkSelectedRange(picker, {
639+
start: aMonthAgo.native,
640+
end: today.native,
641+
});
642+
});
612643

613644
it('should select a range of dates in dialog mode and emit igcChange when done is clicked', async () => {
614645
const eventSpy = spy(picker, 'emitEvent');
@@ -1070,8 +1101,7 @@ describe('Date range picker', () => {
10701101
expect(picker.open).to.be.true;
10711102
});
10721103

1073-
it('should not open the picker in dropdown mode when clicking the clear icon', async () => {
1074-
// TODO -elaborate test scenario?
1104+
it('should clear the inputs on clicking the clear icon - two inputs', async () => {
10751105
picker.value = { start: today.native, end: tomorrow.native };
10761106
await elementUpdated(picker);
10771107

@@ -1080,25 +1110,42 @@ describe('Date range picker', () => {
10801110

10811111
expect(picker.open).to.be.false;
10821112
expect(picker.value).to.deep.equal({ start: null, end: null });
1113+
expect(dateTimeInputs[0].value).to.be.null;
1114+
expect(dateTimeInputs[1].value).to.be.null;
10831115
});
10841116

1085-
it('should not open the picker in dialog mode when clicking the clear icon', async () => {
1086-
// TODO -elaborate test scenario?
1087-
picker.mode = 'dialog';
1117+
it('should clear the inputs on clicking the clear icon - single input', async () => {
1118+
picker.useTwoInputs = false;
10881119
picker.value = { start: today.native, end: tomorrow.native };
10891120
await elementUpdated(picker);
10901121

10911122
simulateClick(getIcon(picker, clearIcon));
10921123
await elementUpdated(picker);
10931124

10941125
expect(picker.open).to.be.false;
1095-
picker.value = { start: null, end: null };
1126+
expect(picker.value).to.deep.equal({ start: null, end: null });
1127+
const input = picker.renderRoot!.querySelector(
1128+
IgcInputComponent.tagName
1129+
)!;
1130+
expect(input.value).to.equal('');
10961131
});
10971132

1098-
// TODO
1099-
it('should emit igcInput and igcChange on input value change', async () => {});
1133+
it('should emit igcInput and igcChange on input value change', async () => {
1134+
const eventSpy = spy(picker, 'emitEvent');
1135+
1136+
dateTimeInputs[0].focus();
1137+
await elementUpdated(dateTimeInputs[0]);
1138+
expect(isFocused(dateTimeInputs[0])).to.be.true;
1139+
1140+
simulateKeyboard(dateTimeInputs[0], arrowUp);
1141+
await elementUpdated(picker);
1142+
1143+
expect(eventSpy).calledWith('igcInput');
1144+
eventSpy.resetHistory();
11001145

1101-
it('should not swap the dates while typing', async () => {});
1146+
simulateKeyboard(dateTimeInputs[0], arrowDown);
1147+
await elementUpdated(picker);
1148+
});
11021149

11031150
it('should set the calendar active date to the start of the range while typing', async () => {});
11041151
});
@@ -1751,9 +1798,12 @@ const checkSelectedRange = (
17511798
IgcCalendarComponent.tagName
17521799
)!;
17531800

1754-
checkDatesEqual(picker.value?.start!, expectedValue?.start!);
1755-
checkDatesEqual(picker.value?.end!, expectedValue?.end!);
1756-
1801+
if (expectedValue?.start) {
1802+
checkDatesEqual(picker.value?.start!, expectedValue.start);
1803+
}
1804+
if (expectedValue?.end) {
1805+
checkDatesEqual(picker.value?.end!, expectedValue.end);
1806+
}
17571807
if (!useTwoInputs) {
17581808
const input = picker.renderRoot.querySelector(IgcInputComponent.tagName)!;
17591809
const start = expectedValue?.start
@@ -1775,8 +1825,12 @@ const checkSelectedRange = (
17751825
const inputs = picker.renderRoot.querySelectorAll(
17761826
IgcDateTimeInputComponent.tagName
17771827
);
1778-
checkDatesEqual(inputs[0].value!, expectedValue?.start!);
1779-
checkDatesEqual(inputs[1].value!, expectedValue?.end!);
1828+
if (expectedValue?.start) {
1829+
checkDatesEqual(inputs[0].value!, expectedValue.start);
1830+
}
1831+
if (expectedValue?.end) {
1832+
checkDatesEqual(inputs[1].value!, expectedValue.end);
1833+
}
17801834
}
17811835

17821836
if (expectedValue?.start) {

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,11 +303,12 @@ export default class IgcDateRangePickerComponent extends FormAssociatedRequiredM
303303
const converted = convertToDateRange(value);
304304
this._startDate = converted?.start ?? null;
305305
this._endDate = converted?.end ?? null;
306-
this.setCalendarRangeValues();
307-
this.updateMaskedRangeValue();
308306

309307
this._formValue.setValueAndFormState(converted);
310308
this._validate();
309+
310+
this.setCalendarRangeValues();
311+
this.updateMaskedRangeValue();
311312
}
312313

313314
public get value(): DateRangeValue | null {
@@ -705,6 +706,7 @@ export default class IgcDateRangePickerComponent extends FormAssociatedRequiredM
705706
} else {
706707
this._endDate = newValue;
707708
}
709+
this._calendar.activeDate = newValue;
708710

709711
this.value = { start: this._startDate, end: this._endDate };
710712
this.emitEvent('igcInput', { detail: this.value });
@@ -739,6 +741,7 @@ export default class IgcDateRangePickerComponent extends FormAssociatedRequiredM
739741
this._defaultMask = DateTimeUtil.getDefaultMask(this.locale);
740742
if (this._inputs[0] && this._inputs[1]) {
741743
this._inputs[0].locale = this.locale;
744+
this._inputs[1].locale = this.locale;
742745
}
743746
this.updateMaskedRangeValue();
744747
}
@@ -895,7 +898,6 @@ export default class IgcDateRangePickerComponent extends FormAssociatedRequiredM
895898
/** Clears the input parts of the component of any user input */
896899
public clear() {
897900
this.value = null;
898-
this._activeDate = this._startDate ?? new Date();
899901
this._inputs[0]?.clear();
900902
this._inputs[1]?.clear();
901903
}

0 commit comments

Comments
 (0)