import { ForwardedRef, forwardRef, MouseEvent, useRef, createContext, useContext, } from 'react'; import { RtData, RtFormField, RtFormFieldSizeUI, RtFormFieldTextProps, } from '@dpa/react-form-field-text'; import { RtBaseProps } from '@dpa/react-hoc'; import { RtText } from '@dpa/react-text'; import styles from './styles/rt-form-field-date-picker.module.css'; import classNames from 'classnames/bind'; import { CalendarTypographyIcon } from '@dpa/react-typography-icons'; import { RtFormFieldDatePickerProps } from './rt-form-field-date-picker.types'; import dayjs from 'dayjs'; import mergeRefs from 'merge-refs'; const cn = classNames.bind(styles); const RtDatepickerContext = createContext>( {} ); const RtDataComponent: RtData = (props) => { const { min, max } = useContext(RtDatepickerContext); return ( ); }; type PropType = RtFormFieldDatePickerProps & RtBaseProps; export const RtFormFieldDatePicker = forwardRef(function RtFormFieldDatePicker( props: PropType, ref: ForwardedRef ) { const localInputRef = useRef(null); const handleChange: RtFormFieldTextProps['onChange'] = ( e ) => { const value = e.value; props.onChange?.({ value: value ? dayjs(e.value).toDate() : null, event: e.event, }); }; function clickHandle(e: MouseEvent) { props.onClick?.(e); localInputRef.current?.showPicker(); } return ( )} {...({ ui: props.ui, size: props.size } as RtFormFieldSizeUI)} value={props.value ? dayjs(props.value).format('YYYY-MM-DD') : ''} type="text" data={RtDataComponent} ref={ref} inputRef={mergeRefs(props.inputRef, localInputRef)} trailingIcon={CalendarTypographyIcon} onChange={handleChange} onClick={clickHandle} /> ); }); RtFormFieldDatePicker.displayName = 'RtFormFieldDatePicker';