import { PickerDateType } from './pickers'; /** * Makes specified keys in a type required. * * @template T - The original type. * @template K - The keys to make required. */ export type MakeRequired = Omit & Required>; /** * Validation error types applicable to both date and time validation */ type CommonDateTimeValidationError = 'invalidDate' | 'disableFuture' | 'disablePast' | null; /** * Specific validation error types for date components. * * @example * ```tsx * const handleValidationError = (error: DateValidationError) => { * switch (error) { * case 'disableFuture': * return 'Future dates are not allowed'; * case 'shouldDisableDate': * return 'This date is not available'; * case 'minDate': * return 'Date is before the minimum allowed date'; * // ... handle other cases * } * }; * ``` */ export type DateValidationError = CommonDateTimeValidationError | 'shouldDisableDate' | 'shouldDisableMonth' | 'shouldDisableYear' | 'minDate' | 'maxDate'; export type TimeValidationError = CommonDateTimeValidationError | 'minutesStep' | 'minTime' | 'maxTime' | 'shouldDisableTime-hours' | 'shouldDisableTime-minutes' | 'shouldDisableTime-seconds'; export type DateTimeValidationError = DateValidationError | TimeValidationError; /** * Base validation props for controlling past and future date restrictions. * * @example * ```tsx * // Only allow past dates * * * // Only allow future dates * * * // Allow all dates * * ``` */ interface FutureAndPastValidationProps { /** * If `true`, disable values before the current date for date components, time for time components and both for date time components. * @default false */ disablePast?: boolean; /** * If `true`, disable values after the current date for date components, time for time components and both for date time components. * @default false */ disableFuture?: boolean; } /** * Base validation props common to all date picker components. * All these props have default values when used inside a Field / Picker / Calendar. * * @example * ```tsx * * ``` */ export interface BaseDateValidationProps extends FutureAndPastValidationProps { /** * Maximal selectable date. * @default 2099-12-31 */ maxDate?: PickerDateType; /** * Minimal selectable date. * @default 1900-01-01 */ minDate?: PickerDateType; } /** * Props used to validate a date value (validates day + month + year). * * @example * ```tsx * const shouldDisableWeekends = (day: PickerDateType) => { * const dayOfWeek = day.day(); * return dayOfWeek === 0 || dayOfWeek === 6; // Sunday = 0, Saturday = 6 * }; * * * ``` */ export interface DayValidationProps { /** * Disable specific date. * * Warning: This function can be called multiple times (for example when rendering date calendar, checking if focus can be moved to a certain date, etc.). Expensive computations can impact performance. * * @param {PickerValidDate} day The date to test. * @returns {boolean} If `true` the date will be disabled. * * @example * ```tsx * // Disable weekends * const shouldDisableDate = (date) => { * const day = date.day(); * return day === 0 || day === 6; * }; * * // Disable specific dates * const blacklistedDates = ['2024-12-25', '2024-01-01']; * const shouldDisableDate = (date) => { * return blacklistedDates.includes(date.format('YYYY-MM-DD')); * }; * ``` */ shouldDisableDate?: (day: PickerDateType) => boolean; } /** * Props used to validate a month value. * Useful for disabling entire months in month/year view. * * @example * ```tsx * const shouldDisableMonth = (month: PickerDateType) => { * // Disable December (month 11, 0-indexed) * return month.month() === 11; * }; * * * ``` */ export interface MonthValidationProps { /** * Disable specific month. * @param {PickerValidDate} month The month to test. * @returns {boolean} If `true`, the month will be disabled. */ shouldDisableMonth?: (month: PickerDateType) => boolean; } /** * Props used to validate a year value. * Useful for disabling entire years in year view. * * @example * ```tsx * const shouldDisableYear = (year: PickerDateType) => { * // Disable odd years * return year.year() % 2 === 1; * }; * * * ``` */ export interface YearValidationProps { /** * Disable specific year. * @param {PickerValidDate} year The year to test. * @returns {boolean} If `true`, the year will be disabled. */ shouldDisableYear?: (year: PickerDateType) => boolean; } /** * Complete validation props used by the Date Picker, Date Field and Date Calendar components. * Combines all validation capabilities for comprehensive date validation. * * @example * ```tsx * const validationProps: ExportedValidateDateProps = { * minDate: dayjs('2020-01-01'), * maxDate: dayjs('2025-12-31'), * disablePast: true, * shouldDisableDate: (date) => date.day() === 0, // Disable Sundays * shouldDisableMonth: (month) => month.month() === 11, // Disable December * shouldDisableYear: (year) => year.year() < 2020, // Disable years before 2020 * }; * * * ``` */ export interface ExportedValidateDateProps extends DayValidationProps, MonthValidationProps, YearValidationProps, BaseDateValidationProps { } export {};