@datability/8ui 1.1.0 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.css +1 -0
- package/dist/index.es.js +1853 -0
- package/dist/index.es.js.map +1 -0
- package/dist/index.umd.js +8 -0
- package/dist/index.umd.js.map +1 -0
- package/dist/types/App.d.ts +2 -0
- package/dist/types/components/blackdrop/index.d.ts +4 -0
- package/dist/types/components/blackdrop/index.type.d.ts +6 -0
- package/dist/types/components/button/index.d.ts +4 -0
- package/dist/types/components/button/index.type.d.ts +12 -0
- package/dist/types/components/chip/index.d.ts +4 -0
- package/dist/types/components/chip/index.type.d.ts +9 -0
- package/dist/types/components/context.d.ts +8 -0
- package/dist/types/components/divider/index.d.ts +3 -0
- package/dist/types/components/index.d.ts +41 -0
- package/dist/types/components/input/input-auto-complete/index.d.ts +4 -0
- package/dist/types/components/input/input-auto-complete/index.type.d.ts +14 -0
- package/dist/types/components/input/input-base/index.d.ts +4 -0
- package/dist/types/components/input/input-base/index.type.d.ts +11 -0
- package/dist/types/components/input/input-basic/index.d.ts +4 -0
- package/dist/types/components/input/input-basic/index.type.d.ts +10 -0
- package/dist/types/components/input/input-checkbox/index.d.ts +4 -0
- package/dist/types/components/input/input-checkbox/index.type.d.ts +13 -0
- package/dist/types/components/input/input-date/index.d.ts +22 -0
- package/dist/types/components/input/input-date/index.type.d.ts +13 -0
- package/dist/types/components/input/input-date-range/index.d.ts +4 -0
- package/dist/types/components/input/input-date-range/index.type.d.ts +13 -0
- package/dist/types/components/input/input-date-time/index.d.ts +4 -0
- package/dist/types/components/input/input-date-time/index.type.d.ts +13 -0
- package/dist/types/components/input/input-number/index.d.ts +4 -0
- package/dist/types/components/input/input-number/index.type.d.ts +12 -0
- package/dist/types/components/input/input-password/index.d.ts +4 -0
- package/dist/types/components/input/input-password/index.type.d.ts +10 -0
- package/dist/types/components/input/input-radio/index.d.ts +4 -0
- package/dist/types/components/input/input-radio/index.type.d.ts +14 -0
- package/dist/types/components/input/input-select/index.d.ts +4 -0
- package/dist/types/components/input/input-select/index.type.d.ts +16 -0
- package/dist/types/components/input/input-switch/index.d.ts +4 -0
- package/dist/types/components/input/input-switch/index.type.d.ts +6 -0
- package/dist/types/components/input/input-textarea/index.d.ts +4 -0
- package/dist/types/components/input/input-textarea/index.type.d.ts +12 -0
- package/dist/types/components/menu/index.d.ts +4 -0
- package/dist/types/components/menu/index.type.d.ts +11 -0
- package/dist/types/components/modal/index.d.ts +4 -0
- package/dist/types/components/modal/index.type.d.ts +7 -0
- package/dist/types/index.d.ts +41 -0
- package/dist/types/main.d.ts +1 -0
- package/package.json +5 -1
- package/.prettierrc +0 -8
- package/.vscode/extensions.json +0 -6
- package/declaration.d.ts +0 -10
- package/docker-compose.yml +0 -20
- package/eslint.config.js +0 -23
- package/index.html +0 -13
- package/src/App.tsx +0 -370
- package/src/components/blackdrop/index.tsx +0 -18
- package/src/components/blackdrop/index.type.ts +0 -7
- package/src/components/button/index.tsx +0 -44
- package/src/components/button/index.type.ts +0 -13
- package/src/components/chip/index.tsx +0 -39
- package/src/components/chip/index.type.ts +0 -12
- package/src/components/context.tsx +0 -26
- package/src/components/divider/index.tsx +0 -13
- package/src/components/index.ts +0 -62
- package/src/components/input/input-auto-complete/index.tsx +0 -140
- package/src/components/input/input-auto-complete/index.type.tsx +0 -13
- package/src/components/input/input-base/index.tsx +0 -39
- package/src/components/input/input-base/index.type.tsx +0 -13
- package/src/components/input/input-basic/index.tsx +0 -47
- package/src/components/input/input-basic/index.type.tsx +0 -8
- package/src/components/input/input-checkbox/index.tsx +0 -69
- package/src/components/input/input-checkbox/index.type.tsx +0 -11
- package/src/components/input/input-date/index.tsx +0 -354
- package/src/components/input/input-date/index.type.tsx +0 -11
- package/src/components/input/input-date-range/index.tsx +0 -284
- package/src/components/input/input-date-range/index.type.tsx +0 -11
- package/src/components/input/input-date-time/index.tsx +0 -367
- package/src/components/input/input-date-time/index.type.tsx +0 -11
- package/src/components/input/input-number/index.tsx +0 -118
- package/src/components/input/input-number/index.type.tsx +0 -11
- package/src/components/input/input-password/index.tsx +0 -60
- package/src/components/input/input-password/index.type.tsx +0 -8
- package/src/components/input/input-radio/index.tsx +0 -72
- package/src/components/input/input-radio/index.type.tsx +0 -12
- package/src/components/input/input-select/index.tsx +0 -113
- package/src/components/input/input-select/index.type.tsx +0 -15
- package/src/components/input/input-switch/index.tsx +0 -44
- package/src/components/input/input-switch/index.type.tsx +0 -4
- package/src/components/input/input-textarea/index.tsx +0 -48
- package/src/components/input/input-textarea/index.type.tsx +0 -10
- package/src/components/menu/index.tsx +0 -136
- package/src/components/menu/index.type.ts +0 -8
- package/src/components/modal/index.tsx +0 -99
- package/src/components/modal/index.type.tsx +0 -8
- package/src/index.scss +0 -44
- package/src/index.ts +0 -62
- package/src/logoDownload.svg +0 -3
- package/src/main.tsx +0 -9
- package/tsconfig.app.json +0 -28
- package/tsconfig.json +0 -42
- package/tsconfig.node.json +0 -29
- package/vite.config.d.ts +0 -2
- package/vite.config.ts +0 -35
- /package/{src → dist}/components/assets/closed.svg +0 -0
- /package/{src → dist}/components/assets/expand-arrow.svg +0 -0
- /package/{src → dist}/components/assets/visibility-off.svg +0 -0
- /package/{src → dist}/components/assets/visibility.svg +0 -0
- /package/{src → dist}/components/blackdrop/index.scss +0 -0
- /package/{src → dist}/components/button/index.scss +0 -0
- /package/{src → dist}/components/chip/index.scss +0 -0
- /package/{src → dist}/components/divider/index.scss +0 -0
- /package/{src → dist}/components/input/extend.scss +0 -0
- /package/{src → dist}/components/input/input-auto-complete/index.scss +0 -0
- /package/{src → dist}/components/input/input-base/index.scss +0 -0
- /package/{src → dist}/components/input/input-basic/index.scss +0 -0
- /package/{src → dist}/components/input/input-checkbox/index.scss +0 -0
- /package/{src → dist}/components/input/input-date/index.scss +0 -0
- /package/{src → dist}/components/input/input-date-range/index.scss +0 -0
- /package/{src → dist}/components/input/input-date-time/index.scss +0 -0
- /package/{src → dist}/components/input/input-number/index.scss +0 -0
- /package/{src → dist}/components/input/input-password/index.scss +0 -0
- /package/{src → dist}/components/input/input-radio/index.scss +0 -0
- /package/{src → dist}/components/input/input-select/index.scss +0 -0
- /package/{src → dist}/components/input/input-switch/index.scss +0 -0
- /package/{src → dist}/components/input/input-textarea/index.scss +0 -0
- /package/{src → dist}/components/menu/index.scss +0 -0
- /package/{src → dist}/components/modal/index.scss +0 -0
- /package/{public → dist}/vite.svg +0 -0
|
@@ -1,284 +0,0 @@
|
|
|
1
|
-
// Lib
|
|
2
|
-
import React, { useEffect, useState } from "react"
|
|
3
|
-
import { Controller, useFormContext } from "react-hook-form"
|
|
4
|
-
|
|
5
|
-
// Images
|
|
6
|
-
import expandArrowSVG from "../../assets/expand-arrow.svg"
|
|
7
|
-
import closedSVG from "../../assets/closed.svg"
|
|
8
|
-
|
|
9
|
-
// Include in project
|
|
10
|
-
import "./index.scss"
|
|
11
|
-
import InputBase from "../input-base"
|
|
12
|
-
import type { PropsInputDateRange } from "./index.type"
|
|
13
|
-
import Menu from "../../menu"
|
|
14
|
-
import Modal from "../../modal"
|
|
15
|
-
import { createPortal } from "react-dom"
|
|
16
|
-
import {
|
|
17
|
-
generateMonthOptions,
|
|
18
|
-
generateYearOptions,
|
|
19
|
-
getDateNow,
|
|
20
|
-
getDaysInMonthWithWeekday,
|
|
21
|
-
getMonthInDateString,
|
|
22
|
-
getYearInDateString,
|
|
23
|
-
updateMonthInDateString,
|
|
24
|
-
updateYearInDateString,
|
|
25
|
-
} from "../input-date"
|
|
26
|
-
|
|
27
|
-
const InputDateRange: React.FC<PropsInputDateRange> = ({
|
|
28
|
-
name,
|
|
29
|
-
label,
|
|
30
|
-
placeholder,
|
|
31
|
-
disabled = false,
|
|
32
|
-
require = false,
|
|
33
|
-
fullWidth = false,
|
|
34
|
-
isHideClearIcon = true,
|
|
35
|
-
maxYear,
|
|
36
|
-
minYear,
|
|
37
|
-
}) => {
|
|
38
|
-
const { control } = useFormContext()
|
|
39
|
-
|
|
40
|
-
const [showDay, setShowDay] = useState<string>(getDateNow())
|
|
41
|
-
const [isOpenModal, setIsOpenModal] = useState(false)
|
|
42
|
-
|
|
43
|
-
const yearOption = generateYearOptions(minYear, maxYear)
|
|
44
|
-
const monthOption = generateMonthOptions()
|
|
45
|
-
|
|
46
|
-
const [mounted, setMounted] = useState(false)
|
|
47
|
-
useEffect(() => {
|
|
48
|
-
setMounted(true)
|
|
49
|
-
return () => setMounted(false)
|
|
50
|
-
}, [])
|
|
51
|
-
|
|
52
|
-
const portalRoot =
|
|
53
|
-
(document.getElementById("root") as HTMLElement) ||
|
|
54
|
-
(document.getElementById("__next") as HTMLElement) ||
|
|
55
|
-
document.body
|
|
56
|
-
|
|
57
|
-
return (
|
|
58
|
-
<Controller
|
|
59
|
-
name={name}
|
|
60
|
-
control={control}
|
|
61
|
-
render={({ field, fieldState }) => {
|
|
62
|
-
const value: [string, string] = field.value ?? ["", ""]
|
|
63
|
-
const [startDate, endDate] = value
|
|
64
|
-
const { onChange } = field
|
|
65
|
-
const { invalid, error } = fieldState
|
|
66
|
-
|
|
67
|
-
// sync เดือนที่โชว์ตอนเปิด modal
|
|
68
|
-
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
69
|
-
useEffect(() => {
|
|
70
|
-
if (isOpenModal) {
|
|
71
|
-
setShowDay(startDate || getDateNow())
|
|
72
|
-
}
|
|
73
|
-
}, [isOpenModal])
|
|
74
|
-
|
|
75
|
-
function CalendarRow({ year, month }: { year: number; month: string }) {
|
|
76
|
-
const days = getDaysInMonthWithWeekday(year, month)
|
|
77
|
-
|
|
78
|
-
const mapToThaiWeek = (dayIndex: number) => dayIndex % 7
|
|
79
|
-
const firstDayThaiIndex = mapToThaiWeek(new Date(`${year}-${month}-01`).getDay())
|
|
80
|
-
const emptyStartDays = Array(firstDayThaiIndex).fill(null)
|
|
81
|
-
|
|
82
|
-
const allDays = [...emptyStartDays, ...days.map((d) => d.date.split("-")[2])]
|
|
83
|
-
while (allDays.length < 42) allDays.push(null)
|
|
84
|
-
|
|
85
|
-
const weeks: Array<Array<string | null>> = []
|
|
86
|
-
for (let i = 0; i < allDays.length; i += 7) weeks.push(allDays.slice(i, i + 7))
|
|
87
|
-
|
|
88
|
-
return (
|
|
89
|
-
<>
|
|
90
|
-
{weeks.map((week, rowIndex) => (
|
|
91
|
-
<div key={rowIndex} className="DBui-inputDateRangeRowDay">
|
|
92
|
-
{week.map((day, i) => {
|
|
93
|
-
if (!day) {
|
|
94
|
-
return (
|
|
95
|
-
<p key={i} className="DBui-inputDateRangeDay" data-hidden-hover>
|
|
96
|
-
{" "}
|
|
97
|
-
</p>
|
|
98
|
-
)
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
const targetDate = `${getYearInDateString(showDay)}-${getMonthInDateString(showDay)}-${day}`
|
|
102
|
-
|
|
103
|
-
const isChecked =
|
|
104
|
-
startDate && !endDate
|
|
105
|
-
? targetDate === startDate
|
|
106
|
-
: startDate && endDate
|
|
107
|
-
? targetDate >= startDate && targetDate <= endDate
|
|
108
|
-
: false
|
|
109
|
-
|
|
110
|
-
return (
|
|
111
|
-
<p
|
|
112
|
-
key={i}
|
|
113
|
-
className="DBui-inputDateRangeDay"
|
|
114
|
-
data-checked={isChecked}
|
|
115
|
-
onClick={() => {
|
|
116
|
-
if (!startDate) {
|
|
117
|
-
onChange([targetDate, ""])
|
|
118
|
-
} else if (startDate && !endDate) {
|
|
119
|
-
if (targetDate >= startDate) {
|
|
120
|
-
onChange([startDate, targetDate])
|
|
121
|
-
setIsOpenModal(false)
|
|
122
|
-
} else {
|
|
123
|
-
onChange([targetDate, ""])
|
|
124
|
-
}
|
|
125
|
-
} else {
|
|
126
|
-
onChange([targetDate, ""])
|
|
127
|
-
}
|
|
128
|
-
}}
|
|
129
|
-
>
|
|
130
|
-
{day}
|
|
131
|
-
</p>
|
|
132
|
-
)
|
|
133
|
-
})}
|
|
134
|
-
</div>
|
|
135
|
-
))}
|
|
136
|
-
</>
|
|
137
|
-
)
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
const handleClear = (e: React.MouseEvent<HTMLImageElement>) => {
|
|
141
|
-
e.stopPropagation()
|
|
142
|
-
onChange(["", ""])
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
return (
|
|
146
|
-
<InputBase
|
|
147
|
-
name={name}
|
|
148
|
-
label={label}
|
|
149
|
-
require={require}
|
|
150
|
-
fullWidth={fullWidth}
|
|
151
|
-
isInvalid={invalid}
|
|
152
|
-
errorMessage={error?.message}
|
|
153
|
-
>
|
|
154
|
-
<>
|
|
155
|
-
<div
|
|
156
|
-
className="DBui-inputDateRange"
|
|
157
|
-
onClick={() => (disabled ? null : setIsOpenModal(true))}
|
|
158
|
-
data-invalid={invalid}
|
|
159
|
-
data-disabled={disabled}
|
|
160
|
-
>
|
|
161
|
-
<p>{startDate ? `${startDate}${endDate ? ` to ${endDate}` : ""}` : placeholder}</p>
|
|
162
|
-
|
|
163
|
-
<img
|
|
164
|
-
src={closedSVG}
|
|
165
|
-
className="DBui-clearIconInputDateRange"
|
|
166
|
-
onClick={handleClear}
|
|
167
|
-
data-hidden={(startDate === "" && endDate === "") || disabled || isHideClearIcon}
|
|
168
|
-
/>
|
|
169
|
-
</div>
|
|
170
|
-
|
|
171
|
-
{mounted &&
|
|
172
|
-
createPortal(
|
|
173
|
-
<Modal id="modalInputDateRange" open={isOpenModal} onClose={() => setIsOpenModal(false)}>
|
|
174
|
-
<div className="DBui-inputDateRangeWrapperCalendar">
|
|
175
|
-
<div className="DBui-inputDateRangeRowMonthYear">
|
|
176
|
-
<img
|
|
177
|
-
src={expandArrowSVG}
|
|
178
|
-
className="DBui-inputDateRangeRowMonthYearSelected"
|
|
179
|
-
style={{ transform: "rotate(90deg)" }}
|
|
180
|
-
onClick={() => {
|
|
181
|
-
setShowDay(
|
|
182
|
-
updateMonthInDateString(
|
|
183
|
-
showDay,
|
|
184
|
-
String(Number(showDay.split("-")[1]) - 1).padStart(2, "0"),
|
|
185
|
-
),
|
|
186
|
-
)
|
|
187
|
-
}}
|
|
188
|
-
/>
|
|
189
|
-
|
|
190
|
-
<Menu
|
|
191
|
-
isInModal
|
|
192
|
-
trigger={() => (
|
|
193
|
-
<h4 className="DBui-inputDateRangeRowMonthYearSelected">{showDay.slice(0, 4)}</h4>
|
|
194
|
-
)}
|
|
195
|
-
>
|
|
196
|
-
{({ close }) =>
|
|
197
|
-
yearOption.map((y, index) => (
|
|
198
|
-
<p
|
|
199
|
-
key={index}
|
|
200
|
-
className="DBui-inputDateRangeRowMonthYearOption"
|
|
201
|
-
onClick={() => {
|
|
202
|
-
setShowDay(updateYearInDateString(showDay, y.value))
|
|
203
|
-
close()
|
|
204
|
-
}}
|
|
205
|
-
data-checked={getYearInDateString(showDay) === y.value}
|
|
206
|
-
>
|
|
207
|
-
{y.label}
|
|
208
|
-
</p>
|
|
209
|
-
))
|
|
210
|
-
}
|
|
211
|
-
</Menu>
|
|
212
|
-
|
|
213
|
-
<Menu
|
|
214
|
-
isInModal
|
|
215
|
-
trigger={() => (
|
|
216
|
-
<h4 className="DBui-inputDateRangeRowMonthYearSelected" style={{ width: "7rem" }}>
|
|
217
|
-
{monthOption.find((m) => m.value === showDay.slice(5, 7))?.label}
|
|
218
|
-
</h4>
|
|
219
|
-
)}
|
|
220
|
-
>
|
|
221
|
-
{({ close }) =>
|
|
222
|
-
monthOption.map((m, index) => (
|
|
223
|
-
<p
|
|
224
|
-
key={index}
|
|
225
|
-
className="DBui-inputDateRangeRowMonthYearOption"
|
|
226
|
-
onClick={() => {
|
|
227
|
-
setShowDay(updateMonthInDateString(showDay, m.value))
|
|
228
|
-
close()
|
|
229
|
-
}}
|
|
230
|
-
data-checked={getMonthInDateString(showDay) === m.value}
|
|
231
|
-
>
|
|
232
|
-
{m.label}
|
|
233
|
-
</p>
|
|
234
|
-
))
|
|
235
|
-
}
|
|
236
|
-
</Menu>
|
|
237
|
-
|
|
238
|
-
<img
|
|
239
|
-
src={expandArrowSVG}
|
|
240
|
-
className="DBui-inputDateRangeRowMonthYearSelected"
|
|
241
|
-
style={{ transform: "rotate(-90deg)" }}
|
|
242
|
-
onClick={() => {
|
|
243
|
-
setShowDay(
|
|
244
|
-
updateMonthInDateString(
|
|
245
|
-
showDay,
|
|
246
|
-
String(Number(showDay.split("-")[1]) + 1).padStart(2, "0"),
|
|
247
|
-
),
|
|
248
|
-
)
|
|
249
|
-
}}
|
|
250
|
-
/>
|
|
251
|
-
</div>
|
|
252
|
-
|
|
253
|
-
<div>
|
|
254
|
-
<div className="DBui-inputDateRangeRowHeaderDay">
|
|
255
|
-
<p className="DBui-inputDateRangeHeader">Sun</p>
|
|
256
|
-
<p className="DBui-inputDateRangeHeader">Mon</p>
|
|
257
|
-
<p className="DBui-inputDateRangeHeader">Tue</p>
|
|
258
|
-
<p className="DBui-inputDateRangeHeader">Wed</p>
|
|
259
|
-
<p className="DBui-inputDateRangeHeader">Thu</p>
|
|
260
|
-
<p className="DBui-inputDateRangeHeader">Fri</p>
|
|
261
|
-
<p className="DBui-inputDateRangeHeader">Sat</p>
|
|
262
|
-
</div>
|
|
263
|
-
|
|
264
|
-
<CalendarRow year={getYearInDateString(showDay)} month={getMonthInDateString(showDay)} />
|
|
265
|
-
</div>
|
|
266
|
-
</div>
|
|
267
|
-
</Modal>,
|
|
268
|
-
portalRoot,
|
|
269
|
-
)}
|
|
270
|
-
|
|
271
|
-
{!mounted && (
|
|
272
|
-
<Modal id="modalInputDateRange" open={isOpenModal} onClose={() => setIsOpenModal(false)}>
|
|
273
|
-
<div className="DBui-inputDateRangeWrapperCalendar" />
|
|
274
|
-
</Modal>
|
|
275
|
-
)}
|
|
276
|
-
</>
|
|
277
|
-
</InputBase>
|
|
278
|
-
)
|
|
279
|
-
}}
|
|
280
|
-
/>
|
|
281
|
-
)
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
export default InputDateRange
|
|
@@ -1,367 +0,0 @@
|
|
|
1
|
-
// Lib
|
|
2
|
-
import React, { useEffect, useRef, useState } from "react"
|
|
3
|
-
import { Controller, useFormContext } from "react-hook-form"
|
|
4
|
-
|
|
5
|
-
// Images
|
|
6
|
-
import expandArrowSVG from "../../assets/expand-arrow.svg"
|
|
7
|
-
import closedSVG from "../../assets/closed.svg"
|
|
8
|
-
|
|
9
|
-
// Include in project
|
|
10
|
-
import "./index.scss"
|
|
11
|
-
import InputBase from "../input-base"
|
|
12
|
-
import type { PropsInputDateTime } from "./index.type"
|
|
13
|
-
import Menu from "../../menu"
|
|
14
|
-
import Modal from "../../modal"
|
|
15
|
-
import Button from "../../button"
|
|
16
|
-
import { createPortal } from "react-dom"
|
|
17
|
-
import {
|
|
18
|
-
generateMonthOptions,
|
|
19
|
-
generateYearOptions,
|
|
20
|
-
getDateNow,
|
|
21
|
-
getDayInDateString,
|
|
22
|
-
getDaysInMonthWithWeekday,
|
|
23
|
-
getMonthInDateString,
|
|
24
|
-
getYearInDateString,
|
|
25
|
-
updateMonthInDateString,
|
|
26
|
-
updateYearInDateString,
|
|
27
|
-
} from "../input-date"
|
|
28
|
-
|
|
29
|
-
const InputDateTime: React.FC<PropsInputDateTime> = ({
|
|
30
|
-
name,
|
|
31
|
-
label,
|
|
32
|
-
placeholder,
|
|
33
|
-
disabled = false,
|
|
34
|
-
require = false,
|
|
35
|
-
fullWidth = false,
|
|
36
|
-
isHideClearIcon = true,
|
|
37
|
-
maxYear,
|
|
38
|
-
minYear,
|
|
39
|
-
}) => {
|
|
40
|
-
const { control } = useFormContext()
|
|
41
|
-
|
|
42
|
-
const hourWrapperRef = useRef<HTMLDivElement>(null)
|
|
43
|
-
const minWrapperRef = useRef<HTMLDivElement>(null)
|
|
44
|
-
|
|
45
|
-
const [isOpenModal, setIsOpenModal] = useState(false)
|
|
46
|
-
|
|
47
|
-
const [showDay, setShowDay] = useState(getDateNow())
|
|
48
|
-
const [selectedDay, setSelectedDay] = useState(getDateNow())
|
|
49
|
-
const [selectedHour, setSelectedHour] = useState("00")
|
|
50
|
-
const [selectedMin, setSelectedMin] = useState("00")
|
|
51
|
-
|
|
52
|
-
const yearOption = generateYearOptions(minYear, maxYear)
|
|
53
|
-
const monthOption = generateMonthOptions()
|
|
54
|
-
const hourOption = generateHourOptions()
|
|
55
|
-
const minOption = generateMinuteOptions()
|
|
56
|
-
|
|
57
|
-
useEffect(() => {
|
|
58
|
-
function scrollToChecked(wrapper: HTMLDivElement | null) {
|
|
59
|
-
if (!wrapper) return
|
|
60
|
-
const checkedEl = wrapper.querySelector('[data-checked="true"]') as HTMLElement
|
|
61
|
-
checkedEl?.scrollIntoView({ behavior: "smooth", block: "center" })
|
|
62
|
-
}
|
|
63
|
-
scrollToChecked(hourWrapperRef.current)
|
|
64
|
-
scrollToChecked(minWrapperRef.current)
|
|
65
|
-
}, [selectedHour, selectedMin])
|
|
66
|
-
|
|
67
|
-
function CalendarRow({ year, month }: { year: number; month: string }) {
|
|
68
|
-
const days = getDaysInMonthWithWeekday(year, month)
|
|
69
|
-
|
|
70
|
-
const mapToThaiWeek = (dayIndex: number) => dayIndex % 7
|
|
71
|
-
const firstDayThaiIndex = mapToThaiWeek(new Date(`${year}-${month}-01`).getDay())
|
|
72
|
-
const emptyStartDays = Array(firstDayThaiIndex).fill(null)
|
|
73
|
-
|
|
74
|
-
const allDays = [...emptyStartDays, ...days.map((d) => d.date.split("-")[2])]
|
|
75
|
-
while (allDays.length < 42) allDays.push(null)
|
|
76
|
-
|
|
77
|
-
const weeks: Array<Array<string | null>> = []
|
|
78
|
-
for (let i = 0; i < allDays.length; i += 7) weeks.push(allDays.slice(i, i + 7))
|
|
79
|
-
|
|
80
|
-
return (
|
|
81
|
-
<>
|
|
82
|
-
{weeks.map((week, rowIndex) => (
|
|
83
|
-
<div key={rowIndex} className="DBui-inputDateTimeRowDay">
|
|
84
|
-
{week.map((day, i) => (
|
|
85
|
-
<p
|
|
86
|
-
key={i}
|
|
87
|
-
className="DBui-inputDateTimeDay"
|
|
88
|
-
onClick={() => {
|
|
89
|
-
if (day) {
|
|
90
|
-
setSelectedDay(`${getYearInDateString(showDay)}-${getMonthInDateString(showDay)}-${day}`)
|
|
91
|
-
}
|
|
92
|
-
}}
|
|
93
|
-
data-checked={
|
|
94
|
-
`${getYearInDateString(selectedDay)}-${getMonthInDateString(selectedDay)}-${
|
|
95
|
-
getDayInDateString(selectedDay).split("T")[0]
|
|
96
|
-
}` === `${getYearInDateString(showDay)}-${getMonthInDateString(showDay)}-${day}`
|
|
97
|
-
}
|
|
98
|
-
data-hidden-hover={!day}
|
|
99
|
-
>
|
|
100
|
-
{day || ""}
|
|
101
|
-
</p>
|
|
102
|
-
))}
|
|
103
|
-
</div>
|
|
104
|
-
))}
|
|
105
|
-
</>
|
|
106
|
-
)
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
const readingValue = (dateString: string) => {
|
|
110
|
-
const [date, time] = dateString.split("T")
|
|
111
|
-
return `${date} ${time}`
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
const portalRoot =
|
|
115
|
-
(document.getElementById("root") as HTMLElement) ||
|
|
116
|
-
(document.getElementById("__next") as HTMLElement) ||
|
|
117
|
-
document.body
|
|
118
|
-
|
|
119
|
-
return (
|
|
120
|
-
<Controller
|
|
121
|
-
name={name}
|
|
122
|
-
control={control}
|
|
123
|
-
render={({ field, fieldState }) => {
|
|
124
|
-
const realValue: string = field.value || ""
|
|
125
|
-
const isInvalid = fieldState.invalid
|
|
126
|
-
const errorMessage = fieldState.error?.message
|
|
127
|
-
const setValue = field.onChange
|
|
128
|
-
|
|
129
|
-
// ✅ sync RHF value -> local state (เหมือน useEffect [realValue] ของ Formik)
|
|
130
|
-
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
131
|
-
useEffect(() => {
|
|
132
|
-
if (!realValue) {
|
|
133
|
-
const now = getDateNow()
|
|
134
|
-
setShowDay(now)
|
|
135
|
-
setSelectedDay(now)
|
|
136
|
-
setSelectedHour("00")
|
|
137
|
-
setSelectedMin("00")
|
|
138
|
-
return
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
const [d, t] = realValue.split("T")
|
|
142
|
-
const [h, m] = t.split(":")
|
|
143
|
-
|
|
144
|
-
setShowDay(d)
|
|
145
|
-
setSelectedDay(d)
|
|
146
|
-
setSelectedHour(h || "00")
|
|
147
|
-
setSelectedMin(m || "00")
|
|
148
|
-
}, [realValue])
|
|
149
|
-
|
|
150
|
-
const handleClearState = (e: React.MouseEvent<HTMLImageElement>) => {
|
|
151
|
-
e.stopPropagation()
|
|
152
|
-
setValue("")
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
return (
|
|
156
|
-
<InputBase
|
|
157
|
-
name={name}
|
|
158
|
-
label={label}
|
|
159
|
-
require={require}
|
|
160
|
-
fullWidth={fullWidth}
|
|
161
|
-
isInvalid={isInvalid}
|
|
162
|
-
errorMessage={errorMessage}
|
|
163
|
-
>
|
|
164
|
-
<>
|
|
165
|
-
<div
|
|
166
|
-
className="DBui-inputDateTime"
|
|
167
|
-
onClick={() => (disabled ? null : setIsOpenModal(true))}
|
|
168
|
-
data-invalid={isInvalid}
|
|
169
|
-
data-disabled={disabled}
|
|
170
|
-
>
|
|
171
|
-
<p>{realValue ? readingValue(realValue) : placeholder}</p>
|
|
172
|
-
<img
|
|
173
|
-
src={closedSVG}
|
|
174
|
-
className="DBui-clearIconInputDateTime"
|
|
175
|
-
onClick={handleClearState}
|
|
176
|
-
data-hidden={realValue === "" || disabled || isHideClearIcon}
|
|
177
|
-
/>
|
|
178
|
-
</div>
|
|
179
|
-
|
|
180
|
-
{createPortal(
|
|
181
|
-
<Modal id="modalInputDateTime" open={isOpenModal} onClose={() => setIsOpenModal(false)}>
|
|
182
|
-
<div className="DBui-inputDateTimeWrapperCalendar">
|
|
183
|
-
<div className="DBui-inputDateTimeWrapperYearMonthDayTime">
|
|
184
|
-
<div className="DBui-inputDateTimeWrapperYearMonthDay">
|
|
185
|
-
<div className="DBui-inputDateTimeRowMonthYear">
|
|
186
|
-
<img
|
|
187
|
-
src={expandArrowSVG}
|
|
188
|
-
className="DBui-inputDateTimeRowMonthYearSelected"
|
|
189
|
-
style={{ transform: "rotate(90deg)" }}
|
|
190
|
-
onClick={() => {
|
|
191
|
-
setShowDay(
|
|
192
|
-
updateMonthInDateString(
|
|
193
|
-
showDay,
|
|
194
|
-
String(Number(showDay.split("-")[1]) - 1).padStart(2, "0"),
|
|
195
|
-
),
|
|
196
|
-
)
|
|
197
|
-
}}
|
|
198
|
-
/>
|
|
199
|
-
|
|
200
|
-
<Menu
|
|
201
|
-
isInModal
|
|
202
|
-
trigger={() => (
|
|
203
|
-
<h4 className="DBui-inputDateTimeRowMonthYearSelected">{showDay.slice(0, 4)}</h4>
|
|
204
|
-
)}
|
|
205
|
-
>
|
|
206
|
-
{({ close }) =>
|
|
207
|
-
yearOption.map((data, index) => (
|
|
208
|
-
<p
|
|
209
|
-
key={index}
|
|
210
|
-
className="DBui-inputDateTimeRowMonthYearOption"
|
|
211
|
-
onClick={() => {
|
|
212
|
-
setShowDay(updateYearInDateString(showDay, data.value))
|
|
213
|
-
close()
|
|
214
|
-
}}
|
|
215
|
-
data-checked={getYearInDateString(showDay) === data.value}
|
|
216
|
-
>
|
|
217
|
-
{data.label}
|
|
218
|
-
</p>
|
|
219
|
-
))
|
|
220
|
-
}
|
|
221
|
-
</Menu>
|
|
222
|
-
|
|
223
|
-
<Menu
|
|
224
|
-
isInModal
|
|
225
|
-
trigger={() => (
|
|
226
|
-
<h4 className="DBui-inputDateTimeRowMonthYearSelected" style={{ width: "7rem" }}>
|
|
227
|
-
{monthOption.find((e) => e.value === showDay.slice(5, 7))?.label}
|
|
228
|
-
</h4>
|
|
229
|
-
)}
|
|
230
|
-
>
|
|
231
|
-
{({ close }) =>
|
|
232
|
-
monthOption.map((data, index) => (
|
|
233
|
-
<p
|
|
234
|
-
key={index}
|
|
235
|
-
className="DBui-inputDateTimeRowMonthYearOption"
|
|
236
|
-
onClick={() => {
|
|
237
|
-
setShowDay(updateMonthInDateString(showDay, data.value))
|
|
238
|
-
close()
|
|
239
|
-
}}
|
|
240
|
-
data-checked={getMonthInDateString(showDay) === data.value}
|
|
241
|
-
>
|
|
242
|
-
{data.label}
|
|
243
|
-
</p>
|
|
244
|
-
))
|
|
245
|
-
}
|
|
246
|
-
</Menu>
|
|
247
|
-
|
|
248
|
-
<img
|
|
249
|
-
src={expandArrowSVG}
|
|
250
|
-
className="DBui-inputDateTimeRowMonthYearSelected"
|
|
251
|
-
style={{ transform: "rotate(-90deg)" }}
|
|
252
|
-
onClick={() => {
|
|
253
|
-
setShowDay(
|
|
254
|
-
updateMonthInDateString(
|
|
255
|
-
showDay,
|
|
256
|
-
String(Number(showDay.split("-")[1]) + 1).padStart(2, "0"),
|
|
257
|
-
),
|
|
258
|
-
)
|
|
259
|
-
}}
|
|
260
|
-
/>
|
|
261
|
-
</div>
|
|
262
|
-
|
|
263
|
-
<div>
|
|
264
|
-
<div className="DBui-inputDateTimeRowHeaderDay">
|
|
265
|
-
<p className="DBui-inputDateTimeHeader">Sun</p>
|
|
266
|
-
<p className="DBui-inputDateTimeHeader">Mon</p>
|
|
267
|
-
<p className="DBui-inputDateTimeHeader">Tue</p>
|
|
268
|
-
<p className="DBui-inputDateTimeHeader">Wed</p>
|
|
269
|
-
<p className="DBui-inputDateTimeHeader">Thu</p>
|
|
270
|
-
<p className="DBui-inputDateTimeHeader">Fri</p>
|
|
271
|
-
<p className="DBui-inputDateTimeHeader">Sat</p>
|
|
272
|
-
</div>
|
|
273
|
-
|
|
274
|
-
<CalendarRow year={getYearInDateString(showDay)} month={getMonthInDateString(showDay)} />
|
|
275
|
-
</div>
|
|
276
|
-
</div>
|
|
277
|
-
|
|
278
|
-
<div className="DBui-inputDateTimeWrapperHourMin">
|
|
279
|
-
<p style={{ textAlign: "center" }}>Hr</p>
|
|
280
|
-
<div className="DBui-inputDateTimeWrapperHourMinOptions" ref={hourWrapperRef}>
|
|
281
|
-
{hourOption.map((hour) => (
|
|
282
|
-
<p
|
|
283
|
-
key={hour.value}
|
|
284
|
-
data-checked={selectedHour === hour.value}
|
|
285
|
-
onClick={() => setSelectedHour(hour.value)}
|
|
286
|
-
>
|
|
287
|
-
{hour.value}
|
|
288
|
-
</p>
|
|
289
|
-
))}
|
|
290
|
-
</div>
|
|
291
|
-
</div>
|
|
292
|
-
|
|
293
|
-
<div className="DBui-inputDateTimeWrapperHourMin">
|
|
294
|
-
<p style={{ textAlign: "center" }}>Min</p>
|
|
295
|
-
<div className="DBui-inputDateTimeWrapperHourMinOptions" ref={minWrapperRef}>
|
|
296
|
-
{minOption.map((min) => (
|
|
297
|
-
<p
|
|
298
|
-
key={min.value}
|
|
299
|
-
data-checked={selectedMin === min.value}
|
|
300
|
-
onClick={() => setSelectedMin(min.value)}
|
|
301
|
-
>
|
|
302
|
-
{min.value}
|
|
303
|
-
</p>
|
|
304
|
-
))}
|
|
305
|
-
</div>
|
|
306
|
-
</div>
|
|
307
|
-
</div>
|
|
308
|
-
|
|
309
|
-
<div className="DBui-inputDateTimeWrapperButton">
|
|
310
|
-
<Button
|
|
311
|
-
type="button"
|
|
312
|
-
onClick={() => {
|
|
313
|
-
let hour = getHourNow()
|
|
314
|
-
let min = getMinNow()
|
|
315
|
-
if (selectedHour) hour = selectedHour
|
|
316
|
-
if (selectedMin) min = selectedMin
|
|
317
|
-
|
|
318
|
-
setValue(`${selectedDay}T${hour}:${min}`)
|
|
319
|
-
setIsOpenModal(false)
|
|
320
|
-
}}
|
|
321
|
-
name="Save"
|
|
322
|
-
className="DBui-inputDateTimeButtonSave"
|
|
323
|
-
/>
|
|
324
|
-
<Button
|
|
325
|
-
type="button"
|
|
326
|
-
onClick={() => setIsOpenModal(false)}
|
|
327
|
-
name="Cancle"
|
|
328
|
-
className="DBui-inputDateTimeButtonCancle"
|
|
329
|
-
/>
|
|
330
|
-
</div>
|
|
331
|
-
</div>
|
|
332
|
-
</Modal>,
|
|
333
|
-
portalRoot,
|
|
334
|
-
)}
|
|
335
|
-
</>
|
|
336
|
-
</InputBase>
|
|
337
|
-
)
|
|
338
|
-
}}
|
|
339
|
-
/>
|
|
340
|
-
)
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
export default InputDateTime
|
|
344
|
-
|
|
345
|
-
function generateHourOptions(): { label: string; value: string }[] {
|
|
346
|
-
return Array.from({ length: 24 }, (_, i) => {
|
|
347
|
-
const value = String(i).padStart(2, "0")
|
|
348
|
-
return { label: value, value }
|
|
349
|
-
})
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
function generateMinuteOptions(): { label: string; value: string }[] {
|
|
353
|
-
return Array.from({ length: 60 }, (_, i) => {
|
|
354
|
-
const value = String(i).padStart(2, "0")
|
|
355
|
-
return { label: value, value }
|
|
356
|
-
})
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
function getHourNow(): string {
|
|
360
|
-
const dateNow = new Date()
|
|
361
|
-
return `${dateNow.getHours()}`.padStart(2, "0")
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
function getMinNow(): string {
|
|
365
|
-
const dateNow = new Date()
|
|
366
|
-
return `${dateNow.getMinutes()}`.padStart(2, "0")
|
|
367
|
-
}
|