-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathTable.tsx
More file actions
224 lines (215 loc) · 8.38 KB
/
Copy pathTable.tsx
File metadata and controls
224 lines (215 loc) · 8.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import type { KeyboardEvent } from 'react'
import { useCallback, useContext, useMemo } from 'react'
import { CellNavigationContext } from '../contexts/CellNavigationContext.js'
import { ColumnsVisibilityContext } from '../contexts/ColumnsVisibilityContext.js'
import { DataFrameMethodsContext, DataVersionContext, NumRowsContext } from '../contexts/DataContext.js'
import { OrderByContext } from '../contexts/OrderByContext.js'
import { RenderedRowsContext } from '../contexts/ScrollContext.js'
import { SelectionContext } from '../contexts/SelectionContext.js'
import { ariaOffset } from '../helpers/constants.js'
import Cell from './Cell.js'
import Row from './Row.js'
import RowHeader from './RowHeader.js'
import TableCorner from './TableCorner.js'
import TableHeader from './TableHeader.js'
export default function Table() {
const { moveCell } = useContext(CellNavigationContext)
const orderBy = useContext(OrderByContext)
const { selectable, toggleAllRows, pendingSelectionGesture, onTableKeyDown: onSelectionTableKeyDown, allRowsSelected, isRowSelected, toggleRowNumber, toggleRangeToRowNumber } = useContext(SelectionContext)
const { visibleColumnsParameters: columnsParameters } = useContext(ColumnsVisibilityContext)
const { renderedRowsStart, renderedRowsEnd } = useContext(RenderedRowsContext)
/** A version number that increments whenever a data frame is updated or resolved (the key remains the same). */
const version = useContext(DataVersionContext)
/** The actual number of rows in the data frame */
const numRows = useContext(NumRowsContext)
const dataFrameMethods = useContext(DataFrameMethodsContext)
const onNavigationTableKeyDown = useMemo(() => {
if (!moveCell) {
// disable keyboard navigation if moveCell is not provided
return
}
return (event: KeyboardEvent) => {
const { key, altKey, ctrlKey, metaKey, shiftKey } = event
// if the user is pressing Alt, Meta or Shift, do not handle the event
if (altKey || metaKey || shiftKey) {
return
}
if (key === 'ArrowRight') {
if (ctrlKey) {
moveCell({ type: 'LAST_COLUMN' })
} else {
moveCell({ type: 'NEXT_COLUMN' })
}
} else if (key === 'ArrowLeft') {
if (ctrlKey) {
moveCell({ type: 'FIRST_COLUMN' })
} else {
moveCell({ type: 'PREVIOUS_COLUMN' })
}
} else if (key === 'ArrowDown') {
if (ctrlKey) {
moveCell({ type: 'LAST_ROW' })
} else {
moveCell({ type: 'NEXT_ROW' })
}
} else if (key === 'ArrowUp') {
if (ctrlKey) {
moveCell({ type: 'FIRST_ROW' })
} else {
moveCell({ type: 'PREVIOUS_ROW' })
}
} else if (key === 'Home') {
if (ctrlKey) {
moveCell({ type: 'FIRST_CELL' })
} else {
moveCell({ type: 'FIRST_COLUMN' })
}
} else if (key === 'End') {
if (ctrlKey) {
moveCell({ type: 'LAST_CELL' })
} else {
moveCell({ type: 'LAST_COLUMN' })
}
} else if (key === 'PageDown') {
moveCell({ type: 'NEXT_ROWS_PAGE' })
// TODO(SL): same for horizontal scrolling with Alt+PageDown?
} else if (key === 'PageUp') {
moveCell({ type: 'PREVIOUS_ROWS_PAGE' })
// TODO(SL): same for horizontal scrolling with Alt+PageUp?
} else if (key !== ' ') {
// if the key is not one of the above, do not handle it
// special case: no action is associated with the Space key, but it's captured
// anyway to prevent the default action (scrolling the page) and stay in navigation mode
return
}
// avoid scrolling the table when the user is navigating with the keyboard
event.stopPropagation()
event.preventDefault()
}
}, [moveCell])
const onTableKeyDown = useMemo(() => {
if (onNavigationTableKeyDown || onSelectionTableKeyDown) {
return (event: KeyboardEvent) => {
onNavigationTableKeyDown?.(event)
onSelectionTableKeyDown?.(event)
}
}
}, [onNavigationTableKeyDown, onSelectionTableKeyDown])
const getOnCheckboxPress = useCallback(({ row, rowNumber }: { row: number, rowNumber?: number }) => {
if (rowNumber === undefined || !toggleRowNumber || !toggleRangeToRowNumber) {
return undefined
}
return ({ shiftKey }: { shiftKey: boolean }) => {
if (shiftKey) {
toggleRangeToRowNumber({ row, rowNumber })
} else {
toggleRowNumber({ rowNumber })
}
}
}, [toggleRowNumber, toggleRangeToRowNumber])
// Prepare the slice of data to render
// TODO(SL): also compute progress percentage here, to show a loading indicator
const slice = useMemo(() => {
if (renderedRowsStart === undefined || renderedRowsEnd === undefined) {
return {
rowContents: [],
canMeasureColumn: {},
version,
}
}
const rows = Array.from({ length: renderedRowsEnd - renderedRowsStart }, (_, i) => renderedRowsStart + i)
const canMeasureColumn: Record<string, boolean> = {}
const rowContents = rows.map((row) => {
const rowNumber = dataFrameMethods.getRowNumber({ row, orderBy })?.value
const cells = (columnsParameters ?? []).map(({ name: column, index: originalColumnIndex, className }) => {
const cell = dataFrameMethods.getCell({ row, column, orderBy })
canMeasureColumn[column] ||= cell !== undefined
return { columnIndex: originalColumnIndex, cell, className }
})
return {
row,
rowNumber,
cells,
}
})
return {
rowContents,
canMeasureColumn,
version,
}
}, [dataFrameMethods, columnsParameters, renderedRowsStart, renderedRowsEnd, orderBy, version])
// don't render table if the data frame has no visible columns
// (it can have zero rows, but must have at least one visible column)
if (!columnsParameters) return
const ariaColCount = columnsParameters.length + 1 // don't forget the selection column
const ariaRowCount = numRows + 1 // don't forget the header row
return (
<table
aria-readonly={true}
aria-colcount={ariaColCount}
aria-rowcount={ariaRowCount}
aria-multiselectable={selectable}
aria-busy={pendingSelectionGesture /* TODO(SL): add other busy states? Used only for tests right now */}
role="grid"
onKeyDown={onTableKeyDown}
>
<caption id="caption" hidden>Virtual-scroll table</caption>
<thead role="rowgroup">
<Row ariaRowIndex={1}>
<TableCorner
onCheckboxPress={toggleAllRows}
checked={allRowsSelected}
pendingSelectionGesture={pendingSelectionGesture}
ariaColIndex={1}
ariaRowIndex={1}
/>
<TableHeader
canMeasureColumn={slice.canMeasureColumn}
columnsParameters={columnsParameters}
ariaRowIndex={1}
/>
</Row>
</thead>
<tbody role="rowgroup">
{slice.rowContents.map(({ row, rowNumber, cells }) => {
const ariaRowIndex = row + ariaOffset
const selected = isRowSelected?.({ rowNumber })
const rowKey = `${row}`
return (
<Row
key={rowKey}
ariaRowIndex={ariaRowIndex}
selected={selected}
rowNumber={rowNumber}
// title={rowError(row, columns.length)} // TODO(SL): re-enable later?
>
<RowHeader
selected={selected}
rowNumber={rowNumber}
onCheckboxPress={getOnCheckboxPress({ rowNumber, row })}
pendingSelectionGesture={pendingSelectionGesture}
ariaColIndex={1}
ariaRowIndex={ariaRowIndex}
/>
{cells.map(({ columnIndex, cell, className }, visibleColumnIndex) => {
return (
<Cell
key={columnIndex}
columnIndex={columnIndex}
visibleColumnIndex={visibleColumnIndex}
className={className}
ariaColIndex={visibleColumnIndex + ariaOffset}
ariaRowIndex={ariaRowIndex}
cellValue={cell?.value}
hasResolved={cell !== undefined}
rowNumber={rowNumber}
/>
)
})}
</Row>
)
})}
</tbody>
</table>
)
}