Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pickers] Improve typing of the range pickers #14716

Merged
merged 2 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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