Skip to content

Commit 705467a

Browse files
author
Brian Ryu
committed
Allow keyboard sorting
1 parent ba3fc32 commit 705467a

File tree

5 files changed

+457
-65
lines changed

5 files changed

+457
-65
lines changed

src/.stories/Storybook.scss

+14
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,14 @@
4141
border-bottom: 1px solid #efefef;
4242
box-sizing: border-box;
4343
user-select: none;
44+
outline: none;
4445

4546
color: #333;
4647
font-weight: 400;
48+
49+
&:focus {
50+
border: 2px solid lightsteelblue
51+
}
4752
}
4853

4954
.disabled {
@@ -52,6 +57,15 @@
5257
}
5358

5459
// Drag handle
60+
.handleWrapper {
61+
width: 18px;
62+
height: 18px;
63+
outline: none;
64+
65+
&:focus {
66+
outline: 4px solid lightsteelblue
67+
}
68+
}
5569
.handle {
5670
display: block;
5771
width: 18px;

src/.stories/index.js

+35-11
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ function getItems(count, height) {
2626
});
2727
}
2828

29-
const Handle = SortableHandle(() => (
30-
<div className={style.handle}>
29+
const Handle = SortableHandle(({tabIndex}) => (
30+
<div className={style.handle} tabIndex={tabIndex}>
3131
<svg viewBox="0 0 50 50">
3232
<path
3333
d="M 0 7.5 L 0 12.5 L 50 12.5 L 50 7.5 L 0 7.5 z M 0 22.5 L 0 27.5 L 50 27.5 L 50 22.5 L 0 22.5 z M 0 37.5 L 0 42.5 L 50 42.5 L 50 37.5 L 0 37.5 z"
@@ -37,21 +37,34 @@ const Handle = SortableHandle(() => (
3737
</div>
3838
));
3939

40-
const Item = SortableElement((props) => {
40+
const Item = SortableElement(({
41+
tabbable,
42+
className,
43+
isDisabled,
44+
height,
45+
style: propStyle,
46+
shouldUseDragHandle,
47+
value
48+
}) => {
49+
const isTabbable = tabbable && !isDisabled;
50+
const bodyTabIndex = isTabbable && !shouldUseDragHandle ? 0 : -1;
51+
const handleTabIndex = isTabbable && shouldUseDragHandle ? 0 : -1;
52+
4153
return (
4254
<div
4355
className={classNames(
44-
props.className,
45-
props.isDisabled && style.disabled,
56+
className,
57+
isDisabled && style.disabled,
4658
)}
4759
style={{
48-
height: props.height,
49-
...props.style,
60+
height,
61+
...propStyle,
5062
}}
63+
tabIndex={bodyTabIndex}
5164
>
52-
{props.shouldUseDragHandle && <Handle />}
65+
{shouldUseDragHandle && <Handle tabIndex={handleTabIndex} />}
5366
<div className={style.wrapper}>
54-
<span>Item</span> {props.value}
67+
<span>Item</span> {value}
5568
</div>
5669
</div>
5770
);
@@ -66,6 +79,7 @@ const SortableList = SortableContainer(
6679

6780
return (
6881
<Item
82+
tabbable
6983
key={`item-${value}`}
7084
disabled={disabled}
7185
isDisabled={disabled}
@@ -103,11 +117,13 @@ class SortableListWithCustomContainer extends React.Component {
103117
}
104118

105119
const Category = SortableElement((props) => {
120+
const tabIndex = props.tabbable ? 0 : -1;
121+
106122
return (
107123
<div className={style.category}>
108124
<div className={style.categoryHeader}>
109125
<Handle />
110-
<span>Category {props.value}</span>
126+
<span tabIndex={tabIndex}>Category {props.value}</span>
111127
</div>
112128
<ListWrapper
113129
component={SortableList}
@@ -212,6 +228,7 @@ const SortableReactWindow = (Component) =>
212228

213229
return (
214230
<Item
231+
tabbable
215232
key={value}
216233
index={index}
217234
className={itemClass}
@@ -236,6 +253,7 @@ const SortableVirtualList = SortableContainer(
236253
const {value, height} = items[index];
237254
return (
238255
<Item
256+
tabbable
239257
key={value}
240258
index={index}
241259
className={itemClass}
@@ -267,6 +285,7 @@ class VirtualizedListWrapper extends Component {
267285
const {value, height} = items[index];
268286
return (
269287
<Item
288+
tabbable
270289
key={value}
271290
index={index}
272291
className={itemClass}
@@ -344,9 +363,13 @@ const SortableInfiniteList = SortableContainer(
344363
className={className}
345364
containerHeight={600}
346365
elementHeight={items.map(({height}) => height)}
366+
// for react-infinite, a larger preload is better for keyboard sorting
367+
preloadBatchSize={Infinite.containerHeightScaleFactor(2)}
368+
preloadAdditionalHeight={Infinite.containerHeightScaleFactor(2)}
347369
>
348370
{items.map(({value, height}, index) => (
349371
<Item
372+
tabbable
350373
key={`item-${index}`}
351374
className={itemClass}
352375
index={index}
@@ -365,6 +388,7 @@ const ShrinkingSortableList = SortableContainer(
365388
<div className={className}>
366389
{items.map(({value, height}, index) => (
367390
<Item
391+
tabbable
368392
key={`item-${value}`}
369393
className={itemClass}
370394
index={index}
@@ -383,7 +407,7 @@ const NestedSortableList = SortableContainer(
383407
return (
384408
<div className={className}>
385409
{items.map((value, index) => (
386-
<Category key={`category-${value}`} index={index} value={value} />
410+
<Category tabbable key={`category-${value}`} index={index} value={value} />
387411
))}
388412
</div>
389413
);

0 commit comments

Comments
 (0)