diff --git a/packages/x-date-pickers-pro/src/internal/utils/valueManagers.ts b/packages/x-date-pickers-pro/src/internal/utils/valueManagers.ts index 8ba18bf29f08..ea36d139a99e 100644 --- a/packages/x-date-pickers-pro/src/internal/utils/valueManagers.ts +++ b/packages/x-date-pickers-pro/src/internal/utils/valueManagers.ts @@ -30,15 +30,11 @@ export const rangeValueManager: RangePickerValueManager = { areValuesEqual: (utils, a, b) => areDatesEqual(utils, a[0], b[0]) && areDatesEqual(utils, a[1], b[1]), isSameError: (a, b) => b !== null && a[1] === b[1] && a[0] === b[0], + hasError: (error) => error[0] != null || error[1] != null, defaultErrorState: [null, null], }; -export const rangeFieldValueManager: FieldValueManager< - DateRange, - any, - RangeFieldSection, - DateRangeValidationError | TimeRangeValidationError | DateTimeRangeValidationError -> = { +export const rangeFieldValueManager: FieldValueManager, any, RangeFieldSection> = { updateReferenceValue: (utils, value, prevReferenceValue) => { const shouldKeepStartDate = value[0] != null && utils.isValid(value[0]); const shouldKeepEndDate = value[1] != null && utils.isValid(value[1]); @@ -144,5 +140,4 @@ export const rangeFieldValueManager: FieldValueManager< }), }; }, - hasError: (error) => error[0] != null || error[1] != null, }; diff --git a/packages/x-date-pickers/src/internals/hooks/useField/useField.ts b/packages/x-date-pickers/src/internals/hooks/useField/useField.ts index fd2a58fc8136..4c3abac5f3ca 100644 --- a/packages/x-date-pickers/src/internals/hooks/useField/useField.ts +++ b/packages/x-date-pickers/src/internals/hooks/useField/useField.ts @@ -398,8 +398,8 @@ export const useField = < return error; } - return fieldValueManager.hasError(validationError); - }, [fieldValueManager, validationError, error]); + return valueManager.hasError(validationError); + }, [valueManager, validationError, error]); React.useEffect(() => { // Select the right section when focused on mount (`autoFocus = true` on the input) diff --git a/packages/x-date-pickers/src/internals/hooks/useField/useField.types.ts b/packages/x-date-pickers/src/internals/hooks/useField/useField.types.ts index e141ccb8f02c..eb201f1bf18f 100644 --- a/packages/x-date-pickers/src/internals/hooks/useField/useField.types.ts +++ b/packages/x-date-pickers/src/internals/hooks/useField/useField.types.ts @@ -19,7 +19,7 @@ export interface UseFieldParams< forwardedProps: TForwardedProps; internalProps: TInternalProps; valueManager: PickerValueManager>; - fieldValueManager: FieldValueManager>; + fieldValueManager: FieldValueManager; validator: Validator< TValue, TDate, @@ -197,7 +197,7 @@ export type FieldSelectedSectionsIndexes = { shouldSelectBoundarySelectors?: boolean; }; -export interface FieldValueManager { +export interface FieldValueManager { /** * Creates the section list from the current value. * The `prevSections` are used on the range fields to avoid losing the sections of a partially filled date when editing the other date. @@ -265,13 +265,6 @@ export interface FieldValueManager TValue; - /** - * Checks if the current error is empty or not. - * @template TError - * @param {TError} error The current error. - * @returns {boolean} `true` if the current error is not empty. - */ - hasError: (error: TError) => boolean; } export interface UseFieldState { diff --git a/packages/x-date-pickers/src/internals/hooks/usePicker/usePickerValue.ts b/packages/x-date-pickers/src/internals/hooks/usePicker/usePickerValue.ts index bf47a101f7dc..fa8e63347088 100644 --- a/packages/x-date-pickers/src/internals/hooks/usePicker/usePickerValue.ts +++ b/packages/x-date-pickers/src/internals/hooks/usePicker/usePickerValue.ts @@ -63,6 +63,13 @@ export interface PickerValueManager { * @returns {boolean} `true` if the new error is different from the previous one. */ isSameError: (error: TError, prevError: TError | null) => boolean; + /** + * Checks if the current error is empty or not. + * @template TError + * @param {TError} error The current error. + * @returns {boolean} `true` if the current error is not empty. + */ + hasError: (error: TError) => boolean; /** * The value identifying no error, used to initialise the error state. */ @@ -505,14 +512,13 @@ export const usePickerValue = < }; const isValid = (testedValue: TValue) => { - const validationResponse = validator({ + const error = validator({ adapter, value: testedValue, props: { ...props, value: testedValue }, }); - return Array.isArray(testedValue) - ? (validationResponse as any[]).every((v) => v === null) - : validationResponse === null; + + return valueManager.hasError(error); }; const layoutResponse: UsePickerValueLayoutResponse = { diff --git a/packages/x-date-pickers/src/internals/utils/valueManagers.ts b/packages/x-date-pickers/src/internals/utils/valueManagers.ts index 19badfe2a70f..3c20a1ff287f 100644 --- a/packages/x-date-pickers/src/internals/utils/valueManagers.ts +++ b/packages/x-date-pickers/src/internals/utils/valueManagers.ts @@ -24,15 +24,11 @@ export const singleItemValueManager: SingleItemPickerValueManager = { cleanValue: replaceInvalidDateByNull, areValuesEqual: areDatesEqual, isSameError: (a, b) => a === b, + hasError: (error) => error != null, defaultErrorState: null, }; -export const singleItemFieldValueManager: FieldValueManager< - any, - any, - FieldSection, - DateValidationError | TimeValidationError | DateTimeValidationError -> = { +export const singleItemFieldValueManager: FieldValueManager = { updateReferenceValue: (utils, value, prevReferenceValue) => value == null || !utils.isValid(value) ? prevReferenceValue : value, getSectionsFromValue: (utils, date, prevSections, isRTL, getSectionsFromDate) => { @@ -59,5 +55,4 @@ export const singleItemFieldValueManager: FieldValueManager< }), parseValueStr: (valueStr, referenceValue, parseDate) => parseDate(valueStr.trim(), referenceValue), - hasError: (error) => error != null, };