diff --git a/packages/lsd-react/src/components/Calendar/Calendar.tsx b/packages/lsd-react/src/components/Calendar/Calendar.tsx index 15d0acb..0a3f9b6 100644 --- a/packages/lsd-react/src/components/Calendar/Calendar.tsx +++ b/packages/lsd-react/src/components/Calendar/Calendar.tsx @@ -11,13 +11,13 @@ import { Month } from './Month' import { useClickAway } from 'react-use' export type CalendarProps = Omit< - React.HTMLAttributes, + React.HTMLAttributes, 'label' > & { open?: boolean disabled?: boolean value?: string - handleDateFieldChange: (data: Date) => void + onChange: (data: Date) => void handleRef: React.RefObject size?: 'large' | 'medium' onClose?: () => void @@ -28,16 +28,19 @@ export const Calendar: React.FC & { } = ({ open, handleRef, - value = null, + value: _value, size = 'large', disabled = false, - handleDateFieldChange, + onChange, onClose, children, ...props }) => { const ref = useRef(null) const [style, setStyle] = useState({}) + const [value, setValue] = useState( + _value ? new Date(_value) : null, + ) useClickAway(ref, (event) => { if (!open || event.composedPath().includes(handleRef.current!)) return @@ -46,10 +49,23 @@ export const Calendar: React.FC & { }) const handleDateChange = (data: OnDatesChangeProps) => { - handleDateFieldChange(data.startDate ?? new Date()) - onDateFocus(data.startDate ?? new Date()) + if (typeof _value !== 'undefined') { + if (typeof onChange !== 'undefined') { + onChange(data.startDate ?? new Date()) + } + } else { + setValue(data.startDate) + } } + useEffect(() => { + onDateFocus(_value ? new Date(_value) : new Date()) + }, [_value]) + + useEffect(() => { + onDateFocus(value ? new Date(value) : new Date()) + }, [value]) + const { activeMonths, isDateSelected, diff --git a/packages/lsd-react/src/components/DatePicker/DatePicker.tsx b/packages/lsd-react/src/components/DatePicker/DatePicker.tsx index bfe3bd9..cd55434 100644 --- a/packages/lsd-react/src/components/DatePicker/DatePicker.tsx +++ b/packages/lsd-react/src/components/DatePicker/DatePicker.tsx @@ -27,18 +27,20 @@ export type DatePickerProps = Omit< export const DatePicker: React.FC & { classes: typeof datePickerClasses -} = ({ value, onChange, withCalendar = true, ...props }) => { +} = ({ value: _value, onChange, withCalendar = true, ...props }) => { const ref = useRef(null) const [openCalendar, setOpenCalendar] = useState(false) - const [date, setDate] = useState(value || '') + const [date, setDate] = useState(_value || '') const handleDateFieldChange = (date: any) => { const offset = new Date(date).getTimezoneOffset() const formattedDate = new Date(date.getTime() - offset * 60 * 1000) const value = formattedDate.toISOString().split('T')[0] - if (typeof onChange === 'function') { - onChange(value) + if (typeof _value !== 'undefined') { + if (typeof onChange !== 'undefined') { + onChange(value) + } } else { setDate(value) } @@ -64,7 +66,7 @@ export const DatePicker: React.FC & { {withCalendar && ( setOpenCalendar(false)} handleRef={ref}