Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Without `activeDate`, a value set from code brings its month into view in the calendar, also while the calendar is open. Before, the calendar stayed on its month until the next open from the calendar icon or the keyboard.
- With `visibleMonths` above 1, an open by the user, from the calendar icon, <kbd>Alt</kbd> + <kbd>Arrow Down</kbd> or a click on the input or the label in dialog mode, shows the value in the first month. Before, the value could open in another month.
- #### Date picker, Date range picker
- While you type, the calendar selects a complete typed date. While the date is incomplete or not valid, the calendar keeps its last selection, and an empty input clears it. In the date range picker, each end follows its own text. The value still changes only when the edit is committed. Before, the date picker calendar showed the committed date until the blur. The date range picker calendar dropped an end while its text was incomplete, and the whole range in single input mode.
- In editable dropdown mode, a clear through the clear icon is reported with `igcChange` when the focus leaves the picker. Before, the input reported it when the focus left the input, also on a move to the calendar or to the other input of a date range picker.
- #### Date picker, Date time input
- `stepUp(DatePart.AmPm)` and `stepDown(DatePart.AmPm)` move the time 12 hours also when the input format has no AM/PM part, as a step does for the other parts that the format does not show. Before, they did nothing.
Expand Down Expand Up @@ -82,6 +83,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- In multiple selection, a change of `name` moves the submitted values to the new name at once. Before, the form submitted them under the old name.
- #### Date picker
- The first render no longer logs a Lit development warning that a cached field was read before the first update.
- The `igcInput` and `igcChange` events of slotted content, such as an input in the `suffix` slot, go on to the application and do not change the value. Before, the picker stopped them, and an `igcChange` from slotted content could change the value.
- #### Date picker, Date range picker
- An editor no longer shows the invalid styles when the focus moves from it to other content of the picker, such as the other editor of a date range picker. The editors show the invalid state of the picker, together with its validation message.
- When you clear the value and then open and close the picker before you leave it, the picker now emits `igcChange` for the clear.
Expand All @@ -92,6 +94,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- A form reset discards the text that you typed and did not commit yet, as for a native date input. Before, a partly typed date stayed in the focused input, and in a picker the next blur committed it as a date, also over the restored default value.
- An empty value that code or a form reset sets while the input has focus replaces the date that the input shows. Before, the input kept showing the old date, and a spin started from it.
- A year spin stops at year 9999 and at year 1, and a year outside them, such as one set from code, only moves toward them. Before, a spin past year 9999 gave a five digit year that did not fit the input.
- A `prompt` that is also a separator of the input format no longer makes a typed date incomplete. Examples are `-` with `yyyy-MM-dd`, and `-` with the ` - ` of the date range picker. Before, the `igcInput` detail of a complete date was `null`, and an arrow key or a spin started again from today.
- Ctrl + Arrow Left and Ctrl + Arrow Right stop at each date part in an input format without separators, such as `yyyyMMdd`, and do not stop inside a separator of more than one character, such as `, `. Before, the date time input went to the start or the end of the input, and the single input of the date range picker skipped a part.
- #### Date picker, Date time input
- `stepUp(DatePart.AmPm)` and `stepDown(DatePart.AmPm)` read AM or PM from the value. Before, while the input did not have focus and the display format was different from the input format, they read the display text, so they did nothing or switched only from PM to AM.
Expand Down
19 changes: 19 additions & 0 deletions src/components/date-picker/date-picker-form.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
type ValidationContainerTestsParams,
ValidityHelpers,
} from '#internals/testing/validity-helpers.spec.js';
import IgcCalendarComponent from '../calendar/calendar.js';
import { type DateRangeDescriptor, DateRangeType } from '../calendar/types.js';
import IgcDateTimeInputComponent from '../date-time-input/date-time-input.js';
import IgcInputComponent from '../input/input.js';
Expand Down Expand Up @@ -194,6 +195,24 @@ describe('igc-datepicker form integration', () => {
spec.assertSubmitHasValue(defaultDate.toISOString());
});

it('drops the typed calendar selection when the reset keeps the value', async () => {
const defaultDate = new Date(2024, 0, 15);
spec.setAttributes({ value: defaultDate.toISOString() });
await elementUpdated(spec.element);
const calendar = spec.element.renderRoot.querySelector(
IgcCalendarComponent.tagName
)!;

await simulateTypingOver(editor, '12122024');
await elementUpdated(spec.element);
checkDatesEqual(calendar.value!, new Date(2024, 11, 12));

await reset();

checkDatesEqual(spec.element.value!, defaultDate);
checkDatesEqual(calendar.value!, defaultDate);
});

it('shows the empty mask when the reset empties the value', async () => {
spec.setProperties({ value: new Date(2024, 2, 3) });
await elementUpdated(spec.element);
Expand Down
56 changes: 50 additions & 6 deletions src/components/date-picker/date-picker.base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,16 @@ export abstract class IgcDatePickerBaseComponent<
read: () => this._value,
write: (value) =>
this._writeFrom('edit', () => {
// A pick or a commit replaces the edit, also with the same value.
this._editValue = undefined;
this._value = value;
}),
report: (force) => this._reportChange(this._value, force),
});

/** The value as the user types it. Without a draft, the calendar shows the value. */
/** The calendar selection while the user types. Wrapped, so a blank edit (`null`) differs from no edit. */
@state({ hasChanged: (value, old) => !equal(value, old) })
protected _draft?: { value: T | null };
protected _editValue?: { value: T | null };

protected _activeDate: Date | null = null;
protected _min: Date | null = null;
Expand All @@ -163,10 +165,12 @@ export abstract class IgcDatePickerBaseComponent<
@query(IgcDialogComponent.tagName)
private readonly _dialog?: IgcDialogComponent;

protected get _calendarValue(): T | null {
return this._editValue ? this._editValue.value : this._value;
}

private get _calendarSelection(): CalendarBinding {
return this._shape.toCalendar(
this._draft ? this._draft.value : this._value
);
return this._shape.toCalendar(this._calendarValue);
}

private get _startDate(): Date | null {
Expand All @@ -181,7 +185,10 @@ export abstract class IgcDatePickerBaseComponent<
protected set _value(value: T | null) {
const previous = this._value;
this._formValue.setValueAndFormState(value);
this._draft = undefined;
// A same-value write, such as a re-render, keeps the edit, as the editor does.
if (!this._formValue.isSameValue(previous, this._value)) {
this._editValue = this._editAfterValueChange(previous);
}
if (!this._activeDate) {
this._setCalendarActiveDate(this._startDate);
}
Expand Down Expand Up @@ -241,6 +248,13 @@ export abstract class IgcDatePickerBaseComponent<

//#region Overridable hooks

/** The edit after a value change. The editor drops its edit for a new value, so the calendar shows the value. */
protected _editAfterValueChange(
_previous: T | null
): { value: T | null } | undefined {
return undefined;
}

protected get _calendarIconTitle(): string | undefined {
return undefined;
}
Expand Down Expand Up @@ -503,6 +517,17 @@ export abstract class IgcDatePickerBaseComponent<
.set(escapeKey, this._onEscapeKey, { when: () => this.open });
}

protected override createRenderRoot(): HTMLElement | DocumentFragment {
const root = super.createRenderRoot();

// Any editor blur ends the edit, also a commit that emits nothing. Focus moves inside the picker never reach the host.
root.addEventListener('focusout', () => {
this._editValue = undefined;
});

return root;
}

protected override willUpdate(props: PropertyValues): void {
super.willUpdate(props);

Expand Down Expand Up @@ -532,6 +557,12 @@ export abstract class IgcDatePickerBaseComponent<
);
}

/** A reset discards the edit of the editors, also when the restored value is the same. */
protected override formResetCallback(): void {
super.formResetCallback();
this._editValue = undefined;
}

protected _setDateConstraints(): void {
this._dateConstraints = createDateConstraints(
this._min,
Expand Down Expand Up @@ -617,6 +648,19 @@ export abstract class IgcDatePickerBaseComponent<
this._calendar.values = values;
}

/**
* Stops the inner event of an editor, and returns whether the picker owns the editor. An editor that a layout
* change removes commits nothing, as its typed text goes with it. The events of slotted content go on to the
* application.
*/
protected _acceptEditorEvent(event: Event): boolean {
const target = event.target as PickerEditor;
if (target.getRootNode() !== this.renderRoot) return false;

event.stopPropagation();
return this._ownedEditors.includes(target);
}

/** Commits a value from an editor. An edit back to the reported value emits nothing. */
protected _commitEditorValue(value: T | null): void {
this._writeFrom('user', () => this._session.commitEdit(value));
Expand Down
180 changes: 180 additions & 0 deletions src/components/date-picker/date-picker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ import {
withDocumentSheets,
} from '#internals/testing/helpers.spec.js';
import {
simulateBackspaceOver,
simulateCancelableKeydown,
simulateClick,
simulateComposingKeys,
simulateInput,
simulateKeyboard,
simulateScroll,
simulateTypingOver,
} from '#internals/testing/simulate.spec.js';
import { isUserChange } from '#internals/user-change.js';
import { formatString } from '#internals/utils/strings.js';
Expand Down Expand Up @@ -1062,6 +1064,158 @@ describe('Date picker', () => {
});
});

describe('Calendar selection while typing', () => {
const march3 = new Date(2020, 2, 3);
const october10 = new Date(2020, 9, 10);
let input: HTMLInputElement;

const typeOver = async (text: string) => {
await simulateTypingOver(dateTimeInput, text);
await elementUpdated(picker);
};

const deleteText = async (start: number, end: number) => {
await simulateBackspaceOver(dateTimeInput, start, end);
await elementUpdated(picker);
};

beforeEach(async () => {
input = dateTimeInput.renderRoot.querySelector('input')!;
picker.inputFormat = 'MM/dd/yyyy';
picker.value = march3;
await picker.show();
});

it('selects a complete typed date and keeps the value', async () => {
await typeOver('10102020');

expect(input.value).to.equal('10/10/2020');
checkDatesEqual(calendar.value!, october10);
checkDatesEqual(picker.value!, march3);
});

it('keeps the last selection while the date is incomplete', async () => {
const eventSpy = spy(picker, 'emitEvent');

await typeOver('1010');

expect(input.value).to.equal('10/10/____');
checkDatesEqual(calendar.value!, march3);
expect(eventSpy).calledWith('igcInput', { detail: null });

await typeOver('10102020');
await deleteText(6, 10);

expect(input.value).to.equal('10/10/____');
checkDatesEqual(calendar.value!, october10);
});

it('keeps the last selection for an invalid date', async () => {
await typeOver('02302020');

expect(input.value).to.equal('02/30/2020');
checkDatesEqual(calendar.value!, march3);
});

it('clears the selection when the input is emptied', async () => {
dateTimeInput.focus();
await elementUpdated(picker);

await deleteText(0, input.value.length);

expect(input.value).to.equal('__/__/____');
expect(calendar.value).to.be.null;
checkDatesEqual(picker.value!, march3);
});

it('keeps the typed selection when code sets the same value', async () => {
await typeOver('10102020');

picker.value = new Date(march3.getTime());
await elementUpdated(picker);

expect(input.value).to.equal('10/10/2020');
checkDatesEqual(calendar.value!, october10);
});

it('selects a new value set from code', async () => {
const january1 = new Date(2021, 0, 1);
await typeOver('10102020');

picker.value = january1;
await elementUpdated(picker);

checkDatesEqual(calendar.value!, january1);
});

it('drops the typed selection on clear()', async () => {
picker.value = null;
await typeOver('10102020');
checkDatesEqual(calendar.value!, october10);

picker.clear();
await elementUpdated(picker);

expect(calendar.value).to.be.null;
});

it('selects the value after a blur that commits the same value', async () => {
const january1 = new Date(2020, 0, 1);
picker.value = january1;
await typeOver('10102020');

// The year stays, so the commit fills the month and the day: January 1.
await deleteText(0, 5);
expect(input.value).to.equal('__/__/2020');
checkDatesEqual(calendar.value!, october10);

const eventSpy = spy(picker, 'emitEvent');
dateTimeInput.blur();
await elementUpdated(picker);

expect(eventSpy).not.calledWith('igcChange');
checkDatesEqual(picker.value!, january1);
checkDatesEqual(calendar.value!, january1);
});

it('selects the value after stepUp and setRangeText', async () => {
const march2 = new Date(2020, 2, 2);

await typeOver('03022020');
checkDatesEqual(calendar.value!, march2);

picker.stepUp(DatePart.Date);
await elementUpdated(picker);

expect(input.value).to.equal('03/03/2020');
checkDatesEqual(picker.value!, march3);
checkDatesEqual(calendar.value!, march3);

await typeOver('03022020');
checkDatesEqual(calendar.value!, march2);

picker.setRangeText('03', 3, 5);
await elementUpdated(picker);

expect(input.value).to.equal('03/03/2020');
checkDatesEqual(calendar.value!, march3);
});

it('selects the value when the focus moves to the calendar', async () => {
const january1 = new Date(2020, 0, 1);
picker.value = january1;
await typeOver('10102020');
await deleteText(0, 5);
checkDatesEqual(calendar.value!, october10);

calendar[focusActiveDate]();
await elementUpdated(picker);

checkDatesEqual(picker.value!, january1);
checkDatesEqual(calendar.value!, january1);
});
});

describe('Calendar view', () => {
it('brings the month of a value set from code into view', async () => {
const march1 = new Date(2025, 2, 1);
Expand Down Expand Up @@ -1531,6 +1685,32 @@ describe('Date picker', () => {
checkDatesEqual(picker.value!, today);
});

it('leaves the input and change events of slotted content alone', async () => {
const value = new Date(2020, 2, 3);
const slotted = document.createElement('span');
slotted.slot = 'suffix';
picker.append(slotted);
picker.value = value;
await elementUpdated(picker);

const eventSpy = spy(picker, 'emitEvent');
const reached: string[] = [];
const record = ({ type }: Event) => reached.push(type);
picker.addEventListener('igcInput', record);
picker.addEventListener('igcChange', record);

for (const type of ['igcInput', 'igcChange']) {
slotted.dispatchEvent(
new CustomEvent(type, { bubbles: true, composed: true, detail: '' })
);
}
await elementUpdated(picker);

expect(reached).to.eql(['igcInput', 'igcChange']);
expect(eventSpy).not.called;
checkDatesEqual(picker.value!, value);
});

it('should emit or not igcInput according to nonEditable property', async () => {
const expectedValue = new Date();
const eventSpy = spy(picker, 'emitEvent');
Expand Down
Loading