Skip to content

Commit e88475b

Browse files
rkaraivanovddaribo
andauthored
fix(date-picker): Emit change event in non-editable configurations (#1885)
Date-picker correctly emits `igcChange` event when in dialog mode or when set to a non-editable configuration, such as readonly or non-editable, after user interaction. Closes #1884 --------- Co-authored-by: Bozhidara Pachilova <[email protected]>
1 parent 2e5174f commit e88475b

File tree

4 files changed

+171
-101
lines changed

4 files changed

+171
-101
lines changed

src/components/calendar/helpers.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
asNumber,
33
findElementFromEventPath,
44
first,
5+
isEmpty,
56
isString,
67
last,
78
modulo,
@@ -255,3 +256,29 @@ export function isDateInRanges(
255256
}
256257
});
257258
}
259+
260+
export function createDateConstraints(
261+
min: Date | null,
262+
max: Date | null,
263+
disabledDates?: DateRangeDescriptor[]
264+
) {
265+
const constraints: DateRangeDescriptor[] = [];
266+
267+
if (min) {
268+
constraints.push({
269+
type: DateRangeType.Before,
270+
dateRange: [min],
271+
});
272+
}
273+
274+
if (max) {
275+
constraints.push({
276+
type: DateRangeType.After,
277+
dateRange: [max],
278+
});
279+
}
280+
281+
constraints.push(...(disabledDates ?? []));
282+
283+
return !isEmpty(constraints) ? constraints : undefined;
284+
}

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

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ import {
1616
} from '../common/controllers/key-bindings.js';
1717
import { defineComponents } from '../common/definitions/defineComponents.js';
1818
import { equal } from '../common/util.js';
19-
import { simulateClick, simulateKeyboard } from '../common/utils.spec.js';
19+
import {
20+
isFocused,
21+
simulateClick,
22+
simulateKeyboard,
23+
} from '../common/utils.spec.js';
2024
import IgcDateTimeInputComponent from '../date-time-input/date-time-input.js';
2125
import IgcDatePickerComponent from './date-picker.js';
2226

@@ -963,6 +967,53 @@ describe('Date picker', () => {
963967
checkDatesEqual(calendar.activeDate, expectedValue);
964968
});
965969

970+
it('issue 1884 - should emit igcChange event in dialog mode after clearing the value and losing focus', async () => {
971+
const eventSpy = spy(picker, 'emitEvent');
972+
// const nativeInput = dateTimeInput.renderRoot.querySelector('input')!;
973+
974+
// Dropdown mode
975+
976+
picker.value = CalendarDay.today.native;
977+
picker.focus();
978+
picker.blur();
979+
await elementUpdated(picker);
980+
981+
picker.focus();
982+
expect(isFocused(dateTimeInput)).to.be.true;
983+
984+
// Simulate clicking the clear button
985+
picker.clear();
986+
987+
picker.blur();
988+
expect(isFocused(dateTimeInput)).to.be.false;
989+
990+
expect(eventSpy).to.be.calledWith('igcChange', {
991+
detail: null,
992+
});
993+
994+
eventSpy.resetHistory();
995+
996+
// Dialog mode
997+
picker.mode = 'dialog';
998+
picker.value = CalendarDay.today.native;
999+
picker.focus();
1000+
picker.blur();
1001+
await elementUpdated(picker);
1002+
1003+
picker.focus();
1004+
expect(isFocused(dateTimeInput)).to.be.true;
1005+
1006+
// Simulate clicking the clear button
1007+
picker.clear();
1008+
1009+
picker.blur();
1010+
expect(isFocused(dateTimeInput)).to.be.false;
1011+
1012+
expect(eventSpy).to.be.calledWith('igcChange', {
1013+
detail: null,
1014+
});
1015+
});
1016+
9661017
describe('Readonly state', () => {
9671018
describe('Dropdown mode', () => {
9681019
beforeEach(async () => {

0 commit comments

Comments
 (0)