Table Builder
TableBuilder builds a typed table configuration. It does not ship its own React table hook.
Imports
import {
TableBuilder,
createTableBuilder,
createColumn,
createColumns,
} from '@codella-software/utils'
import { useFiltersAndSort } from '@codella-software/react'
Core example
type User = {
id: number
name: string
email: string
}
type UserFilters = {
search: string
}
const filters = useFiltersAndSort<UserFilters>({
config: {
storageKey: 'users-table',
defaultFilters: { search: '' },
},
})
const table = new TableBuilder<User, UserFilters>()
.data(users)
.totalRows(users.length)
.columns([
createColumn('name', 'name', 'Name', { sortable: true }),
createColumn('email', 'email', 'Email'),
])
.filtersAndSort(filters.state, filters.getService().getActions())
.selection({ enabled: true, mode: 'multiple' })
.pagination({ enabled: true, pageSizeOptions: [10, 25, 50] })
.build()
Available builder methods
data(data)totalRows(total)columns(columns)column((builder) => builder.accessor(...).header(...).sortable())filtersAndSort(state, actions)row(config)rowActions(actions)rowAction(action)bulkActions(actions)bulkAction(action)selection(config)pagination(config)loading(loading)loadingState(config)emptyState(config?)variant(variant)size(size)theme(theme)className(className)ariaLabel(label)caption(caption)build()
Column shape
type ColumnConfig<TData> = {
id: string
accessorKey?: keyof TData | string
accessorFn?: (row: TData) => unknown
header: string | ReactNode | ((props) => ReactNode)
cell?: (props) => ReactNode
footer?: string | ReactNode | ((props) => ReactNode)
sortable?: boolean
sortField?: string
hidden?: boolean
hideable?: boolean
sticky?: 'left' | 'right'
align?: 'left' | 'center' | 'right'
}
Rows
.row({
getRowId: (user) => user.id,
expandable: true,
expandMode: 'single',
expandedRowId,
onExpandedRowIdChange: setExpandedRowId,
renderExpandedRow: (user) => <UserDetails user={user} />,
})
expandedRowId/onExpandedRowIdChange make the expanded row controlled; expandMode is 'single' or 'multiple'.
Notes
filtersAndSort(state, actions)is optional; static tables can skip it, andbuild()won't throw.- The library has no i18n. If you want a default empty state, set it in a small wrapper around
createTableBuilder(). createColumn(id, accessor, header)accepts a string or aReactNodeheader.