import { forwardRef, type FocusEvent, type ReactElement, ReactNode, Ref, RefAttributes, } from 'react' import { Control, FieldError, FieldPath, FieldValues, PathValue, useController, UseControllerProps, } from 'react-hook-form' import {useForkRef} from '@mui/material' import { DateValidationError, extractValidationProps, MobileDatePicker, MobileDatePickerProps, MobileDatePickerSlotProps, PickerChangeHandlerContext, validateDate, } from '@mui/x-date-pickers' import type {ValidateDateProps} from '@mui/x-date-pickers/validation' import {usePickerAdapter} from '@mui/x-date-pickers/hooks' import {PickerValidDate} from '@mui/x-date-pickers/models' import type {PickersTextFieldProps} from '@mui/x-date-pickers/PickersTextField' import {useFormError} from './FormErrorProvider' import {defaultErrorMessages} from './messages/DatePicker' import {useTransform} from './useTransform' import {getTimezone, readValueAsDate} from './utils' export type MobileDatePickerElementProps< TFieldValues extends FieldValues = FieldValues, TName extends FieldPath = FieldPath, TValue extends PickerValidDate = PickerValidDate, > = Omit & { name: TName required?: boolean isDate?: boolean parseError?: (error: FieldError) => ReactNode rules?: UseControllerProps['rules'] control?: Control inputProps?: Partial helperText?: PickersTextFieldProps['helperText'] slotProps?: Omit overwriteErrorMessages?: typeof defaultErrorMessages transform?: { input?: (value: PathValue) => TValue | null output?: ( value: TValue | null, context: PickerChangeHandlerContext ) => PathValue } } type MobileDatePickerElementComponent = < TFieldValues extends FieldValues = FieldValues, TName extends FieldPath = FieldPath, TValue extends PickerValidDate = PickerValidDate, >( props: MobileDatePickerElementProps & RefAttributes ) => ReactElement const MobileDatePickerElement = forwardRef(function MobileDatePickerElement< TFieldValues extends FieldValues = FieldValues, TName extends FieldPath = FieldPath, TValue extends PickerValidDate = PickerValidDate, >( props: MobileDatePickerElementProps, ref: Ref ) { const { parseError, name, required, rules = {}, inputProps, control, slotProps, overwriteErrorMessages, inputRef, transform, ...rest } = props const adapter = usePickerAdapter() const validationProps = extractValidationProps(rest) const errorMsgFn = useFormError() const customErrorFn = parseError || errorMsgFn const errorMessages = { ...defaultErrorMessages, ...overwriteErrorMessages, } const rulesTmp = { ...rules, ...(required && !rules.required && { required: 'This field is required', }), validate: { internal: (value: TValue | null) => { const date = readValueAsDate(adapter, value) if (!date) { return true } const internalError = validateDate({ props: { disableFuture: false, disablePast: false, ...validationProps, } as ValidateDateProps, timezone: rest.timezone ?? getTimezone(adapter, date) ?? 'default', value: date, adapter: adapter, }) return internalError == null || errorMessages[internalError] }, ...rules.validate, }, } const { field, fieldState: {error}, } = useController({ name, control, rules: rulesTmp, disabled: rest.disabled, defaultValue: null as PathValue, }) const {value, onChange} = useTransform({ value: field.value, onChange: field.onChange, transform: { input: typeof transform?.input === 'function' ? transform.input : (newValue) => readValueAsDate(adapter, newValue), output: typeof transform?.output === 'function' ? transform.output : (newValue) => newValue, }, }) const handleInputRef = useForkRef(field.ref, inputRef) const htmlInputFromUser = inputProps?.slotProps?.htmlInput const priorHtmlInputRef = htmlInputFromUser && typeof htmlInputFromUser === 'object' && 'ref' in htmlInputFromUser ? (htmlInputFromUser as {ref?: React.Ref}).ref : undefined const mergedHtmlInputRef = useForkRef(handleInputRef, priorHtmlInputRef) const textFieldSlot: Partial = { ...inputProps, required, error: !!error, helperText: error ? typeof customErrorFn === 'function' ? customErrorFn(error) : error.message : inputProps?.helperText ?? rest.helperText, onBlur: (event: FocusEvent) => { field.onBlur() inputProps?.onBlur?.(event) }, slotProps: { ...inputProps?.slotProps, htmlInput: { ...(typeof htmlInputFromUser === 'object' && htmlInputFromUser !== null ? htmlInputFromUser : {}), ref: mergedHtmlInputRef, }, }, } return ( { field.onBlur() if (rest.onClose) { rest.onClose(...args) } }} onChange={(newValue, context) => { onChange(newValue, context) if (typeof rest.onChange === 'function') { rest.onChange(newValue, context) } }} slotProps={{ ...slotProps, textField: textFieldSlot, }} /> ) }) MobileDatePickerElement.displayName = 'MobileDatePickerElement' export default MobileDatePickerElement as MobileDatePickerElementComponent