Skip to content

Commit

Permalink
[pickers] Move hasError from fieldValueManager to valueManager (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
flaviendelangle committed Apr 6, 2023
1 parent 6ea7abb commit e5100a1
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
any,
RangeFieldSection,
DateRangeValidationError | TimeRangeValidationError | DateTimeRangeValidationError
> = {
export const rangeFieldValueManager: FieldValueManager<DateRange<any>, any, RangeFieldSection> = {
updateReferenceValue: (utils, value, prevReferenceValue) => {
const shouldKeepStartDate = value[0] != null && utils.isValid(value[0]);
const shouldKeepEndDate = value[1] != null && utils.isValid(value[1]);
Expand Down Expand Up @@ -144,5 +140,4 @@ export const rangeFieldValueManager: FieldValueManager<
}),
};
},
hasError: (error) => error[0] != null || error[1] != null,
};
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export interface UseFieldParams<
forwardedProps: TForwardedProps;
internalProps: TInternalProps;
valueManager: PickerValueManager<TValue, TDate, InferError<TInternalProps>>;
fieldValueManager: FieldValueManager<TValue, TDate, TSection, InferError<TInternalProps>>;
fieldValueManager: FieldValueManager<TValue, TDate, TSection>;
validator: Validator<
TValue,
TDate,
Expand Down Expand Up @@ -197,7 +197,7 @@ export type FieldSelectedSectionsIndexes = {
shouldSelectBoundarySelectors?: boolean;
};

export interface FieldValueManager<TValue, TDate, TSection extends FieldSection, TError> {
export interface FieldValueManager<TValue, TDate, TSection extends FieldSection> {
/**
* 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.
Expand Down Expand Up @@ -265,13 +265,6 @@ export interface FieldValueManager<TValue, TDate, TSection extends FieldSection,
value: TValue,
prevReferenceValue: TValue,
) => 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<TValue, TSection extends FieldSection> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ export interface PickerValueManager<TValue, TDate, TError> {
* @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.
*/
Expand Down Expand Up @@ -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<TValue> = {
Expand Down
9 changes: 2 additions & 7 deletions packages/x-date-pickers/src/internals/utils/valueManagers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any, any, FieldSection> = {
updateReferenceValue: (utils, value, prevReferenceValue) =>
value == null || !utils.isValid(value) ? prevReferenceValue : value,
getSectionsFromValue: (utils, date, prevSections, isRTL, getSectionsFromDate) => {
Expand All @@ -59,5 +55,4 @@ export const singleItemFieldValueManager: FieldValueManager<
}),
parseValueStr: (valueStr, referenceValue, parseDate) =>
parseDate(valueStr.trim(), referenceValue),
hasError: (error) => error != null,
};

0 comments on commit e5100a1

Please sign in to comment.