@sybilion/uilib 1.4.21 → 1.4.22
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/assets/globals.css +3 -0
- package/dist/agent-glossary/workspace.generated.ts +2 -0
- package/dist/agent-glossary/workspace.index.md +2 -0
- package/dist/agent-glossary/workspace.md +2 -0
- package/dist/esm/components/ui/Checkbox/Checkbox.js +2 -2
- package/dist/esm/components/ui/Checkbox/Checkbox.styl.js +2 -2
- package/dist/esm/components/ui/DatePicker/Calendar.js +72 -0
- package/dist/esm/components/ui/DatePicker/DatePicker.js +81 -0
- package/dist/esm/components/ui/DatePicker/DatePicker.styl.js +7 -0
- package/dist/esm/components/ui/DatePicker/DatePickerInput.js +70 -0
- package/dist/esm/components/ui/DatePicker/DateRangePickerInput.js +130 -0
- package/dist/esm/components/ui/DatePicker/dateUtils.js +137 -0
- package/dist/esm/components/ui/RadioGroup/RadioGroup.helpers.js +6 -0
- package/dist/esm/components/ui/RadioGroup/RadioGroup.js +18 -0
- package/dist/esm/components/ui/RadioGroup/RadioGroup.styl.js +7 -0
- package/dist/esm/index.js +5 -0
- package/dist/esm/types/src/components/ui/Checkbox/Checkbox.d.ts +1 -1
- package/dist/esm/types/src/components/ui/Checkbox/Checkbox.types.d.ts +2 -0
- package/dist/esm/types/src/components/ui/Checkbox/index.d.ts +1 -1
- package/dist/esm/types/src/components/ui/DatePicker/Calendar.d.ts +2 -0
- package/dist/esm/types/src/components/ui/DatePicker/DatePicker.d.ts +2 -0
- package/dist/esm/types/src/components/ui/DatePicker/DatePicker.types.d.ts +60 -0
- package/dist/esm/types/src/components/ui/DatePicker/DatePickerInput.d.ts +2 -0
- package/dist/esm/types/src/components/ui/DatePicker/DateRangePickerInput.d.ts +2 -0
- package/dist/esm/types/src/components/ui/DatePicker/dateUtils.d.ts +30 -0
- package/dist/esm/types/src/components/ui/DatePicker/index.d.ts +6 -0
- package/dist/esm/types/src/components/ui/RadioGroup/RadioGroup.d.ts +4 -0
- package/dist/esm/types/src/components/ui/RadioGroup/RadioGroup.example.d.ts +1 -0
- package/dist/esm/types/src/components/ui/RadioGroup/RadioGroup.helpers.d.ts +3 -0
- package/dist/esm/types/src/components/ui/RadioGroup/RadioGroup.types.d.ts +11 -0
- package/dist/esm/types/src/components/ui/RadioGroup/index.d.ts +2 -0
- package/dist/esm/types/src/docs/pages/DatePickerPage.d.ts +1 -0
- package/dist/esm/types/src/docs/pages/RadioGroupPage.d.ts +1 -0
- package/dist/esm/types/src/index.d.ts +2 -0
- package/package.json +2 -1
- package/src/components/ui/Checkbox/AGENT.md +1 -0
- package/src/components/ui/Checkbox/Checkbox.styl +26 -2
- package/src/components/ui/Checkbox/Checkbox.styl.d.ts +4 -0
- package/src/components/ui/Checkbox/Checkbox.tsx +3 -3
- package/src/components/ui/Checkbox/Checkbox.types.ts +5 -1
- package/src/components/ui/Checkbox/index.ts +1 -1
- package/src/components/ui/DatePicker/AGENT.md +45 -0
- package/src/components/ui/DatePicker/Calendar.tsx +160 -0
- package/src/components/ui/DatePicker/DatePicker.styl +219 -0
- package/src/components/ui/DatePicker/DatePicker.styl.d.ts +39 -0
- package/src/components/ui/DatePicker/DatePicker.tsx +160 -0
- package/src/components/ui/DatePicker/DatePicker.types.ts +91 -0
- package/src/components/ui/DatePicker/DatePickerInput.tsx +169 -0
- package/src/components/ui/DatePicker/DateRangePickerInput.tsx +276 -0
- package/src/components/ui/DatePicker/dateUtils.ts +199 -0
- package/src/components/ui/DatePicker/index.ts +13 -0
- package/src/components/ui/RadioGroup/AGENT.md +24 -0
- package/src/components/ui/RadioGroup/RadioGroup.example.tsx +136 -0
- package/src/components/ui/RadioGroup/RadioGroup.helpers.ts +11 -0
- package/src/components/ui/RadioGroup/RadioGroup.styl +70 -0
- package/src/components/ui/RadioGroup/RadioGroup.styl.d.ts +13 -0
- package/src/components/ui/RadioGroup/RadioGroup.tsx +58 -0
- package/src/components/ui/RadioGroup/RadioGroup.types.ts +19 -0
- package/src/components/ui/RadioGroup/index.ts +6 -0
- package/src/docs/pages/CheckboxPage.tsx +129 -18
- package/src/docs/pages/DatePickerPage.tsx +183 -0
- package/src/docs/pages/FormsOverviewPage.tsx +131 -0
- package/src/docs/pages/RadioGroupPage.tsx +21 -0
- package/src/docs/registry.ts +12 -0
- package/src/index.ts +2 -0
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
@import '../../../lib/theme.styl'
|
|
2
|
+
|
|
3
|
+
.inputRoot
|
|
4
|
+
position relative
|
|
5
|
+
display flex
|
|
6
|
+
align-items stretch
|
|
7
|
+
width 100%
|
|
8
|
+
|
|
9
|
+
.inputField
|
|
10
|
+
padding-right 2.5rem
|
|
11
|
+
|
|
12
|
+
.calendarTrigger
|
|
13
|
+
position absolute
|
|
14
|
+
top 50%
|
|
15
|
+
right 0.25rem
|
|
16
|
+
transform translateY(-50%)
|
|
17
|
+
color var(--muted-foreground)
|
|
18
|
+
|
|
19
|
+
&:hover
|
|
20
|
+
color var(--foreground)
|
|
21
|
+
|
|
22
|
+
.popupContent
|
|
23
|
+
min-width 17.5rem
|
|
24
|
+
padding var(--p-2)
|
|
25
|
+
|
|
26
|
+
.popupContent-sm
|
|
27
|
+
width fit-content
|
|
28
|
+
min-width auto
|
|
29
|
+
|
|
30
|
+
.picker
|
|
31
|
+
display flex
|
|
32
|
+
flex-direction column
|
|
33
|
+
gap var(--p-2)
|
|
34
|
+
|
|
35
|
+
.pickerHeader
|
|
36
|
+
display flex
|
|
37
|
+
align-items center
|
|
38
|
+
justify-content space-between
|
|
39
|
+
gap var(--p-1)
|
|
40
|
+
width 100%
|
|
41
|
+
|
|
42
|
+
.pickerMonthLabel
|
|
43
|
+
flex 1
|
|
44
|
+
text-align center
|
|
45
|
+
font-size var(--text-sm)
|
|
46
|
+
font-weight 600
|
|
47
|
+
color var(--foreground)
|
|
48
|
+
|
|
49
|
+
.calendar
|
|
50
|
+
display flex
|
|
51
|
+
flex-direction column
|
|
52
|
+
gap var(--p-1)
|
|
53
|
+
|
|
54
|
+
.weekdayRow
|
|
55
|
+
display grid
|
|
56
|
+
grid-template-columns repeat(7, 1fr)
|
|
57
|
+
gap 0
|
|
58
|
+
|
|
59
|
+
.weekdayCell
|
|
60
|
+
display flex
|
|
61
|
+
align-items center
|
|
62
|
+
justify-content center
|
|
63
|
+
height 2rem
|
|
64
|
+
font-size var(--text-xs)
|
|
65
|
+
font-weight 500
|
|
66
|
+
color var(--muted-foreground)
|
|
67
|
+
|
|
68
|
+
.dayGrid
|
|
69
|
+
display grid
|
|
70
|
+
grid-template-columns repeat(7, 1fr)
|
|
71
|
+
gap 0
|
|
72
|
+
|
|
73
|
+
.dayCell
|
|
74
|
+
display flex
|
|
75
|
+
align-items center
|
|
76
|
+
justify-content center
|
|
77
|
+
border none
|
|
78
|
+
background-color transparent
|
|
79
|
+
color var(--foreground)
|
|
80
|
+
cursor pointer
|
|
81
|
+
transition background-color 150ms, color 150ms
|
|
82
|
+
|
|
83
|
+
&:hover:not(:disabled)
|
|
84
|
+
background-color var(--accent)
|
|
85
|
+
color var(--accent-foreground)
|
|
86
|
+
|
|
87
|
+
&:focus-visible
|
|
88
|
+
outline 2px solid var(--ring)
|
|
89
|
+
outline-offset 1px
|
|
90
|
+
|
|
91
|
+
&:disabled
|
|
92
|
+
cursor not-allowed
|
|
93
|
+
opacity 0.35
|
|
94
|
+
|
|
95
|
+
.dayOutsideMonth
|
|
96
|
+
color var(--muted-foreground)
|
|
97
|
+
|
|
98
|
+
.daySelected
|
|
99
|
+
background-color var(--primary)
|
|
100
|
+
color var(--primary-foreground)
|
|
101
|
+
|
|
102
|
+
&:hover:not(:disabled)
|
|
103
|
+
background-color var(--primary)
|
|
104
|
+
color var(--primary-foreground)
|
|
105
|
+
|
|
106
|
+
.dayToday:not(.daySelected):not(.dayRangeStart):not(.dayRangeEnd)
|
|
107
|
+
box-shadow inset 0 0 0 1px var(--ring)
|
|
108
|
+
|
|
109
|
+
.dayInRange
|
|
110
|
+
background-color var(--accent)
|
|
111
|
+
color var(--accent-foreground)
|
|
112
|
+
border-radius 0
|
|
113
|
+
|
|
114
|
+
&:hover:not(:disabled)
|
|
115
|
+
background-color var(--accent)
|
|
116
|
+
color var(--accent-foreground)
|
|
117
|
+
|
|
118
|
+
.dayInRangePreview
|
|
119
|
+
background-color unquote('color-mix(in srgb, var(--accent) 70%, transparent)')
|
|
120
|
+
|
|
121
|
+
.dayRangeStart
|
|
122
|
+
background-color var(--primary)
|
|
123
|
+
color var(--primary-foreground)
|
|
124
|
+
border-top-right-radius 0
|
|
125
|
+
border-bottom-right-radius 0
|
|
126
|
+
|
|
127
|
+
&:hover:not(:disabled)
|
|
128
|
+
background-color var(--primary)
|
|
129
|
+
color var(--primary-foreground)
|
|
130
|
+
|
|
131
|
+
.dayRangeEnd
|
|
132
|
+
background-color var(--primary)
|
|
133
|
+
color var(--primary-foreground)
|
|
134
|
+
border-top-left-radius 0
|
|
135
|
+
border-bottom-left-radius 0
|
|
136
|
+
|
|
137
|
+
&:hover:not(:disabled)
|
|
138
|
+
background-color var(--primary)
|
|
139
|
+
color var(--primary-foreground)
|
|
140
|
+
|
|
141
|
+
.dayRangeStart.dayRangeEnd
|
|
142
|
+
border-radius var(--radius-input)
|
|
143
|
+
|
|
144
|
+
.rangeInputRoot
|
|
145
|
+
position relative
|
|
146
|
+
display flex
|
|
147
|
+
align-items stretch
|
|
148
|
+
width 100%
|
|
149
|
+
|
|
150
|
+
.rangeInputField
|
|
151
|
+
flex 1
|
|
152
|
+
min-width 0
|
|
153
|
+
|
|
154
|
+
.rangeInputFieldActive
|
|
155
|
+
box-shadow inset 0 0 0 2px var(--brand-color-400) !important
|
|
156
|
+
|
|
157
|
+
.rangeSeparator
|
|
158
|
+
display flex
|
|
159
|
+
align-items center
|
|
160
|
+
padding 0 var(--p-1)
|
|
161
|
+
color var(--muted-foreground)
|
|
162
|
+
user-select none
|
|
163
|
+
|
|
164
|
+
.rangeCalendarTrigger
|
|
165
|
+
position absolute
|
|
166
|
+
top 50%
|
|
167
|
+
right 0.25rem
|
|
168
|
+
transform translateY(-50%)
|
|
169
|
+
color var(--muted-foreground)
|
|
170
|
+
|
|
171
|
+
&:hover
|
|
172
|
+
color var(--foreground)
|
|
173
|
+
|
|
174
|
+
.rangeInputRoot .rangeInputField:last-of-type
|
|
175
|
+
padding-right 2.5rem
|
|
176
|
+
|
|
177
|
+
.inputSize-sm
|
|
178
|
+
.pickerSize-sm
|
|
179
|
+
.calendarSize-sm
|
|
180
|
+
.dayCell
|
|
181
|
+
width var(--p-6)
|
|
182
|
+
height var(--p-6)
|
|
183
|
+
font-size var(--text-xs)
|
|
184
|
+
|
|
185
|
+
.pickerSize-sm
|
|
186
|
+
align-items center
|
|
187
|
+
|
|
188
|
+
.calendarSize-sm
|
|
189
|
+
width fit-content
|
|
190
|
+
|
|
191
|
+
.weekdayRow
|
|
192
|
+
.dayGrid
|
|
193
|
+
width fit-content
|
|
194
|
+
grid-template-columns repeat(7, var(--p-6))
|
|
195
|
+
|
|
196
|
+
.inputSize-md
|
|
197
|
+
.calendarSize-md
|
|
198
|
+
.dayCell
|
|
199
|
+
width var(--p-10)
|
|
200
|
+
height var(--p-10)
|
|
201
|
+
font-size var(--text-sm)
|
|
202
|
+
|
|
203
|
+
.pickerSize-lg
|
|
204
|
+
.calendarSize-lg
|
|
205
|
+
.dayCell
|
|
206
|
+
width var(--p-10)
|
|
207
|
+
height var(--p-10)
|
|
208
|
+
font-size var(--text-base)
|
|
209
|
+
|
|
210
|
+
.srOnly
|
|
211
|
+
position absolute
|
|
212
|
+
width 1px
|
|
213
|
+
height 1px
|
|
214
|
+
padding 0
|
|
215
|
+
margin -1px
|
|
216
|
+
overflow hidden
|
|
217
|
+
clip rect(0, 0, 0, 0)
|
|
218
|
+
white-space nowrap
|
|
219
|
+
border 0
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// This file is automatically generated.
|
|
2
|
+
// Please do not change this file!
|
|
3
|
+
interface CssExports {
|
|
4
|
+
'calendar': string;
|
|
5
|
+
'calendarSize-lg': string;
|
|
6
|
+
'calendarSize-md': string;
|
|
7
|
+
'calendarSize-sm': string;
|
|
8
|
+
'calendarTrigger': string;
|
|
9
|
+
'dayCell': string;
|
|
10
|
+
'dayGrid': string;
|
|
11
|
+
'dayInRange': string;
|
|
12
|
+
'dayInRangePreview': string;
|
|
13
|
+
'dayOutsideMonth': string;
|
|
14
|
+
'dayRangeEnd': string;
|
|
15
|
+
'dayRangeStart': string;
|
|
16
|
+
'daySelected': string;
|
|
17
|
+
'dayToday': string;
|
|
18
|
+
'inputField': string;
|
|
19
|
+
'inputRoot': string;
|
|
20
|
+
'inputSize-md': string;
|
|
21
|
+
'inputSize-sm': string;
|
|
22
|
+
'picker': string;
|
|
23
|
+
'pickerHeader': string;
|
|
24
|
+
'pickerMonthLabel': string;
|
|
25
|
+
'pickerSize-lg': string;
|
|
26
|
+
'pickerSize-sm': string;
|
|
27
|
+
'popupContent': string;
|
|
28
|
+
'popupContent-sm': string;
|
|
29
|
+
'rangeCalendarTrigger': string;
|
|
30
|
+
'rangeInputField': string;
|
|
31
|
+
'rangeInputFieldActive': string;
|
|
32
|
+
'rangeInputRoot': string;
|
|
33
|
+
'rangeSeparator': string;
|
|
34
|
+
'srOnly': string;
|
|
35
|
+
'weekdayCell': string;
|
|
36
|
+
'weekdayRow': string;
|
|
37
|
+
}
|
|
38
|
+
export const cssExports: CssExports;
|
|
39
|
+
export default cssExports;
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import cn from 'classnames';
|
|
2
|
+
import { useCallback, useEffect, useMemo, useState } from 'react';
|
|
3
|
+
|
|
4
|
+
import { CaretLeft, CaretRight } from '@phosphor-icons/react';
|
|
5
|
+
|
|
6
|
+
import { Button } from '../Button';
|
|
7
|
+
import { Calendar } from './Calendar';
|
|
8
|
+
import S from './DatePicker.styl';
|
|
9
|
+
import type { DatePickerProps } from './DatePicker.types';
|
|
10
|
+
import {
|
|
11
|
+
addMonths,
|
|
12
|
+
formatMonthYear,
|
|
13
|
+
getTodayParts,
|
|
14
|
+
normalizeDateRange,
|
|
15
|
+
parseDateString,
|
|
16
|
+
} from './dateUtils';
|
|
17
|
+
|
|
18
|
+
export function DatePicker({
|
|
19
|
+
className,
|
|
20
|
+
mode = 'single',
|
|
21
|
+
value,
|
|
22
|
+
onChange,
|
|
23
|
+
rangeValue,
|
|
24
|
+
onRangeChange,
|
|
25
|
+
size = 'md',
|
|
26
|
+
calendarProps,
|
|
27
|
+
}: DatePickerProps) {
|
|
28
|
+
const selected = parseDateString(value);
|
|
29
|
+
const rangeStartParsed = parseDateString(rangeValue?.start);
|
|
30
|
+
const today = getTodayParts();
|
|
31
|
+
const [viewMonth, setViewMonth] = useState(() => ({
|
|
32
|
+
year:
|
|
33
|
+
selected?.year ??
|
|
34
|
+
rangeStartParsed?.year ??
|
|
35
|
+
parseDateString(rangeValue?.end)?.year ??
|
|
36
|
+
today.year,
|
|
37
|
+
month:
|
|
38
|
+
selected?.month ??
|
|
39
|
+
rangeStartParsed?.month ??
|
|
40
|
+
parseDateString(rangeValue?.end)?.month ??
|
|
41
|
+
today.month,
|
|
42
|
+
}));
|
|
43
|
+
const [pendingStart, setPendingStart] = useState<string | undefined>();
|
|
44
|
+
const [rangeHover, setRangeHover] = useState<string | undefined>();
|
|
45
|
+
|
|
46
|
+
useEffect(() => {
|
|
47
|
+
const parsed = parseDateString(value);
|
|
48
|
+
if (parsed) {
|
|
49
|
+
setViewMonth({ year: parsed.year, month: parsed.month });
|
|
50
|
+
}
|
|
51
|
+
}, [value]);
|
|
52
|
+
|
|
53
|
+
useEffect(() => {
|
|
54
|
+
const parsed =
|
|
55
|
+
parseDateString(rangeValue?.start) ?? parseDateString(rangeValue?.end);
|
|
56
|
+
if (parsed) {
|
|
57
|
+
setViewMonth({ year: parsed.year, month: parsed.month });
|
|
58
|
+
}
|
|
59
|
+
}, [rangeValue?.end, rangeValue?.start]);
|
|
60
|
+
|
|
61
|
+
useEffect(() => {
|
|
62
|
+
if (rangeValue?.start && rangeValue?.end) {
|
|
63
|
+
setPendingStart(undefined);
|
|
64
|
+
setRangeHover(undefined);
|
|
65
|
+
}
|
|
66
|
+
}, [rangeValue?.end, rangeValue?.start]);
|
|
67
|
+
|
|
68
|
+
const monthLabel = useMemo(
|
|
69
|
+
() => formatMonthYear(viewMonth.year, viewMonth.month),
|
|
70
|
+
[viewMonth.month, viewMonth.year],
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
const handlePrevMonth = useCallback(() => {
|
|
74
|
+
setViewMonth(current => addMonths(current, -1));
|
|
75
|
+
}, []);
|
|
76
|
+
|
|
77
|
+
const handleNextMonth = useCallback(() => {
|
|
78
|
+
setViewMonth(current => addMonths(current, 1));
|
|
79
|
+
}, []);
|
|
80
|
+
|
|
81
|
+
const handleSelect = useCallback(
|
|
82
|
+
(nextValue: string) => {
|
|
83
|
+
if (mode === 'single') {
|
|
84
|
+
onChange?.(nextValue);
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const hasCompleteRange = Boolean(rangeValue?.start && rangeValue?.end);
|
|
89
|
+
const activeStart = pendingStart ?? rangeValue?.start;
|
|
90
|
+
|
|
91
|
+
if (!activeStart || hasCompleteRange) {
|
|
92
|
+
setPendingStart(nextValue);
|
|
93
|
+
setRangeHover(undefined);
|
|
94
|
+
onRangeChange?.({ start: nextValue, end: '' });
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const normalized = normalizeDateRange(activeStart, nextValue);
|
|
99
|
+
setPendingStart(undefined);
|
|
100
|
+
setRangeHover(undefined);
|
|
101
|
+
onRangeChange?.(normalized);
|
|
102
|
+
},
|
|
103
|
+
[
|
|
104
|
+
mode,
|
|
105
|
+
onChange,
|
|
106
|
+
onRangeChange,
|
|
107
|
+
pendingStart,
|
|
108
|
+
rangeValue?.end,
|
|
109
|
+
rangeValue?.start,
|
|
110
|
+
],
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
const activeRangeStart = pendingStart ?? rangeValue?.start;
|
|
114
|
+
const activeRangeEnd = rangeValue?.end;
|
|
115
|
+
|
|
116
|
+
return (
|
|
117
|
+
<div
|
|
118
|
+
className={cn(S.picker, S[`pickerSize-${size}`], className)}
|
|
119
|
+
data-slot="date-picker"
|
|
120
|
+
data-mode={mode}
|
|
121
|
+
>
|
|
122
|
+
<div className={S.pickerHeader}>
|
|
123
|
+
<Button
|
|
124
|
+
type="button"
|
|
125
|
+
variant="ghost"
|
|
126
|
+
size={size}
|
|
127
|
+
icon
|
|
128
|
+
aria-label="Previous month"
|
|
129
|
+
onClick={handlePrevMonth}
|
|
130
|
+
>
|
|
131
|
+
<CaretLeft size={16} />
|
|
132
|
+
</Button>
|
|
133
|
+
<div className={S.pickerMonthLabel}>{monthLabel}</div>
|
|
134
|
+
<Button
|
|
135
|
+
type="button"
|
|
136
|
+
variant="ghost"
|
|
137
|
+
size={size}
|
|
138
|
+
icon
|
|
139
|
+
aria-label="Next month"
|
|
140
|
+
onClick={handleNextMonth}
|
|
141
|
+
>
|
|
142
|
+
<CaretRight size={16} />
|
|
143
|
+
</Button>
|
|
144
|
+
</div>
|
|
145
|
+
<Calendar
|
|
146
|
+
mode={mode}
|
|
147
|
+
value={value}
|
|
148
|
+
rangeStart={activeRangeStart}
|
|
149
|
+
rangeEnd={activeRangeEnd}
|
|
150
|
+
rangeHover={rangeHover}
|
|
151
|
+
onDayHover={setRangeHover}
|
|
152
|
+
viewMonth={viewMonth}
|
|
153
|
+
onViewMonthChange={setViewMonth}
|
|
154
|
+
onSelect={handleSelect}
|
|
155
|
+
size={size}
|
|
156
|
+
{...calendarProps}
|
|
157
|
+
/>
|
|
158
|
+
</div>
|
|
159
|
+
);
|
|
160
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import type { DateRangeValue, MonthView } from './dateUtils';
|
|
2
|
+
|
|
3
|
+
export type DatePickerSize = 'sm' | 'md' | 'lg';
|
|
4
|
+
export type DatePickerMode = 'single' | 'range';
|
|
5
|
+
|
|
6
|
+
export type CalendarProps = {
|
|
7
|
+
className?: string;
|
|
8
|
+
mode?: DatePickerMode;
|
|
9
|
+
value?: string;
|
|
10
|
+
rangeStart?: string;
|
|
11
|
+
rangeEnd?: string;
|
|
12
|
+
rangeHover?: string;
|
|
13
|
+
viewMonth?: MonthView;
|
|
14
|
+
onViewMonthChange?: (viewMonth: MonthView) => void;
|
|
15
|
+
onSelect?: (value: string) => void;
|
|
16
|
+
onDayHover?: (value: string | undefined) => void;
|
|
17
|
+
startOfWeek?: 0 | 1;
|
|
18
|
+
minDate?: string;
|
|
19
|
+
maxDate?: string;
|
|
20
|
+
disabledDate?: (date: string) => boolean;
|
|
21
|
+
size?: DatePickerSize;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export type DatePickerProps = {
|
|
25
|
+
className?: string;
|
|
26
|
+
mode?: DatePickerMode;
|
|
27
|
+
value?: string;
|
|
28
|
+
onChange?: (value: string) => void;
|
|
29
|
+
rangeValue?: DateRangeValue | null;
|
|
30
|
+
onRangeChange?: (value: DateRangeValue | null) => void;
|
|
31
|
+
size?: DatePickerSize;
|
|
32
|
+
calendarProps?: Omit<
|
|
33
|
+
CalendarProps,
|
|
34
|
+
| 'value'
|
|
35
|
+
| 'onSelect'
|
|
36
|
+
| 'size'
|
|
37
|
+
| 'mode'
|
|
38
|
+
| 'rangeStart'
|
|
39
|
+
| 'rangeEnd'
|
|
40
|
+
| 'rangeHover'
|
|
41
|
+
| 'onDayHover'
|
|
42
|
+
>;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export type DateRangePickerInputProps = Omit<
|
|
46
|
+
React.ComponentProps<'input'>,
|
|
47
|
+
'type' | 'value' | 'defaultValue' | 'onChange' | 'size'
|
|
48
|
+
> & {
|
|
49
|
+
value?: DateRangeValue | null;
|
|
50
|
+
defaultValue?: DateRangeValue | null;
|
|
51
|
+
onChange?: (value: DateRangeValue | null) => void;
|
|
52
|
+
size?: DatePickerSize;
|
|
53
|
+
variant?: 'default' | 'clean';
|
|
54
|
+
startPlaceholder?: string;
|
|
55
|
+
endPlaceholder?: string;
|
|
56
|
+
minDate?: string;
|
|
57
|
+
maxDate?: string;
|
|
58
|
+
disabledDate?: (date: string) => boolean;
|
|
59
|
+
calendarProps?: Omit<
|
|
60
|
+
CalendarProps,
|
|
61
|
+
| 'value'
|
|
62
|
+
| 'onSelect'
|
|
63
|
+
| 'size'
|
|
64
|
+
| 'mode'
|
|
65
|
+
| 'rangeStart'
|
|
66
|
+
| 'rangeEnd'
|
|
67
|
+
| 'rangeHover'
|
|
68
|
+
| 'onDayHover'
|
|
69
|
+
>;
|
|
70
|
+
open?: boolean;
|
|
71
|
+
onOpenChange?: (open: boolean) => void;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
export type DatePickerInputProps = Omit<
|
|
75
|
+
React.ComponentProps<'input'>,
|
|
76
|
+
'type' | 'value' | 'defaultValue' | 'onChange' | 'size'
|
|
77
|
+
> & {
|
|
78
|
+
value?: string;
|
|
79
|
+
defaultValue?: string;
|
|
80
|
+
onChange?: (value: string | undefined) => void;
|
|
81
|
+
size?: DatePickerSize;
|
|
82
|
+
variant?: 'default' | 'clean';
|
|
83
|
+
placeholder?: string;
|
|
84
|
+
displayFormat?: string;
|
|
85
|
+
minDate?: string;
|
|
86
|
+
maxDate?: string;
|
|
87
|
+
disabledDate?: (date: string) => boolean;
|
|
88
|
+
calendarProps?: Omit<CalendarProps, 'value' | 'onSelect' | 'size'>;
|
|
89
|
+
open?: boolean;
|
|
90
|
+
onOpenChange?: (open: boolean) => void;
|
|
91
|
+
};
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import cn from 'classnames';
|
|
2
|
+
import { useCallback, useId, useState } from 'react';
|
|
3
|
+
|
|
4
|
+
import { CalendarBlank } from '@phosphor-icons/react';
|
|
5
|
+
|
|
6
|
+
import { Button } from '../Button';
|
|
7
|
+
import {
|
|
8
|
+
DropdownMenu,
|
|
9
|
+
DropdownMenuContent,
|
|
10
|
+
DropdownMenuTrigger,
|
|
11
|
+
} from '../DropdownMenu';
|
|
12
|
+
import { Input } from '../Input';
|
|
13
|
+
import { DatePicker } from './DatePicker';
|
|
14
|
+
import S from './DatePicker.styl';
|
|
15
|
+
import type { DatePickerInputProps } from './DatePicker.types';
|
|
16
|
+
import {
|
|
17
|
+
formatDateString,
|
|
18
|
+
formatDisplayDate,
|
|
19
|
+
parseDateString,
|
|
20
|
+
} from './dateUtils';
|
|
21
|
+
|
|
22
|
+
export function DatePickerInput({
|
|
23
|
+
className,
|
|
24
|
+
value: controlledValue,
|
|
25
|
+
defaultValue,
|
|
26
|
+
onChange,
|
|
27
|
+
size = 'md',
|
|
28
|
+
placeholder = 'YYYY-MM-DD',
|
|
29
|
+
disabled,
|
|
30
|
+
minDate,
|
|
31
|
+
maxDate,
|
|
32
|
+
disabledDate,
|
|
33
|
+
calendarProps,
|
|
34
|
+
open: controlledOpen,
|
|
35
|
+
onOpenChange,
|
|
36
|
+
id: idProp,
|
|
37
|
+
displayFormat: _displayFormat,
|
|
38
|
+
variant = 'default',
|
|
39
|
+
...inputProps
|
|
40
|
+
}: DatePickerInputProps) {
|
|
41
|
+
const generatedId = useId();
|
|
42
|
+
const inputId = idProp ?? generatedId;
|
|
43
|
+
const [uncontrolledValue, setUncontrolledValue] = useState(defaultValue);
|
|
44
|
+
const [uncontrolledOpen, setUncontrolledOpen] = useState(false);
|
|
45
|
+
const [draftValue, setDraftValue] = useState<string | undefined>();
|
|
46
|
+
|
|
47
|
+
const value = controlledValue ?? uncontrolledValue;
|
|
48
|
+
const open = controlledOpen ?? uncontrolledOpen;
|
|
49
|
+
|
|
50
|
+
const setOpen = useCallback(
|
|
51
|
+
(nextOpen: boolean) => {
|
|
52
|
+
if (controlledOpen === undefined) {
|
|
53
|
+
setUncontrolledOpen(nextOpen);
|
|
54
|
+
}
|
|
55
|
+
onOpenChange?.(nextOpen);
|
|
56
|
+
},
|
|
57
|
+
[controlledOpen, onOpenChange],
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
const commitValue = useCallback(
|
|
61
|
+
(nextValue: string | undefined) => {
|
|
62
|
+
if (controlledValue === undefined) {
|
|
63
|
+
setUncontrolledValue(nextValue);
|
|
64
|
+
}
|
|
65
|
+
onChange?.(nextValue);
|
|
66
|
+
},
|
|
67
|
+
[controlledValue, onChange],
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
const handleInputChange = useCallback(
|
|
71
|
+
(event: React.ChangeEvent<HTMLInputElement>) => {
|
|
72
|
+
const nextValue = event.target.value;
|
|
73
|
+
setDraftValue(nextValue);
|
|
74
|
+
if (!nextValue) {
|
|
75
|
+
commitValue(undefined);
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
if (parseDateString(nextValue)) {
|
|
79
|
+
commitValue(nextValue);
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
[commitValue],
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
const handleInputBlur = useCallback(() => {
|
|
86
|
+
if (draftValue === undefined) {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
const parsed = parseDateString(draftValue);
|
|
90
|
+
if (parsed) {
|
|
91
|
+
commitValue(formatDateString(parsed));
|
|
92
|
+
} else if (!draftValue) {
|
|
93
|
+
commitValue(undefined);
|
|
94
|
+
}
|
|
95
|
+
setDraftValue(undefined);
|
|
96
|
+
}, [commitValue, draftValue]);
|
|
97
|
+
|
|
98
|
+
const handleDateSelect = useCallback(
|
|
99
|
+
(nextValue: string) => {
|
|
100
|
+
commitValue(nextValue);
|
|
101
|
+
setDraftValue(undefined);
|
|
102
|
+
setOpen(false);
|
|
103
|
+
},
|
|
104
|
+
[commitValue, setOpen],
|
|
105
|
+
);
|
|
106
|
+
|
|
107
|
+
const displayValue = draftValue !== undefined ? draftValue : (value ?? '');
|
|
108
|
+
|
|
109
|
+
return (
|
|
110
|
+
<div
|
|
111
|
+
className={cn(S.inputRoot, S[`inputSize-${size}`], className)}
|
|
112
|
+
data-slot="date-picker-input"
|
|
113
|
+
>
|
|
114
|
+
<Input
|
|
115
|
+
{...inputProps}
|
|
116
|
+
type="text"
|
|
117
|
+
variant={variant}
|
|
118
|
+
id={inputId}
|
|
119
|
+
size={size}
|
|
120
|
+
value={displayValue}
|
|
121
|
+
placeholder={placeholder}
|
|
122
|
+
disabled={disabled}
|
|
123
|
+
onChange={handleInputChange}
|
|
124
|
+
onBlur={handleInputBlur}
|
|
125
|
+
onFocus={() => setOpen(true)}
|
|
126
|
+
className={S.inputField}
|
|
127
|
+
autoComplete="off"
|
|
128
|
+
inputMode="numeric"
|
|
129
|
+
/>
|
|
130
|
+
<DropdownMenu open={open} onOpenChange={setOpen} modal={false}>
|
|
131
|
+
<DropdownMenuTrigger asChild>
|
|
132
|
+
<Button
|
|
133
|
+
type="button"
|
|
134
|
+
variant="ghost"
|
|
135
|
+
size="sm"
|
|
136
|
+
icon
|
|
137
|
+
disabled={disabled}
|
|
138
|
+
className={S.calendarTrigger}
|
|
139
|
+
aria-label="Open calendar"
|
|
140
|
+
onClick={() => setOpen(true)}
|
|
141
|
+
>
|
|
142
|
+
<CalendarBlank size={16} />
|
|
143
|
+
</Button>
|
|
144
|
+
</DropdownMenuTrigger>
|
|
145
|
+
<DropdownMenuContent
|
|
146
|
+
align="end"
|
|
147
|
+
sideOffset={4}
|
|
148
|
+
className={cn(S.popupContent, size === 'sm' && S['popupContent-sm'])}
|
|
149
|
+
onCloseAutoFocus={event => event.preventDefault()}
|
|
150
|
+
>
|
|
151
|
+
<DatePicker
|
|
152
|
+
value={value}
|
|
153
|
+
onChange={handleDateSelect}
|
|
154
|
+
size={size}
|
|
155
|
+
calendarProps={{
|
|
156
|
+
minDate,
|
|
157
|
+
maxDate,
|
|
158
|
+
disabledDate,
|
|
159
|
+
...calendarProps,
|
|
160
|
+
}}
|
|
161
|
+
/>
|
|
162
|
+
</DropdownMenuContent>
|
|
163
|
+
</DropdownMenu>
|
|
164
|
+
<span className={S.srOnly} aria-live="polite">
|
|
165
|
+
{value ? formatDisplayDate(value) : ''}
|
|
166
|
+
</span>
|
|
167
|
+
</div>
|
|
168
|
+
);
|
|
169
|
+
}
|