Skip to content

Commit

Permalink
Work
Browse files Browse the repository at this point in the history
  • Loading branch information
flaviendelangle committed Nov 13, 2024
1 parent cae8762 commit f06c8ca
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 32 deletions.
5 changes: 4 additions & 1 deletion packages/x-date-pickers/src/DateField/useDateField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ export const useDateField = <
props: TAllProps,
) => {
const valueManager = React.useMemo(
() => getDateValueManager(props.enableAccessibleFieldDOMStructure),
() =>
getDateValueManager({
enableAccessibleFieldDOMStructure: props.enableAccessibleFieldDOMStructure,
}),
[props.enableAccessibleFieldDOMStructure],
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ export const useDateTimeField = <
props: TAllProps,
) => {
const valueManager = React.useMemo(
() => getDateTimeValueManager(props.enableAccessibleFieldDOMStructure),
() =>
getDateTimeValueManager({
enableAccessibleFieldDOMStructure: props.enableAccessibleFieldDOMStructure,
}),
[props.enableAccessibleFieldDOMStructure],
);

Expand Down
5 changes: 4 additions & 1 deletion packages/x-date-pickers/src/TimeField/useTimeField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ export const useTimeField = <
props: TAllProps,
) => {
const valueManager = React.useMemo(
() => getTimeValueManager(props.enableAccessibleFieldDOMStructure),
() =>
getTimeValueManager({
enableAccessibleFieldDOMStructure: props.enableAccessibleFieldDOMStructure,
}),
[props.enableAccessibleFieldDOMStructure],
);

Expand Down
18 changes: 13 additions & 5 deletions packages/x-date-pickers/src/models/valueManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@ export interface PickerValueManagerV8<
TError
>,
> {
// The v7 value manager object.
// This will be inlined inside the main `PickerValueManagerV8` object once every object using it is compatible with the new API.
/**
* Object containing basic methods to interact with the value of the picker or field.
* The properties of this object will be inlined inside the main `PickerValueManagerV8` object once every object using it is compatible with the new API.
*/
legacyValueManager: PickerValueManager<TIsRange, TError>;
/**
* Object containing all the necessary methods to interact with the value of the field.
* The properties of this object will be inlined inside the main `PickerValueManagerV8` object once every object using it is compatible with the new API.
*/
fieldValueManager: FieldValueManager<TIsRange>;
/**
* Checks if a value is valid and returns an error code otherwise.
Expand All @@ -45,6 +51,10 @@ export interface PickerValueManagerV8<
* The type of the value (e.g. 'date', 'date-time', 'time').
*/
valueType: FieldValueType;
/**
* `true` if the field is using the accessible DOM structure.
* `false` if the field is using the non-accessible legacy DOM structure (which will be deprecated and removed in the future).
*/
enableAccessibleFieldDOMStructure: TEnableAccessibleFieldDOMStructure;
}

Expand All @@ -55,10 +65,8 @@ interface ApplyDefaultsToFieldInternalPropsParameters<TFieldInternalProps extend

export type PickerAnyValueManagerV8 = PickerValueManagerV8<any, any, any, any, any>;

export type PickerAnyAccessibleValueManagerV8 = PickerValueManagerV8<any, true, any, any, any>;

/**
* Infer all the usual generic in the picker packages from a `PickerValueManager` interface.
* Infer all the usual generic in the picker packages from a `PickerValueManagerV8` interface.
*/
export type PickerManagerProperties<TManager extends PickerAnyValueManagerV8> =
TManager extends PickerValueManagerV8<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ export interface DateTimeFieldInternalPropsWithDefaults<
> extends UseFieldInternalProps<false, TEnableAccessibleFieldDOMStructure, DateTimeValidationError>,
ValidateDateTimeProps {}

type DateTimeFieldPropsToDefault =
| 'format'
// minTime and maxTime can still be undefined after applying defaults.
| 'minTime'
| 'maxTime'
| ValidateDateTimePropsToDefault;

export const getDateTimeFieldInternalPropsDefaults = <
TEnableAccessibleFieldDOMStructure extends boolean,
>({
Expand All @@ -46,22 +53,18 @@ export const getDateTimeFieldInternalPropsDefaults = <
}: Pick<MuiPickersAdapterContextValue, 'defaultDates' | 'utils'> & {
internalProps: Pick<
DateTimeFieldInternalProps<TEnableAccessibleFieldDOMStructure>,
| 'format'
| 'ampm'
| 'minDateTime'
| 'maxDateTime'
| 'minTime'
| 'maxTime'
| ValidateDateTimePropsToDefault
DateTimeFieldPropsToDefault | 'minDateTime' | 'maxDateTime' | 'ampm'
>;
}) => {
}): Pick<
DateTimeFieldInternalPropsWithDefaults<TEnableAccessibleFieldDOMStructure>,
DateTimeFieldPropsToDefault | 'disableIgnoringDatePartForTimeValidation'
> => {
const ampm = internalProps.ampm ?? utils.is12HourCycleInCurrentLocale();
const defaultFormat = ampm
? utils.formats.keyboardDateTime12h
: utils.formats.keyboardDateTime24h;

return {
ampm,
disablePast: internalProps.disablePast ?? false,
disableFuture: internalProps.disableFuture ?? false,
format: internalProps.format ?? defaultFormat,
Expand All @@ -83,9 +86,11 @@ export const getDateTimeFieldInternalPropsDefaults = <
};
};

export const getDateTimeValueManager = <TEnableAccessibleFieldDOMStructure extends boolean = false>(
enableAccessibleFieldDOMStructure: TEnableAccessibleFieldDOMStructure = false as TEnableAccessibleFieldDOMStructure,
): DateTimeValueManager<TEnableAccessibleFieldDOMStructure> => ({
export const getDateTimeValueManager = <TEnableAccessibleFieldDOMStructure extends boolean = true>({
enableAccessibleFieldDOMStructure = true as TEnableAccessibleFieldDOMStructure,
}: {
enableAccessibleFieldDOMStructure: TEnableAccessibleFieldDOMStructure | undefined;
}): DateTimeValueManager<TEnableAccessibleFieldDOMStructure> => ({
legacyValueManager: singleItemValueManager,
fieldValueManager: singleItemFieldValueManager,
validator: validateDateTime,
Expand Down
19 changes: 13 additions & 6 deletions packages/x-date-pickers/src/valueManagers/getDateValueManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export interface DateFieldInternalPropsWithDefaults<
> extends UseFieldInternalProps<false, TEnableAccessibleFieldDOMStructure, DateValidationError>,
ValidateDateProps {}

type DateFieldPropsToDefault = 'format' | ValidateDatePropsToDefault;

export const getDateFieldInternalPropsDefaults = <
TEnableAccessibleFieldDOMStructure extends boolean,
>({
Expand All @@ -44,19 +46,24 @@ export const getDateFieldInternalPropsDefaults = <
}: Pick<MuiPickersAdapterContextValue, 'defaultDates' | 'utils'> & {
internalProps: Pick<
DateFieldInternalProps<TEnableAccessibleFieldDOMStructure>,
'format' | ValidateDatePropsToDefault
DateFieldPropsToDefault
>;
}) => ({
}): Pick<
DateFieldInternalPropsWithDefaults<TEnableAccessibleFieldDOMStructure>,
DateFieldPropsToDefault
> => ({
format: internalProps.format ?? utils.formats.keyboardDate,
disablePast: internalProps.disablePast ?? false,
disableFuture: internalProps.disableFuture ?? false,
format: internalProps.format ?? utils.formats.keyboardDate,
minDate: applyDefaultDate(utils, internalProps.minDate, defaultDates.minDate),
maxDate: applyDefaultDate(utils, internalProps.maxDate, defaultDates.maxDate),
});

export const getDateValueManager = <TEnableAccessibleFieldDOMStructure extends boolean = false>(
enableAccessibleFieldDOMStructure: TEnableAccessibleFieldDOMStructure = false as TEnableAccessibleFieldDOMStructure,
): DateValueManager<TEnableAccessibleFieldDOMStructure> => ({
export const getDateValueManager = <TEnableAccessibleFieldDOMStructure extends boolean = true>({
enableAccessibleFieldDOMStructure = true as TEnableAccessibleFieldDOMStructure,
}: {
enableAccessibleFieldDOMStructure: TEnableAccessibleFieldDOMStructure | undefined;
}): DateValueManager<TEnableAccessibleFieldDOMStructure> => ({
legacyValueManager: singleItemValueManager,
fieldValueManager: singleItemFieldValueManager,
validator: validateDate,
Expand Down
18 changes: 12 additions & 6 deletions packages/x-date-pickers/src/valueManagers/getTimeValueManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export interface TimeFieldInternalPropsWithDefaults<
> extends UseFieldInternalProps<false, TEnableAccessibleFieldDOMStructure, TimeValidationError>,
ValidateTimeProps {}

type TimeFieldPropsToDefault = 'format' | ValidateTimePropsToDefault;

export const getTimeFieldInternalPropsDefaults = <
TEnableAccessibleFieldDOMStructure extends boolean,
>({
Expand All @@ -44,23 +46,27 @@ export const getTimeFieldInternalPropsDefaults = <
}: Pick<MuiPickersAdapterContextValue, 'utils'> & {
internalProps: Pick<
TimeFieldInternalProps<TEnableAccessibleFieldDOMStructure>,
'format' | 'ampm' | ValidateTimePropsToDefault
TimeFieldPropsToDefault | 'ampm'
>;
}) => {
}): Pick<
TimeFieldInternalPropsWithDefaults<TEnableAccessibleFieldDOMStructure>,
TimeFieldPropsToDefault
> => {
const ampm = internalProps.ampm ?? utils.is12HourCycleInCurrentLocale();
const defaultFormat = ampm ? utils.formats.fullTime12h : utils.formats.fullTime24h;

return {
ampm,
disablePast: internalProps.disablePast ?? false,
disableFuture: internalProps.disableFuture ?? false,
format: internalProps.format ?? defaultFormat,
};
};

export const getTimeValueManager = <TEnableAccessibleFieldDOMStructure extends boolean = false>(
enableAccessibleFieldDOMStructure: TEnableAccessibleFieldDOMStructure = false as TEnableAccessibleFieldDOMStructure,
): TimeValueManager<TEnableAccessibleFieldDOMStructure> => ({
export const getTimeValueManager = <TEnableAccessibleFieldDOMStructure extends boolean = true>({
enableAccessibleFieldDOMStructure = true as TEnableAccessibleFieldDOMStructure,
}: {
enableAccessibleFieldDOMStructure: TEnableAccessibleFieldDOMStructure | undefined;
}): TimeValueManager<TEnableAccessibleFieldDOMStructure> => ({
legacyValueManager: singleItemValueManager,
fieldValueManager: singleItemFieldValueManager,
validator: validateTime,
Expand Down

0 comments on commit f06c8ca

Please sign in to comment.