|
| 1 | +import { Action, ControlledHighTable, Selection, State, initialState, reducer } from 'hightable' |
| 2 | +import { StrictMode, useMemo, useReducer } from 'react' |
| 3 | +import { data } from './data' |
| 4 | +import './HighTable.css' |
| 5 | +import './index.css' |
| 6 | + |
| 7 | +// for demo purpose |
| 8 | +function appReducer(state: State, action: Action): State { |
| 9 | + switch (action.type) { |
| 10 | + case 'SET_SELECTION': |
| 11 | + // do something special for the "SET_SELECTION" action |
| 12 | + console.log('SET_SELECTION', action.selection) |
| 13 | + return { ...state, selection: action.selection, anchor: action.anchor } |
| 14 | + default: |
| 15 | + // use the hightable reducer function for the rest of the actions |
| 16 | + return reducer(state, action) |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +export default function App() { |
| 21 | + const columns = data.header |
| 22 | + |
| 23 | + const [state, dispatch] = useReducer(appReducer, initialState) |
| 24 | + const { selection, orderBy } = state |
| 25 | + const columnId = useMemo(() => { |
| 26 | + if (!orderBy) return undefined |
| 27 | + const id = columns.indexOf(orderBy) |
| 28 | + return id === -1 ? undefined : id |
| 29 | + }, [columns, orderBy]) |
| 30 | + |
| 31 | + function onSortClick() { |
| 32 | + const nextId = ((columnId ?? -1) + 1) % columns.length |
| 33 | + dispatch({ type: 'SET_ORDER', orderBy: columns[nextId] }) |
| 34 | + } |
| 35 | + function onSelectionClick() { |
| 36 | + const newSelection = selection.map(({ start, end }) => ({ start: start + 1, end: end + 1 })) |
| 37 | + dispatch({ type: 'SET_SELECTION', selection: newSelection, anchor: undefined }) |
| 38 | + } |
| 39 | + function getSelectionCount(selection: Selection) { |
| 40 | + return selection.reduce((acc: number, { start, end }) => acc + end - start, 0) |
| 41 | + } |
| 42 | + function getFirstRows(selection: Selection, max = 5) { |
| 43 | + const indexes: string[] = [] |
| 44 | + let rangeIdx = 0 |
| 45 | + while (indexes.length < max && rangeIdx < selection.length) { |
| 46 | + const { start, end } = selection[rangeIdx] |
| 47 | + let rowIdx = start |
| 48 | + while (indexes.length < max && rowIdx < end) { |
| 49 | + indexes.push(rowIdx.toString()) |
| 50 | + rowIdx++ |
| 51 | + } |
| 52 | + rangeIdx++ |
| 53 | + } |
| 54 | + if (indexes.length === max) { |
| 55 | + indexes.pop() |
| 56 | + indexes.push('...') |
| 57 | + } |
| 58 | + return indexes |
| 59 | + } |
| 60 | + |
| 61 | + return <StrictMode> |
| 62 | + <div style={{ display: 'flex', flexDirection: 'column', width: '100%' }}> |
| 63 | + <div style={{ padding: '1em' }}> |
| 64 | + <h2>Hightable demo</h2> |
| 65 | + |
| 66 | + <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: '1em' }}> |
| 67 | + <div style={{ padding: '1em', border: '1px solid #ccc' }}> |
| 68 | + <h3>Order by</h3> |
| 69 | + <p>Click the button to sort the table by the next column</p> |
| 70 | + <button onClick={onSortClick}>Sort the following column</button> |
| 71 | + <p>Column ID: {columnId}</p> |
| 72 | + <p>{columnId === undefined ? 'No sorted column' : 'Column name: "' + columns[columnId] + '"'}</p> |
| 73 | + </div> |
| 74 | + <div style={{ padding: '1em', border: '1px solid #ccc' }}> |
| 75 | + <h3>Rows selection</h3> |
| 76 | + <p>Click the button to delete the selected rows</p> |
| 77 | + <button onClick={onSelectionClick}>Move the selection down by one row</button> |
| 78 | + <p>selection: <code style={{ margin: '0.5em', padding: '0.2em 0.5em', backgroundColor: '#ddd' }}>{JSON.stringify(selection)}</code></p> |
| 79 | + <p>{getSelectionCount(selection)} selected rows: {getFirstRows(selection).map(index => <code key={index} style={{ margin: '0.5em', padding: '0.2em 0.5em', backgroundColor: '#ddd' }}>{index}</code>)}</p> |
| 80 | + </div> |
| 81 | + </div> |
| 82 | + </div> |
| 83 | + <ControlledHighTable data={data} cacheKey='demo' selectable state={state} dispatch={dispatch} /> |
| 84 | + </div> |
| 85 | + </StrictMode> |
| 86 | +} |
0 commit comments