@solexllc/usx-react 0.8.0 → 0.9.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/README.md +15 -0
- package/dist/index.cjs +3173 -3238
- package/dist/index.js +3174 -3239
- package/package.json +4 -4
- package/src/components/checkbox/Checkbox.tsx +9 -4
- package/src/components/checkbox/checkbox.django.html +2 -2
- package/src/components/checkbox/config.json +9 -0
- package/src/components/data-table/DataTable.tsx +177 -0
- package/src/components/data-table/README.md +42 -0
- package/src/components/data-table/TableBody.tsx +123 -0
- package/src/components/{table → data-table}/TableCell.tsx +6 -3
- package/src/components/data-table/TableContext.tsx +49 -0
- package/src/components/{table → data-table}/TableFoot.tsx +8 -7
- package/src/components/data-table/TableGroup.tsx +62 -0
- package/src/components/data-table/TableHead.tsx +126 -0
- package/src/components/data-table/TableRow.tsx +24 -0
- package/src/components/data-table/cells.ts +49 -0
- package/src/components/data-table/columns.ts +15 -0
- package/src/components/data-table/config.json +375 -0
- package/src/components/data-table/data-table.django-snapshot.html +13560 -0
- package/src/components/data-table/data-table.django.html +13 -0
- package/src/components/data-table/data-table.html +34 -0
- package/src/components/data-table/data-table.react-snapshot.html +12033 -0
- package/src/components/{table → data-table}/table-body.django.html +12 -12
- package/src/components/{table → data-table}/table-foot.django.html +6 -6
- package/src/components/data-table/table-group.django.html +24 -0
- package/src/components/data-table/table-head.django.html +21 -0
- package/src/components/data-table/table-row.django.html +57 -0
- package/src/components/{table → data-table}/types.ts +48 -5
- package/src/components/pagination/Pagination.tsx +7 -1
- package/src/components/pagination/config.json +8 -0
- package/src/components/pagination/navigation/PaginationNavigation.tsx +10 -0
- package/src/components/pagination/step-options/PaginationStepOptions.tsx +3 -0
- package/src/components/radio-buttons/RadioButtons.tsx +37 -27
- package/src/components/radio-buttons/config.json +19 -1
- package/src/components/radio-buttons/radio-buttons.django-snapshot.html +28 -60
- package/src/components/radio-buttons/radio-buttons.django.html +16 -28
- package/src/components/table/Table.tsx +64 -334
- package/src/components/table/config.json +21 -339
- package/src/components/table/table.django-snapshot.html +712 -1482
- package/src/components/table/table.django.html +14 -59
- package/src/components/table/table.html +7 -2364
- package/src/components/table/table.react-snapshot.html +711 -2085
- package/src/index.js +1 -0
- package/src/components/table/TableBody.tsx +0 -256
- package/src/components/table/TableContext.tsx +0 -87
- package/src/components/table/TableGroup.tsx +0 -69
- package/src/components/table/TableHead.tsx +0 -139
- package/src/components/table/TableRow.tsx +0 -56
- package/src/components/table/hooks/useRowDetails.ts +0 -34
- package/src/components/table/hooks/useTableGroups.ts +0 -61
- package/src/components/table/hooks/useTableSelect.ts +0 -82
- package/src/components/table/hooks/useTableSort.ts +0 -60
- package/src/components/table/table-group.django.html +0 -40
- package/src/components/table/table-head.django.html +0 -85
- package/src/components/table/table-row.django.html +0 -98
- package/src/components/table/table_helper.py +0 -298
- /package/src/components/{table → data-table}/table-detail-row.django.html +0 -0
package/src/index.js
CHANGED
|
@@ -24,6 +24,7 @@ export { default as Collection } from './components/collection/Collection.tsx';
|
|
|
24
24
|
export { default as Combobox } from './components/combobox/Combobox.tsx';
|
|
25
25
|
export { default as Container } from './components/container/Container.tsx';
|
|
26
26
|
export { default as CopyToClipboard } from './components/copy-to-clipboard/CopyToClipboard.tsx';
|
|
27
|
+
export { default as DataTable } from './components/data-table/DataTable.tsx';
|
|
27
28
|
export { default as DatePicker } from './components/date-picker/DatePicker.tsx';
|
|
28
29
|
export { default as DateRangePicker } from './components/date-range-picker/DateRangePicker.tsx';
|
|
29
30
|
export { default as Details } from './components/details/Details.tsx';
|
|
@@ -1,256 +0,0 @@
|
|
|
1
|
-
import React, { useRef, useEffect, useId } from 'react';
|
|
2
|
-
import classnames from 'classnames';
|
|
3
|
-
import { useTableContext } from './TableContext';
|
|
4
|
-
import TableRow from './TableRow';
|
|
5
|
-
import TableGroup from './TableGroup';
|
|
6
|
-
import Button from '../button/Button';
|
|
7
|
-
import Spinner from '../spinner/Spinner';
|
|
8
|
-
import Checkbox from '../checkbox/Checkbox';
|
|
9
|
-
import type { RowKey, TableColumn, TableRowData } from './types';
|
|
10
|
-
|
|
11
|
-
// ─── Infinite scroll sentinel ─────────────────────────────────────────────────
|
|
12
|
-
|
|
13
|
-
function OnMoreSentinel({ onMore }: { onMore: () => void }) {
|
|
14
|
-
const sentinelRef = useRef<HTMLTableRowElement>(null);
|
|
15
|
-
useEffect(() => {
|
|
16
|
-
if (!sentinelRef.current) return;
|
|
17
|
-
const observer = new IntersectionObserver(
|
|
18
|
-
(entries) => {
|
|
19
|
-
if (entries[0].isIntersecting) onMore();
|
|
20
|
-
},
|
|
21
|
-
{ threshold: 0.1 }
|
|
22
|
-
);
|
|
23
|
-
observer.observe(sentinelRef.current);
|
|
24
|
-
return () => observer.disconnect();
|
|
25
|
-
}, [onMore]);
|
|
26
|
-
return (
|
|
27
|
-
<tr ref={sentinelRef} className="usx-table__sentinel" aria-hidden="true">
|
|
28
|
-
<td />
|
|
29
|
-
</tr>
|
|
30
|
-
);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// ─── Helpers ─────────────────────────────────────────────────────────────────
|
|
34
|
-
|
|
35
|
-
function getDataLabel(col: TableColumn): string | undefined {
|
|
36
|
-
if (typeof col.header === 'string') return col.header;
|
|
37
|
-
return col.stackLabel;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
// Parent/group columns exist only in the header; body cells render for leaves only.
|
|
41
|
-
function getLeafCols(cols: TableColumn[]): TableColumn[] {
|
|
42
|
-
return cols.reduce<TableColumn[]>((acc, col) => {
|
|
43
|
-
if (col.hidden) return acc;
|
|
44
|
-
if (col.columns?.length) return acc.concat(getLeafCols(col.columns));
|
|
45
|
-
return acc.concat(col);
|
|
46
|
-
}, []);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
// ─── Single data row ─────────────────────────────────────────────────────────
|
|
50
|
-
|
|
51
|
-
function DataRow({ row, rowIndex }: { row: TableRowData; rowIndex: number }) {
|
|
52
|
-
const {
|
|
53
|
-
columns,
|
|
54
|
-
primaryKey,
|
|
55
|
-
selectionMode,
|
|
56
|
-
selectionPosition,
|
|
57
|
-
isSelected,
|
|
58
|
-
handleSelect,
|
|
59
|
-
disabled: disabledKeys,
|
|
60
|
-
responsive,
|
|
61
|
-
rowDetails,
|
|
62
|
-
toggleRow,
|
|
63
|
-
isRowExpanded,
|
|
64
|
-
} = useTableContext();
|
|
65
|
-
|
|
66
|
-
const selectionUidPrefix = useId();
|
|
67
|
-
const key = row[primaryKey] as RowKey;
|
|
68
|
-
const selected = isSelected(key);
|
|
69
|
-
const isDisabled = Array.isArray(disabledKeys) && disabledKeys.includes(key);
|
|
70
|
-
const visibleCols = getLeafCols(columns);
|
|
71
|
-
const hasRowDetails = !!rowDetails;
|
|
72
|
-
const isExpanded = isRowExpanded(key);
|
|
73
|
-
|
|
74
|
-
const renderDetail = typeof rowDetails === 'function' ? rowDetails : rowDetails?.render || null;
|
|
75
|
-
const expandLabel = rowDetails && typeof rowDetails === 'object' ? rowDetails.expandLabel : undefined;
|
|
76
|
-
|
|
77
|
-
const totalCols = visibleCols.length + (selectionMode ? 1 : 0) + (hasRowDetails ? 1 : 0);
|
|
78
|
-
|
|
79
|
-
function handleToggleDetail(e: React.MouseEvent) {
|
|
80
|
-
e.stopPropagation();
|
|
81
|
-
toggleRow(key);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
const isRadio = selectionMode === 'radio';
|
|
85
|
-
const selectionInputId = `${selectionUidPrefix}-select`;
|
|
86
|
-
|
|
87
|
-
// Real USWDS checkbox visuals are drawn on the <label> (::before), so use the
|
|
88
|
-
// Checkbox component for identical themed markup. It is internally
|
|
89
|
-
// uncontrolled (defaultChecked), so key it on the selected state to force a
|
|
90
|
-
// remount whenever selection changes programmatically (e.g. "select all").
|
|
91
|
-
const selectionCell = (
|
|
92
|
-
<td key="__selection__" className="usx-table__cell usx-table__cell--selection">
|
|
93
|
-
{isRadio ? (
|
|
94
|
-
<div className="usa-radio usx-table__radio">
|
|
95
|
-
<input
|
|
96
|
-
type="radio"
|
|
97
|
-
id={selectionInputId}
|
|
98
|
-
className="usa-radio__input"
|
|
99
|
-
checked={selected}
|
|
100
|
-
disabled={isDisabled}
|
|
101
|
-
aria-label={`Select row ${rowIndex + 1}`}
|
|
102
|
-
onChange={() => handleSelect(key)}
|
|
103
|
-
onClick={(e) => e.stopPropagation()}
|
|
104
|
-
/>
|
|
105
|
-
<label className="usa-radio__label" htmlFor={selectionInputId} />
|
|
106
|
-
</div>
|
|
107
|
-
) : (
|
|
108
|
-
<Checkbox
|
|
109
|
-
key={selected ? 'checked' : 'unchecked'}
|
|
110
|
-
id={selectionInputId}
|
|
111
|
-
checked={selected}
|
|
112
|
-
disabled={isDisabled}
|
|
113
|
-
ariaLabel={`Select row ${rowIndex + 1}`}
|
|
114
|
-
onChange={() => handleSelect(key)}
|
|
115
|
-
onClick={(e) => e.stopPropagation()}
|
|
116
|
-
className="usx-table__checkbox"
|
|
117
|
-
/>
|
|
118
|
-
)}
|
|
119
|
-
</td>
|
|
120
|
-
);
|
|
121
|
-
|
|
122
|
-
const detailToggleCell = hasRowDetails ? (
|
|
123
|
-
<td key="__detail-toggle__" className="usx-table__cell usx-table__cell--detail-toggle" onClick={handleToggleDetail}>
|
|
124
|
-
<Button
|
|
125
|
-
variant="inverse"
|
|
126
|
-
ghost={true}
|
|
127
|
-
className={classnames(
|
|
128
|
-
'usx-table__detail-toggle',
|
|
129
|
-
)}
|
|
130
|
-
aria-expanded={isExpanded}
|
|
131
|
-
aria-label={
|
|
132
|
-
expandLabel
|
|
133
|
-
? typeof expandLabel === 'function'
|
|
134
|
-
? expandLabel(row)
|
|
135
|
-
: expandLabel
|
|
136
|
-
: `${isExpanded ? 'Collapse' : 'Expand'} row details`
|
|
137
|
-
}
|
|
138
|
-
iconProps={[
|
|
139
|
-
{
|
|
140
|
-
name: isExpanded ? 'expand_less' : 'expand_more',
|
|
141
|
-
'aria-hidden': 'true',
|
|
142
|
-
size: 3
|
|
143
|
-
}
|
|
144
|
-
]}
|
|
145
|
-
/>
|
|
146
|
-
</td>
|
|
147
|
-
) : null;
|
|
148
|
-
|
|
149
|
-
const dataCells = visibleCols.map((col) => {
|
|
150
|
-
const Tag = col.primary ? 'th' : 'td';
|
|
151
|
-
const cellClasses = classnames('usx-table__cell', col.align && `usx-table__cell--align-${col.align}`, col.className);
|
|
152
|
-
const dataLabel = responsive === 'stack' || responsive === 'stack-header' ? getDataLabel(col) : undefined;
|
|
153
|
-
const content = col.render ? col.render(row, rowIndex) : (row[col.key] as React.ReactNode);
|
|
154
|
-
|
|
155
|
-
return (
|
|
156
|
-
<Tag
|
|
157
|
-
key={col.key}
|
|
158
|
-
className={cellClasses}
|
|
159
|
-
scope={col.primary ? 'row' : undefined}
|
|
160
|
-
data-label={dataLabel}
|
|
161
|
-
style={col.width ? { width: col.width } : undefined}
|
|
162
|
-
>
|
|
163
|
-
{content ?? '--'}
|
|
164
|
-
</Tag>
|
|
165
|
-
);
|
|
166
|
-
});
|
|
167
|
-
|
|
168
|
-
const cells: React.ReactNode[] = [];
|
|
169
|
-
if (detailToggleCell) cells.push(detailToggleCell);
|
|
170
|
-
if (selectionMode && selectionPosition === 'left') cells.push(selectionCell);
|
|
171
|
-
cells.push(...dataCells);
|
|
172
|
-
if (selectionMode && selectionPosition === 'right') cells.push(selectionCell);
|
|
173
|
-
|
|
174
|
-
return (
|
|
175
|
-
<>
|
|
176
|
-
<TableRow rowData={row} selected={selected} disabled={isDisabled}>
|
|
177
|
-
{cells}
|
|
178
|
-
</TableRow>
|
|
179
|
-
|
|
180
|
-
{hasRowDetails && isExpanded && renderDetail && (
|
|
181
|
-
<tr className="usx-table__detail-row">
|
|
182
|
-
<td className="usx-table__detail-cell" colSpan={totalCols}>
|
|
183
|
-
{renderDetail(row)}
|
|
184
|
-
</td>
|
|
185
|
-
</tr>
|
|
186
|
-
)}
|
|
187
|
-
</>
|
|
188
|
-
);
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
// ─── TableBody ───────────────────────────────────────────────────────────────
|
|
192
|
-
|
|
193
|
-
export type TableBodyProps = React.HTMLAttributes<HTMLTableSectionElement>;
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
* Renders `<tbody>`. Compound mode: pass children directly. Data-driven mode:
|
|
197
|
-
* renders rows from the table's data, respecting groups.
|
|
198
|
-
*/
|
|
199
|
-
export default function TableBody({ className = '', children, ...props }: TableBodyProps) {
|
|
200
|
-
const { data, primaryKey, groupBy, groups, placeholder, totalCols, onMore, loading } = useTableContext();
|
|
201
|
-
const bodyClass = classnames('usx-table__body', className);
|
|
202
|
-
|
|
203
|
-
if (children) {
|
|
204
|
-
return (
|
|
205
|
-
<tbody className={bodyClass} {...props}>
|
|
206
|
-
{children}
|
|
207
|
-
</tbody>
|
|
208
|
-
);
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
const placeholderRow =
|
|
212
|
-
data.length === 0 && placeholder ? (
|
|
213
|
-
<tr className="usx-table__placeholder-row">
|
|
214
|
-
<td className="usx-table__placeholder-cell" colSpan={totalCols}>
|
|
215
|
-
{placeholder}
|
|
216
|
-
</td>
|
|
217
|
-
</tr>
|
|
218
|
-
) : null;
|
|
219
|
-
|
|
220
|
-
const sentinel = onMore ? <OnMoreSentinel onMore={onMore} /> : null;
|
|
221
|
-
|
|
222
|
-
const loadingRow = loading ? (
|
|
223
|
-
<tr className="usx-table__loading-row" aria-live="polite">
|
|
224
|
-
<td className="usx-table__loading-cell" colSpan={totalCols}>
|
|
225
|
-
<Spinner size={3} label="Loading more rows…" />
|
|
226
|
-
</td>
|
|
227
|
-
</tr>
|
|
228
|
-
) : null;
|
|
229
|
-
|
|
230
|
-
const renderRows = (rows: TableRowData[]) =>
|
|
231
|
-
rows.map((row, rowIndex) => <DataRow key={(row[primaryKey] as RowKey) ?? rowIndex} row={row} rowIndex={rowIndex} />);
|
|
232
|
-
|
|
233
|
-
if (!groupBy) {
|
|
234
|
-
return (
|
|
235
|
-
<tbody className={bodyClass} {...props}>
|
|
236
|
-
{placeholderRow}
|
|
237
|
-
{renderRows(data)}
|
|
238
|
-
{loadingRow}
|
|
239
|
-
{sentinel}
|
|
240
|
-
</tbody>
|
|
241
|
-
);
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
return (
|
|
245
|
-
<tbody className={bodyClass} {...props}>
|
|
246
|
-
{placeholderRow}
|
|
247
|
-
{(groups || []).map(({ key, rows }) => (
|
|
248
|
-
<TableGroup key={key} groupKey={key} label={key}>
|
|
249
|
-
{renderRows(rows)}
|
|
250
|
-
</TableGroup>
|
|
251
|
-
))}
|
|
252
|
-
{loadingRow}
|
|
253
|
-
{sentinel}
|
|
254
|
-
</tbody>
|
|
255
|
-
);
|
|
256
|
-
}
|
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
import { createContext, useContext } from 'react';
|
|
2
|
-
import type { MouseEvent, KeyboardEvent, ReactNode } from 'react';
|
|
3
|
-
import type {
|
|
4
|
-
GroupBy,
|
|
5
|
-
Responsive,
|
|
6
|
-
RowDetails,
|
|
7
|
-
RowKey,
|
|
8
|
-
SelectionMode,
|
|
9
|
-
SortState,
|
|
10
|
-
TableColumn,
|
|
11
|
-
TableGroupEntry,
|
|
12
|
-
TableRowData,
|
|
13
|
-
} from './types';
|
|
14
|
-
|
|
15
|
-
export interface TableContextValue {
|
|
16
|
-
id: string;
|
|
17
|
-
columns: TableColumn[];
|
|
18
|
-
data: TableRowData[];
|
|
19
|
-
primaryKey: string;
|
|
20
|
-
sortState: SortState;
|
|
21
|
-
handleSort: (key: string) => void;
|
|
22
|
-
selectionMode: SelectionMode;
|
|
23
|
-
selectionPosition: 'left' | 'right';
|
|
24
|
-
selectedKeys: Set<RowKey>;
|
|
25
|
-
handleSelect: (key: RowKey) => void;
|
|
26
|
-
handleSelectAll: () => void;
|
|
27
|
-
isSelected: (key: RowKey) => boolean;
|
|
28
|
-
isAllSelected: boolean;
|
|
29
|
-
isIndeterminate: boolean;
|
|
30
|
-
allowSelectAll: boolean;
|
|
31
|
-
disabled: RowKey[];
|
|
32
|
-
onClickRow: ((row: TableRowData, e: MouseEvent | KeyboardEvent) => void) | null;
|
|
33
|
-
groupBy: GroupBy;
|
|
34
|
-
groups: TableGroupEntry[] | null;
|
|
35
|
-
expandedGroups: Set<string>;
|
|
36
|
-
toggleGroup: (key: string) => void;
|
|
37
|
-
isGroupExpanded: (key: string) => boolean;
|
|
38
|
-
expandedRows: Set<RowKey>;
|
|
39
|
-
toggleRow: (key: RowKey) => void;
|
|
40
|
-
isRowExpanded: (key: RowKey) => boolean;
|
|
41
|
-
rowDetails: RowDetails;
|
|
42
|
-
responsive: Responsive;
|
|
43
|
-
stickyFirstColumn: boolean;
|
|
44
|
-
totalCols: number;
|
|
45
|
-
placeholder: ReactNode;
|
|
46
|
-
onMore: (() => void) | null;
|
|
47
|
-
loading: boolean;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export const TableContext = createContext<TableContextValue>({
|
|
51
|
-
id: '',
|
|
52
|
-
columns: [],
|
|
53
|
-
data: [],
|
|
54
|
-
primaryKey: 'id',
|
|
55
|
-
sortState: { key: null, direction: null },
|
|
56
|
-
handleSort: () => {},
|
|
57
|
-
selectionMode: null,
|
|
58
|
-
selectionPosition: 'left',
|
|
59
|
-
selectedKeys: new Set(),
|
|
60
|
-
handleSelect: () => {},
|
|
61
|
-
handleSelectAll: () => {},
|
|
62
|
-
isSelected: () => false,
|
|
63
|
-
isAllSelected: false,
|
|
64
|
-
isIndeterminate: false,
|
|
65
|
-
allowSelectAll: true,
|
|
66
|
-
disabled: [],
|
|
67
|
-
onClickRow: null,
|
|
68
|
-
groupBy: null,
|
|
69
|
-
groups: null,
|
|
70
|
-
expandedGroups: new Set(),
|
|
71
|
-
toggleGroup: () => {},
|
|
72
|
-
isGroupExpanded: () => true,
|
|
73
|
-
expandedRows: new Set(),
|
|
74
|
-
toggleRow: () => {},
|
|
75
|
-
isRowExpanded: () => false,
|
|
76
|
-
rowDetails: null,
|
|
77
|
-
responsive: false,
|
|
78
|
-
stickyFirstColumn: false,
|
|
79
|
-
totalCols: 0,
|
|
80
|
-
placeholder: null,
|
|
81
|
-
onMore: null,
|
|
82
|
-
loading: false,
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
export function useTableContext() {
|
|
86
|
-
return useContext(TableContext);
|
|
87
|
-
}
|
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import classnames from 'classnames';
|
|
3
|
-
import { useTableContext } from './TableContext';
|
|
4
|
-
|
|
5
|
-
// `onToggle` exists on HTMLAttributes as a DOM event; the group API redefines it.
|
|
6
|
-
export interface TableGroupProps extends Omit<React.HTMLAttributes<HTMLTableRowElement>, 'onToggle'> {
|
|
7
|
-
/** Display label for the group header. */
|
|
8
|
-
label: React.ReactNode;
|
|
9
|
-
/** Unique key identifying this group (used for context-managed expand state). */
|
|
10
|
-
groupKey: string;
|
|
11
|
-
/** Controlled expanded state; omit to use the table's own group state. */
|
|
12
|
-
expanded?: boolean;
|
|
13
|
-
/** Controlled toggle handler. */
|
|
14
|
-
onToggle?: (groupKey: string) => void;
|
|
15
|
-
/** colSpan for the group header cell. Defaults to totalCols from context. */
|
|
16
|
-
colSpan?: number;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/** Expandable group subheader row inside `<tbody>`. */
|
|
20
|
-
export default function TableGroup({
|
|
21
|
-
label,
|
|
22
|
-
groupKey,
|
|
23
|
-
expanded: expandedProp,
|
|
24
|
-
onToggle: onToggleProp,
|
|
25
|
-
colSpan,
|
|
26
|
-
className = '',
|
|
27
|
-
children,
|
|
28
|
-
...props
|
|
29
|
-
}: TableGroupProps) {
|
|
30
|
-
const { toggleGroup, isGroupExpanded, totalCols } = useTableContext();
|
|
31
|
-
|
|
32
|
-
const isExpanded = expandedProp !== undefined ? expandedProp : isGroupExpanded(groupKey);
|
|
33
|
-
|
|
34
|
-
const handleToggle = (e: React.SyntheticEvent) => {
|
|
35
|
-
e.stopPropagation();
|
|
36
|
-
if (onToggleProp) onToggleProp(groupKey);
|
|
37
|
-
else toggleGroup(groupKey);
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
const handleKeyDown = (e: React.KeyboardEvent<HTMLButtonElement>) => {
|
|
41
|
-
if (e.key === 'Enter' || e.key === ' ') {
|
|
42
|
-
e.preventDefault();
|
|
43
|
-
handleToggle(e);
|
|
44
|
-
}
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
return (
|
|
48
|
-
<>
|
|
49
|
-
<tr className={classnames('usx-table__group-row', className)} {...props}>
|
|
50
|
-
<td colSpan={colSpan || totalCols || 1} className="usx-table__group-cell">
|
|
51
|
-
<div className="usx-accordion usa-accordion">
|
|
52
|
-
<h4 className="usa-accordion__heading">
|
|
53
|
-
<button
|
|
54
|
-
type="button"
|
|
55
|
-
className="usa-accordion__button usx-table__group-toggle"
|
|
56
|
-
aria-expanded={isExpanded}
|
|
57
|
-
onClick={handleToggle}
|
|
58
|
-
onKeyDown={handleKeyDown}
|
|
59
|
-
>
|
|
60
|
-
{label}
|
|
61
|
-
</button>
|
|
62
|
-
</h4>
|
|
63
|
-
</div>
|
|
64
|
-
</td>
|
|
65
|
-
</tr>
|
|
66
|
-
{isExpanded && children}
|
|
67
|
-
</>
|
|
68
|
-
);
|
|
69
|
-
}
|
|
@@ -1,139 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import classnames from 'classnames';
|
|
3
|
-
import { useTableContext } from './TableContext';
|
|
4
|
-
import Icon from '../icon/Icon';
|
|
5
|
-
import Checkbox from '../checkbox/Checkbox';
|
|
6
|
-
import type { SortDirection, TableColumn } from './types';
|
|
7
|
-
|
|
8
|
-
// ─── Multi-level header helpers ──────────────────────────────────────────────
|
|
9
|
-
|
|
10
|
-
function getDepth(columns: TableColumn[]): number {
|
|
11
|
-
let max = 1;
|
|
12
|
-
for (const col of columns) {
|
|
13
|
-
if (col.columns?.length) max = Math.max(max, 1 + getDepth(col.columns));
|
|
14
|
-
}
|
|
15
|
-
return max;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function getLeafCount(col: TableColumn): number {
|
|
19
|
-
if (!col.columns?.length) return 1;
|
|
20
|
-
return col.columns.reduce((sum, c) => sum + getLeafCount(c), 0);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
interface HeaderCell {
|
|
24
|
-
col: TableColumn;
|
|
25
|
-
rowSpan: number;
|
|
26
|
-
colSpan: number;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
// One array of cells per header level; leaf columns span down to the last level.
|
|
30
|
-
function buildHeaderRows(columns: TableColumn[], totalDepth: number): HeaderCell[][] {
|
|
31
|
-
const rows: HeaderCell[][] = Array.from({ length: totalDepth }, () => []);
|
|
32
|
-
function traverse(cols: TableColumn[], level: number) {
|
|
33
|
-
for (const col of cols) {
|
|
34
|
-
const isLeaf = !col.columns?.length;
|
|
35
|
-
rows[level].push({ col, rowSpan: isLeaf ? totalDepth - level : 1, colSpan: isLeaf ? 1 : getLeafCount(col) });
|
|
36
|
-
if (!isLeaf) traverse(col.columns!, level + 1);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
traverse(columns, 0);
|
|
40
|
-
return rows;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
function SortIcon({ direction }: { direction: SortDirection }) {
|
|
44
|
-
const name = direction === 'asc' ? 'arrow_upward' : direction === 'desc' ? 'arrow_downward' : 'sort_arrow';
|
|
45
|
-
return <Icon name={name} aria-hidden="true" />;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export type TableHeadProps = React.HTMLAttributes<HTMLTableSectionElement>;
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Renders `<thead>`. Compound mode: wrap your own `<tr>`/`<th>` children.
|
|
52
|
-
* Data-driven mode: pass no children; renders from the table's columns.
|
|
53
|
-
*/
|
|
54
|
-
export default function TableHead({ className = '', children, ...props }: TableHeadProps) {
|
|
55
|
-
const { id, columns, sortState, handleSort, selectionMode, selectionPosition, handleSelectAll, allowSelectAll, rowDetails } =
|
|
56
|
-
useTableContext();
|
|
57
|
-
|
|
58
|
-
if (children) {
|
|
59
|
-
return (
|
|
60
|
-
<thead className={classnames('usx-table__head', className)} {...props}>
|
|
61
|
-
{children}
|
|
62
|
-
</thead>
|
|
63
|
-
);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const visibleCols = columns.filter((c) => !c.hidden);
|
|
67
|
-
const totalDepth = getDepth(visibleCols);
|
|
68
|
-
const headerRows = buildHeaderRows(visibleCols, totalDepth);
|
|
69
|
-
const hasRowDetails = !!rowDetails;
|
|
70
|
-
const hasSelection = !!selectionMode;
|
|
71
|
-
|
|
72
|
-
const selectAllCell = (
|
|
73
|
-
<th scope="col" rowSpan={totalDepth} className="usx-table__cell usx-table__cell--selection">
|
|
74
|
-
{selectionMode === 'checkbox' && allowSelectAll && (
|
|
75
|
-
<Checkbox id={`${id}-select-all`} onChange={handleSelectAll} ariaLabel="Select all rows" className="usx-table__checkbox" />
|
|
76
|
-
)}
|
|
77
|
-
</th>
|
|
78
|
-
);
|
|
79
|
-
|
|
80
|
-
return (
|
|
81
|
-
<thead className={classnames('usx-table__head', className)} {...props}>
|
|
82
|
-
{/* Sort announcement region for screen readers */}
|
|
83
|
-
<tr hidden aria-hidden="true">
|
|
84
|
-
<td>
|
|
85
|
-
<span className="usa-sr-only usa-table__announcement-region" aria-live="polite" id={`${id}-sort-announcement`} />
|
|
86
|
-
</td>
|
|
87
|
-
</tr>
|
|
88
|
-
|
|
89
|
-
{headerRows.map((row, rowIndex) => (
|
|
90
|
-
<tr key={rowIndex} className="usx-table__head-row">
|
|
91
|
-
{hasRowDetails && rowIndex === 0 && (
|
|
92
|
-
<th scope="col" rowSpan={totalDepth} className="usx-table__cell usx-table__cell--detail-toggle" aria-label="Row details" />
|
|
93
|
-
)}
|
|
94
|
-
{hasSelection && selectionPosition === 'left' && rowIndex === 0 && selectAllCell}
|
|
95
|
-
|
|
96
|
-
{row.map(({ col, rowSpan, colSpan }) => {
|
|
97
|
-
const isSortable = col.sortable === true;
|
|
98
|
-
const sortDir = sortState.key === col.key ? sortState.direction : null;
|
|
99
|
-
const cellClasses = classnames(
|
|
100
|
-
'usx-table__cell',
|
|
101
|
-
col.headerAlign && `usx-table__cell--align-${col.headerAlign}`,
|
|
102
|
-
!col.headerAlign && col.align && `usx-table__cell--align-${col.align}`,
|
|
103
|
-
col.headerClassName
|
|
104
|
-
);
|
|
105
|
-
const ariaSort = !isSortable ? undefined : sortDir === 'asc' ? 'ascending' : sortDir === 'desc' ? 'descending' : undefined;
|
|
106
|
-
|
|
107
|
-
return (
|
|
108
|
-
<th
|
|
109
|
-
key={col.key}
|
|
110
|
-
scope="col"
|
|
111
|
-
className={cellClasses}
|
|
112
|
-
rowSpan={rowSpan > 1 ? rowSpan : undefined}
|
|
113
|
-
colSpan={colSpan > 1 ? colSpan : undefined}
|
|
114
|
-
style={col.width ? { width: col.width } : undefined}
|
|
115
|
-
aria-sort={ariaSort}
|
|
116
|
-
data-sortable={isSortable || undefined}
|
|
117
|
-
>
|
|
118
|
-
{isSortable ? (
|
|
119
|
-
<button
|
|
120
|
-
type="button"
|
|
121
|
-
className={classnames('usx-table__sort-button', sortDir && 'usx-table__sort-button--active')}
|
|
122
|
-
onClick={() => handleSort(col.key)}
|
|
123
|
-
>
|
|
124
|
-
{col.header}
|
|
125
|
-
<SortIcon direction={sortDir} />
|
|
126
|
-
</button>
|
|
127
|
-
) : (
|
|
128
|
-
col.header
|
|
129
|
-
)}
|
|
130
|
-
</th>
|
|
131
|
-
);
|
|
132
|
-
})}
|
|
133
|
-
|
|
134
|
-
{hasSelection && selectionPosition === 'right' && rowIndex === 0 && selectAllCell}
|
|
135
|
-
</tr>
|
|
136
|
-
))}
|
|
137
|
-
</thead>
|
|
138
|
-
);
|
|
139
|
-
}
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import classnames from 'classnames';
|
|
3
|
-
import { useTableContext } from './TableContext';
|
|
4
|
-
import type { TableRowData } from './types';
|
|
5
|
-
|
|
6
|
-
export interface TableRowProps extends Omit<React.HTMLAttributes<HTMLTableRowElement>, 'onClick'> {
|
|
7
|
-
/** The data object for this row (passed to the table-level onClickRow). */
|
|
8
|
-
rowData?: TableRowData | null;
|
|
9
|
-
selected?: boolean;
|
|
10
|
-
disabled?: boolean;
|
|
11
|
-
onClick?: (e: React.MouseEvent<HTMLTableRowElement> | React.KeyboardEvent<HTMLTableRowElement>) => void;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/** Renders a `<tr>`; created internally in data-driven mode, or directly by compound consumers. */
|
|
15
|
-
export default function TableRow({
|
|
16
|
-
rowData = null,
|
|
17
|
-
selected = false,
|
|
18
|
-
disabled = false,
|
|
19
|
-
className = '',
|
|
20
|
-
onClick,
|
|
21
|
-
children,
|
|
22
|
-
...props
|
|
23
|
-
}: TableRowProps) {
|
|
24
|
-
const { onClickRow } = useTableContext();
|
|
25
|
-
|
|
26
|
-
const isClickable = !!(onClick || onClickRow);
|
|
27
|
-
const classes = classnames(
|
|
28
|
-
'usx-table__row',
|
|
29
|
-
selected && 'usx-table__row--selected',
|
|
30
|
-
disabled && 'usx-table__row--disabled',
|
|
31
|
-
isClickable && !disabled && 'usx-table__row--clickable',
|
|
32
|
-
className
|
|
33
|
-
);
|
|
34
|
-
|
|
35
|
-
function handleClick(e: React.MouseEvent<HTMLTableRowElement> | React.KeyboardEvent<HTMLTableRowElement>) {
|
|
36
|
-
if (disabled) return;
|
|
37
|
-
onClick?.(e);
|
|
38
|
-
if (onClickRow && rowData) onClickRow(rowData, e);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function handleKeyDown(e: React.KeyboardEvent<HTMLTableRowElement>) {
|
|
42
|
-
if (disabled) return;
|
|
43
|
-
if (e.key === 'Enter' || e.key === ' ') {
|
|
44
|
-
e.preventDefault();
|
|
45
|
-
handleClick(e);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
const interactiveProps = isClickable && !disabled ? { tabIndex: 0, onClick: handleClick, onKeyDown: handleKeyDown } : {};
|
|
50
|
-
|
|
51
|
-
return (
|
|
52
|
-
<tr className={classes} {...interactiveProps} {...props}>
|
|
53
|
-
{children}
|
|
54
|
-
</tr>
|
|
55
|
-
);
|
|
56
|
-
}
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { useState, useCallback, useMemo } from 'react';
|
|
2
|
-
import type { RowDetails, RowKey } from '../types';
|
|
3
|
-
|
|
4
|
-
export interface UseRowDetailsOptions {
|
|
5
|
-
rowDetails: RowDetails;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export function useRowDetails({ rowDetails }: UseRowDetailsOptions) {
|
|
9
|
-
const config = rowDetails && typeof rowDetails === 'object' ? rowDetails : null;
|
|
10
|
-
const expandProp = config?.expand;
|
|
11
|
-
const onExpandProp = config?.onExpand;
|
|
12
|
-
const isControlled = expandProp !== undefined && expandProp !== null;
|
|
13
|
-
|
|
14
|
-
const [internalExpanded, setInternalExpanded] = useState<Set<RowKey>>(() => new Set(expandProp || []));
|
|
15
|
-
const expandedRows = useMemo(
|
|
16
|
-
() => (isControlled ? new Set<RowKey>(expandProp || []) : internalExpanded),
|
|
17
|
-
[isControlled, expandProp, internalExpanded]
|
|
18
|
-
);
|
|
19
|
-
|
|
20
|
-
const toggleRow = useCallback(
|
|
21
|
-
(key: RowKey) => {
|
|
22
|
-
const next = new Set(expandedRows);
|
|
23
|
-
if (next.has(key)) next.delete(key);
|
|
24
|
-
else next.add(key);
|
|
25
|
-
if (!isControlled) setInternalExpanded(next);
|
|
26
|
-
onExpandProp?.([...next]);
|
|
27
|
-
},
|
|
28
|
-
[expandedRows, isControlled, onExpandProp]
|
|
29
|
-
);
|
|
30
|
-
|
|
31
|
-
const isRowExpanded = useCallback((key: RowKey) => expandedRows.has(key), [expandedRows]);
|
|
32
|
-
|
|
33
|
-
return { expandedRows, toggleRow, isRowExpanded };
|
|
34
|
-
}
|