import React, { useMemo, ReactNode, InputHTMLAttributes, useState, useEffect } from 'react'; import { IconCalendar } from '../icons/calendar'; import { TStyle, useClassnames } from '../../hooks/use-classnames'; import style from './index.module.pcss'; type TAttributes = 'id' | 'onChange' | 'onClick' | 'onBlur' | 'onKeyDownCapture' | 'onKeyUpCapture' | 'onFocus' | 'autoFocus' | 'tabIndex' | 'disabled' | 'maxLength' | 'minLength' | 'pattern' | 'placeholder' | 'readOnly' | 'required' | 'size' | 'autoComplete' | 'inputMode' | 'title' | 'autoCorrect'; export interface IProps extends Pick, TAttributes> { readonly name: string, readonly className?: TStyle | string, readonly direction?: 'row' | 'column', readonly minDate?: string, readonly maxDate?: string, readonly label?: ReactNode, readonly tabIndex?: number, readonly elError?: boolean, readonly elIcon?: boolean, readonly value?: string, readonly defaultValue?: string, readonly type?: 'datetime-local' | 'date' } export const InputDate = ({ type = 'date', elIcon = true, minDate, maxDate, ...props }: IProps) => { const cn = useClassnames(style, props.className); const [inputValue, setInputValue] = useState(props.value); useEffect(() => { setInputValue(props.value); }, [props.value]); const elLabel = useMemo(() => { if(props.label) { return (
); } }, [props.label, props.disabled, props.readOnly, props.required]); const handleOnChange = (event: React.ChangeEvent) => { const { value } = event.target; setInputValue(value); if(props.onChange) { props.onChange(event); } }; const elPlaceholder = useMemo(() => { if(props.placeholder && !inputValue) { return ( {props.placeholder} ); } }, [props.placeholder, inputValue]); const elIconElement = useMemo(() => { if(elIcon) { return ( ); } }, [elIcon]); const elInput = () => { return (
{elPlaceholder} {elIconElement}
); }; return ( ); }; export default InputDate;