Skip to content

Commit

Permalink
[pickers] Improve validation internals (mui#15419)
Browse files Browse the repository at this point in the history
  • Loading branch information
flaviendelangle authored and LukasTy committed Dec 19, 2024
1 parent cfe2671 commit b845084
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 37 deletions.
4 changes: 2 additions & 2 deletions packages/x-date-pickers-pro/src/DateRangePicker/shared.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
useDefaultDates,
useUtils,
applyDefaultDate,
BaseDateValidationProps,
BasePickerInputProps,
PickerViewRendererLookup,
PickerRangeValue,
Expand All @@ -23,6 +22,7 @@ import {
ExportedDateRangePickerToolbarProps,
} from './DateRangePickerToolbar';
import { DateRangeViewRendererProps } from '../dateRangeViewRenderers';
import { ValidateDateRangePropsToDefault } from '../validation/validateDateRange';

export interface BaseDateRangePickerSlots extends DateRangeCalendarSlots {
/**
Expand Down Expand Up @@ -63,7 +63,7 @@ export interface BaseDateRangePickerProps
}

type UseDateRangePickerDefaultizedProps<Props extends BaseDateRangePickerProps> =
LocalizedComponent<DefaultizedProps<Props, keyof BaseDateValidationProps>>;
LocalizedComponent<DefaultizedProps<Props, ValidateDateRangePropsToDefault>>;

export function useDateRangePickerDefaultizedProps<Props extends BaseDateRangePickerProps>(
props: Props,
Expand Down
13 changes: 7 additions & 6 deletions packages/x-date-pickers-pro/src/DateTimeRangePicker/shared.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
useDefaultDates,
useUtils,
applyDefaultDate,
BaseDateValidationProps,
BasePickerInputProps,
PickerViewRendererLookup,
BaseClockProps,
Expand All @@ -15,7 +14,6 @@ import {
TimeViewWithMeridiem,
resolveTimeViewsResponse,
UseViewsOptions,
DateTimeValidationProps,
DateOrTimeViewWithMeridiem,
PickerRangeValue,
} from '@mui/x-date-pickers/internals';
Expand Down Expand Up @@ -43,6 +41,10 @@ import {
DateTimeRangePickerTabsProps,
ExportedDateTimeRangePickerTabsProps,
} from './DateTimeRangePickerTabs';
import {
ExportedValidateDateTimeRangeProps,
ValidateDateTimeRangePropsToDefault,
} from '../validation/validateDateTimeRange';

export interface BaseDateTimeRangePickerSlots
extends DateRangeCalendarSlots,
Expand Down Expand Up @@ -94,12 +96,11 @@ export interface BaseDateTimeRangePickerProps
'orientation' | 'views' | 'openTo'
>,
ExportedDateRangeCalendarProps,
BaseDateValidationProps,
ExportedValidateDateTimeRangeProps,
DesktopOnlyTimePickerProps,
Partial<
Pick<UseViewsOptions<PickerRangeValue, DateTimeRangePickerViewExternal>, 'openTo' | 'views'>
>,
DateTimeValidationProps {
> {
/**
* Overridable component slots.
* @default {}
Expand All @@ -120,7 +121,7 @@ export interface BaseDateTimeRangePickerProps

type UseDateTimeRangePickerDefaultizedProps<Props extends BaseDateTimeRangePickerProps> =
LocalizedComponent<
Omit<DefaultizedProps<Props, 'openTo' | 'ampm' | keyof BaseDateValidationProps>, 'views'>
Omit<DefaultizedProps<Props, 'openTo' | 'ampm' | ValidateDateTimeRangePropsToDefault>, 'views'>
> & {
shouldRenderTimeInASingleColumn: boolean;
views: readonly DateTimeRangePickerView[];
Expand Down
12 changes: 10 additions & 2 deletions packages/x-date-pickers-pro/src/validation/validateDateRange.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { MakeRequired } from '@mui/x-internals/types';
import { validateDate, Validator } from '@mui/x-date-pickers/validation';
import { BaseDateValidationProps, PickerRangeValue } from '@mui/x-date-pickers/internals';
import { isRangeValid } from '../internals/utils/date-utils';
Expand All @@ -12,9 +13,16 @@ export interface ExportedValidateDateRangeProps
extends DayRangeValidationProps,
BaseDateValidationProps {}

/**
* Validation props as received by the validateDateRange method.
*/
export interface ValidateDateRangeProps
extends DayRangeValidationProps,
Required<BaseDateValidationProps> {}
extends MakeRequired<ExportedValidateDateRangeProps, ValidateDateRangePropsToDefault> {}

/**
* Name of the props that should be defaulted before being passed to the validateDateRange method.
*/
export type ValidateDateRangePropsToDefault = keyof BaseDateValidationProps;

export const validateDateRange: Validator<
PickerRangeValue,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@ import { validateDateTime, Validator } from '@mui/x-date-pickers/validation';
import { isRangeValid } from '../internals/utils/date-utils';
import { DateTimeRangeValidationError } from '../models';
import { rangeValueManager } from '../internals/utils/valueManagers';
import { ExportedValidateDateRangeProps, ValidateDateRangeProps } from './validateDateRange';
import { ExportedValidateTimeRangeProps, ValidateTimeRangeProps } from './validateTimeRange';
import {
ExportedValidateDateRangeProps,
ValidateDateRangeProps,
ValidateDateRangePropsToDefault,
} from './validateDateRange';
import {
ExportedValidateTimeRangeProps,
ValidateTimeRangeProps,
ValidateTimeRangePropsToDefault,
} from './validateTimeRange';

/**
* Validation props used by the Date Time Range Picker and Date Time Range Field.
Expand All @@ -14,10 +22,17 @@ export interface ExportedValidateDateTimeRangeProps
ExportedValidateTimeRangeProps,
DateTimeValidationProps {}

/**
* Validation props as received by the validateDateTimeRange method.
*/
export interface ValidateDateTimeRangeProps
extends ValidateDateRangeProps,
ValidateTimeRangeProps {}

export type ValidateDateTimeRangePropsToDefault =
| ValidateDateRangePropsToDefault
| ValidateTimeRangePropsToDefault;

export const validateDateTimeRange: Validator<
PickerRangeValue,
DateTimeRangeValidationError,
Expand Down
12 changes: 10 additions & 2 deletions packages/x-date-pickers-pro/src/validation/validateTimeRange.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { MakeRequired } from '@mui/x-internals/types';
import { validateTime, Validator } from '@mui/x-date-pickers/validation';
import {
TimeValidationProps,
Expand All @@ -15,9 +16,16 @@ export interface ExportedValidateTimeRangeProps
extends BaseTimeValidationProps,
TimeValidationProps {}

/**
* Validation props as received by the validateTimeRange method.
*/
export interface ValidateTimeRangeProps
extends Required<BaseTimeValidationProps>,
TimeValidationProps {}
extends MakeRequired<ExportedValidateTimeRangeProps, ValidateTimeRangePropsToDefault> {}

/**
* Name of the props that should be defaulted before being passed to the validateTimeRange method.
*/
export type ValidateTimeRangePropsToDefault = keyof BaseTimeValidationProps;

export const validateTimeRange: Validator<
PickerRangeValue,
Expand Down
4 changes: 2 additions & 2 deletions packages/x-date-pickers/src/DatePicker/shared.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { applyDefaultViewProps } from '../internals/utils/views';
import { DateValidationError, DateView, PickerValidDate } from '../models';
import { BasePickerInputProps } from '../internals/models/props/basePickerProps';
import { applyDefaultDate } from '../internals/utils/date-utils';
import { BaseDateValidationProps } from '../internals/models/validation';
import { LocalizedComponent, PickersInputLocaleText } from '../locales/utils/pickersLocaleTextApi';
import {
DatePickerToolbar,
Expand All @@ -20,6 +19,7 @@ import {
} from './DatePickerToolbar';
import { PickerViewRendererLookup } from '../internals/hooks/usePicker/usePickerViews';
import { DateViewRendererProps } from '../dateViewRenderers';
import { ValidateDatePropsToDefault } from '../validation/validateDate';

export interface BaseDatePickerSlots extends DateCalendarSlots {
/**
Expand Down Expand Up @@ -65,7 +65,7 @@ export interface BaseDatePickerProps
}

type UseDatePickerDefaultizedProps<Props extends BaseDatePickerProps> = LocalizedComponent<
DefaultizedProps<Props, 'views' | 'openTo' | keyof BaseDateValidationProps>
DefaultizedProps<Props, 'views' | 'openTo' | ValidateDatePropsToDefault>
>;

export function useDatePickerDefaultizedProps<Props extends BaseDatePickerProps>(
Expand Down
18 changes: 6 additions & 12 deletions packages/x-date-pickers/src/DateTimePicker/shared.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ import {
DateTimePickerTabsProps,
ExportedDateTimePickerTabsProps,
} from './DateTimePickerTabs';
import {
BaseDateValidationProps,
BaseTimeValidationProps,
DateTimeValidationProps,
} from '../internals/models/validation';
import { LocalizedComponent, PickersInputLocaleText } from '../locales/utils/pickersLocaleTextApi';
import {
DateTimePickerToolbar,
Expand All @@ -33,6 +28,10 @@ import { TimeViewRendererProps } from '../timeViewRenderers';
import { applyDefaultViewProps } from '../internals/utils/views';
import { BaseClockProps, ExportedBaseClockProps } from '../internals/models/props/time';
import { DateOrTimeViewWithMeridiem, TimeViewWithMeridiem } from '../internals/models';
import {
ExportedValidateDateTimeProps,
ValidateDateTimePropsToDefault,
} from '../validation/validateDateTime';

export interface BaseDateTimePickerSlots extends DateCalendarSlots, TimeClockSlots {
/**
Expand Down Expand Up @@ -76,7 +75,7 @@ export interface BaseDateTimePickerProps<TView extends DateOrTimeViewWithMeridie
extends BasePickerInputProps<PickerValidDate | null, TView, DateTimeValidationError>,
Omit<ExportedDateCalendarProps, 'onViewChange'>,
ExportedBaseClockProps,
DateTimeValidationProps {
ExportedValidateDateTimeProps {
/**
* Display ampm controls under the clock (instead of in the toolbar).
* @default true on desktop, false on mobile
Expand Down Expand Up @@ -106,12 +105,7 @@ type UseDateTimePickerDefaultizedProps<
> = LocalizedComponent<
DefaultizedProps<
Props,
| 'views'
| 'openTo'
| 'orientation'
| 'ampm'
| keyof BaseDateValidationProps
| keyof BaseTimeValidationProps
'views' | 'openTo' | 'orientation' | 'ampm' | ValidateDateTimePropsToDefault
>
>;

Expand Down
4 changes: 2 additions & 2 deletions packages/x-date-pickers/src/TimePicker/shared.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { DefaultizedProps } from '@mui/x-internals/types';
import { useUtils } from '../internals/hooks/useUtils';
import { TimeClockSlots, TimeClockSlotProps } from '../TimeClock/TimeClock.types';
import { BasePickerInputProps } from '../internals/models/props/basePickerProps';
import { BaseTimeValidationProps } from '../internals/models/validation';
import { LocalizedComponent, PickersInputLocaleText } from '../locales/utils/pickersLocaleTextApi';
import {
TimePickerToolbarProps,
Expand All @@ -17,6 +16,7 @@ import { TimeViewRendererProps } from '../timeViewRenderers';
import { applyDefaultViewProps } from '../internals/utils/views';
import { BaseClockProps, ExportedBaseClockProps } from '../internals/models/props/time';
import { TimeViewWithMeridiem } from '../internals/models';
import { ValidateTimePropsToDefault } from '../validation/validateTime';

export interface BaseTimePickerSlots extends TimeClockSlots {
/**
Expand Down Expand Up @@ -70,7 +70,7 @@ type UseTimePickerDefaultizedProps<
TView extends TimeViewWithMeridiem,
Props extends BaseTimePickerProps<TView>,
> = LocalizedComponent<
DefaultizedProps<Props, 'views' | 'openTo' | 'ampm' | keyof BaseTimeValidationProps>
DefaultizedProps<Props, 'views' | 'openTo' | 'ampm' | ValidateTimePropsToDefault>
>;

export function useTimePickerDefaultizedProps<
Expand Down
14 changes: 10 additions & 4 deletions packages/x-date-pickers/src/validation/validateDate.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { MakeRequired } from '@mui/x-internals/types';
import { Validator } from './useValidation';
import {
BaseDateValidationProps,
Expand All @@ -18,11 +19,16 @@ export interface ExportedValidateDateProps
YearValidationProps,
BaseDateValidationProps {}

/**
* Validation props as received by the validateDate method.
*/
export interface ValidateDateProps
extends DayValidationProps,
MonthValidationProps,
YearValidationProps,
Required<BaseDateValidationProps> {}
extends MakeRequired<ExportedValidateDateProps, ValidateDatePropsToDefault> {}

/**
* Name of the props that should be defaulted before being passed to the validateDate method.
*/
export type ValidateDatePropsToDefault = keyof BaseDateValidationProps;

export const validateDate: Validator<
PickerValidDate | null,
Expand Down
24 changes: 22 additions & 2 deletions packages/x-date-pickers/src/validation/validateDateTime.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { Validator } from './useValidation';
import { ExportedValidateDateProps, validateDate, ValidateDateProps } from './validateDate';
import { ExportedValidateTimeProps, validateTime, ValidateTimeProps } from './validateTime';
import {
ExportedValidateDateProps,
validateDate,
ValidateDateProps,
ValidateDatePropsToDefault,
} from './validateDate';
import {
ExportedValidateTimeProps,
validateTime,
ValidateTimeProps,
ValidateTimePropsToDefault,
} from './validateTime';
import { DateTimeValidationError, PickerValidDate } from '../models';
import { singleItemValueManager } from '../internals/utils/valueManagers';
import { DateTimeValidationProps } from '../internals/models/validation';
Expand All @@ -13,8 +23,18 @@ export interface ExportedValidateDateTimeProps
ExportedValidateTimeProps,
DateTimeValidationProps {}

/**
* Validation props as received by the validateDateTime method.
*/
export interface ValidateDateTimeProps extends ValidateDateProps, ValidateTimeProps {}

/**
* Name of the props that should be defaulted before being passed to the validateDateTime method.
*/
export type ValidateDateTimePropsToDefault =
| ValidateDatePropsToDefault
| ValidateTimePropsToDefault;

export const validateDateTime: Validator<
PickerValidDate | null,
DateTimeValidationError,
Expand Down
12 changes: 11 additions & 1 deletion packages/x-date-pickers/src/validation/validateTime.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { MakeRequired } from '@mui/x-internals/types';
import { createIsAfterIgnoreDatePart } from '../internals/utils/time-utils';
import { Validator } from './useValidation';
import { BaseTimeValidationProps, TimeValidationProps } from '../internals/models/validation';
Expand All @@ -9,7 +10,16 @@ import { singleItemValueManager } from '../internals/utils/valueManagers';
*/
export interface ExportedValidateTimeProps extends BaseTimeValidationProps, TimeValidationProps {}

export interface ValidateTimeProps extends Required<BaseTimeValidationProps>, TimeValidationProps {}
/**
* Validation props as received by the validateTime method.
*/
export interface ValidateTimeProps
extends MakeRequired<ExportedValidateTimeProps, ValidateTimePropsToDefault> {}

/**
* Name of the props that should be defaulted before being passed to the validateTime method.
*/
export type ValidateTimePropsToDefault = keyof BaseTimeValidationProps;

export const validateTime: Validator<
PickerValidDate | null,
Expand Down

0 comments on commit b845084

Please sign in to comment.