From 6dd284ac55a338883bbbef744795d29c159e6417 Mon Sep 17 00:00:00 2001 From: flavien Date: Wed, 30 Oct 2024 17:59:41 +0100 Subject: [PATCH] [pickers] Clean definition of validation props --- .../DateRangeCalendar.types.ts | 5 ++-- .../src/DateRangePicker/shared.tsx | 3 +-- .../src/internals/models/dateTimeRange.ts | 16 ++---------- .../src/internals/models/timeRange.ts | 17 +++--------- .../src/models/dateRange.ts | 11 +++----- .../src/validation/validateDateRange.ts | 7 +++++ .../src/validation/validateDateTimeRange.ts | 16 ++++++++---- .../src/validation/validateTimeRange.ts | 22 +++++++++++++--- .../src/DateCalendar/DateCalendar.types.ts | 13 +++------- .../src/DateField/DateField.types.ts | 12 ++------- .../src/DateTimeField/DateTimeField.types.ts | 26 +++---------------- .../src/TimeField/TimeField.types.ts | 13 +++------- .../src/internals/models/props/clock.ts | 13 +++------- .../src/validation/validateDate.ts | 9 +++++++ .../src/validation/validateDateTime.ts | 13 ++++++++-- .../src/validation/validateTime.ts | 13 ++++++++++ 16 files changed, 95 insertions(+), 114 deletions(-) diff --git a/packages/x-date-pickers-pro/src/DateRangeCalendar/DateRangeCalendar.types.ts b/packages/x-date-pickers-pro/src/DateRangeCalendar/DateRangeCalendar.types.ts index ac56c4d44149..da1c6f546c40 100644 --- a/packages/x-date-pickers-pro/src/DateRangeCalendar/DateRangeCalendar.types.ts +++ b/packages/x-date-pickers-pro/src/DateRangeCalendar/DateRangeCalendar.types.ts @@ -19,12 +19,12 @@ import { DayCalendarProps, ExportedUseViewsOptions, } from '@mui/x-date-pickers/internals'; -import { DayRangeValidationProps } from '../internals/models/dateRange'; import { DateRange, RangePosition } from '../models'; import { DateRangeCalendarClasses } from './dateRangeCalendarClasses'; import { DateRangePickerDay, DateRangePickerDayProps } from '../DateRangePickerDay'; import { UseRangePositionProps } from '../internals/hooks/useRangePosition'; import { PickersRangeCalendarHeaderProps } from '../PickersRangeCalendarHeader'; +import { ExportedValidateDateRangeProps } from '../validation/validateDateRange'; export interface DateRangeCalendarSlots extends PickersArrowSwitcherSlots, @@ -62,8 +62,7 @@ export interface DateRangeCalendarSlotProps export interface ExportedDateRangeCalendarProps extends ExportedDayCalendarProps, - BaseDateValidationProps, - DayRangeValidationProps, + ExportedValidateDateRangeProps, TimezoneProps { /** * If `true`, after selecting `start` date calendar will not automatically switch to the month of `end` date. diff --git a/packages/x-date-pickers-pro/src/DateRangePicker/shared.tsx b/packages/x-date-pickers-pro/src/DateRangePicker/shared.tsx index 1099eb02994e..da7f89407687 100644 --- a/packages/x-date-pickers-pro/src/DateRangePicker/shared.tsx +++ b/packages/x-date-pickers-pro/src/DateRangePicker/shared.tsx @@ -43,8 +43,7 @@ export interface BaseDateRangePickerProps BasePickerInputProps, TDate, 'day', DateRangeValidationError>, 'view' | 'views' | 'openTo' | 'onViewChange' | 'orientation' >, - ExportedDateRangeCalendarProps, - BaseDateValidationProps { + ExportedDateRangeCalendarProps { /** * Overridable component slots. * @default {} diff --git a/packages/x-date-pickers-pro/src/internals/models/dateTimeRange.ts b/packages/x-date-pickers-pro/src/internals/models/dateTimeRange.ts index ab37cab3b08d..9ebe0da55b50 100644 --- a/packages/x-date-pickers-pro/src/internals/models/dateTimeRange.ts +++ b/packages/x-date-pickers-pro/src/internals/models/dateTimeRange.ts @@ -1,19 +1,16 @@ import { - BaseDateValidationProps, - TimeValidationProps, MakeOptional, UseFieldInternalProps, - DateTimeValidationProps, DateOrTimeViewWithMeridiem, } from '@mui/x-date-pickers/internals'; import { PickerValidDate } from '@mui/x-date-pickers/models'; -import { DayRangeValidationProps } from './dateRange'; import { DateTimeRangeValidationError, RangeFieldSection, DateRange, RangeFieldSeparatorProps, } from '../../models'; +import { ExportedValidateDateTimeRangeProps } from '../../validation/validateDateTimeRange'; export interface UseDateTimeRangeFieldProps< TDate extends PickerValidDate, @@ -32,16 +29,7 @@ export interface UseDateTimeRangeFieldProps< 'format' >, RangeFieldSeparatorProps, - DayRangeValidationProps, - TimeValidationProps, - BaseDateValidationProps, - DateTimeValidationProps { - /** - * 12h/24h view for hour selection clock. - * @default utils.is12HourCycleInCurrentLocale() - */ - ampm?: boolean; -} + ExportedValidateDateTimeRangeProps {} export type DateTimeRangePickerView = Exclude; diff --git a/packages/x-date-pickers-pro/src/internals/models/timeRange.ts b/packages/x-date-pickers-pro/src/internals/models/timeRange.ts index 010182c83114..60b68735be3a 100644 --- a/packages/x-date-pickers-pro/src/internals/models/timeRange.ts +++ b/packages/x-date-pickers-pro/src/internals/models/timeRange.ts @@ -1,9 +1,4 @@ -import { - BaseTimeValidationProps, - TimeValidationProps, - MakeOptional, - UseFieldInternalProps, -} from '@mui/x-date-pickers/internals'; +import { MakeOptional, UseFieldInternalProps } from '@mui/x-date-pickers/internals'; import { PickerValidDate } from '@mui/x-date-pickers/models'; import { TimeRangeValidationError, @@ -11,6 +6,7 @@ import { DateRange, RangeFieldSeparatorProps, } from '../../models'; +import type { ExportedValidateTimeRangeProps } from '../../validation/validateTimeRange'; export interface UseTimeRangeFieldProps< TDate extends PickerValidDate, @@ -29,11 +25,4 @@ export interface UseTimeRangeFieldProps< 'format' >, RangeFieldSeparatorProps, - TimeValidationProps, - BaseTimeValidationProps { - /** - * 12h/24h view for hour selection clock. - * @default utils.is12HourCycleInCurrentLocale() - */ - ampm?: boolean; -} + ExportedValidateTimeRangeProps {} diff --git a/packages/x-date-pickers-pro/src/models/dateRange.ts b/packages/x-date-pickers-pro/src/models/dateRange.ts index d110c1cb3b40..852145fd0fb4 100644 --- a/packages/x-date-pickers-pro/src/models/dateRange.ts +++ b/packages/x-date-pickers-pro/src/models/dateRange.ts @@ -1,13 +1,9 @@ -import { - BaseDateValidationProps, - MakeOptional, - UseFieldInternalProps, -} from '@mui/x-date-pickers/internals'; +import { MakeOptional, UseFieldInternalProps } from '@mui/x-date-pickers/internals'; import { PickerValidDate } from '@mui/x-date-pickers/models'; import { RangeFieldSection, RangeFieldSeparatorProps } from './fields'; import { DateRangeValidationError } from './validation'; import { DateRange } from './range'; -import { DayRangeValidationProps } from '../internals/models/dateRange'; +import type { ExportedValidateDateRangeProps } from '../validation/validateDateRange'; export interface UseDateRangeFieldProps< TDate extends PickerValidDate, @@ -26,5 +22,4 @@ export interface UseDateRangeFieldProps< 'format' >, RangeFieldSeparatorProps, - DayRangeValidationProps, - BaseDateValidationProps {} + ExportedValidateDateRangeProps {} diff --git a/packages/x-date-pickers-pro/src/validation/validateDateRange.ts b/packages/x-date-pickers-pro/src/validation/validateDateRange.ts index 125449ca9a6f..bf05511c9a38 100644 --- a/packages/x-date-pickers-pro/src/validation/validateDateRange.ts +++ b/packages/x-date-pickers-pro/src/validation/validateDateRange.ts @@ -6,6 +6,13 @@ import { DayRangeValidationProps } from '../internals/models/dateRange'; import { DateRangeValidationError, DateRange } from '../models'; import { rangeValueManager } from '../internals/utils/valueManagers'; +/** + * Validation props used by the Date Range Picker, Date Range Field and Date Range Calendar components. + */ +export interface ExportedValidateDateRangeProps + extends DayRangeValidationProps, + BaseDateValidationProps {} + export interface ValidateDateRangeProps extends DayRangeValidationProps, Required> {} diff --git a/packages/x-date-pickers-pro/src/validation/validateDateTimeRange.ts b/packages/x-date-pickers-pro/src/validation/validateDateTimeRange.ts index d86d65e8d3b3..e4f489c65ffa 100644 --- a/packages/x-date-pickers-pro/src/validation/validateDateTimeRange.ts +++ b/packages/x-date-pickers-pro/src/validation/validateDateTimeRange.ts @@ -1,15 +1,21 @@ import { PickerValidDate } from '@mui/x-date-pickers/models'; import { validateDateTime, Validator } from '@mui/x-date-pickers/validation'; -import { BaseDateValidationProps, TimeValidationProps } from '@mui/x-date-pickers/internals'; import { isRangeValid } from '../internals/utils/date-utils'; -import { DayRangeValidationProps } from '../internals/models/dateRange'; import { DateTimeRangeValidationError, DateRange } from '../models'; import { rangeValueManager } from '../internals/utils/valueManagers'; +import { ExportedValidateDateRangeProps, ValidateDateRangeProps } from './validateDateRange'; +import { ExportedValidateTimeRangeProps, ValidateTimeRangeProps } from './validateTimeRange'; + +/** + * Validation props used by the Date Time Range Picker and Date Time Range Field. + */ +export interface ExportedValidateDateTimeRangeProps + extends ExportedValidateDateRangeProps, + ExportedValidateTimeRangeProps {} export interface ValidateDateTimeRangeProps - extends DayRangeValidationProps, - TimeValidationProps, - Required> {} + extends ValidateDateRangeProps, + ValidateTimeRangeProps {} export const validateDateTimeRange: Validator< DateRange, diff --git a/packages/x-date-pickers-pro/src/validation/validateTimeRange.ts b/packages/x-date-pickers-pro/src/validation/validateTimeRange.ts index 483523a4b499..f5f4eb7cfef3 100644 --- a/packages/x-date-pickers-pro/src/validation/validateTimeRange.ts +++ b/packages/x-date-pickers-pro/src/validation/validateTimeRange.ts @@ -1,16 +1,32 @@ import { validateTime, Validator } from '@mui/x-date-pickers/validation'; -import { BaseTimeValidationProps } from '@mui/x-date-pickers/internals'; +import { PickerValidDate } from '@mui/x-date-pickers/models'; +import { TimeValidationProps, BaseTimeValidationProps } from '@mui/x-date-pickers/internals'; import { isRangeValid } from '../internals/utils/date-utils'; import { TimeRangeValidationError, DateRange } from '../models'; import { rangeValueManager } from '../internals/utils/valueManagers'; -export interface ValidateTimeRangeProps extends Required {} +/** + * Validation props used by the Time Range Picker and Time Range Field. + */ +export interface ExportedValidateTimeRangeProps + extends BaseTimeValidationProps, + TimeValidationProps { + /** + * 12h/24h view for hour selection clock. + * @default utils.is12HourCycleInCurrentLocale() + */ + ampm?: boolean; +} + +export interface ValidateTimeRangeProps + extends Required, + TimeValidationProps {} export const validateTimeRange: Validator< DateRange, any, TimeRangeValidationError, - ValidateTimeRangeProps + ValidateTimeRangeProps > = ({ adapter, value, timezone, props }) => { const [start, end] = value; diff --git a/packages/x-date-pickers/src/DateCalendar/DateCalendar.types.ts b/packages/x-date-pickers/src/DateCalendar/DateCalendar.types.ts index cc378565575d..0d20aec39d8d 100644 --- a/packages/x-date-pickers/src/DateCalendar/DateCalendar.types.ts +++ b/packages/x-date-pickers/src/DateCalendar/DateCalendar.types.ts @@ -10,12 +10,7 @@ import { } from '../PickersCalendarHeader'; import { DayCalendarSlots, DayCalendarSlotProps, ExportedDayCalendarProps } from './DayCalendar'; import { DateCalendarClasses } from './dateCalendarClasses'; -import { - BaseDateValidationProps, - YearValidationProps, - MonthValidationProps, - DayValidationProps, -} from '../internals/models/validation'; +import { BaseDateValidationProps } from '../internals/models/validation'; import { ExportedUseViewsOptions } from '../internals/hooks/useViews'; import { DateView, PickerValidDate, TimezoneProps } from '../models'; import { DefaultizedProps } from '../internals/models/helpers'; @@ -29,6 +24,7 @@ import { MonthCalendarSlots, MonthCalendarSlotProps, } from '../MonthCalendar/MonthCalendar.types'; +import { ExportedValidateDateProps } from '../validation/validateDate'; export interface DateCalendarSlots extends PickersCalendarHeaderSlots, @@ -55,10 +51,7 @@ export interface ExportedDateCalendarProps extends ExportedDayCalendarProps, ExportedMonthCalendarProps, ExportedYearCalendarProps, - BaseDateValidationProps, - DayValidationProps, - YearValidationProps, - MonthValidationProps, + ExportedValidateDateProps, TimezoneProps { /** * If `true`, the picker and text field are disabled. diff --git a/packages/x-date-pickers/src/DateField/DateField.types.ts b/packages/x-date-pickers/src/DateField/DateField.types.ts index 2b6feddbc943..610889b52b13 100644 --- a/packages/x-date-pickers/src/DateField/DateField.types.ts +++ b/packages/x-date-pickers/src/DateField/DateField.types.ts @@ -14,12 +14,7 @@ import { } from '../models'; import { UseFieldInternalProps } from '../internals/hooks/useField'; import { MakeOptional } from '../internals/models/helpers'; -import { - BaseDateValidationProps, - DayValidationProps, - MonthValidationProps, - YearValidationProps, -} from '../internals/models/validation'; +import { ExportedValidateDateProps } from '../validation/validateDate'; export interface UseDateFieldProps< TDate extends PickerValidDate, @@ -34,10 +29,7 @@ export interface UseDateFieldProps< >, 'format' >, - DayValidationProps, - MonthValidationProps, - YearValidationProps, - BaseDateValidationProps, + ExportedValidateDateProps, ExportedUseClearableFieldProps {} export type DateFieldProps< diff --git a/packages/x-date-pickers/src/DateTimeField/DateTimeField.types.ts b/packages/x-date-pickers/src/DateTimeField/DateTimeField.types.ts index f603981c1c95..0aa19fc77477 100644 --- a/packages/x-date-pickers/src/DateTimeField/DateTimeField.types.ts +++ b/packages/x-date-pickers/src/DateTimeField/DateTimeField.types.ts @@ -9,20 +9,12 @@ import { } from '../models'; import { UseFieldInternalProps } from '../internals/hooks/useField'; import { MakeOptional } from '../internals/models/helpers'; -import { - BaseDateValidationProps, - BaseTimeValidationProps, - DateTimeValidationProps, - DayValidationProps, - MonthValidationProps, - TimeValidationProps, - YearValidationProps, -} from '../internals/models/validation'; import { ExportedUseClearableFieldProps, UseClearableFieldSlots, UseClearableFieldSlotProps, } from '../hooks/useClearableField'; +import { ExportedValidateDateTimeProps } from '../validation/validateDateTime'; export interface UseDateTimeFieldProps< TDate extends PickerValidDate, @@ -37,20 +29,8 @@ export interface UseDateTimeFieldProps< >, 'format' >, - DayValidationProps, - MonthValidationProps, - YearValidationProps, - BaseDateValidationProps, - TimeValidationProps, - BaseTimeValidationProps, - DateTimeValidationProps, - ExportedUseClearableFieldProps { - /** - * 12h/24h view for hour selection clock. - * @default utils.is12HourCycleInCurrentLocale() - */ - ampm?: boolean; -} + ExportedValidateDateTimeProps, + ExportedUseClearableFieldProps {} export type DateTimeFieldProps< TDate extends PickerValidDate, diff --git a/packages/x-date-pickers/src/TimeField/TimeField.types.ts b/packages/x-date-pickers/src/TimeField/TimeField.types.ts index 03d2c63932c3..8f8b5c96b9b4 100644 --- a/packages/x-date-pickers/src/TimeField/TimeField.types.ts +++ b/packages/x-date-pickers/src/TimeField/TimeField.types.ts @@ -3,7 +3,6 @@ import { SlotComponentProps } from '@mui/utils'; import TextField from '@mui/material/TextField'; import { UseFieldInternalProps } from '../internals/hooks/useField'; import { MakeOptional } from '../internals/models/helpers'; -import { BaseTimeValidationProps, TimeValidationProps } from '../internals/models/validation'; import { FieldSection, PickerValidDate, @@ -15,6 +14,7 @@ import { UseClearableFieldSlots, UseClearableFieldSlotProps, } from '../hooks/useClearableField'; +import { ExportedValidateTimeProps } from '../validation/validateTime'; export interface UseTimeFieldProps< TDate extends PickerValidDate, @@ -29,15 +29,8 @@ export interface UseTimeFieldProps< >, 'format' >, - TimeValidationProps, - BaseTimeValidationProps, - ExportedUseClearableFieldProps { - /** - * 12h/24h view for hour selection clock. - * @default utils.is12HourCycleInCurrentLocale() - */ - ampm?: boolean; -} + ExportedValidateTimeProps, + ExportedUseClearableFieldProps {} export type TimeFieldProps< TDate extends PickerValidDate, diff --git a/packages/x-date-pickers/src/internals/models/props/clock.ts b/packages/x-date-pickers/src/internals/models/props/clock.ts index 41ac81136ae4..c51b8cf0aea5 100644 --- a/packages/x-date-pickers/src/internals/models/props/clock.ts +++ b/packages/x-date-pickers/src/internals/models/props/clock.ts @@ -1,21 +1,14 @@ import { SxProps, Theme } from '@mui/material/styles'; -import { BaseTimeValidationProps, TimeValidationProps } from '../validation'; import { PickerValidDate, TimeStepOptions, TimezoneProps } from '../../../models'; import type { ExportedDigitalClockProps } from '../../../DigitalClock/DigitalClock.types'; import type { ExportedMultiSectionDigitalClockProps } from '../../../MultiSectionDigitalClock/MultiSectionDigitalClock.types'; import type { ExportedUseViewsOptions } from '../../hooks/useViews'; import { TimeViewWithMeridiem } from '../common'; +import { ExportedValidateTimeProps } from '../../../validation/validateTime'; export interface ExportedBaseClockProps - extends TimeValidationProps, - BaseTimeValidationProps, - TimezoneProps { - /** - * 12h/24h view for hour selection clock. - * @default utils.is12HourCycleInCurrentLocale() - */ - ampm?: boolean; -} + extends ExportedValidateTimeProps, + TimezoneProps {} export interface BaseClockProps extends ExportedUseViewsOptions, diff --git a/packages/x-date-pickers/src/validation/validateDate.ts b/packages/x-date-pickers/src/validation/validateDate.ts index e3bf707969aa..a47967e44d6f 100644 --- a/packages/x-date-pickers/src/validation/validateDate.ts +++ b/packages/x-date-pickers/src/validation/validateDate.ts @@ -9,6 +9,15 @@ import { DateValidationError, PickerValidDate } from '../models'; import { applyDefaultDate } from '../internals/utils/date-utils'; import { singleItemValueManager } from '../internals/utils/valueManagers'; +/** + * Validation props used by the Date Picker, Date Field and Date Calendar components. + */ +export interface ExportedValidateDateProps + extends DayValidationProps, + MonthValidationProps, + YearValidationProps, + BaseDateValidationProps {} + export interface ValidateDateProps extends DayValidationProps, MonthValidationProps, diff --git a/packages/x-date-pickers/src/validation/validateDateTime.ts b/packages/x-date-pickers/src/validation/validateDateTime.ts index 1e4824635532..fa0968313b3c 100644 --- a/packages/x-date-pickers/src/validation/validateDateTime.ts +++ b/packages/x-date-pickers/src/validation/validateDateTime.ts @@ -1,8 +1,17 @@ import { Validator } from './useValidation'; -import { validateDate, ValidateDateProps } from './validateDate'; -import { validateTime, ValidateTimeProps } from './validateTime'; +import { ExportedValidateDateProps, validateDate, ValidateDateProps } from './validateDate'; +import { ExportedValidateTimeProps, validateTime, ValidateTimeProps } from './validateTime'; import { DateTimeValidationError, PickerValidDate } from '../models'; import { singleItemValueManager } from '../internals/utils/valueManagers'; +import { DateTimeValidationProps } from '../internals/models/validation'; + +/** + * Validation props used by the Date Time Picker and Date Time Field components. + */ +export interface ExportedValidateDateTimeProps + extends ExportedValidateDateProps, + ExportedValidateTimeProps, + DateTimeValidationProps {} export interface ValidateDateTimeProps extends ValidateDateProps, diff --git a/packages/x-date-pickers/src/validation/validateTime.ts b/packages/x-date-pickers/src/validation/validateTime.ts index 2124913a3c14..9ed5f6fc7e7e 100644 --- a/packages/x-date-pickers/src/validation/validateTime.ts +++ b/packages/x-date-pickers/src/validation/validateTime.ts @@ -4,6 +4,19 @@ import { BaseTimeValidationProps, TimeValidationProps } from '../internals/model import { PickerValidDate, TimeValidationError } from '../models'; import { singleItemValueManager } from '../internals/utils/valueManagers'; +/** + * Validation props used by the Time Picker, Time Field and Clock components. + */ +export interface ExportedValidateTimeProps + extends BaseTimeValidationProps, + TimeValidationProps { + /** + * 12h/24h view for hour selection clock. + * @default utils.is12HourCycleInCurrentLocale() + */ + ampm?: boolean; +} + export interface ValidateTimeProps extends Required, TimeValidationProps {}