-
Notifications
You must be signed in to change notification settings - Fork 7
/
InputPicker.tsx
152 lines (152 loc) · 4.26 KB
/
InputPicker.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import React, { forwardRef, RefObject } from 'react'
import { genFullIDay, mergeProviders } from '../../helpers'
import {
useSelectedDayActions,
useSelectedDayState
} from '../../store/SelectedDaysProvider'
import { ReactComponent as Close } from '../../Icons/close.svg'
import { useSelectedTimeState } from '../../store/SelectedTimeProvider'
import { useLangOption } from '../../hooks/useLangOption'
import { useCalenderActions } from '../../store/CalenderProvider'
import { IDay, IRange, ITime, ITimeRange } from 'src/type'
interface IInputPicker {
placeholder?: string
type: string
local: string
handelComponentVisible: () => void
onChange: (date: any) => void
clearBtn?: boolean
withTime?: boolean
isDisabled?: boolean
isRequired?: boolean
fromLabel?: string
toLabel?: string
inputClass?: string
clearBtnClass?: string
maxDate: IDay | null | undefined
showTimeInput?: boolean
}
const InputPicker = forwardRef(
(
{
placeholder,
type,
local,
handelComponentVisible,
clearBtn,
withTime,
onChange,
isDisabled,
isRequired,
fromLabel,
toLabel,
inputClass,
clearBtnClass,
maxDate,
showTimeInput
}: IInputPicker,
ref: RefObject<HTMLInputElement>
) => {
const { inputPlaceholder, fromLB, toLB, todayObject } = useLangOption(local)
const { changeCalender } = useCalenderActions()
const selectedDayState = useSelectedDayState()
const selectedTime = useSelectedTimeState()
const {
removeSelectedDay,
changeSelectedDayRange,
removeAllSelectedDayMulti
} = useSelectedDayActions()
const correctValue = () => {
if (type === 'single') {
return genFullIDay(
selectedDayState as IDay,
true,
withTime,
showTimeInput,
selectedTime as ITime
)
}
if (
type === 'range' &&
(selectedDayState as IRange).from &&
(selectedDayState as IRange).to
) {
return `${fromLabel || fromLB} : ${genFullIDay(
(selectedDayState as IRange).from,
true,
withTime,
showTimeInput,
(selectedTime as ITimeRange).from
)} ${toLabel || toLB} : ${genFullIDay(
(selectedDayState as IRange).to,
true,
withTime,
showTimeInput,
(selectedTime as ITimeRange).to
)}`
} else if (type === 'multi') {
const listDate = (selectedDayState as IDay[]).map((day) => {
return genFullIDay(day, true)
})
return listDate.join(' , ')
}
return ''
}
const clearValue = () => {
if (type === 'single') {
removeSelectedDay()
mergeProviders(onChange, type, null, selectedTime, withTime)
} else if (type === 'range') {
changeSelectedDayRange('from', null)
changeSelectedDayRange('to', null)
mergeProviders(
onChange,
type,
{ from: null, to: null },
selectedTime,
withTime
)
} else if (type === 'multi') {
removeAllSelectedDayMulti()
mergeProviders(onChange, type, [], selectedTime, withTime)
}
if (maxDate) {
changeCalender({
year: maxDate?.year,
month: maxDate?.month,
day: maxDate?.day
})
} else {
const today = todayObject()
changeCalender({
year: today.year,
month: today.month,
day: today.day
})
}
}
return (
<div className='input-picker'>
<input
ref={ref}
className={`input-picker--input ${inputClass}`}
readOnly
placeholder={placeholder || inputPlaceholder}
value={correctValue()}
onClick={() => handelComponentVisible()}
disabled={isDisabled}
required={isRequired}
/>
{clearBtn && (
<a
className={`input-picker--clearBtn ${clearBtnClass}`}
onClick={() => clearValue()}
>
<Close />
</a>
)}
</div>
)
}
)
export default InputPicker