Skip to content

Commit 1054cc8

Browse files
committed
add controls to show how to exchange orderBy and selection with Hightable
1 parent 901d95a commit 1054cc8

File tree

3 files changed

+124
-8
lines changed

3 files changed

+124
-8
lines changed

apps/hightable-demo/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"typecheck": "tsc"
1515
},
1616
"dependencies": {
17-
"hightable": "0.7.3",
17+
"hightable": "../../../hightable",
1818
"react": "18.3.1",
1919
"react-dom": "18.3.1"
2020
}

apps/hightable-demo/src/HighTable.css

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,26 @@
102102
min-width: 32px;
103103
max-width: none;
104104
width: 32px;
105+
cursor: pointer;
106+
}
107+
.table td:first-child span {
108+
display: inline;
109+
}
110+
.table td:first-child input {
111+
display: none;
112+
}
113+
.selectable td:first-child:hover span, .selectable tr.selected td:first-child span {
114+
display: none;
115+
}
116+
.selectable td:first-child:hover input, .selectable tr.selected td:first-child input {
117+
display: inline;
118+
cursor: pointer;
119+
}
120+
.selectable tr.selected {
121+
background-color: #fbf7bf;
122+
}
123+
.selectable tr.selected td:first-child {
124+
background-color: #f1edbb;
105125
}
106126

107127
/* cells */
@@ -193,17 +213,35 @@
193213

194214
/* table corner */
195215
.table-corner {
196-
background-color: #e4e4e6;
197216
border-right: 1px solid #ccc;
198217
position: absolute;
199218
height: 33px;
200219
width: 32px;
201220
top: 0;
202221
left: 0;
203222
z-index: 15;
204-
box-shadow: inset 0 0 4px rgba(0, 0, 0, 0.2);
223+
box-shadow: inset 0 0 4px rgba(0, 0, 0, 0.2);
205224
background: url('https://hyperparam.app/assets/table/hightable.svg') #e4e4e6 no-repeat center 6px;
225+
cursor: default;
226+
display: flex;
227+
justify-content: center;
228+
}
229+
.selectable .table-corner {
230+
background: #e4e4e6;
231+
cursor: pointer;
232+
}
233+
.table-corner input {
234+
display: none;
235+
}
236+
.selectable .table-corner span {
237+
display: none;
206238
}
239+
.selectable .table-corner input {
240+
display: inline;
241+
cursor: pointer;
242+
text-align: center;
243+
}
244+
207245
/* mock row numbers */
208246
.mock-row-label {
209247
content: "";

apps/hightable-demo/src/main.tsx

Lines changed: 83 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { HighTable } from 'hightable'
2-
import { StrictMode } from 'react'
1+
import { createTableControl, HighTable, Selection } from 'hightable'
2+
import { StrictMode, useState } from 'react'
33
import ReactDOM from 'react-dom/client'
44
import { data } from './data'
55
import './HighTable.css'
@@ -8,6 +8,84 @@ import './index.css'
88
const app = document.getElementById('app')
99
if (!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

Comments
 (0)