Skip to content

Commit

Permalink
refactor: refactor controlled components
Browse files Browse the repository at this point in the history
  • Loading branch information
jinhojang6 committed Mar 21, 2023
1 parent af27755 commit 7c6f8f3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
28 changes: 22 additions & 6 deletions packages/lsd-react/src/components/Calendar/Calendar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import { Month } from './Month'
import { useClickAway } from 'react-use'

export type CalendarProps = Omit<
React.HTMLAttributes<HTMLUListElement>,
React.HTMLAttributes<HTMLDivElement>,
'label'
> & {
open?: boolean
disabled?: boolean
value?: string
handleDateFieldChange: (data: Date) => void
onChange: (data: Date) => void
handleRef: React.RefObject<HTMLElement>
size?: 'large' | 'medium'
onClose?: () => void
Expand All @@ -28,16 +28,19 @@ export const Calendar: React.FC<CalendarProps> & {
} = ({
open,
handleRef,
value = null,
value: _value,
size = 'large',
disabled = false,
handleDateFieldChange,
onChange,
onClose,
children,
...props
}) => {
const ref = useRef<HTMLDivElement>(null)
const [style, setStyle] = useState<React.CSSProperties>({})
const [value, setValue] = useState<Date | null>(
_value ? new Date(_value) : null,
)

useClickAway(ref, (event) => {
if (!open || event.composedPath().includes(handleRef.current!)) return
Expand All @@ -46,10 +49,23 @@ export const Calendar: React.FC<CalendarProps> & {
})

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,
Expand Down
12 changes: 7 additions & 5 deletions packages/lsd-react/src/components/DatePicker/DatePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,20 @@ export type DatePickerProps = Omit<

export const DatePicker: React.FC<DatePickerProps> & {
classes: typeof datePickerClasses
} = ({ value, onChange, withCalendar = true, ...props }) => {
} = ({ value: _value, onChange, withCalendar = true, ...props }) => {
const ref = useRef<HTMLDivElement>(null)
const [openCalendar, setOpenCalendar] = useState(false)
const [date, setDate] = useState<string>(value || '')
const [date, setDate] = useState<string>(_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)
}
Expand All @@ -64,7 +66,7 @@ export const DatePicker: React.FC<DatePickerProps> & {
<Portal id="calendar">
{withCalendar && (
<Calendar
handleDateFieldChange={handleDateFieldChange}
onChange={handleDateFieldChange}
open={openCalendar}
onClose={() => setOpenCalendar(false)}
handleRef={ref}
Expand Down

0 comments on commit 7c6f8f3

Please sign in to comment.