|
| 1 | +import * as React from 'react'; |
| 2 | + |
| 3 | +import { |
| 4 | + DataSourcePropIsNodeExpanded, |
| 5 | + InfiniteTableColumn, |
| 6 | + TreeDataSource, |
| 7 | + TreeExpandState, |
| 8 | + TreeGrid, |
| 9 | +} from '@infinite-table/infinite-react'; |
| 10 | +import { useState } from 'react'; |
| 11 | + |
| 12 | +export type FileSystemNode = { |
| 13 | + name: string; |
| 14 | + type: 'file' | 'folder'; |
| 15 | + children?: FileSystemNode[] | null; |
| 16 | + sizeKB?: number; |
| 17 | + id: string; |
| 18 | + collapsed?: boolean; |
| 19 | +}; |
| 20 | + |
| 21 | +export const nodes: FileSystemNode[] = [ |
| 22 | + { |
| 23 | + name: 'Documents', |
| 24 | + type: 'folder', |
| 25 | + id: '1', |
| 26 | + children: [ |
| 27 | + { |
| 28 | + name: 'report.doc', |
| 29 | + type: 'file', |
| 30 | + sizeKB: 100, |
| 31 | + id: '2', |
| 32 | + }, |
| 33 | + { |
| 34 | + type: 'folder', |
| 35 | + name: 'pictures', |
| 36 | + id: '3', |
| 37 | + children: [ |
| 38 | + { |
| 39 | + name: 'mountain.jpg', |
| 40 | + type: 'file', |
| 41 | + sizeKB: 302, |
| 42 | + id: '5', |
| 43 | + }, |
| 44 | + ], |
| 45 | + }, |
| 46 | + { |
| 47 | + type: 'folder', |
| 48 | + name: 'diverse', |
| 49 | + id: '4', |
| 50 | + children: [ |
| 51 | + { |
| 52 | + name: 'beach.jpg', |
| 53 | + type: 'file', |
| 54 | + sizeKB: 2024, |
| 55 | + id: '6', |
| 56 | + }, |
| 57 | + ], |
| 58 | + }, |
| 59 | + { |
| 60 | + type: 'file', |
| 61 | + name: 'last.txt', |
| 62 | + id: '7', |
| 63 | + }, |
| 64 | + ], |
| 65 | + }, |
| 66 | +]; |
| 67 | + |
| 68 | +const columns: Record<string, InfiniteTableColumn<FileSystemNode>> = { |
| 69 | + name: { |
| 70 | + field: 'name', |
| 71 | + renderTreeIcon: true, |
| 72 | + |
| 73 | + renderValue: ({ value, data }) => { |
| 74 | + return ( |
| 75 | + <> |
| 76 | + {value} - {data!.id} |
| 77 | + </> |
| 78 | + ); |
| 79 | + }, |
| 80 | + }, |
| 81 | + type: { field: 'type' }, |
| 82 | + sizeKB: { field: 'sizeKB' }, |
| 83 | +}; |
| 84 | + |
| 85 | +export default function DataTestPage() { |
| 86 | + const [treeExpandState] = useState<TreeExpandState>(() => { |
| 87 | + return new TreeExpandState({ |
| 88 | + defaultExpanded: false, |
| 89 | + expandedPaths: [['1', '4', '5'], ['1']], |
| 90 | + }); |
| 91 | + }); |
| 92 | + const [key, setKey] = useState(0); |
| 93 | + const isNodeExpanded = React.useCallback< |
| 94 | + DataSourcePropIsNodeExpanded<FileSystemNode> |
| 95 | + >( |
| 96 | + (rowInfo) => { |
| 97 | + return rowInfo.data.id === '3' |
| 98 | + ? false |
| 99 | + : treeExpandState.isNodeExpanded(rowInfo.nodePath); |
| 100 | + }, |
| 101 | + [key], |
| 102 | + ); |
| 103 | + |
| 104 | + return ( |
| 105 | + <React.StrictMode> |
| 106 | + <button |
| 107 | + onClick={() => { |
| 108 | + treeExpandState.expandAll(); |
| 109 | + setKey((k) => k + 1); |
| 110 | + }} |
| 111 | + > |
| 112 | + expand all |
| 113 | + </button> |
| 114 | + <TreeDataSource<FileSystemNode> |
| 115 | + data={nodes} |
| 116 | + primaryKey="id" |
| 117 | + nodesKey="children" |
| 118 | + treeExpandState={treeExpandState} |
| 119 | + onTreeExpandStateChange={(treeExpandStateValue) => { |
| 120 | + treeExpandState.update(treeExpandStateValue); |
| 121 | + setKey((k) => k + 1); |
| 122 | + }} |
| 123 | + isNodeExpanded={isNodeExpanded} |
| 124 | + > |
| 125 | + <TreeGrid<FileSystemNode> |
| 126 | + wrapRowsHorizontally |
| 127 | + domProps={{ |
| 128 | + style: { |
| 129 | + margin: '5px', |
| 130 | + height: 900, |
| 131 | + border: '1px solid gray', |
| 132 | + position: 'relative', |
| 133 | + }, |
| 134 | + }} |
| 135 | + columns={columns} |
| 136 | + /> |
| 137 | + </TreeDataSource> |
| 138 | + </React.StrictMode> |
| 139 | + ); |
| 140 | +} |
0 commit comments