|
| 1 | +import { |
| 2 | + DataSourceApi, |
| 3 | + InfiniteTableColumn, |
| 4 | + TreeDataSource, |
| 5 | + TreeGrid, |
| 6 | + TreeSelectionValue, |
| 7 | +} from '@infinite-table/infinite-react'; |
| 8 | +import { useState } from 'react'; |
| 9 | + |
| 10 | +type FileSystemNode = { |
| 11 | + id: string; |
| 12 | + name: string; |
| 13 | + type: 'folder' | 'file'; |
| 14 | + extension?: string; |
| 15 | + mimeType?: string; |
| 16 | + sizeInKB: number; |
| 17 | + children?: FileSystemNode[]; |
| 18 | +}; |
| 19 | + |
| 20 | +const columns: Record<string, InfiniteTableColumn<FileSystemNode>> = { |
| 21 | + name: { |
| 22 | + field: 'name', |
| 23 | + header: 'Name', |
| 24 | + defaultWidth: 500, |
| 25 | + renderValue: ({ value, rowInfo }) => { |
| 26 | + return ( |
| 27 | + <> |
| 28 | + {rowInfo.id} - {value} |
| 29 | + </> |
| 30 | + ); |
| 31 | + }, |
| 32 | + renderTreeIcon: true, |
| 33 | + renderSelectionCheckBox: true, |
| 34 | + }, |
| 35 | + type: { field: 'type', header: 'Type' }, |
| 36 | + extension: { field: 'extension', header: 'Extension' }, |
| 37 | + mimeType: { field: 'mimeType', header: 'Mime Type' }, |
| 38 | + size: { field: 'sizeInKB', type: 'number', header: 'Size (KB)' }, |
| 39 | +}; |
| 40 | + |
| 41 | +const defaultTreeSelection: TreeSelectionValue = { |
| 42 | + defaultSelection: true, |
| 43 | + deselectedPaths: [['1', '10']], |
| 44 | + selectedPaths: [['3']], |
| 45 | +}; |
| 46 | + |
| 47 | +export default function App() { |
| 48 | + const [dataSourceApi, setDataSourceApi] = |
| 49 | + useState<DataSourceApi<FileSystemNode> | null>(); |
| 50 | + |
| 51 | + return ( |
| 52 | + <> |
| 53 | + <TreeDataSource |
| 54 | + onReady={setDataSourceApi} |
| 55 | + nodesKey="children" |
| 56 | + primaryKey="id" |
| 57 | + data={dataSource} |
| 58 | + defaultTreeSelection={defaultTreeSelection} |
| 59 | + > |
| 60 | + <div |
| 61 | + style={{ |
| 62 | + color: 'var(--infinite-cell-color)', |
| 63 | + padding: 10, |
| 64 | + display: 'flex', |
| 65 | + gap: 10, |
| 66 | + }} |
| 67 | + > |
| 68 | + <button |
| 69 | + onClick={() => { |
| 70 | + dataSourceApi!.treeApi.selectAll(); |
| 71 | + }} |
| 72 | + > |
| 73 | + Select all |
| 74 | + </button> |
| 75 | + <button |
| 76 | + onClick={() => { |
| 77 | + dataSourceApi!.treeApi.deselectAll(); |
| 78 | + }} |
| 79 | + > |
| 80 | + Deselect all |
| 81 | + </button> |
| 82 | + </div> |
| 83 | + |
| 84 | + <TreeGrid columns={columns} domProps={{ style: { height: '100%' } }} /> |
| 85 | + </TreeDataSource> |
| 86 | + </> |
| 87 | + ); |
| 88 | +} |
| 89 | + |
| 90 | +const dataSource = () => { |
| 91 | + const nodes: FileSystemNode[] = [ |
| 92 | + { |
| 93 | + id: '1', |
| 94 | + name: 'Documents', |
| 95 | + sizeInKB: 1200, |
| 96 | + type: 'folder', |
| 97 | + children: [ |
| 98 | + { |
| 99 | + id: '10', |
| 100 | + name: 'Private', |
| 101 | + sizeInKB: 100, |
| 102 | + type: 'folder', |
| 103 | + children: [ |
| 104 | + { |
| 105 | + id: '100', |
| 106 | + name: 'Report.docx', |
| 107 | + sizeInKB: 210, |
| 108 | + type: 'file', |
| 109 | + extension: 'docx', |
| 110 | + mimeType: 'application/msword', |
| 111 | + }, |
| 112 | + { |
| 113 | + id: '101', |
| 114 | + name: 'Vacation.docx', |
| 115 | + sizeInKB: 120, |
| 116 | + type: 'file', |
| 117 | + extension: 'docx', |
| 118 | + mimeType: 'application/msword', |
| 119 | + }, |
| 120 | + { |
| 121 | + id: '102', |
| 122 | + name: 'CV.pdf', |
| 123 | + sizeInKB: 108, |
| 124 | + type: 'file', |
| 125 | + extension: 'pdf', |
| 126 | + mimeType: 'application/pdf', |
| 127 | + }, |
| 128 | + ], |
| 129 | + }, |
| 130 | + ], |
| 131 | + }, |
| 132 | + { |
| 133 | + id: '2', |
| 134 | + name: 'Desktop', |
| 135 | + sizeInKB: 1000, |
| 136 | + type: 'folder', |
| 137 | + children: [ |
| 138 | + { |
| 139 | + id: '20', |
| 140 | + name: 'unknown.txt', |
| 141 | + sizeInKB: 100, |
| 142 | + type: 'file', |
| 143 | + }, |
| 144 | + ], |
| 145 | + }, |
| 146 | + { |
| 147 | + id: '3', |
| 148 | + name: 'Media', |
| 149 | + sizeInKB: 1000, |
| 150 | + type: 'folder', |
| 151 | + children: [ |
| 152 | + { |
| 153 | + id: '30', |
| 154 | + name: 'Music - empty', |
| 155 | + sizeInKB: 0, |
| 156 | + type: 'folder', |
| 157 | + children: [], |
| 158 | + }, |
| 159 | + { |
| 160 | + id: '31', |
| 161 | + name: 'Videos', |
| 162 | + sizeInKB: 5400, |
| 163 | + type: 'folder', |
| 164 | + children: [ |
| 165 | + { |
| 166 | + id: '310', |
| 167 | + name: 'Vacation.mp4', |
| 168 | + sizeInKB: 108, |
| 169 | + type: 'file', |
| 170 | + extension: 'mp4', |
| 171 | + mimeType: 'video/mp4', |
| 172 | + }, |
| 173 | + ], |
| 174 | + }, |
| 175 | + ], |
| 176 | + }, |
| 177 | + ]; |
| 178 | + return Promise.resolve(nodes); |
| 179 | +}; |
0 commit comments