@cognitap/radiant 0.0.82 → 0.0.84
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/CHANGELOG.md +33 -0
- package/dist/library/color/colorValue.d.ts +8 -0
- package/dist/library/color/colorValue.d.ts.map +1 -0
- package/dist/library/color/colorValue.js +87 -0
- package/dist/library/color/colorValue.js.map +1 -0
- package/dist/library/color/oklch.d.ts +2 -0
- package/dist/library/color/oklch.d.ts.map +1 -1
- package/dist/library/color/oklch.js +27 -11
- package/dist/library/color/oklch.js.map +1 -1
- package/dist/library/components/Cascader/Cascader.d.ts +25 -0
- package/dist/library/components/Cascader/Cascader.d.ts.map +1 -0
- package/dist/library/components/Cascader/Cascader.js +154 -0
- package/dist/library/components/Cascader/Cascader.js.map +1 -0
- package/dist/library/components/Cascader/CascaderColumn.d.ts +25 -0
- package/dist/library/components/Cascader/CascaderColumn.d.ts.map +1 -0
- package/dist/library/components/Cascader/CascaderColumn.js +80 -0
- package/dist/library/components/Cascader/CascaderColumn.js.map +1 -0
- package/dist/library/components/ColorInput/ColorInput.d.ts +22 -0
- package/dist/library/components/ColorInput/ColorInput.d.ts.map +1 -0
- package/dist/library/components/ColorInput/ColorInput.js +63 -0
- package/dist/library/components/ColorInput/ColorInput.js.map +1 -0
- package/dist/library/components/ColorPicker/ColorArea.d.ts +11 -0
- package/dist/library/components/ColorPicker/ColorArea.d.ts.map +1 -0
- package/dist/library/components/ColorPicker/ColorArea.js +58 -0
- package/dist/library/components/ColorPicker/ColorArea.js.map +1 -0
- package/dist/library/components/ColorPicker/ColorPicker.d.ts +37 -0
- package/dist/library/components/ColorPicker/ColorPicker.d.ts.map +1 -0
- package/dist/library/components/ColorPicker/ColorPicker.js +135 -0
- package/dist/library/components/ColorPicker/ColorPicker.js.map +1 -0
- package/dist/library/components/DataTable/DataTable.d.ts +47 -0
- package/dist/library/components/DataTable/DataTable.d.ts.map +1 -0
- package/dist/library/components/DataTable/DataTable.js +146 -0
- package/dist/library/components/DataTable/DataTable.js.map +1 -0
- package/dist/library/components/DatePicker/DatePicker.d.ts +3 -1
- package/dist/library/components/DatePicker/DatePicker.d.ts.map +1 -1
- package/dist/library/components/DatePicker/DatePicker.js +2 -2
- package/dist/library/components/DatePicker/DatePicker.js.map +1 -1
- package/dist/library/components/DateRangePicker/DateRangePicker.d.ts +4 -1
- package/dist/library/components/DateRangePicker/DateRangePicker.d.ts.map +1 -1
- package/dist/library/components/DateRangePicker/DateRangePicker.js +7 -4
- package/dist/library/components/DateRangePicker/DateRangePicker.js.map +1 -1
- package/dist/library/components/Rating/Rating.d.ts +21 -0
- package/dist/library/components/Rating/Rating.d.ts.map +1 -0
- package/dist/library/components/Rating/Rating.js +81 -0
- package/dist/library/components/Rating/Rating.js.map +1 -0
- package/dist/library/components/Select/Select.d.ts +3 -9
- package/dist/library/components/Select/Select.d.ts.map +1 -1
- package/dist/library/components/Select/Select.js +16 -156
- package/dist/library/components/Select/Select.js.map +1 -1
- package/dist/library/components/Select/SelectPopup.d.ts +29 -0
- package/dist/library/components/Select/SelectPopup.d.ts.map +1 -0
- package/dist/library/components/Select/SelectPopup.js +179 -0
- package/dist/library/components/Select/SelectPopup.js.map +1 -0
- package/dist/library/components/Slider/Slider.d.ts +2 -2
- package/dist/library/components/Slider/Slider.d.ts.map +1 -1
- package/dist/library/components/Slider/Slider.js +44 -3
- package/dist/library/components/Slider/Slider.js.map +1 -1
- package/dist/library/components/Stepper/Stepper.d.ts +48 -0
- package/dist/library/components/Stepper/Stepper.d.ts.map +1 -0
- package/dist/library/components/Stepper/Stepper.js +81 -0
- package/dist/library/components/Stepper/Stepper.js.map +1 -0
- package/dist/library/components/TimeInput/TimeInput.d.ts +4 -1
- package/dist/library/components/TimeInput/TimeInput.d.ts.map +1 -1
- package/dist/library/components/TimeInput/TimeInput.js +2 -2
- package/dist/library/components/TimeInput/TimeInput.js.map +1 -1
- package/dist/library/components/Timeline/Timeline.d.ts +30 -0
- package/dist/library/components/Timeline/Timeline.d.ts.map +1 -0
- package/dist/library/components/Timeline/Timeline.js +15 -0
- package/dist/library/components/Timeline/Timeline.js.map +1 -0
- package/dist/library/components/Tree/Tree.d.ts +48 -0
- package/dist/library/components/Tree/Tree.d.ts.map +1 -0
- package/dist/library/components/Tree/Tree.js +298 -0
- package/dist/library/components/Tree/Tree.js.map +1 -0
- package/dist/library/components/Tree/treeModel.d.ts +43 -0
- package/dist/library/components/Tree/treeModel.d.ts.map +1 -0
- package/dist/library/components/Tree/treeModel.js +83 -0
- package/dist/library/components/Tree/treeModel.js.map +1 -0
- package/dist/library/components/Tree/useTreeLoading.d.ts +6 -0
- package/dist/library/components/Tree/useTreeLoading.d.ts.map +1 -0
- package/dist/library/components/Tree/useTreeLoading.js +69 -0
- package/dist/library/components/Tree/useTreeLoading.js.map +1 -0
- package/dist/library/components/TreeSelect/TreeSelect.d.ts +29 -0
- package/dist/library/components/TreeSelect/TreeSelect.d.ts.map +1 -0
- package/dist/library/components/TreeSelect/TreeSelect.js +163 -0
- package/dist/library/components/TreeSelect/TreeSelect.js.map +1 -0
- package/dist/library/index.d.ts +10 -0
- package/dist/library/index.d.ts.map +1 -1
- package/dist/library/index.js +9 -0
- package/dist/library/index.js.map +1 -1
- package/dist/library/react/useFormReset.d.ts +1 -1
- package/dist/library/react/useFormReset.d.ts.map +1 -1
- package/dist/library/react/useFormReset.js.map +1 -1
- package/dist/library/styles.css +1 -1
- package/package.json +1 -1
- package/src/color/colorValue.ts +89 -0
- package/src/color/oklch.ts +33 -14
- package/src/components/Cascader/Cascader.tsx +315 -0
- package/src/components/Cascader/CascaderColumn.tsx +203 -0
- package/src/components/ColorInput/ColorInput.tsx +191 -0
- package/src/components/ColorPicker/ColorArea.tsx +98 -0
- package/src/components/ColorPicker/ColorPicker.tsx +283 -0
- package/src/components/DataTable/DataTable.tsx +384 -0
- package/src/components/DatePicker/DatePicker.tsx +4 -0
- package/src/components/DateRangePicker/DateRangePicker.tsx +11 -3
- package/src/components/Rating/Rating.tsx +202 -0
- package/src/components/Select/Select.tsx +20 -192
- package/src/components/Select/SelectPopup.tsx +246 -0
- package/src/components/Slider/Slider.tsx +51 -2
- package/src/components/Stepper/Stepper.tsx +306 -0
- package/src/components/TimeInput/TimeInput.tsx +5 -0
- package/src/components/Timeline/Timeline.tsx +139 -0
- package/src/components/Tree/Tree.tsx +530 -0
- package/src/components/Tree/treeModel.ts +110 -0
- package/src/components/Tree/useTreeLoading.ts +73 -0
- package/src/components/TreeSelect/TreeSelect.tsx +326 -0
- package/src/index.ts +35 -0
- package/src/react/useFormReset.ts +4 -1
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import { Fragment, useLayoutEffect, useMemo, useRef, useState, type ReactNode } from 'react'
|
|
4
|
+
import { cn } from 'cn'
|
|
5
|
+
import { requireAccessibleName } from '../../internal/accessibleName.js'
|
|
6
|
+
import type { PartStyle } from '../../presentation.js'
|
|
7
|
+
import { useControllableState } from '../../react/useControllableState.js'
|
|
8
|
+
import type { StateOwner, ValueOwner } from '../../react/useNativeOwner.js'
|
|
9
|
+
import { useComposedRefs } from '../../react/useComposedRefs.js'
|
|
10
|
+
import { Button } from '../Button/Button.js'
|
|
11
|
+
import { Checkbox } from '../Checkbox/Checkbox.js'
|
|
12
|
+
import { WithoutField } from '../Field/Field.js'
|
|
13
|
+
import {
|
|
14
|
+
Table,
|
|
15
|
+
TableBody,
|
|
16
|
+
TableCaption,
|
|
17
|
+
TableCell,
|
|
18
|
+
TableContainer,
|
|
19
|
+
TableHead,
|
|
20
|
+
TableHeader,
|
|
21
|
+
TableRow,
|
|
22
|
+
type TableContainerProps
|
|
23
|
+
} from '../Table/Table.js'
|
|
24
|
+
|
|
25
|
+
export type DataTableColumn<Row> = {
|
|
26
|
+
id: string
|
|
27
|
+
header: string
|
|
28
|
+
cell: (row: Row) => ReactNode
|
|
29
|
+
compare?: (left: Row, right: Row) => number
|
|
30
|
+
width?: number | string
|
|
31
|
+
/** Exposes cells in this column as native row headers. */
|
|
32
|
+
rowHeader?: boolean
|
|
33
|
+
}
|
|
34
|
+
export type DataTableSort = { column: string; direction: 'ascending' | 'descending' }
|
|
35
|
+
export type DataTableSelection<Row> = ValueOwner<readonly string[]> & {
|
|
36
|
+
onValueChange?: (keys: readonly string[]) => void
|
|
37
|
+
allLabel: string
|
|
38
|
+
labelForRow: (row: Row) => string
|
|
39
|
+
isRowDisabled?: (row: Row) => boolean
|
|
40
|
+
}
|
|
41
|
+
export type DataTableVirtual = { height: number; rowHeight: number }
|
|
42
|
+
export type DataTableProps<Row> = Omit<
|
|
43
|
+
TableContainerProps,
|
|
44
|
+
'aria-label' | 'aria-labelledby' | 'children'
|
|
45
|
+
> &
|
|
46
|
+
StateOwner<'sort', DataTableSort | null> & {
|
|
47
|
+
caption: string
|
|
48
|
+
rows: readonly Row[]
|
|
49
|
+
columns: readonly DataTableColumn<Row>[]
|
|
50
|
+
getRowId: (row: Row) => string
|
|
51
|
+
onSortChange?: (next: DataTableSort | null) => void
|
|
52
|
+
selection?: DataTableSelection<Row>
|
|
53
|
+
stickyHeader?: boolean
|
|
54
|
+
virtual?: DataTableVirtual
|
|
55
|
+
emptyText?: string
|
|
56
|
+
parts?: {
|
|
57
|
+
table?: PartStyle
|
|
58
|
+
caption?: PartStyle
|
|
59
|
+
header?: PartStyle
|
|
60
|
+
row?: PartStyle
|
|
61
|
+
cell?: PartStyle
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function DataTable<Row>({
|
|
66
|
+
caption,
|
|
67
|
+
rows,
|
|
68
|
+
columns,
|
|
69
|
+
getRowId,
|
|
70
|
+
sort,
|
|
71
|
+
defaultSort,
|
|
72
|
+
onSortChange,
|
|
73
|
+
selection,
|
|
74
|
+
virtual,
|
|
75
|
+
stickyHeader = !!virtual,
|
|
76
|
+
emptyText = 'No rows',
|
|
77
|
+
ref,
|
|
78
|
+
style,
|
|
79
|
+
parts,
|
|
80
|
+
'data-slot': slot,
|
|
81
|
+
onScroll,
|
|
82
|
+
onFocusCapture,
|
|
83
|
+
onBlurCapture,
|
|
84
|
+
...props
|
|
85
|
+
}: DataTableProps<Row>) {
|
|
86
|
+
requireAccessibleName('DataTable', { 'aria-label': caption })
|
|
87
|
+
const count = columns.length + Number(!!selection)
|
|
88
|
+
if (!columns.length || new Set(columns.map(column => column.id)).size !== columns.length) {
|
|
89
|
+
throw new TypeError('DataTable requires columns with unique IDs.')
|
|
90
|
+
}
|
|
91
|
+
if (
|
|
92
|
+
virtual &&
|
|
93
|
+
[virtual.height, virtual.rowHeight].some(size => !Number.isFinite(size) || size <= 0)
|
|
94
|
+
) {
|
|
95
|
+
throw new RangeError('DataTable virtual height and rowHeight must be positive finite numbers.')
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const ordering = useControllableState<DataTableSort | null>({
|
|
99
|
+
name: 'DataTable sort',
|
|
100
|
+
value: sort,
|
|
101
|
+
defaultValue: defaultSort,
|
|
102
|
+
initialValue: null,
|
|
103
|
+
onChange: onSortChange
|
|
104
|
+
})
|
|
105
|
+
const picking = useControllableState<readonly string[]>({
|
|
106
|
+
name: 'DataTable selection',
|
|
107
|
+
value: selection?.value,
|
|
108
|
+
defaultValue: selection?.defaultValue,
|
|
109
|
+
initialValue: [],
|
|
110
|
+
onChange: selection?.onValueChange
|
|
111
|
+
})
|
|
112
|
+
const sorted = columns.find(column => column.compare && column.id === ordering.value?.column)
|
|
113
|
+
const direction = ordering.value?.direction
|
|
114
|
+
const indexed = useMemo(() => {
|
|
115
|
+
const seen = new Set<string>()
|
|
116
|
+
return rows.map(row => {
|
|
117
|
+
const key = getRowId(row)
|
|
118
|
+
if (!key || seen.has(key))
|
|
119
|
+
throw new TypeError('DataTable requires unique, nonempty row keys.')
|
|
120
|
+
seen.add(key)
|
|
121
|
+
return { key, row }
|
|
122
|
+
})
|
|
123
|
+
}, [getRowId, rows])
|
|
124
|
+
const ordered = useMemo(() => {
|
|
125
|
+
const compare = sorted?.compare
|
|
126
|
+
return compare
|
|
127
|
+
? [...indexed].sort((a, b) => compare(a.row, b.row) * (direction === 'descending' ? -1 : 1))
|
|
128
|
+
: indexed
|
|
129
|
+
}, [direction, indexed, sorted])
|
|
130
|
+
|
|
131
|
+
const selected = new Set(picking.value)
|
|
132
|
+
const enabled = selection
|
|
133
|
+
? indexed.filter(({ row }) => !selection.isRowDisabled?.(row)).map(({ key }) => key)
|
|
134
|
+
: []
|
|
135
|
+
const pickedCount = enabled.filter(key => selected.has(key)).length
|
|
136
|
+
const all = enabled.length > 0 && pickedCount === enabled.length
|
|
137
|
+
const viewport = useRef<HTMLElement>(null)
|
|
138
|
+
const viewportRef = useComposedRefs(ref, viewport)
|
|
139
|
+
const head = useRef<HTMLTableSectionElement>(null)
|
|
140
|
+
const [top, setTop] = useState(0)
|
|
141
|
+
const [focusedKey, setFocusedKey] = useState<string | undefined>()
|
|
142
|
+
const [size, setSize] = useState({ head: 0, viewport: 0 })
|
|
143
|
+
const rowHeight = virtual?.rowHeight ?? 0
|
|
144
|
+
|
|
145
|
+
useLayoutEffect(() => {
|
|
146
|
+
const node = viewport.current
|
|
147
|
+
const header = head.current
|
|
148
|
+
const view = node?.ownerDocument.defaultView
|
|
149
|
+
if (!rowHeight || !node || !header || !view) return
|
|
150
|
+
const observer = new view.ResizeObserver(() => {
|
|
151
|
+
setSize({ head: header.offsetHeight, viewport: node.clientHeight })
|
|
152
|
+
})
|
|
153
|
+
observer.observe(node)
|
|
154
|
+
observer.observe(header)
|
|
155
|
+
return () => observer.disconnect()
|
|
156
|
+
}, [rowHeight])
|
|
157
|
+
|
|
158
|
+
useLayoutEffect(() => {
|
|
159
|
+
if (viewport.current && rowHeight) viewport.current.scrollTop = 0
|
|
160
|
+
}, [direction, sorted?.id, rowHeight])
|
|
161
|
+
|
|
162
|
+
let first = 0
|
|
163
|
+
let end = ordered.length
|
|
164
|
+
if (virtual) {
|
|
165
|
+
const visibleTop = Math.max(0, top - (stickyHeader ? 0 : size.head))
|
|
166
|
+
first = Math.max(0, Math.min(ordered.length - 1, Math.floor(visibleTop / rowHeight)) - 3)
|
|
167
|
+
end = Math.min(
|
|
168
|
+
ordered.length,
|
|
169
|
+
Math.ceil((visibleTop + (size.viewport || virtual.height)) / rowHeight) + 3
|
|
170
|
+
)
|
|
171
|
+
}
|
|
172
|
+
const windowed = ordered
|
|
173
|
+
.slice(first, end)
|
|
174
|
+
.map((entry, offset) => ({ ...entry, index: first + offset }))
|
|
175
|
+
if (virtual && focusedKey) {
|
|
176
|
+
const index = ordered.findIndex(entry => entry.key === focusedKey)
|
|
177
|
+
if (index >= 0 && index < first) windowed.unshift({ ...ordered[index], index })
|
|
178
|
+
if (index >= end) windowed.push({ ...ordered[index], index })
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function gap(length: number) {
|
|
182
|
+
return (
|
|
183
|
+
length > 0 && (
|
|
184
|
+
<TableRow
|
|
185
|
+
aria-hidden="true"
|
|
186
|
+
className="border-0"
|
|
187
|
+
data-slot="data-table-spacer"
|
|
188
|
+
>
|
|
189
|
+
<TableCell
|
|
190
|
+
colSpan={count}
|
|
191
|
+
className="p-0"
|
|
192
|
+
style={{ height: length * rowHeight }}
|
|
193
|
+
/>
|
|
194
|
+
</TableRow>
|
|
195
|
+
)
|
|
196
|
+
)
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const headerClass = cn(
|
|
200
|
+
stickyHeader &&
|
|
201
|
+
'sticky top-0 z-10 shadow-[inset_0_-1px_var(--rad-table-border-color,var(--rad-color-border))]',
|
|
202
|
+
parts?.header?.className
|
|
203
|
+
)
|
|
204
|
+
const cellClass = cn(virtual && 'px-0 py-0 align-middle', parts?.cell?.className)
|
|
205
|
+
const contentClass =
|
|
206
|
+
'box-border flex min-w-0 items-center overflow-hidden border-b border-[var(--rad-table-border-color,var(--rad-color-border))] forced-colors:border-[CanvasText] px-[var(--rad-table-padding-inline,var(--rad-control-small-padding-inline,0.75rem))]'
|
|
207
|
+
|
|
208
|
+
return (
|
|
209
|
+
<TableContainer
|
|
210
|
+
{...props}
|
|
211
|
+
ref={viewportRef}
|
|
212
|
+
aria-label={caption}
|
|
213
|
+
data-slot={slot ? 'data-table ' + slot : 'data-table'}
|
|
214
|
+
style={{ ...(virtual && { height: virtual.height }), ...style }}
|
|
215
|
+
onScroll={event => {
|
|
216
|
+
onScroll?.(event)
|
|
217
|
+
if (virtual) setTop(event.currentTarget.scrollTop)
|
|
218
|
+
}}
|
|
219
|
+
onFocusCapture={event => {
|
|
220
|
+
onFocusCapture?.(event)
|
|
221
|
+
if (virtual)
|
|
222
|
+
setFocusedKey(
|
|
223
|
+
(event.target as Element).closest<HTMLElement>('[data-row-key]')?.dataset.rowKey
|
|
224
|
+
)
|
|
225
|
+
}}
|
|
226
|
+
onBlurCapture={event => {
|
|
227
|
+
onBlurCapture?.(event)
|
|
228
|
+
if (virtual && !event.currentTarget.contains(event.relatedTarget)) setFocusedKey(undefined)
|
|
229
|
+
}}
|
|
230
|
+
>
|
|
231
|
+
<Table
|
|
232
|
+
aria-rowcount={virtual && rows.length ? rows.length + 1 : undefined}
|
|
233
|
+
data-slot="data-table-table"
|
|
234
|
+
className={cn(
|
|
235
|
+
virtual && 'table-fixed border-separate border-spacing-0',
|
|
236
|
+
parts?.table?.className
|
|
237
|
+
)}
|
|
238
|
+
style={parts?.table?.style}
|
|
239
|
+
>
|
|
240
|
+
<TableCaption
|
|
241
|
+
className={cn('sr-only', parts?.caption?.className)}
|
|
242
|
+
style={parts?.caption?.style}
|
|
243
|
+
>
|
|
244
|
+
{caption}
|
|
245
|
+
</TableCaption>
|
|
246
|
+
<TableHead ref={head}>
|
|
247
|
+
<TableRow aria-rowindex={virtual && rows.length ? 1 : undefined}>
|
|
248
|
+
{selection && (
|
|
249
|
+
<TableHeader
|
|
250
|
+
className={cn('w-12', headerClass)}
|
|
251
|
+
style={parts?.header?.style}
|
|
252
|
+
>
|
|
253
|
+
<WithoutField>
|
|
254
|
+
<Checkbox
|
|
255
|
+
aria-label={selection.allLabel}
|
|
256
|
+
checked={all}
|
|
257
|
+
indeterminate={pickedCount > 0 && !all}
|
|
258
|
+
disabled={!enabled.length}
|
|
259
|
+
onChange={event => {
|
|
260
|
+
const checked = event.currentTarget.checked
|
|
261
|
+
picking.request(previous => {
|
|
262
|
+
const next = new Set(previous)
|
|
263
|
+
for (const key of enabled) {
|
|
264
|
+
if (checked) next.add(key)
|
|
265
|
+
else next.delete(key)
|
|
266
|
+
}
|
|
267
|
+
return [...next]
|
|
268
|
+
})
|
|
269
|
+
}}
|
|
270
|
+
/>
|
|
271
|
+
</WithoutField>
|
|
272
|
+
</TableHeader>
|
|
273
|
+
)}
|
|
274
|
+
{columns.map(column => {
|
|
275
|
+
const active = sorted?.id === column.id
|
|
276
|
+
return (
|
|
277
|
+
<TableHeader
|
|
278
|
+
key={column.id}
|
|
279
|
+
aria-sort={active ? direction : undefined}
|
|
280
|
+
className={headerClass}
|
|
281
|
+
style={{ width: column.width, ...parts?.header?.style }}
|
|
282
|
+
>
|
|
283
|
+
{column.compare ? (
|
|
284
|
+
<Button
|
|
285
|
+
variant="ghost"
|
|
286
|
+
tone="neutral"
|
|
287
|
+
controlSize="small"
|
|
288
|
+
className="max-w-full px-1 text-start"
|
|
289
|
+
onClick={() =>
|
|
290
|
+
ordering.request({
|
|
291
|
+
column: column.id,
|
|
292
|
+
direction:
|
|
293
|
+
active && direction === 'ascending' ? 'descending' : 'ascending'
|
|
294
|
+
})
|
|
295
|
+
}
|
|
296
|
+
>
|
|
297
|
+
{column.header}
|
|
298
|
+
<span
|
|
299
|
+
aria-hidden="true"
|
|
300
|
+
className="text-xs"
|
|
301
|
+
>
|
|
302
|
+
{active ? (direction === 'ascending' ? '↑' : '↓') : '↕'}
|
|
303
|
+
</span>
|
|
304
|
+
</Button>
|
|
305
|
+
) : (
|
|
306
|
+
column.header
|
|
307
|
+
)}
|
|
308
|
+
</TableHeader>
|
|
309
|
+
)
|
|
310
|
+
})}
|
|
311
|
+
</TableRow>
|
|
312
|
+
</TableHead>
|
|
313
|
+
<TableBody>
|
|
314
|
+
{!ordered.length && (
|
|
315
|
+
<TableRow>
|
|
316
|
+
<TableCell colSpan={count}>{emptyText}</TableCell>
|
|
317
|
+
</TableRow>
|
|
318
|
+
)}
|
|
319
|
+
{windowed.map(({ key, row, index }, offset) => (
|
|
320
|
+
<Fragment key={key}>
|
|
321
|
+
{virtual && gap(index - (offset ? windowed[offset - 1].index + 1 : 0))}
|
|
322
|
+
<TableRow
|
|
323
|
+
aria-rowindex={virtual ? index + 2 : undefined}
|
|
324
|
+
data-row-key={key}
|
|
325
|
+
data-selected={selected.has(key) ? '' : undefined}
|
|
326
|
+
className={parts?.row?.className}
|
|
327
|
+
style={parts?.row?.style}
|
|
328
|
+
>
|
|
329
|
+
{selection && (
|
|
330
|
+
<TableCell
|
|
331
|
+
className={cellClass}
|
|
332
|
+
style={parts?.cell?.style}
|
|
333
|
+
>
|
|
334
|
+
<div
|
|
335
|
+
className={(virtual && contentClass) || undefined}
|
|
336
|
+
style={virtual ? { height: rowHeight } : undefined}
|
|
337
|
+
>
|
|
338
|
+
<WithoutField>
|
|
339
|
+
<Checkbox
|
|
340
|
+
aria-label={selection.labelForRow(row)}
|
|
341
|
+
checked={selected.has(key)}
|
|
342
|
+
disabled={selection.isRowDisabled?.(row)}
|
|
343
|
+
onChange={event => {
|
|
344
|
+
const checked = event.currentTarget.checked
|
|
345
|
+
picking.request(previous =>
|
|
346
|
+
checked ? [...previous, key] : previous.filter(value => value !== key)
|
|
347
|
+
)
|
|
348
|
+
}}
|
|
349
|
+
/>
|
|
350
|
+
</WithoutField>
|
|
351
|
+
</div>
|
|
352
|
+
</TableCell>
|
|
353
|
+
)}
|
|
354
|
+
{columns.map(column => {
|
|
355
|
+
const Cell = column.rowHeader ? TableHeader : TableCell
|
|
356
|
+
return (
|
|
357
|
+
<Cell
|
|
358
|
+
key={column.id}
|
|
359
|
+
scope={column.rowHeader ? 'row' : undefined}
|
|
360
|
+
className={cellClass}
|
|
361
|
+
style={parts?.cell?.style}
|
|
362
|
+
>
|
|
363
|
+
{virtual ? (
|
|
364
|
+
<div
|
|
365
|
+
className={contentClass}
|
|
366
|
+
style={{ height: rowHeight }}
|
|
367
|
+
>
|
|
368
|
+
<div className="min-w-0 truncate">{column.cell(row)}</div>
|
|
369
|
+
</div>
|
|
370
|
+
) : (
|
|
371
|
+
column.cell(row)
|
|
372
|
+
)}
|
|
373
|
+
</Cell>
|
|
374
|
+
)
|
|
375
|
+
})}
|
|
376
|
+
</TableRow>
|
|
377
|
+
</Fragment>
|
|
378
|
+
))}
|
|
379
|
+
{virtual && gap(ordered.length - (windowed.at(-1)?.index ?? -1) - 1)}
|
|
380
|
+
</TableBody>
|
|
381
|
+
</Table>
|
|
382
|
+
</TableContainer>
|
|
383
|
+
)
|
|
384
|
+
}
|
|
@@ -39,6 +39,8 @@ export type DatePickerProps = ValueOwner<string | null> &
|
|
|
39
39
|
> & {
|
|
40
40
|
onValueChange?: (iso: string | null) => void
|
|
41
41
|
calendarLabel?: string
|
|
42
|
+
/** Initial calendar focus when the field is empty. Defaults to today. */
|
|
43
|
+
defaultFocusedDate?: CalendarProps['defaultFocusedDate']
|
|
42
44
|
className?: string
|
|
43
45
|
}
|
|
44
46
|
|
|
@@ -51,6 +53,7 @@ export function DatePicker({
|
|
|
51
53
|
previousMonthLabel,
|
|
52
54
|
nextMonthLabel,
|
|
53
55
|
today,
|
|
56
|
+
defaultFocusedDate,
|
|
54
57
|
ref,
|
|
55
58
|
...props
|
|
56
59
|
}: DatePickerProps) {
|
|
@@ -101,6 +104,7 @@ export function DatePicker({
|
|
|
101
104
|
isDateDisabled={props.isDateDisabled}
|
|
102
105
|
controlSize={props.controlSize}
|
|
103
106
|
today={today}
|
|
107
|
+
defaultFocusedDate={owner.value === null ? defaultFocusedDate : undefined}
|
|
104
108
|
firstDayOfWeek={firstDayOfWeek}
|
|
105
109
|
previousMonthLabel={previousMonthLabel}
|
|
106
110
|
nextMonthLabel={nextMonthLabel}
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
import { useRef, useState, type ReactNode } from 'react'
|
|
4
4
|
import { cn } from 'cn'
|
|
5
|
+
import type { CalendarProps } from '../Calendar/Calendar.js'
|
|
5
6
|
import type { DatePickerProps } from '../DatePicker/DatePicker.js'
|
|
6
7
|
import { DateEditor } from '../DatePicker/DateEditor.js'
|
|
7
8
|
import { DateCalendarPopup, DateCalendarTrigger } from '../DatePicker/DateCalendar.js'
|
|
@@ -11,6 +12,7 @@ import { Field, useField, useFieldControl } from '../Field/Field.js'
|
|
|
11
12
|
import { useControllableState } from '../../react/useControllableState.js'
|
|
12
13
|
import type { ValueOwner } from '../../react/useNativeOwner.js'
|
|
13
14
|
import { civilTime, rangeHasDisabledDate } from '../../internal/calendar.js'
|
|
15
|
+
import { joinIds } from '../../internal/ids.js'
|
|
14
16
|
|
|
15
17
|
/** Either endpoint can be typed first. Values use ISO civil dates. */
|
|
16
18
|
export type DateRangeValue = { start: string | null; end: string | null }
|
|
@@ -41,6 +43,8 @@ export type DateRangePickerProps = ValueOwner<DateRangeValue> &
|
|
|
41
43
|
endLabel: ReactNode
|
|
42
44
|
startName?: string
|
|
43
45
|
endName?: string
|
|
46
|
+
/** ISO date to focus when opening the calendar for an empty range. */
|
|
47
|
+
defaultFocusedDate?: CalendarProps['defaultFocusedDate']
|
|
44
48
|
onValueChange?: (range: DateRangeValue) => void
|
|
45
49
|
presets?: readonly DateRangePreset[]
|
|
46
50
|
}
|
|
@@ -59,6 +63,7 @@ export function DateRangePicker({
|
|
|
59
63
|
calendarLabel = 'Choose date range',
|
|
60
64
|
firstDayOfWeek,
|
|
61
65
|
today,
|
|
66
|
+
defaultFocusedDate,
|
|
62
67
|
previousMonthLabel,
|
|
63
68
|
nextMonthLabel,
|
|
64
69
|
className,
|
|
@@ -88,10 +93,12 @@ export function DateRangePicker({
|
|
|
88
93
|
const editorProps = {
|
|
89
94
|
...props,
|
|
90
95
|
disabled: control.disabled,
|
|
91
|
-
'aria-invalid': control['aria-invalid'],
|
|
92
|
-
'aria-describedby': control['aria-describedby']
|
|
96
|
+
'aria-invalid': field?.invalid || control['aria-invalid'],
|
|
97
|
+
'aria-describedby': joinIds(field?.describedBy, control['aria-describedby']),
|
|
93
98
|
revision
|
|
94
99
|
}
|
|
100
|
+
// Calendar accepts either selection order. A lone end is its first picked day.
|
|
101
|
+
const calendarValue = start ? { start, end } : end ? { start: end, end: null } : null
|
|
95
102
|
function unavailable(from: string, to: string) {
|
|
96
103
|
return (
|
|
97
104
|
from > to ||
|
|
@@ -189,7 +196,8 @@ export function DateRangePicker({
|
|
|
189
196
|
controlSize={props.controlSize}
|
|
190
197
|
previousMonthLabel={previousMonthLabel}
|
|
191
198
|
nextMonthLabel={nextMonthLabel}
|
|
192
|
-
|
|
199
|
+
defaultFocusedDate={calendarValue ? undefined : defaultFocusedDate}
|
|
200
|
+
value={calendarValue}
|
|
193
201
|
onValueChange={range => {
|
|
194
202
|
if (range) selectRange(range)
|
|
195
203
|
}}
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import { useRef, type KeyboardEvent, type ReactNode } from 'react'
|
|
4
|
+
import { cn } from 'cn'
|
|
5
|
+
import { joinIds } from '../../internal/ids.js'
|
|
6
|
+
import type { PartStyle } from '../../presentation.js'
|
|
7
|
+
import { useComposedRefs } from '../../react/useComposedRefs.js'
|
|
8
|
+
import { useControllableState } from '../../react/useControllableState.js'
|
|
9
|
+
import { useFormReset } from '../../react/useFormReset.js'
|
|
10
|
+
import type { ValueOwner } from '../../react/useNativeOwner.js'
|
|
11
|
+
import { Field } from '../Field/Field.js'
|
|
12
|
+
import { Radio, RadioGroup, type RadioGroupProps } from '../RadioGroup/RadioGroup.js'
|
|
13
|
+
import { controlMetrics } from '../shared/controlStyles.js'
|
|
14
|
+
import { focusWithinRingClass } from '../shared/focusStyles.js'
|
|
15
|
+
|
|
16
|
+
export type RatingProps = Omit<
|
|
17
|
+
RadioGroupProps,
|
|
18
|
+
'children' | 'value' | 'defaultValue' | 'onValueChange' | 'onChange' | 'parts'
|
|
19
|
+
> &
|
|
20
|
+
ValueOwner<number | null> & {
|
|
21
|
+
max?: number
|
|
22
|
+
readOnly?: boolean
|
|
23
|
+
onValueChange?: (value: number) => void
|
|
24
|
+
/** Rating marks are decorative. Supply both states for a custom mark. */
|
|
25
|
+
icon?: { filled: ReactNode; empty: ReactNode }
|
|
26
|
+
formatValue?: (value: number, max: number) => string
|
|
27
|
+
parts?: RadioGroupProps['parts'] & { item?: PartStyle; icon?: PartStyle }
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const formatRating = (value: number, max: number) => `${value} out of ${max}`
|
|
31
|
+
|
|
32
|
+
// WebKit and Chromium disagree about native horizontal radio keys in RTL.
|
|
33
|
+
function navigateRtl(event: KeyboardEvent<HTMLInputElement>) {
|
|
34
|
+
if (
|
|
35
|
+
event.defaultPrevented ||
|
|
36
|
+
event.altKey ||
|
|
37
|
+
event.ctrlKey ||
|
|
38
|
+
event.metaKey ||
|
|
39
|
+
(event.key !== 'ArrowLeft' && event.key !== 'ArrowRight')
|
|
40
|
+
)
|
|
41
|
+
return
|
|
42
|
+
const input = event.currentTarget
|
|
43
|
+
if (input.ownerDocument.defaultView?.getComputedStyle(input).direction !== 'rtl') return
|
|
44
|
+
const group = input.closest('[data-slot~="rating"]')
|
|
45
|
+
const options = Array.from(
|
|
46
|
+
group?.querySelectorAll<HTMLInputElement>('[data-slot~="rating-option"]:enabled') ?? []
|
|
47
|
+
)
|
|
48
|
+
const index = options.indexOf(input)
|
|
49
|
+
if (index < 0) return
|
|
50
|
+
event.preventDefault()
|
|
51
|
+
const step = event.key === 'ArrowLeft' ? 1 : -1
|
|
52
|
+
const next = options[(index + step + options.length) % options.length]
|
|
53
|
+
next.focus()
|
|
54
|
+
next.click()
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function RatingMark({
|
|
58
|
+
filled,
|
|
59
|
+
icon,
|
|
60
|
+
part
|
|
61
|
+
}: {
|
|
62
|
+
filled: boolean
|
|
63
|
+
icon: RatingProps['icon']
|
|
64
|
+
part: PartStyle | undefined
|
|
65
|
+
}) {
|
|
66
|
+
return (
|
|
67
|
+
<span
|
|
68
|
+
aria-hidden="true"
|
|
69
|
+
data-slot="rating-icon"
|
|
70
|
+
data-filled={filled ? '' : undefined}
|
|
71
|
+
className={cn(
|
|
72
|
+
'pointer-events-none grid size-[var(--_rad-control-height)] place-items-center text-[length:calc(var(--_rad-control-height)*0.69)] leading-none [&_svg]:size-[65%]',
|
|
73
|
+
filled
|
|
74
|
+
? '[color:var(--rad-rating-filled,var(--rad-color-accent))] forced-colors:text-[Highlight]'
|
|
75
|
+
: '[color:var(--rad-rating-empty,var(--rad-color-muted-foreground))] forced-colors:text-[CanvasText]',
|
|
76
|
+
part?.className
|
|
77
|
+
)}
|
|
78
|
+
style={part?.style}
|
|
79
|
+
>
|
|
80
|
+
{filled ? (icon?.filled ?? '★') : (icon?.empty ?? '☆')}
|
|
81
|
+
</span>
|
|
82
|
+
)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function Rating({
|
|
86
|
+
max = 5,
|
|
87
|
+
value,
|
|
88
|
+
defaultValue,
|
|
89
|
+
onValueChange,
|
|
90
|
+
readOnly = false,
|
|
91
|
+
icon,
|
|
92
|
+
formatValue = formatRating,
|
|
93
|
+
controlSize = 'medium',
|
|
94
|
+
parts,
|
|
95
|
+
ref,
|
|
96
|
+
'data-slot': slot,
|
|
97
|
+
...props
|
|
98
|
+
}: RatingProps) {
|
|
99
|
+
const selection = useControllableState<number | null>({
|
|
100
|
+
name: 'Rating value',
|
|
101
|
+
value,
|
|
102
|
+
defaultValue,
|
|
103
|
+
initialValue: null,
|
|
104
|
+
onChange: next => {
|
|
105
|
+
if (next !== null) onValueChange?.(next)
|
|
106
|
+
}
|
|
107
|
+
})
|
|
108
|
+
const host = useRef<HTMLFieldSetElement>(null)
|
|
109
|
+
const hostRef = useComposedRefs(ref, host)
|
|
110
|
+
useFormReset(host, selection.reset)
|
|
111
|
+
if (!Number.isInteger(max) || max < 1)
|
|
112
|
+
throw new RangeError('Rating max must be a positive integer.')
|
|
113
|
+
if (
|
|
114
|
+
selection.value !== null &&
|
|
115
|
+
(!Number.isInteger(selection.value) || selection.value < 1 || selection.value > max)
|
|
116
|
+
) {
|
|
117
|
+
throw new RangeError('Rating value must be null or a whole number within the rating scale.')
|
|
118
|
+
}
|
|
119
|
+
const marks = Array.from({ length: max }, (_, index) => index + 1)
|
|
120
|
+
const { options, item, icon: iconPart, ...fieldParts } = parts ?? {}
|
|
121
|
+
const slotName = joinIds('rating', slot)
|
|
122
|
+
|
|
123
|
+
if (readOnly) {
|
|
124
|
+
return (
|
|
125
|
+
<Field
|
|
126
|
+
{...props}
|
|
127
|
+
ref={hostRef}
|
|
128
|
+
layout="group"
|
|
129
|
+
data-slot={slotName}
|
|
130
|
+
parts={fieldParts}
|
|
131
|
+
data-readonly=""
|
|
132
|
+
>
|
|
133
|
+
<span
|
|
134
|
+
className={cn(
|
|
135
|
+
'flex flex-wrap items-center',
|
|
136
|
+
controlMetrics({ controlSize }),
|
|
137
|
+
options?.className
|
|
138
|
+
)}
|
|
139
|
+
style={options?.style}
|
|
140
|
+
data-slot="rating-value"
|
|
141
|
+
>
|
|
142
|
+
<span className="sr-only">{formatValue(selection.value ?? 0, max)}</span>
|
|
143
|
+
{marks.map(mark => (
|
|
144
|
+
<RatingMark
|
|
145
|
+
key={mark}
|
|
146
|
+
filled={mark <= (selection.value ?? 0)}
|
|
147
|
+
icon={icon}
|
|
148
|
+
part={iconPart}
|
|
149
|
+
/>
|
|
150
|
+
))}
|
|
151
|
+
</span>
|
|
152
|
+
</Field>
|
|
153
|
+
)
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return (
|
|
157
|
+
<RadioGroup
|
|
158
|
+
{...props}
|
|
159
|
+
ref={hostRef}
|
|
160
|
+
data-slot={slotName}
|
|
161
|
+
controlSize={controlSize}
|
|
162
|
+
value={selection.value === null ? null : String(selection.value)}
|
|
163
|
+
onValueChange={key => selection.request(Number(key))}
|
|
164
|
+
parts={{
|
|
165
|
+
...fieldParts,
|
|
166
|
+
options: {
|
|
167
|
+
...options,
|
|
168
|
+
className: cn('flex flex-wrap items-center gap-0.5', options?.className)
|
|
169
|
+
}
|
|
170
|
+
}}
|
|
171
|
+
>
|
|
172
|
+
{marks.map(mark => (
|
|
173
|
+
<Radio
|
|
174
|
+
key={mark}
|
|
175
|
+
data-slot="rating-option"
|
|
176
|
+
value={String(mark)}
|
|
177
|
+
aria-label={formatValue(mark, max)}
|
|
178
|
+
onKeyDown={navigateRtl}
|
|
179
|
+
className="sr-only"
|
|
180
|
+
label={
|
|
181
|
+
<RatingMark
|
|
182
|
+
filled={mark <= (selection.value ?? 0)}
|
|
183
|
+
icon={icon}
|
|
184
|
+
part={iconPart}
|
|
185
|
+
/>
|
|
186
|
+
}
|
|
187
|
+
parts={{
|
|
188
|
+
wrapper: {
|
|
189
|
+
...item,
|
|
190
|
+
className: cn(
|
|
191
|
+
'relative rounded-[var(--rad-control-radius,0.5rem)] gap-0',
|
|
192
|
+
focusWithinRingClass,
|
|
193
|
+
item?.className
|
|
194
|
+
)
|
|
195
|
+
},
|
|
196
|
+
indicator: { className: 'hidden' }
|
|
197
|
+
}}
|
|
198
|
+
/>
|
|
199
|
+
))}
|
|
200
|
+
</RadioGroup>
|
|
201
|
+
)
|
|
202
|
+
}
|