|
| 1 | +import * as React from 'react'; |
| 2 | + |
| 3 | +import { |
| 4 | + InfiniteTable, |
| 5 | + DataSource, |
| 6 | + InfiniteTableColumn, |
| 7 | + useDataSourceState, |
| 8 | + DataSourceState, |
| 9 | + DataSourceProps, |
| 10 | +} from '@infinite-table/infinite-react'; |
| 11 | +import { CarSale } from '@examples/datasets/CarSale'; |
| 12 | + |
| 13 | +const carsales: CarSale[] = [ |
| 14 | + { |
| 15 | + category: '1 - Category 1 Truck', |
| 16 | + make: 'Acura', |
| 17 | + model: 'RDX 2WD', |
| 18 | + year: 2010, |
| 19 | + sales: 15, |
| 20 | + color: 'red', |
| 21 | + id: 0, |
| 22 | + }, |
| 23 | + { |
| 24 | + category: '1 - Category 1 Truck', |
| 25 | + make: 'Acura', |
| 26 | + model: 'RDX 4WD', |
| 27 | + year: 2007, |
| 28 | + sales: 1, |
| 29 | + color: 'red', |
| 30 | + id: 1, |
| 31 | + }, |
| 32 | + { |
| 33 | + category: '1 - Category 1 Truck', |
| 34 | + make: 'Acura', |
| 35 | + model: 'RDX 4WD', |
| 36 | + year: 2008, |
| 37 | + sales: 2, |
| 38 | + color: 'magenta', |
| 39 | + id: 2, |
| 40 | + }, |
| 41 | + { |
| 42 | + category: '1 - Category 1 Truck', |
| 43 | + make: 'Acura', |
| 44 | + model: 'RDX 4WD', |
| 45 | + year: 2009, |
| 46 | + sales: 136, |
| 47 | + color: 'blue', |
| 48 | + id: 3, |
| 49 | + }, |
| 50 | + { |
| 51 | + category: '1 - Category 1 Truck', |
| 52 | + make: 'Acura', |
| 53 | + model: 'RDX 4WD', |
| 54 | + year: 2010, |
| 55 | + color: 'blue', |
| 56 | + sales: 30, |
| 57 | + id: 4, |
| 58 | + }, |
| 59 | + { |
| 60 | + category: '1 - Category 1 Truck', |
| 61 | + make: 'Acura', |
| 62 | + model: 'TSX', |
| 63 | + year: 2009, |
| 64 | + sales: 14, |
| 65 | + color: 'yellow', |
| 66 | + id: 5, |
| 67 | + }, |
| 68 | + { |
| 69 | + category: '1 - Category 1 Truck', |
| 70 | + make: 'Acura', |
| 71 | + model: 'TSX', |
| 72 | + year: 2010, |
| 73 | + sales: 14, |
| 74 | + color: 'red', |
| 75 | + id: 6, |
| 76 | + }, |
| 77 | + { |
| 78 | + category: '1 - Category 1 Truck', |
| 79 | + make: 'Audi', |
| 80 | + model: 'A3', |
| 81 | + year: 2009, |
| 82 | + sales: 2, |
| 83 | + color: 'magenta', |
| 84 | + id: 7, |
| 85 | + }, |
| 86 | +]; |
| 87 | + |
| 88 | +(globalThis as any).carsales = carsales; |
| 89 | + |
| 90 | +const columns: Record<string, InfiniteTableColumn<CarSale>> = { |
| 91 | + make: { field: 'make' }, |
| 92 | + model: { field: 'model' }, |
| 93 | + |
| 94 | + category: { |
| 95 | + field: 'category', |
| 96 | + }, |
| 97 | + count: { |
| 98 | + field: 'sales', |
| 99 | + }, |
| 100 | + year: { |
| 101 | + field: 'year', |
| 102 | + type: 'number', |
| 103 | + }, |
| 104 | +}; |
| 105 | + |
| 106 | +function AppGrid() { |
| 107 | + const dataArrayLength = useDataSourceState( |
| 108 | + (state: DataSourceState<CarSale>) => state.dataArray.length, |
| 109 | + ); |
| 110 | + return ( |
| 111 | + <div> |
| 112 | + <p data-name="test">Showing {dataArrayLength} rows.</p> |
| 113 | + <InfiniteTable<CarSale> |
| 114 | + debugId="test" |
| 115 | + domProps={{ |
| 116 | + style: { |
| 117 | + margin: '5px', |
| 118 | + height: 900, |
| 119 | + border: '1px solid gray', |
| 120 | + position: 'relative', |
| 121 | + }, |
| 122 | + }} |
| 123 | + columns={columns} |
| 124 | + /> |
| 125 | + </div> |
| 126 | + ); |
| 127 | +} |
| 128 | + |
| 129 | +const groupRowsState: DataSourceProps<CarSale>['defaultGroupRowsState'] = { |
| 130 | + expandedRows: true, |
| 131 | + collapsedRows: [[2010]], |
| 132 | +}; |
| 133 | + |
| 134 | +export default function DataTestPage() { |
| 135 | + return ( |
| 136 | + <React.StrictMode> |
| 137 | + <DataSource<CarSale> |
| 138 | + data={carsales} |
| 139 | + primaryKey="id" |
| 140 | + defaultGroupBy={[{ field: 'year' }]} |
| 141 | + defaultGroupRowsState={groupRowsState} |
| 142 | + > |
| 143 | + <AppGrid /> |
| 144 | + </DataSource> |
| 145 | + </React.StrictMode> |
| 146 | + ); |
| 147 | +} |
0 commit comments