Table
A column-config-driven table. Supports sorting, fixed columns, striped rows, custom cells, multi-level headers, filtering, column resizing, and virtual scrolling for large datasets (the visible window is computed by pure core functions — no external library). Structurally identical to lh-ui-plus's LhTable: column normalization, sorting, filtering, and the virtual range all come from the same pure functions in share core; this library only adds the React glue and JSX. The DOM semantics are upgraded to native table / thead / tbody / th / td, with BEM class names identical to plus.
Basic usage
From top to bottom, the overview demo below shows: a basic table, sorting + fixed columns + stripes, custom cells (render), and pagination linked with LhPagination.
| Name | Role | City |
|---|---|---|
| 沈砚 | Architecture | 杭州 |
| 苏晚 | Frontend | 苏州 |
| 陆离 | Design | 北京 |
| 墨白 | Frontend | 杭州 |
| 素笺 | Architecture | 苏州 |
| 留白 | Design | 北京 |
| Name | Role | City | |
|---|---|---|---|
| 沈砚 | 34 | Architecture | 杭州 |
| 苏晚 | 28 | Frontend | 苏州 |
| 陆离 | 31 | Design | 北京 |
| 墨白 | 26 | Frontend | 杭州 |
| 素笺 | 29 | Architecture | 苏州 |
| 留白 | 24 | Design | 北京 |
| Name | Role |
|---|---|
| 沈砚 | Architecture |
| 苏晚 | Frontend |
| 陆离 | Design |
| 墨白 | Frontend |
| 素笺 | Architecture |
| 留白 | Design |
| Name | Role | City |
|---|---|---|
| 沈砚 | Architecture | 杭州 |
| 苏晚 | Frontend | 苏州 |
import { LhTable } from '@lh-ui/react'
export default function Demo() {
const rows = [
{ id: 1, name: 'Shen Yan', role: 'Architect', city: 'Hangzhou' },
{ id: 2, name: 'Su Wan', role: 'Frontend', city: 'Suzhou' },
{ id: 3, name: 'Lu Li', role: 'Designer', city: 'Beijing' },
]
const columns = [
{ prop: 'name', label: 'Name', width: 140 },
{ prop: 'role', label: 'Role' },
{ prop: 'city', label: 'City', width: 120 },
]
return <LhTable data={rows} columns={columns} />
}
Sorting and fixed columns
Set sortable on a column to enable sorting: the first click starts at asc, clicking the same column cycles asc → desc → off, and switching columns restarts at asc; fixed pins a column (width required). Fixed columns stick via CSS position: sticky and show a shadow when scrolled out of bounds.
import { LhTable } from '@lh-ui/react'
export default function Demo() {
const columns = [
{ prop: 'name', label: 'Name', width: 140, fixed: 'left' },
{ prop: 'age', label: 'Age', width: 100, sortable: true, align: 'center' },
{ prop: 'role', label: 'Role' },
{ prop: 'city', label: 'City', width: 120 },
]
return <LhTable data={rows} columns={columns} stripe border size="small" />
}
Virtual scrolling (large datasets)
Setting height enables it: only the rows inside the visible window are rendered (3 extra rows buffered above and below), and rowHeight must match the actual row height.
import { LhTable } from '@lh-ui/react'
export default function Demo() {
const bigRows = Array.from({ length: 1000 }, (_, i) => ({
id: i + 1,
name: `Row ${i + 1}`,
role: i % 2 === 0 ? 'Architect' : 'Frontend',
city: 'Hangzhou',
}))
return <LhTable data={bigRows} columns={columns} height={240} rowHeight={48} />
}
Custom cells
plus's <column.slot> scoped slot maps to the column's render function in React, called with (row, value, index); columns without a render function fall back to the text formatted by formatter.
import { LhTable, LhTag } from '@lh-ui/react'
export default function Demo() {
const columns = [
{ prop: 'name', label: 'Name', width: 140 },
{
prop: 'role',
label: 'Role',
render: (row, value) => (
<LhTag type={row.role === 'Architect' ? 'primary' : 'info'}>{String(value)}</LhTag>
),
},
]
return <LhTable data={rows} columns={columns} />
}
Multi-level headers
Nesting children in the column config yields multi-level headers, nestable without limit; the body aligns to the leaf columns.
import { LhTable } from '@lh-ui/react'
export default function Demo() {
const columns = [
{ prop: 'name', label: 'Name', width: 140, fixed: 'left' },
{
label: 'Project info',
children: [
{ prop: 'role', label: 'Role' },
{ prop: 'city', label: 'City', width: 120 },
],
},
{ prop: 'age', label: 'Age', sortable: true, align: 'center' },
]
return <LhTable data={rows} columns={columns} border />
}
Filtering
Once a column configures filters, a filter trigger appears in the header; check options and click "OK" to apply, "Reset" to clear. filterMultiple: false makes it single-select and filterMethod customizes the predicate; for controlled usage pass filteredValues + onFilteredValuesChange.
import { useState } from 'react'
import { LhTable } from '@lh-ui/react'
export default function Demo() {
const [filteredValues, setFilteredValues] = useState({})
const columns = [
{ prop: 'name', label: 'Name', width: 140 },
{
prop: 'role',
label: 'Role',
filters: [
{ text: 'Architect', value: 'Architect' },
{ text: 'Frontend', value: 'Frontend' },
{ text: 'Designer', value: 'Designer' },
],
},
]
return (
<LhTable
data={rows}
columns={columns}
filteredValues={filteredValues}
onFilteredValuesChange={setFilteredValues}
onFilterChange={({ prop, values }) => console.log('filterChange', prop, values)}
/>
)
}
Column resizing
Enable resizable (the master switch) and mark columns with resizable: true (leaf columns with a numeric width). A drag handle appears at the right edge of the header; once focused, ←/→ adjust by ±10px per step and the drag minimum is 40px.
import { LhTable } from '@lh-ui/react'
export default function Demo() {
const columns = [
{ prop: 'name', label: 'Name', width: 140, resizable: true, fixed: 'left' },
{ prop: 'role', label: 'Role' },
{ prop: 'city', label: 'City', width: 120, resizable: true },
]
return (
<LhTable
data={rows}
columns={columns}
resizable
border
onColumnResize={({ prop, width }) => console.log(`${prop} → ${width}px`)}
/>
)
}
Row key, max height, and default sorting
rowKey specifies the unique row key (recommended); maxHeight caps the height so the header pins and the body scrolls; sorter provides a custom compare function; defaultSortProp / defaultSortOrder set the initial sorting; defaultFilteredValues sets the initial filtering.
import { LhTable } from '@lh-ui/react'
export default function Demo() {
const columns = [
{ prop: 'name', label: 'Name', width: 140 },
{ prop: 'age', label: 'Age', sortable: true, sorter: (a, b) => a.age - b.age },
{ prop: 'role', label: 'Role' },
]
return (
<LhTable
data={rows}
columns={columns}
rowKey="name"
maxHeight={200}
defaultSortProp="age"
defaultSortOrder="desc"
/>
)
}
Pagination linkage
LhTable itself does not embed pagination: data paging is driven by LhPagination, which slices the full dataset locally on page change; each of the two pieces keeps its own responsibility (as in the overview demo above).
import { useState } from 'react'
import { LhPagination, LhTable } from '@lh-ui/react'
export default function Demo() {
const [current, setCurrent] = useState(1)
const pageSize = 2
const pagedRows = rows.slice((current - 1) * pageSize, current * pageSize)
return (
<>
<LhTable data={pagedRows} columns={columns} />
<LhPagination total={rows.length} pageSize={pageSize} current={current} onChange={setCurrent} />
</>
)
}
API mapping from plus
plus's named slots and v-model merge into prop / callback forms in React:
| plus | React |
|---|---|
The <column.slot> scoped slot ({ row, column, index, value }) | The column's render(row, value, index) function |
The #empty named slot | The empty prop (ReactNode) |
update:filteredValues (v-model) | The onFilteredValuesChange callback (controlled via filteredValues) |
@sort-change / @row-click / @filter-change / @column-resize | onSortChange / onRowClick / onFilterChange / onColumnResize |
LhTable Props
| Prop | Description | Type | Default |
|---|---|---|---|
| data | Row data | LhTableRow[] | — |
| columns | Column config | LhTableColumn[] | — |
| height | Body height (enables virtual scrolling; number = px) | string | number | — |
| maxHeight | Max height in non-virtual mode | string | number | — |
| rowHeight | Virtual scrolling row height (must match the actual row height) | number | 48 |
| stripe | Striped rows | boolean | false |
| border | Cell borders | boolean | false |
| size | Size | 'small' | 'default' | 'large' | 'default' |
| rowKey | Row key field name | string | 'id' |
| defaultSortProp | Default sort column | string | — |
| defaultSortOrder | Default sort order | 'asc' | 'desc' | — |
| sorter | Custom compare function | (a, b) => number | — |
| resizable | Enable column resizing (column-level resizable still required) | boolean | false |
| defaultFilteredValues | Default filter state (uncontrolled, keyed by column prop) | Record<string, (string | number)[]> | {} |
| filteredValues | Controlled filter state (keyed by column prop) | Record<string, (string | number)[]> | — |
| empty | Custom empty state (merging plus's #empty slot) | ReactNode | — |
| sx | Shorthand layout props | SxProps | — |
LhTableColumn
| Field | Description | Type |
|---|---|---|
| prop | Data field name | string |
| label | Column title | string |
| width | Column width (number = px; unset columns share the remaining space) | number | string |
| align | Alignment | 'left' | 'center' | 'right' |
| fixed | Fixed column (width required) | 'left' | 'right' |
| sortable | Sortable | boolean |
| render | Custom cell renderer (merging plus's scoped slot) | (row, value, index) => ReactNode |
| formatter | Value formatter (effective when render is absent) | (row, value) => string |
| children | Child columns (configuring them yields multi-level headers) | LhTableColumn[] |
| filters | Column filter options (leaf columns only) | LhTableFilterOption[] |
| filterMethod | Custom filter predicate (strict prop comparison by default) | (value, row) => boolean |
| filterMultiple | Whether the filter panel is multi-select | boolean |
| resizable | Whether the column can be resized (numeric-width leaf columns) | boolean |
Events
| Event | Description | Signature |
|---|---|---|
| onSortChange | Fired when sorting changes (switching columns restarts at asc; the same column cycles asc → desc → off) | ({ prop, order }) => void |
| onRowClick | Fired when a row is clicked (Enter / Space activation included) | (row, index) => void |
| onFilterChange | Fired after the filter is committed via "OK" or "Reset" | ({ prop, values }) => void |
| onFilteredValuesChange | Fired when the filter state changes | (values) => void |
| onColumnResize | Fired after a column resize completes (drag end or ←/→) | ({ prop, width }) => void |
Accessibility
- The root element is a native
<table>and carries row / header / cell semantics out of the box, witharia-rowcount/aria-colcountadded (virtual scrolling only renders windowed rows; screen readers learn the full scale from these attributes) - Sortable headers are
<th>+aria-sort(none/ascending/descending) with real<button>triggers; group headers in multi-level headers express themselves viacolspan/rowspanandscope="colgroup" - Fixed columns stick via CSS
position: sticky(no homemade pinning math) - The filter trigger has
aria-haspopup+aria-expanded, and the panel is arole="group" - The resize handle is a
role="separator", focusable, adjusting ±10px with the ←/→ keys - Rows are focusable (
tabindex="0"); Enter / Space activate them and fireonRowClick - The placeholder spacer rows of virtual scrolling carry
aria-hidden="true"and are removed from the screen-reader tree
ref
forwardRef<HTMLTableElement> — the root <table> element.
Styles
import '@lh-ui/react/theme.css'
import '@lh-ui/react/style/table'