-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathDatePicker.tsx
121 lines (115 loc) · 3.48 KB
/
DatePicker.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import React, { useMemo, useRef, useState } from 'react'
import RenderCalendar from '../../components/RenderCalendar'
import Calendar from '../Calendar'
import useClickOutside from '../../hooks/useClickOutside'
import formatDate from '../../utils/format'
import locales from '../../utils/locales'
import type { DatePickerProps } from './DatePicker.types'
import CalendarProvider from '../CalendarProvider/CalendarProvider'
import localeCache from '../../utils/locale'
import type { OnChangePayload } from '../Calendar/Calendar.types'
export const DatePicker = (props: DatePickerProps) => {
const {
defaultValue,
locale = 'fa',
weekends = [],
direction = 'rtl',
accentColor
} = props
useMemo(() => localeCache.setLocale(locale), [locale])
// refs
const inputRef = useRef<HTMLInputElement>(null)
const containerRef = useRef<HTMLDivElement>(null)
// states
const [value, setValue] = useState<Date | undefined>(
defaultValue !== undefined ? new Date(defaultValue) : undefined
)
const [from, setFrom] = useState<Date | undefined>(
props.range === true && props.from !== undefined
? new Date(props.from)
: undefined
)
const [to, setTo] = useState<Date | undefined>(
props.range === true && props.to !== undefined
? new Date(props.to)
: undefined
)
const [showCalendar, setShowCalendar] = useState<boolean>(false)
// hooks
useClickOutside(containerRef, () => setShowCalendar(false))
// handlers
const toggleShowCalendar = () => {
setShowCalendar(!showCalendar)
}
const handleChangeDay = (e: OnChangePayload) => {
if (props.range === true && typeof props.onChange === 'function') {
const from = 'from' in e ? e.from : new Date()
const to = 'to' in e ? e.to : new Date()
setFrom(from)
setTo(to)
props.onChange({
from,
to
})
} else if (
(props.range === false || props.range === undefined) &&
typeof props.onChange === 'function'
) {
const value = 'value' in e ? e.value : new Date()
setValue(value)
props.onChange({
value
})
}
}
const getInputValue = useMemo(() => {
const format = props?.customShowDateFormat ?? locales[locale].format
if (props.range === undefined && value !== undefined) {
return formatDate(value, format)
}
console.log('from >>', from)
if (from !== undefined && to !== undefined) {
return `
${formatDate(from, format)}
-
${formatDate(to, format)}
`
}
return ''
}, [value, from, to])
return (
<CalendarProvider
accentColor={accentColor}
round={props.round}
direction={direction}
>
<input
ref={inputRef}
{...props?.inputAttributes}
onClick={toggleShowCalendar}
type="text"
value={getInputValue}
className={props.inputClass !== null ? props.inputClass : ''}
readOnly
/>
<RenderCalendar
toggleOpen={toggleShowCalendar}
showCalendar={showCalendar}
destinationRef={inputRef}
position={props.position}
>
<Calendar
defaultValue={value}
ref={containerRef}
className={props.className}
weekends={weekends}
onChange={handleChangeDay}
range={props.range}
from={props.range === true ? props.from : undefined}
to={props.range === true ? props.to : undefined}
/>
</RenderCalendar>
</CalendarProvider>
)
}
export default DatePicker