@hozo/semantics 0.2.0 → 0.4.0
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 +39 -0
- package/README.md +40 -1
- package/dist/generated/badge.d.ts +2 -0
- package/dist/generated/badge.d.ts.map +1 -0
- package/dist/generated/badge.js +2 -0
- package/dist/generated/badge.js.map +1 -0
- package/dist/generated/badge.native.d.ts +2 -0
- package/dist/generated/badge.native.d.ts.map +1 -0
- package/dist/generated/badge.native.js +2 -0
- package/dist/generated/badge.native.js.map +1 -0
- package/dist/generated/meter.d.ts +2 -0
- package/dist/generated/meter.d.ts.map +1 -0
- package/dist/generated/meter.js +2 -0
- package/dist/generated/meter.js.map +1 -0
- package/dist/generated/meter.native.d.ts +2 -0
- package/dist/generated/meter.native.d.ts.map +1 -0
- package/dist/generated/meter.native.js +2 -0
- package/dist/generated/meter.native.js.map +1 -0
- package/dist/generated/table.d.ts +2 -0
- package/dist/generated/table.d.ts.map +1 -0
- package/dist/generated/table.js +2 -0
- package/dist/generated/table.js.map +1 -0
- package/dist/generated/table.native.d.ts +2 -0
- package/dist/generated/table.native.d.ts.map +1 -0
- package/dist/generated/table.native.js +2 -0
- package/dist/generated/table.native.js.map +1 -0
- package/dist/index.d.ts +58 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +74 -16
- package/dist/index.js.map +1 -1
- package/dist/index.native.d.ts +60 -0
- package/dist/index.native.d.ts.map +1 -1
- package/dist/index.native.js +83 -6
- package/dist/index.native.js.map +1 -1
- package/dist/meter-gauge.d.ts +30 -0
- package/dist/meter-gauge.d.ts.map +1 -0
- package/dist/meter-gauge.js +36 -0
- package/dist/meter-gauge.js.map +1 -0
- package/dist/table.d.ts +45 -0
- package/dist/table.d.ts.map +1 -0
- package/dist/table.js +31 -0
- package/dist/table.js.map +1 -0
- package/dist/table.native.d.ts +92 -0
- package/dist/table.native.d.ts.map +1 -0
- package/dist/table.native.js +207 -0
- package/dist/table.native.js.map +1 -0
- package/package.json +28 -4
- package/src/generated/badge.native.ts +1 -0
- package/src/generated/badge.ts +1 -0
- package/src/generated/meter.native.ts +1 -0
- package/src/generated/meter.ts +1 -0
- package/src/generated/table.native.ts +7 -0
- package/src/generated/table.ts +7 -0
- package/src/index.native.tsx +151 -6
- package/src/index.tsx +255 -45
- package/src/meter-gauge.ts +54 -0
- package/src/table.native.tsx +446 -0
- package/src/table.tsx +144 -0
|
@@ -0,0 +1,446 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Children,
|
|
3
|
+
type ComponentRef,
|
|
4
|
+
createContext,
|
|
5
|
+
isValidElement,
|
|
6
|
+
type ReactElement,
|
|
7
|
+
type ReactNode,
|
|
8
|
+
type Ref,
|
|
9
|
+
useCallback,
|
|
10
|
+
useContext,
|
|
11
|
+
useMemo,
|
|
12
|
+
useRef,
|
|
13
|
+
useState,
|
|
14
|
+
} from 'react'
|
|
15
|
+
import {
|
|
16
|
+
type LayoutChangeEvent,
|
|
17
|
+
ScrollView,
|
|
18
|
+
type StyleProp,
|
|
19
|
+
StyleSheet,
|
|
20
|
+
Text,
|
|
21
|
+
type TextStyle,
|
|
22
|
+
View,
|
|
23
|
+
type ViewStyle,
|
|
24
|
+
} from 'react-native'
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* A data table on React Native, which has none.
|
|
28
|
+
*
|
|
29
|
+
* ## The columns are sized the way a browser sizes them
|
|
30
|
+
*
|
|
31
|
+
* A Web table's columns are as wide as their widest cell, so every row lines
|
|
32
|
+
* up; a React Native row is a flex row that knows nothing of the rows above
|
|
33
|
+
* it. So the table measures: each cell renders at its natural width first,
|
|
34
|
+
* reports it, and every cell in a column is then given the widest. That is
|
|
35
|
+
* the browser's automatic table layout (CSS 2.2 section 17.5.2.2), reduced
|
|
36
|
+
* to what a reader of the result can see:
|
|
37
|
+
*
|
|
38
|
+
* - a table is as wide as its columns, unless it is told to fill (`w-full`),
|
|
39
|
+
* when the room left over is shared in proportion to the columns' widths;
|
|
40
|
+
* - a table wider than the room it has shrinks its columns in the same
|
|
41
|
+
* proportion and lets their text wrap -- or, with `scrollable`, keeps its
|
|
42
|
+
* width and scrolls sideways;
|
|
43
|
+
* - a cell spanning columns takes their sum and is not measured into them.
|
|
44
|
+
*
|
|
45
|
+
* The table is invisible until the measurement is in, so the frame with
|
|
46
|
+
* every row its own width is never seen; a change to a cell's text measures
|
|
47
|
+
* again.
|
|
48
|
+
*
|
|
49
|
+
* ## A reader hears the column with the cell
|
|
50
|
+
*
|
|
51
|
+
* Android has no table role and no table navigation, so a data cell whose
|
|
52
|
+
* content is text is named with its column's header -- "Price, $12" -- and
|
|
53
|
+
* every cell carries `accessibilityCollectionItem`, the row and column
|
|
54
|
+
* TalkBack reads where it has a collection to read them against.
|
|
55
|
+
*/
|
|
56
|
+
|
|
57
|
+
type Element = ReactElement<Record<string, unknown>>
|
|
58
|
+
|
|
59
|
+
interface ColumnInfo {
|
|
60
|
+
widths: readonly number[] | null
|
|
61
|
+
headers: readonly (string | undefined)[]
|
|
62
|
+
measuring: boolean
|
|
63
|
+
report: (key: string, column: number, width: number) => void
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const TableContext = createContext<ColumnInfo | null>(null)
|
|
67
|
+
const RowContext = createContext<{ row: number } | null>(null)
|
|
68
|
+
const CellContext = createContext<{ column: number; span: number } | null>(null)
|
|
69
|
+
|
|
70
|
+
/** The words in a cell, for naming it, or `undefined` if it holds more. */
|
|
71
|
+
function textOf(node: ReactNode): string | undefined {
|
|
72
|
+
if (typeof node === 'string' || typeof node === 'number') return String(node)
|
|
73
|
+
if (Array.isArray(node)) {
|
|
74
|
+
const parts = node.map(textOf)
|
|
75
|
+
return parts.every((part) => part !== undefined) ? parts.join('') : undefined
|
|
76
|
+
}
|
|
77
|
+
if (isValidElement<{ children?: ReactNode }>(node)) return textOf(node.props.children)
|
|
78
|
+
return undefined
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const elements = (children: ReactNode) =>
|
|
82
|
+
Children.toArray(children).filter(isValidElement) as Element[]
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* What a child of the table is: read off a marker on its component rather
|
|
86
|
+
* than by identity, so `TableHeader` and `TableHead` -- the names an
|
|
87
|
+
* uncompiled file renders -- are recognised as the section and the header
|
|
88
|
+
* cell they stand for.
|
|
89
|
+
*/
|
|
90
|
+
type Part = 'section' | 'row' | 'cell' | 'head'
|
|
91
|
+
const part = (element: Element): Part | undefined =>
|
|
92
|
+
(element.type as { hozoTablePart?: Part }).hozoTablePart
|
|
93
|
+
const isHeadCell = (cell: Element) => part(cell) === 'head' || isHead(cell.props.head)
|
|
94
|
+
|
|
95
|
+
const span = (cell: Element) =>
|
|
96
|
+
typeof cell.props.colSpan === 'number' && cell.props.colSpan > 1 ? cell.props.colSpan : 1
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Rows in document order and the cells in each, read off the element tree
|
|
100
|
+
* rather than counted as they render, so the numbering does not depend on
|
|
101
|
+
* render order and survives `rows.map(...)`.
|
|
102
|
+
*/
|
|
103
|
+
function structure(children: ReactNode): Element[][] {
|
|
104
|
+
const rows: Element[] = []
|
|
105
|
+
for (const child of elements(children)) {
|
|
106
|
+
if (part(child) === 'section') rows.push(...elements(child.props.children as ReactNode))
|
|
107
|
+
else if (part(child) === 'row') rows.push(child)
|
|
108
|
+
}
|
|
109
|
+
return rows.map((row) => elements(row.props.children as ReactNode))
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export interface HozoTableProps {
|
|
113
|
+
children?: ReactNode
|
|
114
|
+
style?: StyleProp<ViewStyle | TextStyle>
|
|
115
|
+
/** Keep the table's width and scroll sideways when it does not fit. */
|
|
116
|
+
scrollable?: boolean
|
|
117
|
+
accessibilityLabel?: string
|
|
118
|
+
testID?: string
|
|
119
|
+
/** Read by the compiler; see `SemanticsNativeProps.className`. */
|
|
120
|
+
className?: string
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export function HozoTable({
|
|
124
|
+
children,
|
|
125
|
+
style,
|
|
126
|
+
scrollable,
|
|
127
|
+
accessibilityLabel,
|
|
128
|
+
testID,
|
|
129
|
+
}: HozoTableProps) {
|
|
130
|
+
const rows = structure(children)
|
|
131
|
+
const columnCount = Math.max(
|
|
132
|
+
0,
|
|
133
|
+
...rows.map((cells) => cells.reduce((sum, cell) => sum + span(cell), 0)),
|
|
134
|
+
)
|
|
135
|
+
// What the measurement depends on: every cell's text, and the shape. A
|
|
136
|
+
// cell holding more than text contributes only its place.
|
|
137
|
+
const signature = rows
|
|
138
|
+
.map((cells) =>
|
|
139
|
+
cells
|
|
140
|
+
.map((cell) => `${span(cell)}:${textOf(cell.props.children as ReactNode) ?? '*'}`)
|
|
141
|
+
.join('|'),
|
|
142
|
+
)
|
|
143
|
+
.join('\n')
|
|
144
|
+
const headers: (string | undefined)[] = []
|
|
145
|
+
for (const cells of rows) {
|
|
146
|
+
let column = 0
|
|
147
|
+
for (const cell of cells) {
|
|
148
|
+
if (isHeadCell(cell) && cell.props.scope !== 'row' && headers[column] === undefined) {
|
|
149
|
+
headers[column] = textOf(cell.props.children as ReactNode)
|
|
150
|
+
}
|
|
151
|
+
column += span(cell)
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const measured = useRef(new Map<string, { column: number; width: number }>())
|
|
156
|
+
const takenFor = useRef(signature)
|
|
157
|
+
if (takenFor.current !== signature) {
|
|
158
|
+
// The cells changed since these were taken: start again.
|
|
159
|
+
measured.current = new Map()
|
|
160
|
+
takenFor.current = signature
|
|
161
|
+
}
|
|
162
|
+
const [natural, setNatural] = useState<number[] | null>(null)
|
|
163
|
+
const [measuredFor, setMeasuredFor] = useState<string | null>(null)
|
|
164
|
+
const [available, setAvailable] = useState<number | null>(null)
|
|
165
|
+
const measuring = measuredFor !== signature
|
|
166
|
+
const expected = rows.reduce(
|
|
167
|
+
(sum, cells) => sum + cells.filter((cell) => span(cell) === 1).length,
|
|
168
|
+
0,
|
|
169
|
+
)
|
|
170
|
+
|
|
171
|
+
const report = useCallback(
|
|
172
|
+
(key: string, column: number, width: number) => {
|
|
173
|
+
measured.current.set(key, { column, width })
|
|
174
|
+
if (measured.current.size < expected) return
|
|
175
|
+
const widths = Array.from({ length: columnCount }, () => 0)
|
|
176
|
+
for (const entry of measured.current.values()) {
|
|
177
|
+
widths[entry.column] = Math.max(widths[entry.column] ?? 0, entry.width)
|
|
178
|
+
}
|
|
179
|
+
setNatural(widths)
|
|
180
|
+
setMeasuredFor(signature)
|
|
181
|
+
},
|
|
182
|
+
[columnCount, expected, signature],
|
|
183
|
+
)
|
|
184
|
+
|
|
185
|
+
const flat = (StyleSheet.flatten(style) ?? {}) as ViewStyle
|
|
186
|
+
const fills = flat.width !== undefined || flat.flex !== undefined || flat.alignSelf === 'stretch'
|
|
187
|
+
const widths = useMemo(() => {
|
|
188
|
+
if (!natural || measuring) return null
|
|
189
|
+
const total = natural.reduce((sum, w) => sum + w, 0)
|
|
190
|
+
if (available === null || total === 0) return natural
|
|
191
|
+
const scale =
|
|
192
|
+
(fills && total < available) || (!scrollable && total > available) ? available / total : 1
|
|
193
|
+
return natural.map((w) => w * scale)
|
|
194
|
+
}, [available, fills, measuring, natural, scrollable])
|
|
195
|
+
|
|
196
|
+
const info: ColumnInfo = { widths, headers, measuring, report }
|
|
197
|
+
const table = (
|
|
198
|
+
<View
|
|
199
|
+
style={[TABLE, style, measuring ? HIDDEN : null]}
|
|
200
|
+
accessibilityLabel={accessibilityLabel}
|
|
201
|
+
testID={testID}
|
|
202
|
+
>
|
|
203
|
+
<TableContext.Provider value={info}>{numbered(children)}</TableContext.Provider>
|
|
204
|
+
</View>
|
|
205
|
+
)
|
|
206
|
+
return (
|
|
207
|
+
<View
|
|
208
|
+
onLayout={(event: LayoutChangeEvent) => setAvailable(event.nativeEvent.layout.width)}
|
|
209
|
+
style={OUTER}
|
|
210
|
+
>
|
|
211
|
+
{scrollable ? (
|
|
212
|
+
<ScrollView horizontal showsHorizontalScrollIndicator>
|
|
213
|
+
{table}
|
|
214
|
+
</ScrollView>
|
|
215
|
+
) : (
|
|
216
|
+
table
|
|
217
|
+
)}
|
|
218
|
+
</View>
|
|
219
|
+
)
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const isHead = (value: unknown) => value !== undefined && value !== false
|
|
223
|
+
|
|
224
|
+
/** Gives each row its index in document order, sections included. */
|
|
225
|
+
function numbered(children: ReactNode): ReactNode {
|
|
226
|
+
let row = 0
|
|
227
|
+
const wrap = (child: Element): ReactNode => {
|
|
228
|
+
const index = row++
|
|
229
|
+
return (
|
|
230
|
+
<RowContext.Provider key={child.key ?? index} value={{ row: index }}>
|
|
231
|
+
{child}
|
|
232
|
+
</RowContext.Provider>
|
|
233
|
+
)
|
|
234
|
+
}
|
|
235
|
+
return elements(children).map((child) => {
|
|
236
|
+
if (part(child) === 'section') {
|
|
237
|
+
const rows = elements(child.props.children as ReactNode).map(wrap)
|
|
238
|
+
return { ...child, props: { ...child.props, children: rows } }
|
|
239
|
+
}
|
|
240
|
+
if (part(child) === 'row') return wrap(child)
|
|
241
|
+
return child
|
|
242
|
+
})
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
export interface HozoTableSectionProps {
|
|
246
|
+
children?: ReactNode
|
|
247
|
+
section?: 'header' | 'body' | 'footer'
|
|
248
|
+
style?: StyleProp<ViewStyle>
|
|
249
|
+
testID?: string
|
|
250
|
+
/** Read by the compiler; see `SemanticsNativeProps.className`. */
|
|
251
|
+
className?: string
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/** `<thead>`, `<tbody>` or `<tfoot>`: a group of rows, and nothing to read. */
|
|
255
|
+
export function HozoTableSection({ children, style, testID }: HozoTableSectionProps) {
|
|
256
|
+
return (
|
|
257
|
+
<View style={style} testID={testID}>
|
|
258
|
+
{children}
|
|
259
|
+
</View>
|
|
260
|
+
)
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
export interface HozoTableRowProps {
|
|
264
|
+
children?: ReactNode
|
|
265
|
+
style?: StyleProp<ViewStyle>
|
|
266
|
+
testID?: string
|
|
267
|
+
/** Read by the compiler; see `SemanticsNativeProps.className`. */
|
|
268
|
+
className?: string
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/** `<tr>`: numbers its cells, columns spanned included. */
|
|
272
|
+
export function HozoTableRow({ children, style, testID }: HozoTableRowProps) {
|
|
273
|
+
let column = 0
|
|
274
|
+
return (
|
|
275
|
+
<View style={[ROW, style]} testID={testID}>
|
|
276
|
+
{elements(children).map((cell, index) => {
|
|
277
|
+
const at = column
|
|
278
|
+
column += span(cell)
|
|
279
|
+
return (
|
|
280
|
+
<CellContext.Provider key={cell.key ?? index} value={{ column: at, span: span(cell) }}>
|
|
281
|
+
{cell}
|
|
282
|
+
</CellContext.Provider>
|
|
283
|
+
)
|
|
284
|
+
})}
|
|
285
|
+
</View>
|
|
286
|
+
)
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
export interface HozoTableCellProps {
|
|
290
|
+
children?: ReactNode
|
|
291
|
+
/** A header cell: `<th>`. */
|
|
292
|
+
head?: boolean
|
|
293
|
+
/** `"row"` for a header that heads its row rather than its column. */
|
|
294
|
+
scope?: 'col' | 'row'
|
|
295
|
+
colSpan?: number
|
|
296
|
+
accessibilityLabel?: string
|
|
297
|
+
style?: StyleProp<ViewStyle | TextStyle>
|
|
298
|
+
testID?: string
|
|
299
|
+
/** Read by the compiler; see `SemanticsNativeProps.className`. */
|
|
300
|
+
className?: string
|
|
301
|
+
/**
|
|
302
|
+
* The cell's View: for focusing it, or measuring where it landed. A prop
|
|
303
|
+
* rather than `forwardRef`, as React 19 hands a function component its ref.
|
|
304
|
+
*/
|
|
305
|
+
ref?: Ref<ComponentRef<typeof View>>
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/** What a `View` cannot draw: handed to the cell's `Text`. */
|
|
309
|
+
const TEXT_KEYS = new Set([
|
|
310
|
+
'color',
|
|
311
|
+
'fontFamily',
|
|
312
|
+
'fontSize',
|
|
313
|
+
'fontStyle',
|
|
314
|
+
'fontVariant',
|
|
315
|
+
'fontWeight',
|
|
316
|
+
'letterSpacing',
|
|
317
|
+
'lineHeight',
|
|
318
|
+
'textAlign',
|
|
319
|
+
'textDecorationLine',
|
|
320
|
+
'textTransform',
|
|
321
|
+
])
|
|
322
|
+
|
|
323
|
+
/** `<th>` or `<td>`. */
|
|
324
|
+
export function HozoTableCell({
|
|
325
|
+
children,
|
|
326
|
+
head,
|
|
327
|
+
scope,
|
|
328
|
+
accessibilityLabel,
|
|
329
|
+
style,
|
|
330
|
+
testID,
|
|
331
|
+
ref,
|
|
332
|
+
}: HozoTableCellProps) {
|
|
333
|
+
const table = useContext(TableContext)
|
|
334
|
+
const row = useContext(RowContext)
|
|
335
|
+
const cell = useContext(CellContext) ?? { column: 0, span: 1 }
|
|
336
|
+
const key = `${row?.row ?? 0}:${cell.column}`
|
|
337
|
+
const box: Record<string, unknown> = {}
|
|
338
|
+
const text: Record<string, unknown> = {}
|
|
339
|
+
for (const [name, value] of Object.entries(StyleSheet.flatten(style) ?? {})) {
|
|
340
|
+
;(TEXT_KEYS.has(name) ? text : box)[name] = value
|
|
341
|
+
}
|
|
342
|
+
const width = table?.widths
|
|
343
|
+
? table.widths.slice(cell.column, cell.column + cell.span).reduce((sum, w) => sum + w, 0)
|
|
344
|
+
: undefined
|
|
345
|
+
const words = textOf(children)
|
|
346
|
+
const header = isHead(head)
|
|
347
|
+
const columnHeader = table?.headers[cell.column]
|
|
348
|
+
const label =
|
|
349
|
+
accessibilityLabel ??
|
|
350
|
+
(!header && words !== undefined && columnHeader ? `${columnHeader}, ${words}` : undefined)
|
|
351
|
+
|
|
352
|
+
return (
|
|
353
|
+
<View
|
|
354
|
+
ref={ref}
|
|
355
|
+
style={[CELL, box, width !== undefined ? { width } : null]}
|
|
356
|
+
testID={testID}
|
|
357
|
+
onLayout={
|
|
358
|
+
table?.measuring && cell.span === 1
|
|
359
|
+
? (event: LayoutChangeEvent) =>
|
|
360
|
+
table.report(key, cell.column, event.nativeEvent.layout.width)
|
|
361
|
+
: undefined
|
|
362
|
+
}
|
|
363
|
+
accessible={words !== undefined ? true : undefined}
|
|
364
|
+
accessibilityLabel={label}
|
|
365
|
+
// Not `accessibilityRole="header"`: TalkBack calls that a heading,
|
|
366
|
+
// and it made row headers headings too (#804). A column header is
|
|
367
|
+
// still read "Price, Heading", from `heading` in its collection item
|
|
368
|
+
// below, which is Android's own mark for a header cell.
|
|
369
|
+
// A View prop on Android that React Native's types do not declare;
|
|
370
|
+
// `BaseViewManager` reads it and `ReactAccessibilityDelegate` turns
|
|
371
|
+
// it into the node's `CollectionItemInfo`.
|
|
372
|
+
{...({
|
|
373
|
+
accessibilityCollectionItem: {
|
|
374
|
+
rowIndex: row?.row ?? 0,
|
|
375
|
+
rowSpan: 1,
|
|
376
|
+
columnIndex: cell.column,
|
|
377
|
+
columnSpan: cell.span,
|
|
378
|
+
heading: header && scope !== 'row',
|
|
379
|
+
},
|
|
380
|
+
} as object)}
|
|
381
|
+
>
|
|
382
|
+
{words !== undefined ? <Text style={text}>{children}</Text> : children}
|
|
383
|
+
</View>
|
|
384
|
+
)
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
export interface HozoTableCaptionProps {
|
|
388
|
+
children?: ReactNode
|
|
389
|
+
style?: StyleProp<TextStyle>
|
|
390
|
+
testID?: string
|
|
391
|
+
/** Read by the compiler; see `SemanticsNativeProps.className`. */
|
|
392
|
+
className?: string
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/** `<caption>`: the table's name, above it. */
|
|
396
|
+
export function HozoTableCaption({ children, style, testID }: HozoTableCaptionProps) {
|
|
397
|
+
return (
|
|
398
|
+
<Text style={style} testID={testID}>
|
|
399
|
+
{children}
|
|
400
|
+
</Text>
|
|
401
|
+
)
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
const OUTER: ViewStyle = { alignSelf: 'stretch' }
|
|
405
|
+
// Shrink-to-fit, as a Web table with no width is.
|
|
406
|
+
const TABLE: ViewStyle = { alignSelf: 'flex-start' }
|
|
407
|
+
const ROW: ViewStyle = { flexDirection: 'row' }
|
|
408
|
+
const CELL: ViewStyle = { flexShrink: 0 }
|
|
409
|
+
const HIDDEN: ViewStyle = { opacity: 0 }
|
|
410
|
+
|
|
411
|
+
HozoTableSection.hozoTablePart = 'section' as const
|
|
412
|
+
HozoTableRow.hozoTablePart = 'row' as const
|
|
413
|
+
HozoTableCell.hozoTablePart = 'cell' as const
|
|
414
|
+
|
|
415
|
+
/**
|
|
416
|
+
* The names a file the compiler did not read renders. Compiled, each is
|
|
417
|
+
* lowered to the component above it.
|
|
418
|
+
*/
|
|
419
|
+
export function TableHeader(props: Omit<HozoTableSectionProps, 'section'>) {
|
|
420
|
+
return <HozoTableSection {...props} section="header" />
|
|
421
|
+
}
|
|
422
|
+
TableHeader.hozoTablePart = 'section' as const
|
|
423
|
+
export function TableBody(props: Omit<HozoTableSectionProps, 'section'>) {
|
|
424
|
+
return <HozoTableSection {...props} section="body" />
|
|
425
|
+
}
|
|
426
|
+
TableBody.hozoTablePart = 'section' as const
|
|
427
|
+
export function TableFooter(props: Omit<HozoTableSectionProps, 'section'>) {
|
|
428
|
+
return <HozoTableSection {...props} section="footer" />
|
|
429
|
+
}
|
|
430
|
+
TableFooter.hozoTablePart = 'section' as const
|
|
431
|
+
export function TableHead(props: Omit<HozoTableCellProps, 'head'>) {
|
|
432
|
+
return <HozoTableCell {...props} head />
|
|
433
|
+
}
|
|
434
|
+
TableHead.hozoTablePart = 'head' as const
|
|
435
|
+
|
|
436
|
+
export {
|
|
437
|
+
HozoTable as Table,
|
|
438
|
+
HozoTableCaption as TableCaption,
|
|
439
|
+
type HozoTableCaptionProps as TableCaptionProps,
|
|
440
|
+
HozoTableCell as TableCell,
|
|
441
|
+
type HozoTableCellProps as TableCellProps,
|
|
442
|
+
type HozoTableProps as TableProps,
|
|
443
|
+
HozoTableRow as TableRow,
|
|
444
|
+
type HozoTableRowProps as TableRowProps,
|
|
445
|
+
type HozoTableSectionProps as TableSectionProps,
|
|
446
|
+
}
|
package/src/table.tsx
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import type { CSSProperties, ReactNode, Ref } from 'react'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A data table on the Web: the table elements themselves, which already lay
|
|
5
|
+
* out their columns from every cell and give a reader row and column
|
|
6
|
+
* navigation. These are what a file the compiler did not read renders;
|
|
7
|
+
* compiled, the same names lower to the elements directly.
|
|
8
|
+
*
|
|
9
|
+
* The `Hozo…` names are the ones compiled Native output imports. They exist
|
|
10
|
+
* here too so a module that names them type-checks on both platforms.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
interface PartProps {
|
|
14
|
+
children?: ReactNode
|
|
15
|
+
className?: string
|
|
16
|
+
style?: CSSProperties
|
|
17
|
+
testID?: string
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface HozoTableProps extends PartProps {
|
|
21
|
+
/** Native only: scroll sideways when the table does not fit. */
|
|
22
|
+
scrollable?: boolean
|
|
23
|
+
accessibilityLabel?: string
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function HozoTable({
|
|
27
|
+
children,
|
|
28
|
+
className,
|
|
29
|
+
style,
|
|
30
|
+
testID,
|
|
31
|
+
accessibilityLabel,
|
|
32
|
+
}: HozoTableProps) {
|
|
33
|
+
return (
|
|
34
|
+
<table className={className} style={style} data-testid={testID} aria-label={accessibilityLabel}>
|
|
35
|
+
{children}
|
|
36
|
+
</table>
|
|
37
|
+
)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export type HozoTableCaptionProps = PartProps
|
|
41
|
+
|
|
42
|
+
export function HozoTableCaption({ children, className, style }: HozoTableCaptionProps) {
|
|
43
|
+
return (
|
|
44
|
+
<caption className={className} style={style}>
|
|
45
|
+
{children}
|
|
46
|
+
</caption>
|
|
47
|
+
)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface HozoTableSectionProps extends PartProps {
|
|
51
|
+
section?: 'header' | 'body' | 'footer'
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function HozoTableSection({
|
|
55
|
+
section = 'body',
|
|
56
|
+
children,
|
|
57
|
+
className,
|
|
58
|
+
style,
|
|
59
|
+
}: HozoTableSectionProps) {
|
|
60
|
+
const Tag = section === 'header' ? 'thead' : section === 'footer' ? 'tfoot' : 'tbody'
|
|
61
|
+
return (
|
|
62
|
+
<Tag className={className} style={style}>
|
|
63
|
+
{children}
|
|
64
|
+
</Tag>
|
|
65
|
+
)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export type HozoTableRowProps = PartProps
|
|
69
|
+
|
|
70
|
+
export function HozoTableRow({ children, className, style }: HozoTableRowProps) {
|
|
71
|
+
return (
|
|
72
|
+
<tr className={className} style={style}>
|
|
73
|
+
{children}
|
|
74
|
+
</tr>
|
|
75
|
+
)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface HozoTableCellProps extends PartProps {
|
|
79
|
+
/** The cell element, for focusing or measuring it. */
|
|
80
|
+
ref?: Ref<HTMLTableCellElement>
|
|
81
|
+
head?: boolean
|
|
82
|
+
scope?: 'col' | 'row'
|
|
83
|
+
colSpan?: number
|
|
84
|
+
accessibilityLabel?: string
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function HozoTableCell({
|
|
88
|
+
head,
|
|
89
|
+
scope,
|
|
90
|
+
colSpan,
|
|
91
|
+
accessibilityLabel,
|
|
92
|
+
children,
|
|
93
|
+
className,
|
|
94
|
+
style,
|
|
95
|
+
ref,
|
|
96
|
+
}: HozoTableCellProps) {
|
|
97
|
+
return head ? (
|
|
98
|
+
<th
|
|
99
|
+
ref={ref}
|
|
100
|
+
scope={scope ?? 'col'}
|
|
101
|
+
colSpan={colSpan}
|
|
102
|
+
aria-label={accessibilityLabel}
|
|
103
|
+
className={className}
|
|
104
|
+
style={style}
|
|
105
|
+
>
|
|
106
|
+
{children}
|
|
107
|
+
</th>
|
|
108
|
+
) : (
|
|
109
|
+
<td
|
|
110
|
+
ref={ref}
|
|
111
|
+
colSpan={colSpan}
|
|
112
|
+
aria-label={accessibilityLabel}
|
|
113
|
+
className={className}
|
|
114
|
+
style={style}
|
|
115
|
+
>
|
|
116
|
+
{children}
|
|
117
|
+
</td>
|
|
118
|
+
)
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export function TableHeader(props: Omit<HozoTableSectionProps, 'section'>) {
|
|
122
|
+
return <HozoTableSection {...props} section="header" />
|
|
123
|
+
}
|
|
124
|
+
export function TableBody(props: Omit<HozoTableSectionProps, 'section'>) {
|
|
125
|
+
return <HozoTableSection {...props} section="body" />
|
|
126
|
+
}
|
|
127
|
+
export function TableFooter(props: Omit<HozoTableSectionProps, 'section'>) {
|
|
128
|
+
return <HozoTableSection {...props} section="footer" />
|
|
129
|
+
}
|
|
130
|
+
export function TableHead(props: Omit<HozoTableCellProps, 'head'>) {
|
|
131
|
+
return <HozoTableCell {...props} head />
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export {
|
|
135
|
+
HozoTable as Table,
|
|
136
|
+
HozoTableCaption as TableCaption,
|
|
137
|
+
type HozoTableCaptionProps as TableCaptionProps,
|
|
138
|
+
HozoTableCell as TableCell,
|
|
139
|
+
type HozoTableCellProps as TableCellProps,
|
|
140
|
+
type HozoTableProps as TableProps,
|
|
141
|
+
HozoTableRow as TableRow,
|
|
142
|
+
type HozoTableRowProps as TableRowProps,
|
|
143
|
+
type HozoTableSectionProps as TableSectionProps,
|
|
144
|
+
}
|