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
15 changes: 10 additions & 5 deletions src/material/datepicker/datepicker-input-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,22 +289,27 @@ export abstract class MatDatepickerInputBase<S, D = ExtractDateTypeFromSelection
return this._validator ? this._validator(c) : null;
}

// Implemented as part of ControlValueAccessor.
/** Implemented as part of ControlValueAccessor. */
writeValue(value: D): void {
this._assignValueProgrammatically(value);
// We produce a different date object on each keystroke which can cause signal forms'
// interop logic to keep calling `writeValue` with the same value as the user is typing.
// Skip such cases since they can prevent the user from typing (see #32442).
if (!value || value !== this.value) {
this._assignValueProgrammatically(value);
}
}

// Implemented as part of ControlValueAccessor.
/** Implemented as part of ControlValueAccessor. */
registerOnChange(fn: (value: any) => void): void {
this._cvaOnChange = fn;
}

// Implemented as part of ControlValueAccessor.
/** Implemented as part of ControlValueAccessor. */
registerOnTouched(fn: () => void): void {
this._onTouched = fn;
}

// Implemented as part of ControlValueAccessor.
/** Implemented as part of ControlValueAccessor. */
setDisabledState(isDisabled: boolean): void {
this.disabled = isDisabled;
}
Expand Down
16 changes: 16 additions & 0 deletions src/material/datepicker/datepicker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,22 @@ describe('MatDatepicker', () => {

expect(formControl.hasError('matDatepickerParse')).toBe(true);
});

it('should not re-format the input value if the forms module re-assigns the same date', () => {
const input = fixture.nativeElement.querySelector('input');
const date = new Date(2017, JAN, 1);
testComponent.formControl.setValue(date);
fixture.detectChanges();
expect(input.value).toContain('2017');

// Note: this isn't how users would behave, but it captures
// the sequence of events with signal forms.
input.value = 'foo';
testComponent.formControl.setValue(date);
fixture.detectChanges();

expect(input.value).toBe('foo');
});
});

describe('datepicker with mat-datepicker-toggle', () => {
Expand Down
Loading