Skip to content

Commit fb6e202

Browse files
committedOct 4, 2024
fix(material/timepicker): text field in parse error not up to date
Fixes that the `text` field in the `matTimepickerParse` error wasn't up-to-date with the element's value.
1 parent 1eda486 commit fb6e202

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed
 

‎src/material/timepicker/timepicker-input.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,14 @@ export class MatTimepickerInput<D> implements ControlValueAccessor, Validator, O
251251
const date = this._dateAdapter.parseTime(value, this._dateFormats.parse.timeInput);
252252
const hasChanged = !this._dateAdapter.sameTime(date, currentValue);
253253

254-
// We need to fire the CVA change event for all nulls, otherwise the validators won't run.
255-
this._assignUserSelection(date, !date || hasChanged || !!(value && !currentValue));
254+
if (!date || hasChanged || !!(value && !currentValue)) {
255+
// We need to fire the CVA change event for all nulls, otherwise the validators won't run.
256+
this._assignUserSelection(date, true);
257+
} else {
258+
// Call the validator even if the value hasn't changed since
259+
// some fields change depending on what the user has entered.
260+
this._validatorOnChange?.();
261+
}
256262
}
257263

258264
/** Handles the `blur` event. */

‎src/material/timepicker/timepicker.spec.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,22 @@ describe('MatTimepicker', () => {
10071007
clearElement(input);
10081008
typeInElement(input, 'not a valid date');
10091009
fixture.detectChanges();
1010-
expect(control.errors?.['matTimepickerParse']).toBeTruthy();
1010+
expect(control.errors?.['matTimepickerParse']).toEqual(
1011+
jasmine.objectContaining({
1012+
text: 'not a valid date',
1013+
}),
1014+
);
1015+
expect(control.value).toBeTruthy();
1016+
expect(adapter.isValid(control.value!)).toBe(false);
1017+
1018+
// Change from one invalid value to the other to make sure that the object stays in sync.
1019+
typeInElement(input, ' (changed)');
1020+
fixture.detectChanges();
1021+
expect(control.errors?.['matTimepickerParse']).toEqual(
1022+
jasmine.objectContaining({
1023+
text: 'not a valid date (changed)',
1024+
}),
1025+
);
10111026
expect(control.value).toBeTruthy();
10121027
expect(adapter.isValid(control.value!)).toBe(false);
10131028

0 commit comments

Comments
 (0)
Please sign in to comment.