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 dc89d3c commit 01934b9
Show file tree
Hide file tree
Showing 16 changed files with 331 additions and 362 deletions.
Original file line number Diff line number Diff line change
@@ -1,40 +1,37 @@
'use client';
import * as React from 'react';
import { useField, useDefaultizedDateField } from '@mui/x-date-pickers/internals';
import { useField, useFieldInternalPropsWithDefaults } from '@mui/x-date-pickers/internals';
import { useSplitFieldProps } from '@mui/x-date-pickers/hooks';
import { UseSingleInputDateRangeFieldProps } from './SingleInputDateRangeField.types';
import { rangeValueManager, getRangeFieldValueManager } from '../internals/utils/valueManagers';
import { validateDateRange } from '../validation';
import { getDateRangeValueManager } from '../valueManagers';

export const useSingleInputDateRangeField = <
TEnableAccessibleFieldDOMStructure extends boolean,
TAllProps extends UseSingleInputDateRangeFieldProps<TEnableAccessibleFieldDOMStructure>,
>(
inProps: TAllProps,
props: TAllProps,
) => {
const props = useDefaultizedDateField<
UseSingleInputDateRangeFieldProps<TEnableAccessibleFieldDOMStructure>,
TAllProps
>(inProps);
const valueManager = React.useMemo(
() =>
getDateRangeValueManager({
enableAccessibleFieldDOMStructure: props.enableAccessibleFieldDOMStructure,
dateSeparator: props.dateSeparator,
}),
[props.enableAccessibleFieldDOMStructure, props.dateSeparator],
);

const { forwardedProps, internalProps } = useSplitFieldProps(props, 'date');
const internalPropsWithDefaults = useFieldInternalPropsWithDefaults({
valueManager,
internalProps,
});

const fieldValueManager = React.useMemo(
() => getRangeFieldValueManager({ dateSeparator: internalProps.dateSeparator }),
[internalProps.dateSeparator],
);

return useField<
true,
TEnableAccessibleFieldDOMStructure,
typeof forwardedProps,
typeof internalProps
>({
return useField({
forwardedProps,
internalProps,
valueManager: rangeValueManager,
fieldValueManager,
validator: validateDateRange,
valueType: 'date',
internalProps: internalPropsWithDefaults,
valueManager: valueManager.legacyValueManager,
fieldValueManager: valueManager.fieldValueManager,
validator: valueManager.validator,
valueType: valueManager.valueType,
});
};
Original file line number Diff line number Diff line change
@@ -1,40 +1,37 @@
'use client';
import * as React from 'react';
import { useField, useDefaultizedDateTimeField } from '@mui/x-date-pickers/internals';
import { useField, useFieldInternalPropsWithDefaults } from '@mui/x-date-pickers/internals';
import { useSplitFieldProps } from '@mui/x-date-pickers/hooks';
import { UseSingleInputDateTimeRangeFieldProps } from './SingleInputDateTimeRangeField.types';
import { rangeValueManager, getRangeFieldValueManager } from '../internals/utils/valueManagers';
import { validateDateTimeRange } from '../validation';
import { getDateTimeRangeValueManager } from '../valueManagers';

export const useSingleInputDateTimeRangeField = <
TEnableAccessibleFieldDOMStructure extends boolean,
TAllProps extends UseSingleInputDateTimeRangeFieldProps<TEnableAccessibleFieldDOMStructure>,
>(
inProps: TAllProps,
props: TAllProps,
) => {
const props = useDefaultizedDateTimeField<
UseSingleInputDateTimeRangeFieldProps<TEnableAccessibleFieldDOMStructure>,
TAllProps
>(inProps);

const { forwardedProps, internalProps } = useSplitFieldProps(props, 'date-time');

const fieldValueManager = React.useMemo(
() => getRangeFieldValueManager({ dateSeparator: internalProps.dateSeparator }),
[internalProps.dateSeparator],
const valueManager = React.useMemo(
() =>
getDateTimeRangeValueManager({
enableAccessibleFieldDOMStructure: props.enableAccessibleFieldDOMStructure,
dateSeparator: props.dateSeparator,
}),
[props.enableAccessibleFieldDOMStructure, props.dateSeparator],
);

return useField<
true,
TEnableAccessibleFieldDOMStructure,
typeof forwardedProps,
typeof internalProps
>({
forwardedProps,
const { forwardedProps, internalProps } = useSplitFieldProps(props, 'date');
const internalPropsWithDefaults = useFieldInternalPropsWithDefaults({
valueManager,
internalProps,
valueManager: rangeValueManager,
fieldValueManager,
validator: validateDateTimeRange,
valueType: 'date-time',
});

return useField({
forwardedProps,
internalProps: internalPropsWithDefaults,
valueManager: valueManager.legacyValueManager,
fieldValueManager: valueManager.fieldValueManager,
validator: valueManager.validator,
valueType: valueManager.valueType,
});
};
Original file line number Diff line number Diff line change
@@ -1,40 +1,37 @@
'use client';
import * as React from 'react';
import { useField, useDefaultizedTimeField } from '@mui/x-date-pickers/internals';
import { useField, useFieldInternalPropsWithDefaults } from '@mui/x-date-pickers/internals';
import { useSplitFieldProps } from '@mui/x-date-pickers/hooks';
import { UseSingleInputTimeRangeFieldProps } from './SingleInputTimeRangeField.types';
import { rangeValueManager, getRangeFieldValueManager } from '../internals/utils/valueManagers';
import { validateTimeRange } from '../validation';
import { getTimeRangeValueManager } from '../valueManagers';

export const useSingleInputTimeRangeField = <
TEnableAccessibleFieldDOMStructure extends boolean,
TAllProps extends UseSingleInputTimeRangeFieldProps<TEnableAccessibleFieldDOMStructure>,
>(
inProps: TAllProps,
props: TAllProps,
) => {
const props = useDefaultizedTimeField<
UseSingleInputTimeRangeFieldProps<TEnableAccessibleFieldDOMStructure>,
TAllProps
>(inProps);

const { forwardedProps, internalProps } = useSplitFieldProps(props, 'time');

const fieldValueManager = React.useMemo(
() => getRangeFieldValueManager({ dateSeparator: internalProps.dateSeparator }),
[internalProps.dateSeparator],
const valueManager = React.useMemo(
() =>
getTimeRangeValueManager({
enableAccessibleFieldDOMStructure: props.enableAccessibleFieldDOMStructure,
dateSeparator: props.dateSeparator,
}),
[props.enableAccessibleFieldDOMStructure, props.dateSeparator],
);

return useField<
true,
TEnableAccessibleFieldDOMStructure,
typeof forwardedProps,
typeof internalProps
>({
forwardedProps,
const { forwardedProps, internalProps } = useSplitFieldProps(props, 'date');
const internalPropsWithDefaults = useFieldInternalPropsWithDefaults({
valueManager,
internalProps,
valueManager: rangeValueManager,
fieldValueManager,
validator: validateTimeRange,
valueType: 'time',
});

return useField({
forwardedProps,
internalProps: internalPropsWithDefaults,
valueManager: valueManager.legacyValueManager,
fieldValueManager: valueManager.fieldValueManager,
validator: valueManager.validator,
valueType: valueManager.valueType,
});
};
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as React from 'react';
import useEventCallback from '@mui/utils/useEventCallback';
import { unstable_useDateField as useDateField } from '@mui/x-date-pickers/DateField';
import {
Expand All @@ -6,26 +7,24 @@ import {
PickerRangeValue,
UseFieldResponse,
useControlledValueWithTimezone,
useDefaultizedDateField,
useLocalizationContext,
} from '@mui/x-date-pickers/internals';
import { useValidation } from '@mui/x-date-pickers/validation';
import { DateValidationError } from '@mui/x-date-pickers/models';
import {
UseMultiInputDateRangeFieldParams,
UseMultiInputDateRangeFieldProps,
} from '../../../MultiInputDateRangeField/MultiInputDateRangeField.types';
import { UseMultiInputDateRangeFieldParams } from '../../../MultiInputDateRangeField/MultiInputDateRangeField.types';
import { validateDateRange } from '../../../validation';
import { rangeValueManager } from '../../utils/valueManagers';
import type { UseMultiInputRangeFieldResponse } from './useMultiInputRangeField.types';
import { DateRangeValidationError } from '../../../models';
import { excludeProps } from './shared';
import { useMultiInputFieldSelectedSections } from '../useMultiInputFieldSelectedSections';
import { getDateRangeValueManager } from '../../../valueManagers';

export const useMultiInputDateRangeField = <
TEnableAccessibleFieldDOMStructure extends boolean,
TTextFieldSlotProps extends {},
>({
sharedProps: inSharedProps,
sharedProps,
startTextFieldProps,
unstableStartFieldRef,
endTextFieldProps,
Expand All @@ -34,10 +33,24 @@ export const useMultiInputDateRangeField = <
TEnableAccessibleFieldDOMStructure,
TTextFieldSlotProps
>): UseMultiInputRangeFieldResponse<TEnableAccessibleFieldDOMStructure, TTextFieldSlotProps> => {
const sharedProps = useDefaultizedDateField<
UseMultiInputDateRangeFieldProps<TEnableAccessibleFieldDOMStructure>,
typeof inSharedProps
>(inSharedProps);
const valueManager = React.useMemo(
() =>
getDateRangeValueManager({
enableAccessibleFieldDOMStructure: sharedProps.enableAccessibleFieldDOMStructure,
dateSeparator: sharedProps.dateSeparator,
}),
[sharedProps.enableAccessibleFieldDOMStructure, sharedProps.dateSeparator],
);

const localizationContext = useLocalizationContext();
const sharedPropsWithDefaults = React.useMemo(
() =>
valueManager.applyDefaultsToFieldInternalProps({
...localizationContext,
internalProps: sharedProps,
}),
[sharedProps, localizationContext, valueManager],
);

const {
value: valueProp,
Expand All @@ -53,7 +66,7 @@ export const useMultiInputDateRangeField = <
timezone: timezoneProp,
enableAccessibleFieldDOMStructure,
autoFocus,
} = sharedProps;
} = sharedPropsWithDefaults;

const { value, handleValueChange, timezone } = useControlledValueWithTimezone({
name: 'useMultiInputDateRangeField',
Expand All @@ -65,11 +78,11 @@ export const useMultiInputDateRangeField = <
});

const { validationError, getValidationErrorForNewValue } = useValidation({
props: sharedProps,
props: sharedPropsWithDefaults,
value,
timezone,
validator: validateDateRange,
onError: sharedProps.onError,
onError: sharedPropsWithDefaults.onError,
});

// TODO: Maybe export utility from `useField` instead of copy/pasting the logic
Expand Down
Loading

0 comments on commit 01934b9

Please sign in to comment.