Skip to content

Repository files navigation

@rc-component/table

Ant Design Part of the Ant Design ecosystem.

📋 Low-level table primitives for React, maintained in the Ant Design ecosystem.

NPM versionnpm downloadsbuild statusCodecovbundle sizedumi

English | 简体中文

Highlights

  • Flexible column, summary, fixed header, sticky, expandable row, and virtual table support.
  • TypeScript-first API designed for composition in design systems.
  • Used by Ant Design Table and other React data display experiences.

Install

npm install @rc-component/table

Usage

importTablefrom'@rc-component/table';importtype{ColumnsType}from'@rc-component/table';interfaceUser{key: string;name: string;age: number;address: string;}constcolumns: ColumnsType<User>=[{title: 'Name',dataIndex: 'name',key: 'name',width: 120},{title: 'Age',dataIndex: 'age',key: 'age',width: 80},{title: 'Address',dataIndex: 'address',key: 'address'},];constdata: User[]=[{key: '1',name: 'Jack',age: 28,address: 'Somewhere'},{key: '2',name: 'Rose',age: 36,address: 'Somewhere else'},];exportdefault()=><Tablecolumns={columns}data={data}/>;

Examples

Run the local dumi site:

npm install
npm start

Then open http://localhost:8000.

API

Properties

NameTypeDefaultDescription
tableLayoutauto | fixedauto | fixed for any columns is fixed or ellipsis or header is fixedhttps://developer.mozilla.org/en-US/docs/Web/CSS/table-layout
prefixClsStringrc-table
classNameStringadditional className
idStringidentifier of the container div
useFixedHeaderBooleanfalsewhether use separator table for header. better set width for columns
scrollObject{x: false, y: false}whether table can be scroll in x/y direction, x or y can be a number that indicated the width and height of table body
expandableObjectConfig expand props
expandable.defaultExpandAllRowsBooleanfalseExpand All Rows initially
expandable.defaultExpandedRowKeysString[][]initial expanded rows keys
expandable.expandedRowKeysString[]current expanded rows keys
expandable.expandedRowRenderFunction(recode, index, indent, expanded):ReactNodeContent render to expanded row
expandable.forceRenderBooleanfalseForce render expanded row content before expansion. In virtual mode, only rows currently mounted by the virtual list are force-rendered; off-screen rows may still be unmounted
expandable.expandedRowClassNamestring | (recode, index, indent) => stringget expanded row's className
expandable.expandRowByClickbooleanSupport expand by click row
expandable.expandIconColumnIndexNumber0The index of expandIcon which column will be inserted when expandIconAsCell is false
expandable.expandIconprops => ReactNodeCustomize expand icon
expandable.indentSizeNumber15indentSize for every level of data.i.children, better using with column.width specified
expandable.rowExpandable(record) => booleanConfig row support expandable
expandable.onExpandFunction(expanded, record)function to call when click expand icon
expandable.onExpandedRowsChangeFunction(expandedRows)function to call when the expanded rows change
expandable.fixedString | Boolean-this expand icon will be fixed when table scroll horizontally: true or left or right and expandIconColumnIndex need to stay first or last
rowKeystring or Function(record, index):string'key'If rowKey is string, record[rowKey] will be used as key. If rowKey is function, the return value of rowKey(record, index) will be use as key.
rowClassNamestring or Function(record, index, indent):stringget row's className
rowRefFunction(record, index, indent):stringget row's ref key
dataObject[]data record array to be rendered
onRowFunction(record, index)Set custom props per each row.
onHeaderRowFunction(record, index)Set custom props per each header row.
showHeaderBooleantruewhether table head is shown
hiddenBooleanfalseHidden column.
titleFunction(currentData)table title render function
footerFunction(currentData)table footer render function
emptyTextReact.Node or FunctionNo DataDisplay text when data is empty
columnsObject[]The columns config of table, see table below
componentsObjectOverride table elements, see #171 for more details
stickyboolean | {offsetHeader?: number, offsetScroll?: number, getContainer?: () => Window | HTMLElement }falsestick header and scroll bar
summary(data: readonly RecordType[]) => React.ReactNode-summary attribute in table component is used to define the summary row.
rowHoverablebooleantrueTable hover interaction

Methods

scrollTo

Table component exposes scrollTo method to scroll to a specific position:

consttblRef=useRef();tblRef.current?.scrollTo({key: 'rowKey',align: 'start'});
NameTypeDefaultDescription
indexnumber-Row index to scroll to
topnumber-Scroll to specific top position (in px)
keystring-Scroll to row by row key
offsetnumber-Additional offset from target position
alignstart | center | end | nearestnearestAlignment of the target element within the scroll container. start aligns to top, center to middle, end to bottom, nearest automatically chooses the closest alignment. Note: Virtual table does not support center.

Column Props

NameTypeDefaultDescription
keyStringkey of this column
classNameStringclassName of this column
colSpanNumberthead colSpan of this column
titleReact Nodetitle of this column
dataIndexStringdisplay field of the data record
widthString | Numberwidth of the specific proportion calculation according to the width of the columns
minWidthNumberthe minimum width of the column, only worked when tableLayout is auto
fixedString | Booleanthis column will be fixed when table scroll horizontally: true or 'left' or 'right'
alignStringspecify how cell content is aligned
ellipsisBooleanspecify whether cell content be ellipsized
rowScope'row' | 'rowgroup'Set scope attribute for all cells in this column
onCellFunction(record, index)Set custom props per each cell.
onHeaderCellFunction(record)Set custom props per each header cell.
renderFunction(value, row, index)The render function of cell, has three params: the text of this cell, the record of this row, the index of this row, it's return an object:{ children: value, props: { colSpan: 1, rowSpan:1 } } ==> 'children' is the text of this cell, props is some setting of this cell, eg: 'colspan' set td colspan, 'rowspan' set td rowspan

Summary Props

Table.Summary

NameTypeDefaultDescription
keyStringkey of this summary
fixedboolean | 'top' | 'bottom'-true fixes the summary row at the bottom of the table. top fixes the summary row at the top of the table, while bottom fixes it at the bottom. undefined or false makes the summary row scrollable along with the table.

Table.Summary.Row

NameTypeDefaultDescription
keyStringkey of this summary
classNameString-className of this summary row
styleReact.CSSProperties-style of this summary row
onClick(e?: React.MouseEvent<HTMLElement>) => void-The onClick attribute in Table.Summary.Row component can be used to set a click event handler for the summary row.

Development

npm install
npm start

The dumi site runs at http://localhost:8000 by default.

Run checks before sending a pull request:

npm run lint
npm run tsc
npm test
npm run build

Release

npm run prepublishOnly

The release flow is handled by @rc-component/np through the rc-np command after the package build.

Ecosystem

This package is part of the React Component organization and is maintained alongside Ant Design. The Ant Design mark above is used only as ecosystem context; the package itself stays framework-level and unstyled except for its bundled assets.

License

@rc-component/table is released under the MIT license.

About

📋 Low-level table primitives for React, maintained in the Ant Design ecosystem.

Topics

Resources

Stars

1.4k stars

Watchers

43 watching

Forks

Releases

Sponsor this project

Used by

Contributors

Languages