-
Notifications
You must be signed in to change notification settings - Fork 4k
/
navigable-table.js
163 lines (138 loc) · 4.19 KB
/
navigable-table.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
/**
* WordPress dependencies
*/
import {
isTextField,
isHorizontalEdge,
isVerticalEdge,
placeCaretAtHorizontalEdge,
} from '@wordpress/dom';
import { useRef } from '@wordpress/element';
import { UP, DOWN, LEFT, RIGHT, HOME, END, isKeyboardModifierEvent } from '@wordpress/keycodes';
/**
* Internal dependencies
*/
import {
getLocationOfCellAbove,
getLocationOfCellBelow,
getLocationOfCellToLeft,
getLocationOfCellToRight,
getLocationOfFirstCellInColumn,
getLocationOfLastCellInColumn,
getLocationOfFirstCellInRow,
getLocationOfLastCellInRow,
getLocationOfFirstCellInTable,
getLocationOfLastCellInTable,
} from './state';
function getNextCellLocation( tableState, selectedCell, { isPrimary, isUp, isDown, isLeft, isRight, isHome, isEnd } ) {
if ( isUp ) {
if ( isPrimary ) {
return getLocationOfFirstCellInColumn( tableState, selectedCell );
}
return getLocationOfCellAbove( tableState, selectedCell );
}
if ( isDown ) {
if ( isPrimary ) {
return getLocationOfLastCellInColumn( tableState, selectedCell );
}
return getLocationOfCellBelow( tableState, selectedCell );
}
if ( isLeft ) {
if ( isPrimary ) {
return getLocationOfFirstCellInRow( selectedCell );
}
return getLocationOfCellToLeft( selectedCell );
}
if ( isRight ) {
if ( isPrimary ) {
return getLocationOfLastCellInRow( tableState, selectedCell );
}
return getLocationOfCellToRight( tableState, selectedCell );
}
if ( isHome ) {
if ( isPrimary ) {
return getLocationOfFirstCellInTable( tableState );
}
return getLocationOfFirstCellInRow( selectedCell );
}
if ( isEnd ) {
if ( isPrimary ) {
return getLocationOfLastCellInTable( tableState );
}
return getLocationOfLastCellInRow( tableState, selectedCell );
}
}
function getCellContentEditableElement( tableElement, { sectionName, rowIndex, columnIndex } ) {
if ( ! tableElement ) {
return;
}
const rowElement = tableElement.querySelectorAll( `t${ sectionName } tr` )[ rowIndex ];
if ( ! rowElement ) {
return;
}
return rowElement.querySelectorAll( '*[contenteditable="true"]' )[ columnIndex ];
}
export default function NavigableTable( { children, className, tableState, selectedCell } ) {
const tableRef = useRef();
const handleKeyDown = ( event ) => {
if ( ! selectedCell ) {
return;
}
const { keyCode, target } = event;
//
if ( ! isTextField( target ) ) {
return;
}
const isUp = keyCode === UP;
const isDown = keyCode === DOWN;
const isLeft = keyCode === LEFT;
const isRight = keyCode === RIGHT;
const isHome = keyCode === HOME;
const isEnd = keyCode === END;
const isHorizontal = isLeft || isRight || isHome || isEnd;
const isVertical = isUp || isDown;
const isNav = isHorizontal || isVertical;
// The user hasn't pressed a navigation key, abort early.
if ( ! isNav ) {
return;
}
// Abort if navigation has already been handled (e.g. RichText inline
// boundaries).
if ( event.nativeEvent.defaultPrevented ) {
return;
}
const isAtEdge = isVertical ? isVerticalEdge : isHorizontalEdge;
const isReverse = isUp || isLeft || isHome;
const isCaretAtEdgeOfField = isAtEdge( target, isReverse );
if ( ! isCaretAtEdgeOfField ) {
return;
}
event.stopPropagation();
event.preventDefault();
const isPrimary = isKeyboardModifierEvent.primary( event );
const nextCellLocation = getNextCellLocation( tableState, selectedCell, { isPrimary, isUp, isDown, isLeft, isRight, isHome, isEnd } );
if ( ! nextCellLocation ) {
return;
}
const contentEditableElement = getCellContentEditableElement( tableRef.current, nextCellLocation );
if ( ! contentEditableElement ) {
return;
}
placeCaretAtHorizontalEdge( contentEditableElement, isReverse );
};
// Disable reason: Wrapper itself is non-interactive, but must capture
// bubbling events from children to determine focus transition intents.
/* eslint-disable jsx-a11y/no-static-element-interactions */
/* eslint-disable jsx-a11y/no-noninteractive-element-interactions */
return (
<table
ref={ tableRef }
className={ className }
onKeyDown={ handleKeyDown }
>
{ children }
</table>
);
/* eslint-enable jsx-a11y/no-static-element-interactions */
/* eslint-enable jsx-a11y/no-noninteractive-element-interactions */
}