Skip to content

Commit

Permalink
[pickers] Improve typing of the range pickers (#14716)
Browse files Browse the repository at this point in the history
  • Loading branch information
flaviendelangle authored Oct 4, 2024
1 parent c0ab117 commit eb5b6fd
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,19 @@ import {
DateOrTimeViewWithMeridiem,
ExportedBaseTabsProps,
} from '@mui/x-date-pickers/internals';
import {
PickerValidDate,
FieldRef,
BaseSingleInputFieldProps,
InferError,
} from '@mui/x-date-pickers/models';
import { PickerValidDate, FieldRef, InferError } from '@mui/x-date-pickers/models';
import {
DesktopRangePickerAdditionalViewProps,
UseDesktopRangePickerParams,
UseDesktopRangePickerProps,
UseDesktopRangePickerSlotProps,
} from './useDesktopRangePicker.types';
import { useEnrichedRangePickerFieldProps } from '../useEnrichedRangePickerFieldProps';
import {
RangePickerPropsForFieldSlot,
useEnrichedRangePickerFieldProps,
} from '../useEnrichedRangePickerFieldProps';
import { getReleaseInfo } from '../../utils/releaseInfo';
import { DateRange, BaseMultiInputFieldProps, RangeFieldSection } from '../../../models';
import { DateRange, RangeFieldSection } from '../../../models';
import { useRangePosition } from '../useRangePosition';

const releaseInfo = getReleaseInfo();
Expand Down Expand Up @@ -138,24 +136,12 @@ export const useDesktopRangePicker = <
const fieldProps = useSlotProps<
typeof Field,
UseDesktopRangePickerSlotProps<TDate, TView, TEnableAccessibleFieldDOMStructure>['field'],
| Partial<
BaseSingleInputFieldProps<
DateRange<TDate>,
TDate,
RangeFieldSection,
TEnableAccessibleFieldDOMStructure,
InferError<TExternalProps>
>
>
| Partial<
BaseMultiInputFieldProps<
DateRange<TDate>,
TDate,
RangeFieldSection,
TEnableAccessibleFieldDOMStructure,
InferError<TExternalProps>
>
>,
RangePickerPropsForFieldSlot<
boolean,
TDate,
TEnableAccessibleFieldDOMStructure,
InferError<TExternalProps>
>,
TExternalProps
>({
elementType: Field,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
} from '@mui/x-date-pickers/hooks';
import { PickersInputLocaleText } from '@mui/x-date-pickers/locales';
import {
BaseFieldProps,
onSpaceOrEnter,
UsePickerResponse,
WrapperVariant,
Expand Down Expand Up @@ -83,24 +82,37 @@ export interface RangePickerFieldSlotProps<
>;
}

export type RangePickerPropsForFieldSlot<
TIsSingleInput extends boolean,
TDate extends PickerValidDate,
TEnableAccessibleFieldDOMStructure extends boolean,
TError,
> =
| (TIsSingleInput extends true
? BaseSingleInputFieldProps<
DateRange<TDate>,
TDate,
RangeFieldSection,
TEnableAccessibleFieldDOMStructure,
TError
>
: never)
| (TIsSingleInput extends false
? BaseMultiInputFieldProps<
DateRange<TDate>,
TDate,
RangeFieldSection,
TEnableAccessibleFieldDOMStructure,
TError
>
: never);

export interface UseEnrichedRangePickerFieldPropsParams<
TIsSingleInput extends boolean,
TDate extends PickerValidDate,
TView extends DateOrTimeViewWithMeridiem,
TEnableAccessibleFieldDOMStructure extends boolean,
TError,
FieldProps extends BaseFieldProps<
DateRange<TDate>,
TDate,
RangeFieldSection,
TEnableAccessibleFieldDOMStructure,
TError
> = BaseFieldProps<
DateRange<TDate>,
TDate,
RangeFieldSection,
TEnableAccessibleFieldDOMStructure,
TError
>,
> extends Pick<
UsePickerResponse<DateRange<TDate>, TView, RangeFieldSection, any>,
'open' | 'actions'
Expand All @@ -116,7 +128,12 @@ export interface UseEnrichedRangePickerFieldPropsParams<
localeText: PickersInputLocaleText<TDate> | undefined;
pickerSlotProps: RangePickerFieldSlotProps<TDate, TEnableAccessibleFieldDOMStructure> | undefined;
pickerSlots: RangePickerFieldSlots | undefined;
fieldProps: FieldProps;
fieldProps: RangePickerPropsForFieldSlot<
TIsSingleInput,
TDate,
TEnableAccessibleFieldDOMStructure,
TError
>;
anchorRef?: React.Ref<HTMLDivElement>;
currentView?: TView | null;
initialView?: TView;
Expand Down Expand Up @@ -151,17 +168,11 @@ const useMultiInputFieldSlotProps = <
startFieldRef,
endFieldRef,
}: UseEnrichedRangePickerFieldPropsParams<
false,
TDate,
TView,
TEnableAccessibleFieldDOMStructure,
TError,
BaseMultiInputFieldProps<
DateRange<TDate>,
TDate,
RangeFieldSection,
TEnableAccessibleFieldDOMStructure,
TError
>
TError
>) => {
type ReturnType = BaseMultiInputFieldProps<
DateRange<TDate>,
Expand Down Expand Up @@ -336,17 +347,11 @@ const useSingleInputFieldSlotProps = <
anchorRef,
currentView,
}: UseEnrichedRangePickerFieldPropsParams<
true,
TDate,
TView,
TEnableAccessibleFieldDOMStructure,
TError,
BaseSingleInputFieldProps<
DateRange<TDate>,
TDate,
RangeFieldSection,
TEnableAccessibleFieldDOMStructure,
TError
>
TError
>) => {
type ReturnType = BaseSingleInputFieldProps<
DateRange<TDate>,
Expand Down Expand Up @@ -453,6 +458,7 @@ export const useEnrichedRangePickerFieldProps = <
TError,
>(
params: UseEnrichedRangePickerFieldPropsParams<
boolean,
TDate,
TView,
TEnableAccessibleFieldDOMStructure,
Expand All @@ -470,9 +476,25 @@ export const useEnrichedRangePickerFieldProps = <
}

if (params.fieldType === 'multi-input') {
return useMultiInputFieldSlotProps(params);
return useMultiInputFieldSlotProps(
params as unknown as UseEnrichedRangePickerFieldPropsParams<
false,
TDate,
TView,
TEnableAccessibleFieldDOMStructure,
TError
>,
);
}

return useSingleInputFieldSlotProps(params);
return useSingleInputFieldSlotProps(
params as UseEnrichedRangePickerFieldPropsParams<
true,
TDate,
TView,
TEnableAccessibleFieldDOMStructure,
TError
>,
);
/* eslint-enable react-hooks/rules-of-hooks */
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,20 @@ import {
ExportedBaseTabsProps,
} from '@mui/x-date-pickers/internals';
import { usePickersTranslations } from '@mui/x-date-pickers/hooks';
import {
PickerValidDate,
FieldRef,
BaseSingleInputFieldProps,
InferError,
} from '@mui/x-date-pickers/models';
import { PickerValidDate, FieldRef, InferError } from '@mui/x-date-pickers/models';
import useId from '@mui/utils/useId';
import {
MobileRangePickerAdditionalViewProps,
UseMobileRangePickerParams,
UseMobileRangePickerProps,
UseMobileRangePickerSlotProps,
} from './useMobileRangePicker.types';
import { useEnrichedRangePickerFieldProps } from '../useEnrichedRangePickerFieldProps';
import {
RangePickerPropsForFieldSlot,
useEnrichedRangePickerFieldProps,
} from '../useEnrichedRangePickerFieldProps';
import { getReleaseInfo } from '../../utils/releaseInfo';
import { DateRange, BaseMultiInputFieldProps, RangeFieldSection } from '../../../models';
import { DateRange, RangeFieldSection } from '../../../models';
import { useRangePosition } from '../useRangePosition';

const releaseInfo = getReleaseInfo();
Expand Down Expand Up @@ -114,24 +112,12 @@ export const useMobileRangePicker = <
const fieldProps = useSlotProps<
typeof Field,
UseMobileRangePickerSlotProps<TDate, TView, TEnableAccessibleFieldDOMStructure>['field'],
| Partial<
BaseSingleInputFieldProps<
DateRange<TDate>,
TDate,
RangeFieldSection,
TEnableAccessibleFieldDOMStructure,
InferError<TExternalProps>
>
>
| Partial<
BaseMultiInputFieldProps<
DateRange<TDate>,
TDate,
RangeFieldSection,
TEnableAccessibleFieldDOMStructure,
InferError<TExternalProps>
>
>,
RangePickerPropsForFieldSlot<
boolean,
TDate,
TEnableAccessibleFieldDOMStructure,
InferError<TExternalProps>
>,
TExternalProps
>({
elementType: Field,
Expand Down

0 comments on commit eb5b6fd

Please sign in to comment.