1- import { HighTable } from 'hightable'
2- import { StrictMode } from 'react'
1+ import { createTableControl , HighTable , Selection } from 'hightable'
2+ import { StrictMode , useState } from 'react'
33import ReactDOM from 'react-dom/client'
44import { data } from './data'
55import './HighTable.css'
@@ -8,6 +8,84 @@ import './index.css'
88const app = document . getElementById ( 'app' )
99if ( ! app ) throw new Error ( 'missing app element' )
1010
11- ReactDOM . createRoot ( app ) . render ( < StrictMode >
12- < HighTable data = { data } cacheKey = 'demo' />
13- </ StrictMode > )
11+ function App ( ) {
12+ const tableControl = createTableControl ( )
13+ const columns = data . header
14+
15+ const [ columnId , setColumnId ] = useState < number | undefined > ( )
16+ const [ selection , setSelection ] = useState < Selection > ( [ ] )
17+
18+ function onOrderByChange ( orderBy : string | undefined ) {
19+ console . log ( "New value for orderBy: " + orderBy )
20+ if ( ! orderBy ) {
21+ setColumnId ( undefined )
22+ return
23+ }
24+ const id = columns . indexOf ( orderBy )
25+ if ( id === - 1 ) {
26+ setColumnId ( undefined )
27+ }
28+ setColumnId ( id )
29+ }
30+ function onSelectionChange ( selection : Selection ) {
31+ setSelection ( selection )
32+ }
33+
34+ function onSortClick ( ) {
35+ const nextId = ( ( columnId ?? - 1 ) + 1 ) % columns . length
36+ tableControl . setOrderBy ( columns [ nextId ] )
37+ }
38+ function onSelectionClick ( ) {
39+ const newSelection = selection . map ( ( { start, end} ) => ( { start : start + 1 , end : end + 1 } ) )
40+ tableControl . setSelection ( newSelection )
41+ }
42+ function getSelectionCount ( selection : Selection ) {
43+ return selection . reduce ( ( acc : number , { start, end} ) => acc + end - start , 0 )
44+ }
45+ function getFirstRows ( selection : Selection , max = 5 ) {
46+ const indexes : string [ ] = [ ]
47+ let rangeIdx = 0
48+ while ( indexes . length < max && rangeIdx < selection . length ) {
49+ const { start, end} = selection [ rangeIdx ]
50+ let rowIdx = start
51+ while ( indexes . length < max && rowIdx < end ) {
52+ indexes . push ( rowIdx . toString ( ) )
53+ rowIdx ++
54+ }
55+ rangeIdx ++
56+ }
57+ if ( indexes . length === max ) {
58+ indexes . pop ( )
59+ indexes . push ( '...' )
60+ }
61+ return indexes
62+ }
63+
64+ return ( < StrictMode >
65+ < div style = { { display : 'flex' , flexDirection : 'column' , width : '100%' } } >
66+ < div style = { { padding : '1em' } } >
67+ < h2 > Hightable demo</ h2 >
68+
69+ < div style = { { display : 'grid' , gridTemplateColumns : '1fr 1fr' , gap : '1em' } } >
70+ < div style = { { padding : '1em' , border : '1px solid #ccc' } } >
71+ < h3 > Order by</ h3 >
72+ < p > Click the button to sort the table by the next column</ p >
73+ < button onClick = { onSortClick } > Sort the following column</ button >
74+ < p > Column ID: { columnId } </ p >
75+ < p > { columnId === undefined ? 'No sorted column' : ( 'Column name: "' + columns [ columnId ] + '"' ) } </ p >
76+ </ div >
77+ < div style = { { padding : '1em' , border : '1px solid #ccc' } } >
78+ < h3 > Rows selection</ h3 >
79+ < p > Click the button to delete the selected rows</ p >
80+ < button onClick = { onSelectionClick } > Move the selection down by one row</ button >
81+ < p > selection: < code style = { { margin : '0.5em' , padding : '0.2em 0.5em' , backgroundColor : '#ddd' } } > { JSON . stringify ( selection ) } </ code > </ p >
82+ < p > { getSelectionCount ( selection ) } selected rows: { getFirstRows ( selection ) . map ( index => < code style = { { margin : '0.5em' , padding : '0.2em 0.5em' , backgroundColor : '#ddd' } } > { index } </ code > ) } </ p >
83+ </ div >
84+ </ div >
85+ </ div >
86+ < HighTable data = { data } cacheKey = 'demo' selectable tableControl = { tableControl } onOrderByChange = { onOrderByChange } onSelectionChange = { onSelectionChange } />
87+ </ div >
88+ </ StrictMode > )
89+ }
90+
91+ ReactDOM . createRoot ( app ) . render ( < App > </ App > )
0 commit comments