API
A Table holds Column children, and each column renders a Cell for every row. The other components customize cells and headers.
Usage#
import {
Cell,
Column,
ColumnHeaderCell,
EditableCell,
JSONFormat,
RowHeaderCell,
Table,
TruncatedFormat,
} from "@clawscale/react/table";Table#
The root component. Set numRows and pass Column children. The table fills the height of its container.
interface TableProps extends Props, Partial<RowHeights>, Partial<ColumnWidths>@clawscale/react/table| Props | Description |
|---|---|
bodyContextMenuRenderer | ContextMenuRendererAn optional callback for displaying a context menu when right-clicking on the table body. The callback is supplied with an array of Regions. If the mouse click was on a selection, the array will contain all selected regions. Otherwise it will have one Region that represents the clicked cell. |
cellRendererDependencies | React.DependencyListThis dependency list may be used to trigger a re-render of all cells when one of its elements changes (compared using shallow equality checks). This is done by invalidating the grid, which forces TableQuadrantStack to re-render. |
children | React.ReactElement<ColumnProps> | Array<React.ReactElement<ColumnProps>>The children of a Table component, which must be React elements that use ColumnProps. |
className | stringA space-delimited list of class names to pass along to a child element. Inherited from Props |
columnWidths | Array<number | null | undefined>A sparse number array with a length equal to the number of columns. Any non-null value will be used to set the width of the column at the same index. Note that if you want to update these values when the user drag-resizes a column, you may define a callback for onColumnWidthChanged. |
defaultColumnWidth | numberColumnWidths |
defaultRowHeight | numberRowHeights |
enableBodyContextMenu | boolean = true if bodyContextMenuRenderer is definedWhether the body context menu is enabled. |
enableColumnHeader | boolean = trueIf false, hides the column headers. |
enableColumnInteractionBar | boolean = falseIf true, adds an interaction bar on top of all column header cells, and moves interaction triggers into it. |
enableColumnReordering | boolean = falseIf false, disables reordering of columns. |
enableColumnResizing | boolean = trueIf false, disables resizing of columns. |
enableFocusedCelldeprecated | boolean = falseIf true, there will be a single "focused" cell at all times, which can be used to interact with the table as though it is a spreadsheet. When false, no such cell will exist.When using Table2, use the focusMode prop instead. |
enableGhostCells | boolean = falseIf true, empty space in the table container will be filled with empty cells instead of a blank background. |
enableMultipleSelection | boolean = trueIf false, only a single region of a single column/row/cell may be selected at one time. Using ctrl or meta key will have no effect, and a mouse drag will select the current column/row/cell only. |
enableRowHeader | boolean = trueIf false, hides the row headers and settings menu. |
enableRowReordering | boolean = falseIf false, disables reordering of rows. |
enableRowResizing | boolean = trueIf false, disables resizing of rows. |
focusedCelldeprecated | FocusedCellCoordinatesIf defined, will set the focused cell state. This changes the focused cell to controlled mode, meaning you are in charge of setting the focus in response to events in the onFocusedCell callback.When using Table2, use the focusedRegion prop instead |
focusedRegion | FocusedRegionIf defined, will set the focused region state. This changes the focused region to controlled mode, meaning yo uare in charge of setting the focus in response to events in the onFocusedRegion callback. The shape of the region is defined by the focusMode prop. |
focusMode | FocusMode = undefinedIf this is defined, there will be a single focused cell or row at all times which can be used to interact with the table as though it is a spread sheet. The type of allowed focus area is given by the value. If undefined is passed, then this focus state will be disabled. |
forceRerenderOnSelectionChange | boolean = falseIf true, selection state changes will cause the component to re-render. If false, selection state is ignored when deciding to re-render. |
getCellClipboardData | (row: number, col: number, celRenderer: CellRenderer) => anyIf defined, this callback will be invoked for each cell when the user attempts to copy a selection via mod+c. The returned data will be copied to the clipboard and need not match the display value of the <Cell>. The data will be invisibly added as textContent into the DOM before copying. If not defined, a default implementation will be used that turns the rendered cell elements into strings using 'react-innertext'. |
loadingOptions | TableLoadingOption[]A list of TableLoadingOption. Set this prop to specify whether to render the loading state for the column header, row header, and body sections of the table. |
maxColumnWidth | numberColumnWidths |
maxRowHeight | numberRowHeights |
minColumnWidth | numberColumnWidths |
minRowHeight | numberRowHeights |
numFrozenColumns | number = 0The number of columns to freeze to the left side of the table, counting from the leftmost column. |
numFrozenRows | number = 0The number of rows to freeze to the top of the table, counting from the topmost row. |
numRows | numberThe number of rows in the table. |
onColumnsReordered | (oldIndex: number, newIndex: number, length: number) => voidIf reordering is enabled, this callback will be invoked when the user finishes drag-reordering one or more columns. |
onColumnWidthChanged | IndexedResizeCallbackIf resizing is enabled, this callback will be invoked when the user finishes drag-resizing a column. |
onCompleteRender | () => voidAn optional callback invoked when all cells in view have completely rendered. Will be invoked on initial mount and whenever cells update (e.g., on scroll). |
onCopy | (success: boolean) => voidIf you want to do something after the copy or if you want to notify the user if a copy fails, you may provide this optional callback. |
onFocusedCelldeprecated | (focusedCell: FocusedCellCoordinates) => voidA callback called when the focus is changed in the table. When using Table2, use the onFocusedRegion prop instead |
onFocusedRegion | (focusedRegion: FocusedRegion) => voidA callback called when the focused region is changed in the table. |
onRowHeightChanged | IndexedResizeCallbackIf resizing is enabled, this callback will be invoked when the user finishes drag-resizing a row. |
onRowsReordered | (oldIndex: number, newIndex: number, length: number) => voidIf reordering is enabled, this callback will be invoked when the user finishes drag-reordering one or more rows. |
onSelection | (selectedRegions: Region[]) => voidA callback called when the selection is changed in the table. |
onVisibleCellsChange | (rowIndices: RowIndices, columnIndices: ColumnIndices) => voidA callback called when the visible cell indices change in the table. |
renderMode | RenderMode = RenderMode.BATCH_ON_UPDATEDictates how cells should be rendered. Supported modes are: - RenderMode.BATCH: renders cells in batches to improve performance - RenderMode.BATCH_ON_UPDATE: renders cells synchronously on mount and in batches on update - RenderMode.NONE: renders cells synchronously all at once |
rowHeaderCellRenderer | RowHeaderRendererRender each row's header cell. |
rowHeights | Array<number | null | undefined>A sparse number array with a length equal to the number of rows. Any non-null value will be used to set the height of the row at the same index. Note that if you want to update these values when the user drag-resizes a row, you may define a callback for onRowHeightChanged. |
selectedRegions | Region[]If defined, will set the selected regions in the cells. If defined, this changes table selection to controlled mode, meaning you in charge of setting the selections in response to events in the onSelection callback. |
selectedRegionTransform | SelectedRegionTransformAn optional transform function that will be applied to the located Region. |
selectionModes | RegionCardinality[] = SelectionModes.ALLA SelectionModes enum value indicating the selection mode. You may equivalently provide an array of RegionCardinality enum values for precise configuration. |
styledRegionGroups | StyledRegionGroup[]Styled region groups are rendered as overlays above the table and are marked with their own className for custom styling. |
Instance methods#
Get the instance with a ref, for example useRef<Table>(null).
| Method | What it does |
|---|---|
scrollToRegion(region) | Scrolls a region to the top left of the viewport. |
scrollByOffset({ left, top }) | Scrolls the body by a number of pixels. |
resizeRowsByTallestCell(columns?) | Sets every row to the height of the tallest visible cell. |
resizeRowsByApproximateHeight(getCellText) | Sizes each row to fit its wrapped text, by estimate. Experimental. |
Regions#
A region is a rectangle of cells. Selection, styled overlays and scrollToRegion all take regions. Build them with the Regions helpers.
import { Regions } from "@clawscale/react/table";
Regions.cell(0, 2); // one cell: row 0, column 2
Regions.row(4, 6); // rows 4 to 6
Regions.column(1); // column 1
Regions.table(); // every cellColumn#
One column: its name, its cell renderer and an optional header renderer. Give each column a stable id, so its width survives reordering.
interface ColumnProps extends ColumnNameProps, Props@clawscale/react/table| Props | Description |
|---|---|
cellRenderer | CellRendererAn instance of CellRenderer, a function that takes a row and column index, and returns a Cell React element. |
className | stringA space-delimited list of class names to pass along to a child element. Inherited from Props |
columnHeaderCellRenderer | ColumnHeaderRendererAn instance of ColumnHeaderRenderer, a function that takes a column index and returns a ColumnHeaderCell React element. |
id | string | numberA unique ID, similar to React's key. This is used, for example, to maintain the width of a column between re-ordering and rendering. If no IDs are provided, widths will be persisted across renders using a column's index only. Columns widths can also be persisted outside the Table component, then passed in with the columnWidths prop. |
loadingOptions | ColumnLoadingOption[]Set this prop to specify whether to render the loading state of the column header and cells in this column. Column-level loadingOptions override Table-level loadingOptions. For example, if you set loadingOptions=[ TableLoadingOption.CELLS ] on Table and loadingOptions=[ ColumnLoadingOption.HEADER ] on a Column, the cells in that column will _not_ show their loading state. |
name | stringThe name displayed in the header of the column. Inherited from ColumnNameProps |
nameRenderer | (name: string, index?: number) => React.ReactElement<Props>A callback to override the default name rendering behavior. The default behavior is to simply use the Inherited from ColumnHeaderCells name prop.ColumnNameProps |
Cell#
The content of one cell, returned from a column's cellRenderer. intent colors the cell, wrapText allows several lines and loading shows a skeleton.
interface CellProps extends IntentProps, Props@clawscale/react/table| Props | Description |
|---|---|
cellRef | React.Ref<HTMLDivElement>A ref handle to capture the outer div of this cell. Used internally. |
children | React.ReactNode |
className | stringA space-delimited list of class names to pass along to a child element. Inherited from Props |
columnIndex | numberThe column index of the cell. If provided, this will be passed as an argument to any callbacks when they are invoked. |
intent | IntentVisual intent color to apply to element. Inherited from IntentProps |
interactive | boolean = falseIf true, the cell will be rendered above overlay layers to enable mouse interactions within the cell. |
key | string |
loading | boolean = falseAn optional native tooltip that is displayed on hover. If true, content will be replaced with a fixed-height skeleton. |
onKeyDown | React.KeyboardEventHandler<HTMLElement>Callback invoked when the cell is focused and a key is pressed down. |
onKeyPress | React.KeyboardEventHandler<HTMLElement>Callback invoked when a character-key is pressed. |
onKeyUp | React.KeyboardEventHandler<HTMLElement>Callback invoked when the cell is focused and a key is released. |
rowIndex | numberThe row index of the cell. If provided, this will be passed as an argument to any callbacks when they are invoked. |
style | React.CSSProperties |
tabIndex | numberAllows for setting a tab index on the cell, so the cell can be browser-focusable. |
tooltip | stringAn optional native tooltip that is displayed on hover. |
truncated | boolean = trueIf true, the cell contents will be wrapped in a div with styling that will prevent the content from overflowing the cell. |
wrapText | boolean = falseIf true, the cell contents will be wrapped in a div with styling that will cause text to wrap, rather than displaying it on a single line. |
EditableCell#
A cell that turns into a text input on double click. onConfirm receives the new value.
interface EditableCellProps extends Omit<CellProps, "onKeyDown" | "onKeyUp">@clawscale/react/table| Props | Description |
|---|---|
cellRef | React.Ref<HTMLDivElement>A ref handle to capture the outer div of this cell. Used internally. Inherited from CellProps |
children | React.ReactNodeCellProps |
className | stringA space-delimited list of class names to pass along to a child element. Inherited from Props |
columnIndex | numberThe column index of the cell. If provided, this will be passed as an argument to any callbacks when they are invoked. Inherited from CellProps |
editableTextProps | Omit<EditableTextProps, "elementRef">Props that should be passed to the EditableText when it is used to edit |
intent | IntentVisual intent color to apply to element. Inherited from IntentProps |
interactive | boolean = falseIf Inherited from true, the cell will be rendered above overlay layers to enable mouse interactions within the cell.CellProps |
isFocused | booleanWhether the given cell is the current active/focused cell. |
key | stringCellProps |
loading | boolean = falseAn optional native tooltip that is displayed on hover. If Inherited from true, content will be replaced with a fixed-height skeleton.CellProps |
onCancel | (value: string, rowIndex?: number, columnIndex?: number) => voidA listener that is triggered if the user cancels the edit. This is important to listen to if you are doing anything with onChange events, since you'll likely want to revert whatever changes you made. The callback will also receive the row index and column index if they were originally provided via props. |
onChange | (value: string, rowIndex?: number, columnIndex?: number) => voidA listener that is triggered as soon as the editable name is modified. This can be due, for example, to keyboard input or the clipboard. The callback will also receive the row index and column index if they were originally provided via props. |
onConfirm | (value: string, rowIndex?: number, columnIndex?: number) => voidA listener that is triggered once the editing is confirmed. This is usually due to the <code>return</code> (or <code>enter</code>) key press. The callback will also receive the row index and column index if they were originally provided via props. |
onKeyPress | React.KeyboardEventHandler<HTMLElement>Callback invoked when a character-key is pressed. Inherited from CellProps |
rowIndex | numberThe row index of the cell. If provided, this will be passed as an argument to any callbacks when they are invoked. Inherited from CellProps |
style | React.CSSPropertiesCellProps |
tabIndex | numberAllows for setting a tab index on the cell, so the cell can be browser-focusable. Inherited from CellProps |
tooltip | stringAn optional native tooltip that is displayed on hover. Inherited from CellProps |
truncated | boolean = trueIf Inherited from true, the cell contents will be wrapped in a div with styling that will prevent the content from overflowing the cell.CellProps |
value | stringThe value displayed in the text box. Be sure to update this value when rendering this component after a confirmed change. |
wrapText | boolean = falseIf Inherited from true, the cell contents will be wrapped in a div with styling that will cause text to wrap, rather than displaying it on a single line.CellProps |
ColumnHeaderCell#
Return one from a column's columnHeaderCellRenderer to customize its header. menuRenderer adds a menu that opens from a header button or on right click. Children render below the name.
<Column
name="Rows"
cellRenderer={renderRows}
columnHeaderCellRenderer={() => (
<ColumnHeaderCell
name="Rows"
menuRenderer={() => (
<Menu>
<MenuItem icon="sort-desc" text="Sort largest first" onClick={sortByRows} />
</Menu>
)}
/>
)}
/>interface ColumnHeaderCellProps extends HeaderCellProps, ColumnNameProps@clawscale/react/table| Props | Description |
|---|---|
children | React.ReactNodeHeaderCellProps |
className | stringA space-delimited list of class names to pass along to a child element. Inherited from Props |
enableColumnInteractionBar | boolean = falseIf true, adds an interaction bar on top of all column header cells, and moves interaction triggers into it. |
enableColumnReordering | booleanSpecifies if the column is reorderable. |
index | numberThe index of the cell in the header. If provided, this will be passed as an argument to any callbacks when they are invoked. Inherited from HeaderCellProps |
isActive | booleanIf Inherited from true, will apply the active class to the header to indicate it is part of an external operation.HeaderCellProps |
isColumnSelected | booleanSpecifies if the full column is part of a selection. |
loading | boolean = falseIf Inherited from true, the row/column name will be replaced with a fixed-height skeleton, and the resizeHandle will not be rendered. If passing in additional children to this component, you will also want to conditionally apply Classes.SKELETON where appropriate.HeaderCellProps |
menuIcon | IconName | React.JSX.Element = "chevron-down"The icon name or element for the header's menu button. |
menuPopoverProps | Omit<PopoverProps, "content" | keyof OverlayLifecycleProps>Optional props to forward to the dropdown menu popover. This has no effect if menuRenderer is undefined. |
menuRenderer | (index?: number) => React.JSX.ElementA callback that returns an element, like a Inherited from <Menu>, which is displayed by right-clicking anywhere in the header. The callback will receive the cell index if it was provided via props.HeaderCellProps |
name | stringThe name displayed in the header of the row/column. Inherited from HeaderCellProps |
nameRenderer | (name: string, index?: number) => React.ReactElement<Props>A callback to override the default name rendering behavior. The default behavior is to simply use the Inherited from ColumnHeaderCells name prop.ColumnNameProps |
reorderHandle | React.JSX.ElementA Inherited from ReorderHandle React component that allows users to drag-reorder the column header.HeaderCellProps |
resizeHandle | React.JSX.ElementA Inherited from ResizeHandle React component that allows users to drag-resize the header.HeaderCellProps |
selectCellsOnMenuClick | boolean = trueIf true, clicks on the header menu target element will cause the column's cells to be selected. |
style | React.CSSPropertiesCSS styles for the top level element. Inherited from HeaderCellProps |
targetRef | React.RefObject<HTMLElement>Required for Draggable to attach a DOM ref. Inherited from HeaderCellProps |
RowHeaderCell#
Return one from the table's rowHeaderCellRenderer to replace the default row numbers.
<Table numRows={runs.length} rowHeaderCellRenderer={(row) => <RowHeaderCell name={runs[row]?.id} />}>
{columns}
</Table>interface RowHeaderCellProps extends HeaderCellProps, Props@clawscale/react/table| Props | Description |
|---|---|
children | React.ReactNodeHeaderCellProps |
className | stringA space-delimited list of class names to pass along to a child element. Inherited from Props |
enableRowReordering | booleanSpecifies if the row is reorderable. |
index | numberThe index of the cell in the header. If provided, this will be passed as an argument to any callbacks when they are invoked. Inherited from HeaderCellProps |
isActive | booleanIf Inherited from true, will apply the active class to the header to indicate it is part of an external operation.HeaderCellProps |
isRowSelected | booleanSpecifies whether the full row is part of a selection. |
loading | boolean = falseIf Inherited from true, the row/column name will be replaced with a fixed-height skeleton, and the resizeHandle will not be rendered. If passing in additional children to this component, you will also want to conditionally apply Classes.SKELETON where appropriate.HeaderCellProps |
menuRenderer | (index?: number) => React.JSX.ElementA callback that returns an element, like a Inherited from <Menu>, which is displayed by right-clicking anywhere in the header. The callback will receive the cell index if it was provided via props.HeaderCellProps |
name | stringThe name displayed in the header of the row/column. Inherited from HeaderCellProps |
nameRenderer | (name: string, index?: number) => React.ReactElement<Props>A callback to override the default name rendering behavior. The default behavior is to simply use the RowHeaderCells name prop. |
reorderHandle | React.JSX.ElementA Inherited from ReorderHandle React component that allows users to drag-reorder the column header.HeaderCellProps |
resizeHandle | React.JSX.ElementA Inherited from ResizeHandle React component that allows users to drag-resize the header.HeaderCellProps |
style | React.CSSPropertiesCSS styles for the top level element. Inherited from HeaderCellProps |
targetRef | React.RefObject<HTMLElement>Required for Draggable to attach a DOM ref. Inherited from HeaderCellProps |
TruncatedFormat#
Wraps long text in a cell. With detectTruncation, a button opens the full text when it does not fit.
interface TruncatedFormatProps extends Props@clawscale/react/table| Props | Description |
|---|---|
children | string |
className | stringA space-delimited list of class names to pass along to a child element. Inherited from Props |
detectTruncation | boolean = false;Should the component keep track of the truncation state of the string content. If true, the value of truncateLength is ignored. When combined with a showPopover value of WHEN_TRUNCATED, popovers will only render when necessary. |
measureByApproxOptions | TrucatedFormateMeasureByApproximateOptionsValues to use for character width, line height, cell padding, and buffer lines desired, when using approximate truncation. These values are used to guess at the size of the text and determine if the popover should be drawn. They should work well enough for default table styles, but may need to be overridden for more accuracy if the default styles or font size, etc are changed. |
parentCellHeight | numberHeight of the parent cell. Used by shouldComponentUpdate only |
parentCellWidth | numberWidth of the parent cell. Used by shouldComponentUpdate only |
preformatted | boolean = falseSets the popover content style to white-space: pre if true or white-space: normal if false. |
showPopover | TruncatedPopoverMode = WHEN_TRUNCATEDConfigures when the popover is shown with the TruncatedPopoverMode enum. |
truncateLength | number = 2000Number of characters that are displayed before being truncated and appended with the truncationSuffix prop. A value of 0 will disable truncation. This prop is ignored if detectTruncation is true. |
truncationSuffix | string = "..."The string that is appended to the display string if it is truncated. |
JSONFormat#
Prints an object as JSON and truncates it like TruncatedFormat.
interface JSONFormatProps extends TruncatedFormatProps@clawscale/react/table| Props | Description |
|---|---|
children | any |
className | stringA space-delimited list of class names to pass along to a child element. Inherited from Props |
detectTruncation | boolean = false;Should the component keep track of the truncation state of the string content. If true, the value of Inherited from truncateLength is ignored. When combined with a showPopover value of WHEN_TRUNCATED, popovers will only render when necessary.TruncatedFormatProps |
measureByApproxOptions | TrucatedFormateMeasureByApproximateOptionsValues to use for character width, line height, cell padding, and buffer lines desired, when using approximate truncation. These values are used to guess at the size of the text and determine if the popover should be drawn. They should work well enough for default table styles, but may need to be overridden for more accuracy if the default styles or font size, etc are changed. Inherited from TruncatedFormatProps |
omitQuotesOnStrings | boolean = trueBy default, we omit stringifying native JavaScript strings since JSON.stringify awkwardly adds double-quotes to the display value. This behavior can be turned off by setting this boolean to false. |
parentCellHeight | numberHeight of the parent cell. Used by shouldComponentUpdate only Inherited from TruncatedFormatProps |
parentCellWidth | numberWidth of the parent cell. Used by shouldComponentUpdate only Inherited from TruncatedFormatProps |
preformatted | boolean = falseSets the popover content style to Inherited from white-space: pre if true or white-space: normal if false.TruncatedFormatProps |
showPopover | TruncatedPopoverMode = WHEN_TRUNCATEDConfigures when the popover is shown with the Inherited from TruncatedPopoverMode enum.TruncatedFormatProps |
stringify | (obj: any) => stringOptionally specify the stringify method. Default is JSON.stringify with 2-space indentation. |
truncateLength | number = 2000Number of characters that are displayed before being truncated and appended with the Inherited from truncationSuffix prop. A value of 0 will disable truncation. This prop is ignored if detectTruncation is true.TruncatedFormatProps |
truncationSuffix | string = "..."The string that is appended to the display string if it is truncated. Inherited from TruncatedFormatProps |