Table
Basic Usage
For detailed react table usage, please refer to react-table
useTable Extension Points
useTable(options)
options.meta
Most of the extension points in the table are configured through meta.
1interface TableMeta<TData extends RowData> {2tableName: string; // table name, now only used for storage state3refetch?: () => void; // refetch data4storageStateKeys?: (keyof TableState)[] | '*'; // state keys need to be stored, used with Status2StorageFeature5manual?: boolean; // whether to manually control the data6enable?: {7// whether to enable some features8pagination?: boolean; // whether to enable pagination9toolbar?: boolean; // whether to enable toolbar10visible?: boolean; // whether to enable hidden columns11filters?: boolean; // whether to enable filters12};13enableDefault?: {14// whether to enable default functions15toolbar?: boolean; // toolbar default functions, including multi-selection display, multi-selection operation status switching, etc.16th?: boolean; // when th is more than one line, enable border17};18getProps?: {19// custom props of each component in BaseTable, can be returned by function. Refer to BaseTable for the props of each component20filters?: (table: Table<TData>) => BaseTable.ToolbarProps['filterProps'];21toolbar?: (22table: Table<TData>23) => Partial<Omit<BaseTable.ToolbarProps, 'filtersProps' | 'enableFilters'>>;24table?: (table: Table<TData>) => Partial<BaseTable.TableProps>;25th?: (26table: Table<TData>,27header: Header<TData, unknown>28) => Omit<BaseTable.TableCellProps, 'ref'>;29pagination?: (table: Table<TData>) => Partial<BaseTable.BaseTablePaginationProps> & {30total?: number;31};32tr?: (table: Table<TData>, row: Row<TData>) => Partial<BaseTable.TableRowProps>;33td?: (table: Table<TData>, props: Record<string, any>) => Omit<BaseTable.TableCellProps, 'ref'>;34empty?: () => Partial<BaseTable.TableFilteredEmptyProps>;35};3637registerHandlers?: StateHandler[];38}
options.meta.registerHandlers
Extend the state processing function of the table by registering through registerHandlers
.
When stateKeys
is '*', all state changes are listened to through useEffect
. When (keyof TableState[])
, state changes are listened to through onXXXChange.
handleName
and callback
are optional, and callback
has a higher priority than handleName
.
1interface StateHandler {2handlerName?: string; // options 中的方法名3stateKeys: (keyof TableState)[] | '*'; // 监听的状态4callback?: (state: Partial<TableState>) => void; // 状态变化时的回调5}
options.columns
Customize column configuration by setting column.meta
1interface ColumnMeta<TData extends RowData, TValue = unknown> {2description?: Record<string, any>; // icon ? tooltip props3// filterOptions?: { key: string; label: React.ReactNode }[]; // filter options4selectType?: 'single' | 'multiple'; // multiple selection type5sortable?: boolean; // whether it can be sorted6searchKey?: string; // custom search key7th?: Partial<BaseTable.TableCellProps>; // baseTable th props, priority is higher than getProps.th8td?: Partial<BaseTable.TableCellProps>; //baseTable td props, priority is higher than getProps.td9}
Feature
Status2StorageFeature
After being added to _features, the fields in the configured state are stored in localStorage by default. getDefaultTableOptions will automatically register this feature.
options.state2Storage
The method of converting state to storage, the method of converting state to storage.options.storage2State
The method of converting storage to state, the method of converting storage to state.options.meta.storageKeys
Fields that need to be stored.
1// storageKey field consists of kube-table-${tableName}-state2interface StorageStateOptions {3storage2State?: (storageKey: string) => Partial<TableState>;4state2Storage?: (storageKey: string, state: Partial<TableState>) => void;5}67interface TableMeta<TData extends RowData> {8storageKeys?: Array<keyof TableState>;9tableName: string;10}
getDefaultTableOptions(config)
Quickly generate default options configuration
1**config**:23```ts4interface Config {5tableName: string;6manual: boolean; // whether to manually control the data7enableToolbar?: boolean; // whether to enable toolbar8enablePagination?: boolean; // whether to enable pagination9enableVisible?: boolean; // whether to enable hidden columns10enableFilters?: boolean; // whether to enable filters11enableStateToStorage?: boolean; // whether to store state to storage, used with options.meta.storageKeys12enableSelection?: boolean; // whether to enable selection13enableSort?: boolean; // whether to enable sorting14enableMultiSelection?: boolean; // whether to enable multi-selection15enableInitParamsByUrl?: boolean; // TODO: whether to enable url parameter initialization (not implemented)16enableParamsToUrl?: boolean; // TODO: whether to enable url parameters (not implemented)17}
Default enabled features:
enableFilters
enablePagination
enableToolbar
enableVisible
enableStateToStorage
enableSort
Default enabled Feature:
Status2StorageFeature
Default generated configuration:
1{2storageStateKeys: ['columnVisibility'],3registerHandlers: manual4? [5{6handlerName: 'onParamsChange',7stateKeys: ['pagination', 'columnFilters', 'sorting'],8},9]10: [],11}
BaseTable
BaseTable.TableProps
1interface TableInnerProps {2padding?: 'normal' | 'none';3size?: 'small' | 'medium';4stickyHeader?: boolean; // whether to fix the table header5className?: string;6style?: React.CSSProperties;7tableWrapperClassName?: string;8}
BaseTable.TableHeadProps
1interface TableHeadProps {2className?: string;3style?: React.CSSProperties;4hasBorder?: boolean; // whether the table th has a border5}
BaseTable.TableBodyProps
1interface TableBodyProps {2className?: string;3style?: React.CSSProperties;4hasBorder?: boolean; // whether the td in the table body has a border5}
BaseTable.TableRowProps
1export interface TableRowProps {2className?: string;3hover?: boolean; // whether hover effect4selected?: boolean; // whether selected5style?: React.CSSProperties;6}
BaseTable.TableCellProps
1/**2when padding is normal, variant and size correspond to the padding attributeQ3'head-small': '8px 12px',4'head-medium': '16px 12px',5'body-small': '4px 8px',6'body-medium': '8px 12px',7*/8export type TableCellProps = {9padding?: 'none' | 'normal'; // cell padding10align?: 'left' | 'center' | 'right' | 'justify'; // cell alignment11className?: string;12size?: 'small' | 'medium'; // cell size13ariaSort?: 'asc' | 'desc' | false; // whether to sort14variant?: 'head' | 'body'; // distinguish th and td15stickyHeader?: boolean; // whether to fix the table header16fixed?: 'left' | 'right'; // fixed column17fixedWidth?: number; // width when the column is fixed (when position is sticky; left or right value)18fixedLastLeft?: boolean; // whether it is the most left fixed, mainly used to separate shadow effects19fixedLastRight?: boolean; // whether it is the most right fixed, mainly used to separate shadow effects20style?: React.CSSProperties;21hasBorder?: boolean;22};
BaseTable.ToolbarProps
1interface ToolbarProps {2enableBatchActions: boolean; // Whether to operate when multiple selections3enableSettingMenu?: boolean; // Whether to temporarily operate the button4onDisableBatchActions?: () => void; // Cancel the multi-selection operation box5enableFilters?: boolean; // Whether to enable filtering6settingMenu?: React.ReactNode; // Operation list7settingMenuText?: string;8toolbarLeft?: React.ReactNode; // left component9toolbarRight?: React.ReactNode; // right component10batchActions?: React.ReactNode; // Multi-selection operation button11filterProps: {12// FilterInput props13filters?: any;14placeholder?: string;15suggestions: Suggestions;16simpleMode?: boolean;17onChange?: (value: any) => void;18initialKeyword?: string;19};20onFilterInputChange?: (value: any) => void; // FilterInput onChange21refetch?: any; // refresh22loading?: boolean; // loading on the refresh button23}
BaseTable.EmptyProps
1// empty create button props2interface CreateButtonProps {3enableCreate?: boolean;4createButton?: ReactElement;5clickCreateButtonFn?: (event: MouseEvent<HTMLButtonElement>) => void;6}78// data is empty, no filter conditions9export type TableEmptyProps = PropsWithChildren<EmptyProps & CreateButtonProps>;1011interface DescriptionProps {12clearAndRefetch?: false | (() => void); // clear filter conditions13refetch?: false | (() => void); // refresh14}1516// data is empty, with filter conditions17export type TableFilteredEmptyProps = TableEmptyProps & DescriptionProps;
Full example
Base Table
BaseTable is a basic table component that provides some fundamental table components, such as Table, TableHead, TableBody, TableRow, TableCell.