|
1 | | -import * as React from 'react' |
2 | | - |
3 | | -import { _createTable } from '@tanstack/table-core' |
4 | | -import type { |
5 | | - RowData, |
6 | | - TableFeatures, |
7 | | - TableOptions, |
8 | | - TableOptionsResolved, |
9 | | -} from '@tanstack/table-core' |
10 | | - |
11 | 1 | export * from '@tanstack/table-core' |
12 | 2 |
|
13 | | -export type Renderable<TProps> = React.ReactNode | React.ComponentType<TProps> |
14 | | - |
15 | | -// |
16 | | - |
17 | | -/** |
18 | | - * If rendering headers, cells, or footers with custom markup, use flexRender instead of `cell.getValue()` or `cell.renderValue()`. |
19 | | - */ |
20 | | -export function flexRender<TProps extends object>( |
21 | | - Comp: Renderable<TProps>, |
22 | | - props: TProps, |
23 | | -): React.ReactNode | JSX.Element { |
24 | | - return !Comp ? null : isReactComponent<TProps>(Comp) ? ( |
25 | | - <Comp {...props} /> |
26 | | - ) : ( |
27 | | - Comp |
28 | | - ) |
29 | | -} |
30 | | - |
31 | | -function isReactComponent<TProps>( |
32 | | - component: unknown, |
33 | | -): component is React.ComponentType<TProps> { |
34 | | - return ( |
35 | | - isClassComponent(component) || |
36 | | - typeof component === 'function' || |
37 | | - isExoticComponent(component) |
38 | | - ) |
39 | | -} |
40 | | - |
41 | | -function isClassComponent(component: any) { |
42 | | - return ( |
43 | | - typeof component === 'function' && |
44 | | - (() => { |
45 | | - const proto = Object.getPrototypeOf(component) |
46 | | - return proto.prototype && proto.prototype.isReactComponent |
47 | | - })() |
48 | | - ) |
49 | | -} |
50 | | - |
51 | | -function isExoticComponent(component: any) { |
52 | | - return ( |
53 | | - typeof component === 'object' && |
54 | | - typeof component.$$typeof === 'symbol' && |
55 | | - ['react.memo', 'react.forward_ref'].includes(component.$$typeof.description) |
56 | | - ) |
57 | | -} |
58 | | - |
59 | | -export function useTable< |
60 | | - TFeatures extends TableFeatures, |
61 | | - TData extends RowData, |
62 | | ->(options: TableOptions<TFeatures, TData>) { |
63 | | - // Compose in the generic options to the user options |
64 | | - const resolvedOptions: TableOptionsResolved<TFeatures, TData> = { |
65 | | - state: {}, // Dummy state |
66 | | - onStateChange: () => {}, // noop |
67 | | - renderFallbackValue: null, |
68 | | - ...options, |
69 | | - } |
70 | | - |
71 | | - // Create a new table and store it in state |
72 | | - const [tableRef] = React.useState(() => ({ |
73 | | - current: _createTable<TFeatures, TData>(resolvedOptions), |
74 | | - })) |
75 | | - |
76 | | - // By default, manage table state here using the table's initial state |
77 | | - const [state, setState] = React.useState(() => tableRef.current.initialState) |
78 | | - |
79 | | - // Compose the default state above with any user state. This will allow the user |
80 | | - // to only control a subset of the state if desired. |
81 | | - tableRef.current.setOptions((prev) => ({ |
82 | | - ...prev, |
83 | | - ...options, |
84 | | - state: { |
85 | | - ...state, |
86 | | - ...options.state, |
87 | | - }, |
88 | | - // Similarly, we'll maintain both our internal state and any user-provided |
89 | | - // state. |
90 | | - onStateChange: (updater) => { |
91 | | - setState(updater) |
92 | | - options.onStateChange?.(updater) |
93 | | - }, |
94 | | - })) |
95 | | - |
96 | | - return tableRef.current |
97 | | -} |
| 3 | +export * from './useTable' |
| 4 | +export * from './FlexRender' |
| 5 | +export * from './tableFactory' |
0 commit comments