`), `Cell` (``; in a grid one body cell is the Tab stop — arrows, Home /
* End, Ctrl+Home / End move it, Enter / Space press a control in it; an editable cell opens its editor on
* Enter, F2, a double click, a `data-edit-icon` click or typing), `CellValue`, `CellInput` / `CellSelect`
* (the editor: Enter saves, Escape cancels, Tab / Shift+Tab save and edit the next / previous editable
* cell, leaving saves, `validate` keeps it open) with `CellError`, `EditHint` ("Press Enter to edit").
* - `SelectAll` and `SelectRow` (native checkboxes, select-all as Carbon: it clears when any row is
* selected; with `radio` a radio per row, one Tab stop, the arrows move and select, wrapping).
* - `ExpandToggle`, `ExpandAll` (buttons with `aria-expanded` / `aria-controls`) and `ExpandedRow`.
* - `Filler`: the empty cell after resized columns.
*
* State shows as data attributes to style on: `data-direction` on `SortButton`, `data-sortable` on
* `ColumnHeader`, `data-selected` / `data-expanded` on `Row`, `data-editable` / `data-editing` on `Cell`,
* `data-checked` / `data-indeterminate` on the selection checkboxes, `data-expanded` on the toggles,
* `data-resizing` on a header while dragged.
*
* ```tsx
* const table = useDataTable({ headers, rows, sortable: true, selectable: true });
*
*
*
* |
* {table.columns.map((c) => {c.header})}
*
*
* {table.pageRows.map((row) => (
*
*
* {table.columns.map((c, i) => {String(row[c.key])})}
*
* ))}
*
*
*
* ```
*/
export interface DataTableEditOption {
value: string;
label: string;
}
/** A column (Carbon `headers`): the row property it shows and what the user may do with it. */
export interface DataTableColumn {
/** Property of each row shown in this column. */
key: string;
/** Column title (names the column's controls). */
header: string;
/** Overrides the table's `sortable` for this column. */
sortable?: boolean;
/** The column's cells can be edited in place (makes the table an APG editable grid). */
editable?: CellEditor;
/** Choices of an `editable: 'select'` column. */
options?: DataTableEditOption[];
/** Checks an edited value before it is saved; return a message to keep the editor open, '' to accept. */
validate?: (value: string | number, row: TableRow) => string;
/** Overrides the table's `resizable` for this column. */
resizable?: boolean;
/** Starting width in px (a resized width in `columnWidths` wins). */
width?: number;
/** Narrowest the column can be resized to, in px (default 50). */
minWidth?: number;
/** Widest the column can be resized to, in px (default: the scroll box's width). */
maxWidth?: number;
/** `false`: the column can't be hidden. */
hideable?: boolean;
/** Hidden until shown; resetting the columns hides it again. */
hidden?: boolean;
/** Keeps the column in view while the table scrolls sideways: `'start'` on the first column, `'end'` on the last. */
pinned?: 'start' | 'end';
}
export interface DataTableSort {
key: string;
direction: SortDirection;
}
export interface DataTableExpandChange {
expandedIds: string[];
expanded: boolean;
/** The row that opened / closed; absent for expand-all. */
id?: string;
}
export interface DataTablePageChange {
page: number;
pageSize: number;
}
/** `onColumnsChange`: a column was shown / hidden (`key` '' after a reset). */
export interface DataTableColumnsChange {
hiddenColumns: string[];
key: string;
hidden: boolean;
}
/** `onColumnResize`: `widths` is the new `columnWidths`. */
export interface DataTableColumnResize {
key: string;
width: number;
widths: Record;
}
/** `onCellEdit`: the saved value and `rows` with it (set them, or keep yours to refuse it). */
export interface DataTableCellEdit {
rowId: string;
key: string;
value: string | number;
previous: unknown;
row: R;
rows: R[];
}
export interface UseDataTableOptions {
/** Base of the ids the parts give their elements (default: generated). */
id?: string;
headers: readonly C[];
rows: readonly R[];
/** Every column sorts unless its header sets `sortable: false`. */
sortable?: boolean;
sortKey?: string;
sortDirection?: SortDirection;
defaultSortKey?: string;
defaultSortDirection?: SortDirection;
onSortChange?: (sort: DataTableSort) => void;
/** Keep `rows` in the given order (sort on the server); the headers still show the sort. */
manualSorting?: boolean;
/** Replaces the comparison (Carbon `sortRow`). */
sortRow?: (a: unknown, b: unknown, context: SortContext) => number;
/** Orders the sort (default 'en'). */
locale?: string;
/** Checkboxes with select-all. */
selectable?: boolean;
/** One choice with a radio per row (Carbon `radio`). */
radio?: boolean;
selectedIds?: string[];
defaultSelectedIds?: string[];
onSelectionChange?: (selectedIds: string[], rows: R[]) => void;
filterText?: string;
defaultFilterText?: string;
onFilterChange?: (value: string) => void;
/** Replaces the row test of the search. */
filterRows?: (row: R, text: string, headers: C[]) => boolean;
/** Keep every row; filter yourself on `onFilterChange`. */
manualFiltering?: boolean;
/** Rows open and close (`ExpandToggle`). */
expandable?: boolean;
expandedIds?: string[];
defaultExpandedIds?: string[];
onExpandChange?: (change: DataTableExpandChange) => void;
/** Rows per page (0: one page of every row). */
pageSize?: number;
defaultPageSize?: number;
/** 1-based. */
page?: number;
defaultPage?: number;
onPageChange?: (change: DataTablePageChange) => void;
/** Keys of the columns not shown (controlled; `hideable: false` columns always show). */
hiddenColumns?: string[];
/** Default: the headers marked `hidden`. */
defaultHiddenColumns?: string[];
onColumnsChange?: (change: DataTableColumnsChange) => void;
/** Every column gets a resize handle (a header's `resizable` overrides). */
resizable?: boolean;
/** Column widths in px by key. */
columnWidths?: Record;
defaultColumnWidths?: Record;
onColumnResize?: (change: DataTableColumnResize) => void;
/** The table is as wide as its columns (resized: their sum), not its box. */
staticWidth?: boolean;
/** An edited cell was saved: set `rows` to `edit.rows`. */
onCellEdit?: (edit: DataTableCellEdit) => void;
}
interface Editing {
rowId: string;
key: string;
draft: string;
error: string;
}
/**
* Leading cells that stick with a `pinned: 'start'` first column — expand, select, the column (0: none)
* — and where a `pinned: 'end'` last column sits from the row's end: 1, or 2 before the filler (0: none).
*/
export declare function pinnedCells(columns: readonly {
pinned?: 'start' | 'end';
}[], cells: {
expand: boolean;
select: boolean;
filler: boolean;
}): {
start: number;
end: number;
};
/**
* The state of a data table and what to do with it (see the module's notes): the rows to show
* (`pageRows`), the columns shown (`columns`), actions and prop getters. Give it to `DataTable.Root`
* for the parts.
*/
export declare function useDataTable(options: UseDataTableOptions): {
id: string;
/** Attach to the `` (`DataTable.Table` does). */
tableRef: RefObject;
/**
* Attach to the table's scroll box (default: the table's parent): resizing measures it. Take it
* out first (`const { boxRef } = table`): React Compiler's ref lint reads `ref={table.boxRef}`
* as the whole table being a ref.
*/
boxRef: (node: HTMLElement | null) => void;
/** Every header, shown or hidden. */
headers: readonly C[];
/** The headers shown. */
columns: C[];
rows: readonly R[];
rowById: Map;
/** Rows the search keeps, in the order of `rows`. */
filteredRows: R[];
/** …sorted. */
sortedRows: R[];
/** …of the current page: the rows to render. */
pageRows: R[];
sortKey: string;
sortDirection: SortDirection;
isSortable: (column: DataTableColumn) => boolean;
/** A column's direction (`none` unless it is the sorted one). */
sortDirectionOf: (key: string) => SortDirection;
/** The next step of a column's sort cycle; `onSortChange`. Returns the new direction. */
sortBy: (key: string) => SortDirection;
filterText: string;
/** A new search text (`onFilterChange`; back to page 1). */
setFilterText: (value: string) => void;
paged: boolean;
page: number;
pageSize: number;
pageCount: number;
/** A new page and page size (`onPageChange`). */
setPagination: (change: DataTablePageChange) => void;
selectable: boolean;
radio: boolean;
selectedIds: string[];
isSelected: (rowId: string) => boolean;
setSelectedIds: (next: string[]) => void;
toggleRow: (row: R, checked: boolean) => void;
/** Carbon select-all: clears the page's (or every) row when any is selected, else selects the enabled ones shown. */
toggleAll: () => void;
/** Every enabled row the search keeps, across pages ("Select all N"). */
selectAll: () => void;
allSelected: boolean;
someSelected: boolean;
/** The radio that holds the Tab stop. */
radioStop: string | undefined;
getSelectAllProps: () => {
checked: boolean;
indeterminate: boolean;
onChange: () => void;
};
getSelectRowProps: (row: R) => {
checked: boolean;
disabled: boolean | undefined;
onChange: (event: ChangeEvent) => void;
};
expandable: boolean;
expandedIds: string[];
isExpanded: (rowId: string) => boolean;
allExpanded: boolean;
/** The id of a row's expanded content (`ExpandedRow`). */
expandedRowId: (rowId: string) => string;
toggleExpanded: (rowId: string) => void;
/** Carbon expand-all: opens every row shown unless all are open, then closes them all. */
toggleAllExpanded: () => void;
hiddenColumns: string[];
/** Shows (`show`) or hides a column (`onColumnsChange`); the last one shown stays. */
toggleColumn: (key: string, show: boolean) => void;
/** Back to the headers marked `hidden`. */
resetColumns: () => void;
/** The column can't be hidden (`hideable: false`, or the last one shown). */
isColumnLocked: (key: string) => boolean;
/** The hidden columns differ from the headers' `hidden`. */
columnsChanged: boolean;
/** How many leading cells stick (expand, select, the first column) and where a pinned last column sits. */
pins: {
start: number;
end: number;
};
columnWidths: Record;
resize: {
/** Some column has a resize handle. */
enabled: boolean;
/** Fixed layout: every shown column has a width (after the first resize, or all given). */
fixed: boolean;
/** The empty column after the resized ones takes the rest of the row (`Filler`). */
hasFiller: boolean;
isResizable: (column: DataTableColumn) => boolean;
/** The width a column renders at (undefined: automatic). */
widthOf: (column: DataTableColumn) => number | undefined;
/** The widest the handle goes, for render. */
maxOf: (column: DataTableColumn) => number;
/** A static-width table's own width once fixed. */
tableStyle: CSSProperties | undefined;
handlers: {
onPointerDown: (event: PointerEvent) => void;
onPointerMove: (event: PointerEvent) => void;
onPointerEnd: (event: PointerEvent) => void;
onKeyDown: (event: KeyboardEvent) => void;
/** A key press or leaving the handle ends a keyboard resize. */
onKeyUp: () => void;
/** A value set some other way (assistive technology increments the slider). */
onChange: (event: {
currentTarget: HTMLInputElement;
}) => void;
onFocus: () => void;
};
};
grid: {
/** A column is `editable`: the table is an APG grid. */
enabled: boolean;
hintId: string;
errorId: string;
/** The cell being edited, if it is this one. */
editingAt: (rowId: string, key: string) => Editing | null;
cellAttributes: (rowId: string, cell: number, editable?: boolean) => {
tabIndex: number;
'aria-readonly': true | undefined;
} | {
tabIndex?: undefined;
'aria-readonly'?: undefined;
};
setDraft: (draft: string) => void;
onEditorKeyDown: (event: KeyboardEvent) => void;
onEditorBlur: (event: FocusEvent) => void;
bodyHandlers: {
onKeyDown: (event: KeyboardEvent) => void;
onFocus: (event: FocusEvent) => void;
onClick: (event: MouseEvent) => void;
onDoubleClick: (event: MouseEvent) => void;
};
};
};
export type DataTableInstance = ReturnType>;
/** The table of the enclosing `DataTable.Root`. */
export declare function useDataTableContext(part?: string): {
id: string;
/** Attach to the `` (`DataTable.Table` does). */
tableRef: RefObject;
/**
* Attach to the table's scroll box (default: the table's parent): resizing measures it. Take it
* out first (`const { boxRef } = table`): React Compiler's ref lint reads `ref={table.boxRef}`
* as the whole table being a ref.
*/
boxRef: (node: HTMLElement | null) => void;
/** Every header, shown or hidden. */
headers: readonly DataTableColumn[];
/** The headers shown. */
columns: DataTableColumn[];
rows: readonly TableRow[];
rowById: Map;
/** Rows the search keeps, in the order of `rows`. */
filteredRows: TableRow[];
/** …sorted. */
sortedRows: TableRow[];
/** …of the current page: the rows to render. */
pageRows: TableRow[];
sortKey: string;
sortDirection: SortDirection;
isSortable: (column: DataTableColumn) => boolean;
/** A column's direction (`none` unless it is the sorted one). */
sortDirectionOf: (key: string) => SortDirection;
/** The next step of a column's sort cycle; `onSortChange`. Returns the new direction. */
sortBy: (key: string) => SortDirection;
filterText: string;
/** A new search text (`onFilterChange`; back to page 1). */
setFilterText: (value: string) => void;
paged: boolean;
page: number;
pageSize: number;
pageCount: number;
/** A new page and page size (`onPageChange`). */
setPagination: (change: DataTablePageChange) => void;
selectable: boolean;
radio: boolean;
selectedIds: string[];
isSelected: (rowId: string) => boolean;
setSelectedIds: (next: string[]) => void;
toggleRow: (row: TableRow, checked: boolean) => void;
/** Carbon select-all: clears the page's (or every) row when any is selected, else selects the enabled ones shown. */
toggleAll: () => void;
/** Every enabled row the search keeps, across pages ("Select all N"). */
selectAll: () => void;
allSelected: boolean;
someSelected: boolean;
/** The radio that holds the Tab stop. */
radioStop: string | undefined;
getSelectAllProps: () => {
checked: boolean;
indeterminate: boolean;
onChange: () => void;
};
getSelectRowProps: (row: TableRow) => {
checked: boolean;
disabled: boolean | undefined;
onChange: (event: ChangeEvent) => void;
};
expandable: boolean;
expandedIds: string[];
isExpanded: (rowId: string) => boolean;
allExpanded: boolean;
/** The id of a row's expanded content (`ExpandedRow`). */
expandedRowId: (rowId: string) => string;
toggleExpanded: (rowId: string) => void;
/** Carbon expand-all: opens every row shown unless all are open, then closes them all. */
toggleAllExpanded: () => void;
hiddenColumns: string[];
/** Shows (`show`) or hides a column (`onColumnsChange`); the last one shown stays. */
toggleColumn: (key: string, show: boolean) => void;
/** Back to the headers marked `hidden`. */
resetColumns: () => void;
/** The column can't be hidden (`hideable: false`, or the last one shown). */
isColumnLocked: (key: string) => boolean;
/** The hidden columns differ from the headers' `hidden`. */
columnsChanged: boolean;
/** How many leading cells stick (expand, select, the first column) and where a pinned last column sits. */
pins: {
start: number;
end: number;
};
columnWidths: Record;
resize: {
/** Some column has a resize handle. */
enabled: boolean;
/** Fixed layout: every shown column has a width (after the first resize, or all given). */
fixed: boolean;
/** The empty column after the resized ones takes the rest of the row (`Filler`). */
hasFiller: boolean;
isResizable: (column: DataTableColumn) => boolean;
/** The width a column renders at (undefined: automatic). */
widthOf: (column: DataTableColumn) => number | undefined;
/** The widest the handle goes, for render. */
maxOf: (column: DataTableColumn) => number;
/** A static-width table's own width once fixed. */
tableStyle: CSSProperties | undefined;
handlers: {
onPointerDown: (event: PointerEvent) => void;
onPointerMove: (event: PointerEvent) => void;
onPointerEnd: (event: PointerEvent) => void;
onKeyDown: (event: KeyboardEvent) => void;
/** A key press or leaving the handle ends a keyboard resize. */
onKeyUp: () => void;
/** A value set some other way (assistive technology increments the slider). */
onChange: (event: {
currentTarget: HTMLInputElement;
}) => void;
onFocus: () => void;
};
};
grid: {
/** A column is `editable`: the table is an APG grid. */
enabled: boolean;
hintId: string;
errorId: string;
/** The cell being edited, if it is this one. */
editingAt: (rowId: string, key: string) => Editing | null;
cellAttributes: (rowId: string, cell: number, editable?: boolean) => {
tabIndex: number;
'aria-readonly': true | undefined;
} | {
tabIndex?: undefined;
'aria-readonly'?: undefined;
};
setDraft: (draft: string) => void;
onEditorKeyDown: (event: KeyboardEvent) => void;
onEditorBlur: (event: FocusEvent) => void;
bodyHandlers: {
onKeyDown: (event: KeyboardEvent) => void;
onFocus: (event: FocusEvent) => void;
onClick: (event: MouseEvent) => void;
onDoubleClick: (event: MouseEvent) => void;
};
};
};
export interface RootProps {
/** What `useDataTable` returned. */
table: DataTableInstance;
children?: ReactNode;
}
export declare function Root({ table, children }: Readonly>): import("react").JSX.Element;
/** The ``: `role="grid"` once a column is `editable`. */
export declare function Table({ ref, ...rest }: Readonly>): import("react").JSX.Element;
/** The ``: the editable grid's keys, clicks and focus. */
export declare function Body(props: Readonly>): import("react").JSX.Element;
export interface ColumnHeaderProps extends ComponentProps<'th'> {
/** The column's key. */
column: string;
}
/** A column's `| `: `aria-sort` when it sorts, its resized width, `data-key`. */
export declare function ColumnHeader({ column: key, ...rest }: Readonly): import("react").JSX.Element;
/** Sorts its column: none → ascending → descending → none (`data-direction`). Describe the next step with `aria-describedby`. */
export declare function SortButton(props: Readonly, 'type'>>): import("react").JSX.Element;
export interface ResizeHandleProps extends Omit, 'type' | 'value' | 'defaultValue' | 'min' | 'max' | 'step'> {
/** The width as words for assistive tech ("120 pixels"). */
valueText?: (width: number) => string;
}
/**
* Resizes its column: a native range input (name it: "Resize Name column") — drag it, or the arrow
* keys (2px; Shift, Page Up / Down 16px; Home / End narrowest / widest). Nothing for a column that
* doesn't resize.
*/
export declare function ResizeHandle({ valueText, ...rest }: Readonly): import("react").JSX.Element | null;
export interface RowProps extends ComponentProps<'tr'> {
rowId: string;
}
/** A body row (`data-row-id`, which the grid and the radios find it by). */
export declare function Row({ rowId, ...rest }: Readonly): import("react").JSX.Element;
interface CellContextValue {
rowId: string;
column?: DataTableColumn;
/** The editor's state while this cell is edited. */
editing: {
draft: string;
error: string;
} | null;
}
/** The enclosing `DataTable.Cell`: its row, column and editing state. */
export declare function useCellContext(part?: string): CellContextValue;
export interface CellProps extends ComponentProps<'td'> {
/** The cell's place in its row (0-based, every cell counted: expand, select, data). */
index: number;
/** The column's key, for a data cell (an `editable` one becomes editable). */
column?: string;
}
/** A body cell: in a grid one of them is the Tab stop (`aria-readonly` unless editable); an editable one opens its editor. */
export declare function Cell({ index, column: key, ...rest }: Readonly): import("react").JSX.Element;
/** The value of an editable cell: a click on it focuses the cell. */
export declare function CellValue(props: Readonly>): import("react").JSX.Element;
/** "Press Enter to edit": the description of every editable cell (rendered only in a grid; hide it visually). */
export declare function EditHint(props: Readonly>): import("react").JSX.Element | null;
export type CellInputProps = Omit, 'type' | 'value' | 'defaultValue'>;
/** The editor of a `text` or `number` cell (name it with `aria-label`: the column and row). */
export declare function CellInput(props: Readonly): import("react").JSX.Element;
export type CellSelectProps = Omit, 'value' | 'defaultValue'>;
/** The editor of a `select` cell: the column's `options` (unless you give the options as children). */
export declare function CellSelect({ children, ...rest }: Readonly): import("react").JSX.Element;
/** The message of an edited value `validate` refused (`role="alert"`; the editor's description). Nothing while valid. */
export declare function CellError({ children, ...rest }: Readonly>): import("react").JSX.Element | null;
export type SelectAllProps = Omit;
/** Select-all (a native checkbox; mixed while some rows are): Carbon's toggle. Nothing in a radio table. */
export declare function SelectAll(props: Readonly): import("react").JSX.Element | null;
export type SelectRowProps = Omit, 'type' | 'checked' | 'defaultChecked' | 'name' | 'value'>;
/**
* Selects its row: a native checkbox, or with `radio` a radio — one Tab stop among them (the chosen
* row, else the first enabled one; none in a grid, where the cell is the stop), the arrows move to the
* next / previous enabled row and select it, wrapping.
*/
export declare function SelectRow(props: Readonly): import("react").JSX.Element | null;
/** Opens / closes its row (`aria-expanded`, `aria-controls` its `ExpandedRow` while open; name it with `aria-label`). */
export declare function ExpandToggle(props: Readonly, 'type'>>): import("react").JSX.Element;
/** Opens every row shown, or closes them all once all are open (Carbon expand-all). */
export declare function ExpandAll(props: Readonly, 'type'>>): import("react").JSX.Element;
export interface ExpandedRowProps extends ComponentProps<'tr'> {
rowId: string;
}
/** The row under an open row, holding its content (the id its toggle controls). */
export declare function ExpandedRow({ rowId, ...rest }: Readonly): import("react").JSX.Element;
/** The empty cell that takes the rest of a row of resized columns (only then); never a grid cell. */
export declare function Filler(props: Readonly>): import("react").JSX.Element | null;
export {};
| |