@bwui/react 0.0.0-stage → 0.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/LICENSE +21 -0
- package/README.md +36 -2
- package/dist/alert.d.ts +8 -0
- package/dist/alert.js +11 -0
- package/dist/badge.d.ts +6 -0
- package/dist/badge.js +5 -0
- package/dist/button.d.ts +16 -0
- package/dist/button.js +12 -0
- package/dist/card.d.ts +9 -0
- package/dist/card.js +20 -0
- package/dist/chrome.d.ts +59 -0
- package/dist/chrome.js +75 -0
- package/dist/cn.d.ts +6 -0
- package/dist/cn.js +8 -0
- package/dist/combobox.d.ts +20 -0
- package/dist/combobox.js +70 -0
- package/dist/command.d.ts +16 -0
- package/dist/command.js +74 -0
- package/dist/data-table.d.ts +17 -0
- package/dist/data-table.js +17 -0
- package/dist/date-picker.d.ts +30 -0
- package/dist/date-picker.js +146 -0
- package/dist/form.d.ts +31 -0
- package/dist/form.js +55 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +17 -0
- package/dist/kbd.d.ts +2 -0
- package/dist/kbd.js +5 -0
- package/dist/layout.d.ts +10 -0
- package/dist/layout.js +23 -0
- package/dist/modal.d.ts +11 -0
- package/dist/modal.js +18 -0
- package/dist/pages.d.ts +42 -0
- package/dist/pages.js +84 -0
- package/dist/toast.d.ts +18 -0
- package/dist/toast.js +24 -0
- package/package.json +43 -3
- package/src/alert.tsx +36 -0
- package/src/badge.tsx +24 -0
- package/src/button.tsx +66 -0
- package/src/card.tsx +34 -0
- package/src/chrome.tsx +263 -0
- package/src/cn.ts +8 -0
- package/src/combobox.tsx +162 -0
- package/src/command.tsx +145 -0
- package/src/data-table.tsx +114 -0
- package/src/date-picker.tsx +357 -0
- package/src/form.tsx +201 -0
- package/src/index.ts +90 -0
- package/src/kbd.tsx +9 -0
- package/src/layout.tsx +44 -0
- package/src/modal.tsx +70 -0
- package/src/pages.tsx +361 -0
- package/src/toast.tsx +84 -0
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import * as React from 'react'
|
|
4
|
+
import { cn } from './cn'
|
|
5
|
+
|
|
6
|
+
export type DataTableColumn<T> = {
|
|
7
|
+
id: string
|
|
8
|
+
header: React.ReactNode
|
|
9
|
+
cell: (row: T) => React.ReactNode
|
|
10
|
+
align?: 'start' | 'center' | 'end'
|
|
11
|
+
sortable?: boolean
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export type DataTableProps<T> = {
|
|
15
|
+
columns: DataTableColumn<T>[]
|
|
16
|
+
rows: T[]
|
|
17
|
+
getRowId: (row: T) => string
|
|
18
|
+
selectedIds?: string[]
|
|
19
|
+
onSelectionChange?: (ids: string[]) => void
|
|
20
|
+
className?: string
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function DataTable<T>({
|
|
24
|
+
columns,
|
|
25
|
+
rows,
|
|
26
|
+
getRowId,
|
|
27
|
+
selectedIds = [],
|
|
28
|
+
onSelectionChange,
|
|
29
|
+
className,
|
|
30
|
+
}: DataTableProps<T>) {
|
|
31
|
+
const allSelected = rows.length > 0 && rows.every((r) => selectedIds.includes(getRowId(r)))
|
|
32
|
+
|
|
33
|
+
return (
|
|
34
|
+
<div className={cn('bw-data-table-wrap bw-data-table-wrap--sticky', className)} tabIndex={0}>
|
|
35
|
+
<table className="bw-data-table">
|
|
36
|
+
<thead>
|
|
37
|
+
<tr className="bw-data-table__row">
|
|
38
|
+
{onSelectionChange ? (
|
|
39
|
+
<th className="bw-data-table__th bw-data-table__th--select">
|
|
40
|
+
<input
|
|
41
|
+
type="checkbox"
|
|
42
|
+
aria-label="Select all"
|
|
43
|
+
checked={allSelected}
|
|
44
|
+
onChange={(e) => {
|
|
45
|
+
onSelectionChange(e.target.checked ? rows.map(getRowId) : [])
|
|
46
|
+
}}
|
|
47
|
+
/>
|
|
48
|
+
</th>
|
|
49
|
+
) : null}
|
|
50
|
+
{columns.map((col) => (
|
|
51
|
+
<th
|
|
52
|
+
key={col.id}
|
|
53
|
+
className={cn(
|
|
54
|
+
'bw-data-table__th',
|
|
55
|
+
col.align === 'end' && 'bw-data-table__th--end',
|
|
56
|
+
col.align === 'center' && 'bw-data-table__th--center',
|
|
57
|
+
)}
|
|
58
|
+
>
|
|
59
|
+
{col.sortable ? (
|
|
60
|
+
<button type="button" className="bw-data-table__sort">
|
|
61
|
+
{col.header} <span className="bw-data-table__sort-idle">v</span>
|
|
62
|
+
</button>
|
|
63
|
+
) : (
|
|
64
|
+
col.header
|
|
65
|
+
)}
|
|
66
|
+
</th>
|
|
67
|
+
))}
|
|
68
|
+
</tr>
|
|
69
|
+
</thead>
|
|
70
|
+
<tbody>
|
|
71
|
+
{rows.map((row) => {
|
|
72
|
+
const id = getRowId(row)
|
|
73
|
+
const selected = selectedIds.includes(id)
|
|
74
|
+
return (
|
|
75
|
+
<tr
|
|
76
|
+
key={id}
|
|
77
|
+
className={cn('bw-data-table__row', selected && 'bw-data-table__row--selected')}
|
|
78
|
+
>
|
|
79
|
+
{onSelectionChange ? (
|
|
80
|
+
<td className="bw-data-table__td bw-data-table__td--select">
|
|
81
|
+
<input
|
|
82
|
+
type="checkbox"
|
|
83
|
+
aria-label={`Select ${id}`}
|
|
84
|
+
checked={selected}
|
|
85
|
+
onChange={(e) => {
|
|
86
|
+
onSelectionChange(
|
|
87
|
+
e.target.checked
|
|
88
|
+
? [...selectedIds, id]
|
|
89
|
+
: selectedIds.filter((x) => x !== id),
|
|
90
|
+
)
|
|
91
|
+
}}
|
|
92
|
+
/>
|
|
93
|
+
</td>
|
|
94
|
+
) : null}
|
|
95
|
+
{columns.map((col) => (
|
|
96
|
+
<td
|
|
97
|
+
key={col.id}
|
|
98
|
+
className={cn(
|
|
99
|
+
'bw-data-table__td',
|
|
100
|
+
col.align === 'end' && 'bw-data-table__td--end',
|
|
101
|
+
col.align === 'center' && 'bw-data-table__td--center',
|
|
102
|
+
)}
|
|
103
|
+
>
|
|
104
|
+
{col.cell(row)}
|
|
105
|
+
</td>
|
|
106
|
+
))}
|
|
107
|
+
</tr>
|
|
108
|
+
)
|
|
109
|
+
})}
|
|
110
|
+
</tbody>
|
|
111
|
+
</table>
|
|
112
|
+
</div>
|
|
113
|
+
)
|
|
114
|
+
}
|
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import * as React from 'react'
|
|
4
|
+
import { cn } from './cn'
|
|
5
|
+
import { Input } from './form'
|
|
6
|
+
|
|
7
|
+
const MONTHS = [
|
|
8
|
+
'January',
|
|
9
|
+
'February',
|
|
10
|
+
'March',
|
|
11
|
+
'April',
|
|
12
|
+
'May',
|
|
13
|
+
'June',
|
|
14
|
+
'July',
|
|
15
|
+
'August',
|
|
16
|
+
'September',
|
|
17
|
+
'October',
|
|
18
|
+
'November',
|
|
19
|
+
'December',
|
|
20
|
+
]
|
|
21
|
+
const DOW = ['Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su']
|
|
22
|
+
|
|
23
|
+
function parseISO(s: string | undefined): Date | null {
|
|
24
|
+
if (!s) return null
|
|
25
|
+
const m = /^(\d{4})-(\d{2})-(\d{2})$/.exec(s)
|
|
26
|
+
if (!m) return null
|
|
27
|
+
return new Date(Number(m[1]), Number(m[2]) - 1, Number(m[3]))
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function toISO(d: Date): string {
|
|
31
|
+
const y = d.getFullYear()
|
|
32
|
+
const m = String(d.getMonth() + 1).padStart(2, '0')
|
|
33
|
+
const day = String(d.getDate()).padStart(2, '0')
|
|
34
|
+
return `${y}-${m}-${day}`
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function sameDay(a: Date | null | undefined, b: Date | null | undefined): boolean {
|
|
38
|
+
return Boolean(
|
|
39
|
+
a &&
|
|
40
|
+
b &&
|
|
41
|
+
a.getFullYear() === b.getFullYear() &&
|
|
42
|
+
a.getMonth() === b.getMonth() &&
|
|
43
|
+
a.getDate() === b.getDate(),
|
|
44
|
+
)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function daysFor(view: Date): Array<Date | null> {
|
|
48
|
+
const first = new Date(view.getFullYear(), view.getMonth(), 1)
|
|
49
|
+
const startDow = (first.getDay() + 6) % 7
|
|
50
|
+
const count = new Date(view.getFullYear(), view.getMonth() + 1, 0).getDate()
|
|
51
|
+
const cells: Array<Date | null> = Array.from({ length: startDow }, () => null)
|
|
52
|
+
for (let day = 1; day <= count; day += 1) {
|
|
53
|
+
cells.push(new Date(view.getFullYear(), view.getMonth(), day))
|
|
54
|
+
}
|
|
55
|
+
return cells
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
type CalendarProps = {
|
|
59
|
+
view: Date
|
|
60
|
+
onViewChange: (view: Date) => void
|
|
61
|
+
selected?: Date | null
|
|
62
|
+
rangeStart?: Date | null
|
|
63
|
+
rangeEnd?: Date | null
|
|
64
|
+
onSelect: (date: Date) => void
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function Calendar({
|
|
68
|
+
view,
|
|
69
|
+
onViewChange,
|
|
70
|
+
selected,
|
|
71
|
+
rangeStart,
|
|
72
|
+
rangeEnd,
|
|
73
|
+
onSelect,
|
|
74
|
+
}: CalendarProps) {
|
|
75
|
+
return (
|
|
76
|
+
<div className="bw-calendar">
|
|
77
|
+
<div className="bw-calendar__nav">
|
|
78
|
+
<button
|
|
79
|
+
type="button"
|
|
80
|
+
className="bw-btn bw-btn--ghost bw-btn--sm"
|
|
81
|
+
data-bw-cal-prev=""
|
|
82
|
+
aria-label="Previous month"
|
|
83
|
+
onClick={() =>
|
|
84
|
+
onViewChange(new Date(view.getFullYear(), view.getMonth() - 1, 1))
|
|
85
|
+
}
|
|
86
|
+
>
|
|
87
|
+
Prev
|
|
88
|
+
</button>
|
|
89
|
+
<p className="bw-calendar__title">
|
|
90
|
+
{MONTHS[view.getMonth()]} {view.getFullYear()}
|
|
91
|
+
</p>
|
|
92
|
+
<button
|
|
93
|
+
type="button"
|
|
94
|
+
className="bw-btn bw-btn--ghost bw-btn--sm"
|
|
95
|
+
data-bw-cal-next=""
|
|
96
|
+
aria-label="Next month"
|
|
97
|
+
onClick={() =>
|
|
98
|
+
onViewChange(new Date(view.getFullYear(), view.getMonth() + 1, 1))
|
|
99
|
+
}
|
|
100
|
+
>
|
|
101
|
+
Next
|
|
102
|
+
</button>
|
|
103
|
+
</div>
|
|
104
|
+
<div className="bw-calendar__grid">
|
|
105
|
+
{DOW.map((d) => (
|
|
106
|
+
<div key={d} className="bw-calendar__dow">
|
|
107
|
+
{d}
|
|
108
|
+
</div>
|
|
109
|
+
))}
|
|
110
|
+
{daysFor(view).map((date, i) => {
|
|
111
|
+
if (!date) return <div key={`blank-${i}`} />
|
|
112
|
+
const inRange = Boolean(
|
|
113
|
+
rangeStart &&
|
|
114
|
+
rangeEnd &&
|
|
115
|
+
date > rangeStart &&
|
|
116
|
+
date < rangeEnd,
|
|
117
|
+
)
|
|
118
|
+
const isSelected =
|
|
119
|
+
sameDay(date, selected) ||
|
|
120
|
+
sameDay(date, rangeStart ?? null) ||
|
|
121
|
+
sameDay(date, rangeEnd ?? null)
|
|
122
|
+
return (
|
|
123
|
+
<button
|
|
124
|
+
key={toISO(date)}
|
|
125
|
+
type="button"
|
|
126
|
+
className="bw-calendar__day"
|
|
127
|
+
aria-label={toISO(date)}
|
|
128
|
+
aria-selected={isSelected}
|
|
129
|
+
{...(inRange ? { 'data-bw-in-range': '' } : {})}
|
|
130
|
+
onClick={() => onSelect(date)}
|
|
131
|
+
>
|
|
132
|
+
{date.getDate()}
|
|
133
|
+
</button>
|
|
134
|
+
)
|
|
135
|
+
})}
|
|
136
|
+
</div>
|
|
137
|
+
</div>
|
|
138
|
+
)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export type DatePickerProps = {
|
|
142
|
+
id?: string
|
|
143
|
+
name?: string
|
|
144
|
+
value?: string
|
|
145
|
+
defaultValue?: string
|
|
146
|
+
onValueChange?: (value: string) => void
|
|
147
|
+
placeholder?: string
|
|
148
|
+
disabled?: boolean
|
|
149
|
+
required?: boolean
|
|
150
|
+
className?: string
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export function DatePicker({
|
|
154
|
+
id,
|
|
155
|
+
name,
|
|
156
|
+
value,
|
|
157
|
+
defaultValue = '',
|
|
158
|
+
onValueChange,
|
|
159
|
+
placeholder = 'YYYY-MM-DD',
|
|
160
|
+
disabled,
|
|
161
|
+
required,
|
|
162
|
+
className,
|
|
163
|
+
}: DatePickerProps) {
|
|
164
|
+
const [open, setOpen] = React.useState(false)
|
|
165
|
+
const [uncontrolled, setUncontrolled] = React.useState(defaultValue)
|
|
166
|
+
const selected = value ?? uncontrolled
|
|
167
|
+
const parsed = parseISO(selected) || new Date()
|
|
168
|
+
const [view, setView] = React.useState(
|
|
169
|
+
new Date(parsed.getFullYear(), parsed.getMonth(), 1),
|
|
170
|
+
)
|
|
171
|
+
const rootRef = React.useRef<HTMLDivElement>(null)
|
|
172
|
+
|
|
173
|
+
React.useEffect(() => {
|
|
174
|
+
function onPointer(e: PointerEvent) {
|
|
175
|
+
if (!rootRef.current?.contains(e.target as Node)) setOpen(false)
|
|
176
|
+
}
|
|
177
|
+
function onKey(e: KeyboardEvent) {
|
|
178
|
+
if (e.key === 'Escape') setOpen(false)
|
|
179
|
+
}
|
|
180
|
+
document.addEventListener('pointerdown', onPointer)
|
|
181
|
+
document.addEventListener('keydown', onKey)
|
|
182
|
+
return () => {
|
|
183
|
+
document.removeEventListener('pointerdown', onPointer)
|
|
184
|
+
document.removeEventListener('keydown', onKey)
|
|
185
|
+
}
|
|
186
|
+
}, [])
|
|
187
|
+
|
|
188
|
+
function commit(next: string) {
|
|
189
|
+
if (value === undefined) setUncontrolled(next)
|
|
190
|
+
onValueChange?.(next)
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
return (
|
|
194
|
+
<div ref={rootRef} className={cn('bw-date-picker-chrome', className)}>
|
|
195
|
+
<div className="bw-date-picker-chrome__field">
|
|
196
|
+
<Input
|
|
197
|
+
id={id}
|
|
198
|
+
name={name}
|
|
199
|
+
value={selected}
|
|
200
|
+
required={required}
|
|
201
|
+
disabled={disabled}
|
|
202
|
+
placeholder={placeholder}
|
|
203
|
+
autoComplete="off"
|
|
204
|
+
data-bw-date-input=""
|
|
205
|
+
onChange={(e) => commit(e.target.value)}
|
|
206
|
+
onFocus={() => setOpen(true)}
|
|
207
|
+
/>
|
|
208
|
+
<button
|
|
209
|
+
type="button"
|
|
210
|
+
className="bw-date-picker-chrome__trigger"
|
|
211
|
+
aria-label="Open calendar"
|
|
212
|
+
disabled={disabled}
|
|
213
|
+
onClick={() => setOpen((v) => !v)}
|
|
214
|
+
>
|
|
215
|
+
Cal
|
|
216
|
+
</button>
|
|
217
|
+
</div>
|
|
218
|
+
<div className="bw-date-picker-chrome__panel" hidden={!open}>
|
|
219
|
+
<Calendar
|
|
220
|
+
view={view}
|
|
221
|
+
onViewChange={setView}
|
|
222
|
+
selected={parseISO(selected)}
|
|
223
|
+
onSelect={(date) => {
|
|
224
|
+
commit(toISO(date))
|
|
225
|
+
setOpen(false)
|
|
226
|
+
}}
|
|
227
|
+
/>
|
|
228
|
+
</div>
|
|
229
|
+
</div>
|
|
230
|
+
)
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
export type DateRangePickerProps = {
|
|
234
|
+
startId?: string
|
|
235
|
+
endId?: string
|
|
236
|
+
startName?: string
|
|
237
|
+
endName?: string
|
|
238
|
+
startValue?: string
|
|
239
|
+
endValue?: string
|
|
240
|
+
defaultStartValue?: string
|
|
241
|
+
defaultEndValue?: string
|
|
242
|
+
onValueChange?: (range: { start: string; end: string }) => void
|
|
243
|
+
disabled?: boolean
|
|
244
|
+
className?: string
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
export function DateRangePicker({
|
|
248
|
+
startId,
|
|
249
|
+
endId,
|
|
250
|
+
startName,
|
|
251
|
+
endName,
|
|
252
|
+
startValue,
|
|
253
|
+
endValue,
|
|
254
|
+
defaultStartValue = '',
|
|
255
|
+
defaultEndValue = '',
|
|
256
|
+
onValueChange,
|
|
257
|
+
disabled,
|
|
258
|
+
className,
|
|
259
|
+
}: DateRangePickerProps) {
|
|
260
|
+
const [open, setOpen] = React.useState(false)
|
|
261
|
+
const [start, setStart] = React.useState(defaultStartValue)
|
|
262
|
+
const [end, setEnd] = React.useState(defaultEndValue)
|
|
263
|
+
const startSelected = startValue ?? start
|
|
264
|
+
const endSelected = endValue ?? end
|
|
265
|
+
const parsed = parseISO(startSelected) || new Date()
|
|
266
|
+
const [view, setView] = React.useState(
|
|
267
|
+
new Date(parsed.getFullYear(), parsed.getMonth(), 1),
|
|
268
|
+
)
|
|
269
|
+
const rootRef = React.useRef<HTMLDivElement>(null)
|
|
270
|
+
|
|
271
|
+
React.useEffect(() => {
|
|
272
|
+
function onPointer(e: PointerEvent) {
|
|
273
|
+
if (!rootRef.current?.contains(e.target as Node)) setOpen(false)
|
|
274
|
+
}
|
|
275
|
+
function onKey(e: KeyboardEvent) {
|
|
276
|
+
if (e.key === 'Escape') setOpen(false)
|
|
277
|
+
}
|
|
278
|
+
document.addEventListener('pointerdown', onPointer)
|
|
279
|
+
document.addEventListener('keydown', onKey)
|
|
280
|
+
return () => {
|
|
281
|
+
document.removeEventListener('pointerdown', onPointer)
|
|
282
|
+
document.removeEventListener('keydown', onKey)
|
|
283
|
+
}
|
|
284
|
+
}, [])
|
|
285
|
+
|
|
286
|
+
function emit(nextStart: string, nextEnd: string) {
|
|
287
|
+
if (startValue === undefined) setStart(nextStart)
|
|
288
|
+
if (endValue === undefined) setEnd(nextEnd)
|
|
289
|
+
onValueChange?.({ start: nextStart, end: nextEnd })
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
return (
|
|
293
|
+
<div
|
|
294
|
+
ref={rootRef}
|
|
295
|
+
className={cn('bw-date-picker-chrome', className)}
|
|
296
|
+
data-bw-range="true"
|
|
297
|
+
>
|
|
298
|
+
<div className="bw-date-picker-chrome__range">
|
|
299
|
+
<Input
|
|
300
|
+
id={startId}
|
|
301
|
+
name={startName}
|
|
302
|
+
value={startSelected}
|
|
303
|
+
disabled={disabled}
|
|
304
|
+
placeholder="From"
|
|
305
|
+
autoComplete="off"
|
|
306
|
+
data-bw-date-input=""
|
|
307
|
+
onChange={(e) => emit(e.target.value, endSelected)}
|
|
308
|
+
onFocus={() => setOpen(true)}
|
|
309
|
+
/>
|
|
310
|
+
<span className="bw-date-picker-chrome__separator">to</span>
|
|
311
|
+
<Input
|
|
312
|
+
id={endId}
|
|
313
|
+
name={endName}
|
|
314
|
+
value={endSelected}
|
|
315
|
+
disabled={disabled}
|
|
316
|
+
placeholder="To"
|
|
317
|
+
autoComplete="off"
|
|
318
|
+
data-bw-date-end=""
|
|
319
|
+
onChange={(e) => emit(startSelected, e.target.value)}
|
|
320
|
+
onFocus={() => setOpen(true)}
|
|
321
|
+
/>
|
|
322
|
+
<button
|
|
323
|
+
type="button"
|
|
324
|
+
className="bw-date-picker-chrome__trigger"
|
|
325
|
+
aria-label="Open range calendar"
|
|
326
|
+
disabled={disabled}
|
|
327
|
+
onClick={() => setOpen((v) => !v)}
|
|
328
|
+
>
|
|
329
|
+
Cal
|
|
330
|
+
</button>
|
|
331
|
+
</div>
|
|
332
|
+
<div className="bw-date-picker-chrome__panel" hidden={!open}>
|
|
333
|
+
<Calendar
|
|
334
|
+
view={view}
|
|
335
|
+
onViewChange={setView}
|
|
336
|
+
rangeStart={parseISO(startSelected)}
|
|
337
|
+
rangeEnd={parseISO(endSelected)}
|
|
338
|
+
onSelect={(date) => {
|
|
339
|
+
const currentStart = parseISO(startSelected)
|
|
340
|
+
const currentEnd = parseISO(endSelected)
|
|
341
|
+
if (!currentStart || currentEnd) {
|
|
342
|
+
emit(toISO(date), '')
|
|
343
|
+
return
|
|
344
|
+
}
|
|
345
|
+
if (date < currentStart) {
|
|
346
|
+
emit(toISO(date), toISO(currentStart))
|
|
347
|
+
setOpen(false)
|
|
348
|
+
return
|
|
349
|
+
}
|
|
350
|
+
emit(toISO(currentStart), toISO(date))
|
|
351
|
+
setOpen(false)
|
|
352
|
+
}}
|
|
353
|
+
/>
|
|
354
|
+
</div>
|
|
355
|
+
</div>
|
|
356
|
+
)
|
|
357
|
+
}
|
package/src/form.tsx
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import * as React from 'react'
|
|
4
|
+
import { cn } from './cn'
|
|
5
|
+
|
|
6
|
+
export const Input = React.forwardRef<
|
|
7
|
+
HTMLInputElement,
|
|
8
|
+
React.InputHTMLAttributes<HTMLInputElement>
|
|
9
|
+
>(function Input({ className, ...props }, ref) {
|
|
10
|
+
return <input ref={ref} className={cn('bw-input', className)} {...props} />
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
export const Textarea = React.forwardRef<
|
|
14
|
+
HTMLTextAreaElement,
|
|
15
|
+
React.TextareaHTMLAttributes<HTMLTextAreaElement>
|
|
16
|
+
>(function Textarea({ className, ...props }, ref) {
|
|
17
|
+
return (
|
|
18
|
+
<textarea ref={ref} className={cn('bw-textarea', className)} {...props} />
|
|
19
|
+
)
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
export const Select = React.forwardRef<
|
|
23
|
+
HTMLSelectElement,
|
|
24
|
+
React.SelectHTMLAttributes<HTMLSelectElement>
|
|
25
|
+
>(function Select({ className, ...props }, ref) {
|
|
26
|
+
return <select ref={ref} className={cn('bw-select', className)} {...props} />
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
export const Form = React.forwardRef<
|
|
30
|
+
HTMLFormElement,
|
|
31
|
+
React.FormHTMLAttributes<HTMLFormElement>
|
|
32
|
+
>(function Form({ className, ...props }, ref) {
|
|
33
|
+
return <form ref={ref} className={cn('bw-form', 'bw-stack', className)} {...props} />
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
export function Field({
|
|
37
|
+
className,
|
|
38
|
+
...props
|
|
39
|
+
}: React.HTMLAttributes<HTMLDivElement>) {
|
|
40
|
+
return <div className={cn('bw-field', className)} {...props} />
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function Label({
|
|
44
|
+
className,
|
|
45
|
+
...props
|
|
46
|
+
}: React.LabelHTMLAttributes<HTMLLabelElement>) {
|
|
47
|
+
return <label className={cn('bw-field__label', className)} {...props} />
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function Hint({
|
|
51
|
+
className,
|
|
52
|
+
...props
|
|
53
|
+
}: React.HTMLAttributes<HTMLParagraphElement>) {
|
|
54
|
+
return <p className={cn('bw-field__hint', className)} {...props} />
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function FieldError({
|
|
58
|
+
className,
|
|
59
|
+
...props
|
|
60
|
+
}: React.HTMLAttributes<HTMLParagraphElement>) {
|
|
61
|
+
return <p className={cn('bw-field__error', className)} {...props} />
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function FormErrors({
|
|
65
|
+
title = 'Please fix the following',
|
|
66
|
+
errors,
|
|
67
|
+
className,
|
|
68
|
+
}: {
|
|
69
|
+
title?: string
|
|
70
|
+
errors: string[]
|
|
71
|
+
className?: string
|
|
72
|
+
}) {
|
|
73
|
+
if (errors.length === 0) return null
|
|
74
|
+
return (
|
|
75
|
+
<div className={cn('bw-form-errors', className)} role="alert">
|
|
76
|
+
<p className="bw-form-errors__title">{title}</p>
|
|
77
|
+
<ul className="bw-form-errors__list">
|
|
78
|
+
{errors.map((item) => (
|
|
79
|
+
<li key={item}>{item}</li>
|
|
80
|
+
))}
|
|
81
|
+
</ul>
|
|
82
|
+
</div>
|
|
83
|
+
)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function FormActions({
|
|
87
|
+
className,
|
|
88
|
+
...props
|
|
89
|
+
}: React.HTMLAttributes<HTMLDivElement>) {
|
|
90
|
+
return <div className={cn('bw-form__actions', 'bw-cluster', className)} {...props} />
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export interface SwitchProps
|
|
94
|
+
extends Omit<React.InputHTMLAttributes<HTMLInputElement>, 'type'> {
|
|
95
|
+
className?: string
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export const Switch = React.forwardRef<HTMLInputElement, SwitchProps>(
|
|
99
|
+
function Switch({ className, ...props }, ref) {
|
|
100
|
+
return (
|
|
101
|
+
<span className={cn('bw-switch', className)}>
|
|
102
|
+
<input ref={ref} type="checkbox" {...props} />
|
|
103
|
+
<span className="bw-switch__track" aria-hidden="true" />
|
|
104
|
+
<span className="bw-switch__thumb" aria-hidden="true" />
|
|
105
|
+
</span>
|
|
106
|
+
)
|
|
107
|
+
},
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
export const Checkbox = React.forwardRef<
|
|
111
|
+
HTMLInputElement,
|
|
112
|
+
Omit<React.InputHTMLAttributes<HTMLInputElement>, 'type'>
|
|
113
|
+
>(function Checkbox({ className, ...props }, ref) {
|
|
114
|
+
return (
|
|
115
|
+
<input
|
|
116
|
+
ref={ref}
|
|
117
|
+
type="checkbox"
|
|
118
|
+
className={cn('bw-checkbox', className)}
|
|
119
|
+
{...props}
|
|
120
|
+
/>
|
|
121
|
+
)
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
export const Radio = React.forwardRef<
|
|
125
|
+
HTMLInputElement,
|
|
126
|
+
Omit<React.InputHTMLAttributes<HTMLInputElement>, 'type'>
|
|
127
|
+
>(function Radio({ className, ...props }, ref) {
|
|
128
|
+
return (
|
|
129
|
+
<input
|
|
130
|
+
ref={ref}
|
|
131
|
+
type="radio"
|
|
132
|
+
className={cn('bw-radio', className)}
|
|
133
|
+
{...props}
|
|
134
|
+
/>
|
|
135
|
+
)
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
export const TimeInput = React.forwardRef<
|
|
139
|
+
HTMLInputElement,
|
|
140
|
+
Omit<React.InputHTMLAttributes<HTMLInputElement>, 'type'>
|
|
141
|
+
>(function TimeInput({ className, ...props }, ref) {
|
|
142
|
+
return (
|
|
143
|
+
<input
|
|
144
|
+
ref={ref}
|
|
145
|
+
type="time"
|
|
146
|
+
className={cn('bw-input', className)}
|
|
147
|
+
{...props}
|
|
148
|
+
/>
|
|
149
|
+
)
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
export const DateTimeInput = React.forwardRef<
|
|
153
|
+
HTMLInputElement,
|
|
154
|
+
Omit<React.InputHTMLAttributes<HTMLInputElement>, 'type'>
|
|
155
|
+
>(function DateTimeInput({ className, ...props }, ref) {
|
|
156
|
+
return (
|
|
157
|
+
<input
|
|
158
|
+
ref={ref}
|
|
159
|
+
type="datetime-local"
|
|
160
|
+
className={cn('bw-input', className)}
|
|
161
|
+
{...props}
|
|
162
|
+
/>
|
|
163
|
+
)
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
export type PasswordFieldProps = Omit<
|
|
167
|
+
React.InputHTMLAttributes<HTMLInputElement>,
|
|
168
|
+
'type'
|
|
169
|
+
> & {
|
|
170
|
+
revealLabel?: string
|
|
171
|
+
hideLabel?: string
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export const PasswordField = React.forwardRef<
|
|
175
|
+
HTMLInputElement,
|
|
176
|
+
PasswordFieldProps
|
|
177
|
+
>(function PasswordField(
|
|
178
|
+
{ className, revealLabel = 'Show', hideLabel = 'Hide', id, ...props },
|
|
179
|
+
ref,
|
|
180
|
+
) {
|
|
181
|
+
const [visible, setVisible] = React.useState(false)
|
|
182
|
+
return (
|
|
183
|
+
<div className="bw-password">
|
|
184
|
+
<input
|
|
185
|
+
ref={ref}
|
|
186
|
+
id={id}
|
|
187
|
+
type={visible ? 'text' : 'password'}
|
|
188
|
+
className={cn('bw-input', className)}
|
|
189
|
+
{...props}
|
|
190
|
+
/>
|
|
191
|
+
<button
|
|
192
|
+
type="button"
|
|
193
|
+
className="bw-password__toggle"
|
|
194
|
+
aria-pressed={visible}
|
|
195
|
+
onClick={() => setVisible((v) => !v)}
|
|
196
|
+
>
|
|
197
|
+
{visible ? hideLabel : revealLabel}
|
|
198
|
+
</button>
|
|
199
|
+
</div>
|
|
200
|
+
)
|
|
201
|
+
})
|