-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
ArrowKeyStepper.example.js
208 lines (191 loc) · 5.92 KB
/
ArrowKeyStepper.example.js
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
/** @flow */
import * as React from 'react';
import {
ContentBox,
ContentBoxHeader,
ContentBoxParagraph,
} from '../demo/ContentBox';
import ArrowKeyStepper, {type ScrollIndices} from './';
import AutoSizer from '../AutoSizer';
import Grid from '../Grid';
import clsx from 'clsx';
import styles from './ArrowKeyStepper.example.css';
type State = {
mode: 'edges' | 'cells',
isClickable: boolean,
scrollToColumn: number,
scrollToRow: number,
};
export default class ArrowKeyStepperExample extends React.PureComponent<
{},
State,
> {
state = {
mode: 'edges',
isClickable: true,
scrollToColumn: 0,
scrollToRow: 0,
};
render() {
const {mode, isClickable, scrollToColumn, scrollToRow} = this.state;
return (
<ContentBox>
<ContentBoxHeader
text="ArrowKeyStepper"
sourceLink="https://github.com/bvaughn/react-virtualized/blob/master/source/ArrowKeyStepper/ArrowKeyStepper.example.js"
docsLink="https://github.com/bvaughn/react-virtualized/blob/master/docs/ArrowKeyStepper.md"
/>
<ContentBoxParagraph>
This high-order component decorates a <code>List</code>,{' '}
<code>Table</code>, or <code>Grid</code> and responds to arrow-key
events by scrolling one row or column at a time. Focus in the `Grid`
below and use the left, right, up, or down arrow keys to move around
within the grid.
</ContentBoxParagraph>
<ContentBoxParagraph>
Note that unlike the other HOCs in react-virtualized, the{' '}
<code>ArrowKeyStepper</code> adds a <code><div></code> element
around its children in order to attach a key-down event handler.
</ContentBoxParagraph>
<ContentBoxParagraph>
<strong>mode</strong>:
<label>
<input
aria-label='Set mode equal to "cells"'
checked={mode === 'cells'}
className={styles.Radio}
type="radio"
onChange={event =>
event.target.checked && this.setState({mode: 'cells'})
}
value="cells"
/>
cells
</label>
<label>
<input
aria-label='Set mode equal to "edges"'
checked={mode === 'edges'}
className={styles.Radio}
type="radio"
onChange={event =>
event.target.checked && this.setState({mode: 'edges'})
}
value="edges"
/>
edges (default)
</label>
</ContentBoxParagraph>
<ContentBoxParagraph>
<label className={styles.checkboxLabel}>
<input
aria-label="Enable click selection? (resets selection)"
className={styles.checkbox}
type="checkbox"
checked={isClickable}
onChange={this._onClickableChange}
/>
Enable click selection? (resets selection)
</label>
</ContentBoxParagraph>
<ArrowKeyStepper
columnCount={100}
isControlled={isClickable}
onScrollToChange={isClickable ? this._selectCell : undefined}
mode={mode}
rowCount={100}
scrollToColumn={scrollToColumn}
scrollToRow={scrollToRow}>
{({onSectionRendered, scrollToColumn, scrollToRow}) => (
<div>
<ContentBoxParagraph>
{`Most-recently-stepped column: ${scrollToColumn}, row: ${scrollToRow}`}
</ContentBoxParagraph>
<AutoSizer disableHeight>
{({width}) => (
<Grid
className={styles.Grid}
columnWidth={this._getColumnWidth}
columnCount={100}
height={200}
onSectionRendered={onSectionRendered}
cellRenderer={({columnIndex, key, rowIndex, style}) =>
this._cellRenderer({
columnIndex,
key,
rowIndex,
scrollToColumn,
scrollToRow,
style,
})
}
rowHeight={this._getRowHeight}
rowCount={100}
scrollToColumn={scrollToColumn}
scrollToRow={scrollToRow}
width={width}
/>
)}
</AutoSizer>
</div>
)}
</ArrowKeyStepper>
</ContentBox>
);
}
_getColumnWidth = ({index}: {index: number}) => {
return (1 + (index % 3)) * 60;
};
_getRowHeight = ({index}: {index: number}) => {
return (1 + (index % 3)) * 30;
};
_cellRenderer = ({
columnIndex,
key,
rowIndex,
scrollToColumn,
scrollToRow,
style,
}: {
columnIndex: number,
key: string,
rowIndex: number,
scrollToColumn: number,
scrollToRow: number,
style: Object,
}) => {
const className = clsx(styles.Cell, {
[styles.FocusedCell]:
columnIndex === scrollToColumn && rowIndex === scrollToRow,
});
return (
<span
role="none"
className={className}
key={key}
onClick={
this.state.isClickable &&
(() =>
this._selectCell({
scrollToColumn: columnIndex,
scrollToRow: rowIndex,
}))
}
style={style}>
{`r:${rowIndex}, c:${columnIndex}`}
</span>
);
};
_selectCell = ({scrollToColumn, scrollToRow}: ScrollIndices) => {
this.setState({scrollToColumn, scrollToRow});
};
_onClickableChange = (event: Event) => {
if (event.target instanceof HTMLInputElement) {
this.setState({
isClickable: event.target.checked,
scrollToColumn: 0,
scrollToRow: 0,
});
}
};
}