4
4
5
5
import * as React from 'react' ;
6
6
import VirtualList from 'rc-virtual-list' ;
7
- import KeyCode from 'rc-util/lib/KeyCode' ;
8
7
import { FlattenNode , Key , DataEntity , DataNode } from './interface' ;
9
8
import MotionTreeNode from './MotionTreeNode' ;
10
- import { TreeContext } from './contextTypes' ;
11
9
import { findExpandedKeys , getExpandRange } from './utils/diffUtil' ;
12
- import { getTreeNodeProps , getKey , convertNodePropsToEventData } from './utils/treeUtil' ;
10
+ import { getTreeNodeProps , getKey } from './utils/treeUtil' ;
13
11
14
12
const HIDDEN_STYLE = {
15
13
width : 0 ,
@@ -55,7 +53,7 @@ interface NodeListProps {
55
53
data : FlattenNode [ ] ;
56
54
motion : any ;
57
55
focusable ?: boolean ;
58
- activeKey ?: Key ;
56
+ activeItem : FlattenNode ;
59
57
focused ?: boolean ;
60
58
tabIndex : number ;
61
59
checkable ?: boolean ;
@@ -143,7 +141,7 @@ const NodeList: React.FC<NodeListProps> = props => {
143
141
itemHeight,
144
142
145
143
focusable,
146
- activeKey ,
144
+ activeItem ,
147
145
focused,
148
146
tabIndex,
149
147
@@ -240,120 +238,13 @@ const NodeList: React.FC<NodeListProps> = props => {
240
238
} ;
241
239
242
240
// =========================== Accessibility ==========================
243
- function setActiveKey ( key : Key ) {
244
- onActiveChange ( key ) ;
245
- }
246
-
247
- const activeItem = data . find ( ( { data : { key } } ) => key === activeKey ) ;
248
-
249
- const changeActive = ( offset : number ) => {
250
- let index = data . findIndex ( ( { data : { key } } ) => key === activeKey ) ;
251
-
252
- // Align with index
253
- if ( index === - 1 && offset < 0 ) {
254
- index = data . length ;
255
- }
256
-
257
- index = ( index + offset + data . length ) % data . length ;
258
-
259
- const item = data [ index ] ;
260
- if ( item ) {
261
- setActiveKey ( item . data . key ) ;
262
- } else {
263
- setActiveKey ( null ) ;
264
- }
265
- } ;
266
-
267
241
// Clean up `activeKey` if blur
268
242
React . useEffect ( ( ) => {
269
243
if ( ! focused ) {
270
- setActiveKey ( null ) ;
244
+ onActiveChange ( null ) ;
271
245
}
272
246
} , [ focused ] ) ;
273
247
274
- // ============================= Keyboard =============================
275
- const { onNodeExpand, onNodeCheck, onNodeSelect } = React . useContext ( TreeContext ) ;
276
-
277
- const onInternalKeyDown : React . KeyboardEventHandler < HTMLDivElement > = event => {
278
- // >>>>>>>>>> Direction
279
- switch ( event . which ) {
280
- case KeyCode . UP : {
281
- changeActive ( - 1 ) ;
282
- event . preventDefault ( ) ;
283
- break ;
284
- }
285
- case KeyCode . DOWN : {
286
- changeActive ( 1 ) ;
287
- event . preventDefault ( ) ;
288
- break ;
289
- }
290
- }
291
-
292
- if ( activeItem && activeItem . data ) {
293
- // >>>>>>>>>> Expand & Selection
294
- const expandable =
295
- activeItem . data . isLeaf === false || ! ! ( activeItem . data . children || [ ] ) . length ;
296
- const eventNode = convertNodePropsToEventData ( {
297
- ...getTreeNodeProps ( activeKey , treeNodeRequiredProps ) ,
298
- data : activeItem . data ,
299
- active : true ,
300
- } ) ;
301
-
302
- switch ( event . which ) {
303
- // >>> Expand
304
- case KeyCode . LEFT : {
305
- // Collapse if possible
306
- if ( expandable && expandedKeys . includes ( activeKey ) ) {
307
- onNodeExpand ( { } as React . MouseEvent < HTMLDivElement > , eventNode ) ;
308
- } else if ( activeItem . parent ) {
309
- setActiveKey ( activeItem . parent . data . key ) ;
310
- }
311
- event . preventDefault ( ) ;
312
- break ;
313
- }
314
- case KeyCode . RIGHT : {
315
- // Expand if possible
316
- if ( expandable && ! expandedKeys . includes ( activeKey ) ) {
317
- onNodeExpand ( { } as React . MouseEvent < HTMLDivElement > , eventNode ) ;
318
- } else if ( activeItem . children && activeItem . children . length ) {
319
- setActiveKey ( activeItem . children [ 0 ] . data . key ) ;
320
- }
321
- event . preventDefault ( ) ;
322
- break ;
323
- }
324
-
325
- // Selection
326
- case KeyCode . ENTER :
327
- case KeyCode . SPACE : {
328
- if (
329
- checkable &&
330
- ! eventNode . disabled &&
331
- eventNode . checkable !== false &&
332
- ! eventNode . disableCheckbox
333
- ) {
334
- onNodeCheck (
335
- { } as React . MouseEvent < HTMLDivElement > ,
336
- eventNode ,
337
- ! checkedKeys . includes ( activeKey ) ,
338
- ) ;
339
- } else if (
340
- ! checkable &&
341
- selectable &&
342
- ! eventNode . disabled &&
343
- eventNode . selectable !== false
344
- ) {
345
- onNodeSelect ( { } as React . MouseEvent < HTMLDivElement > , eventNode ) ;
346
- }
347
- break ;
348
- }
349
- }
350
- }
351
-
352
- if ( onKeyDown ) {
353
- onKeyDown ( event ) ;
354
- }
355
- } ;
356
-
357
248
return (
358
249
< >
359
250
{ focused && activeItem && (
@@ -367,7 +258,7 @@ const NodeList: React.FC<NodeListProps> = props => {
367
258
style = { HIDDEN_STYLE }
368
259
disabled = { focusable === false || disabled }
369
260
tabIndex = { focusable !== false ? tabIndex : null }
370
- onKeyDown = { onInternalKeyDown }
261
+ onKeyDown = { onKeyDown }
371
262
onFocus = { onFocus }
372
263
onBlur = { onBlur }
373
264
value = ""
@@ -401,7 +292,7 @@ const NodeList: React.FC<NodeListProps> = props => {
401
292
< MotionTreeNode
402
293
{ ...restProps }
403
294
{ ...treeNodeProps }
404
- active = { activeKey !== null && key === activeKey }
295
+ active = { activeItem && key === activeItem . data . key }
405
296
pos = { pos }
406
297
data = { treeNode . data }
407
298
isStart = { isStart }
@@ -412,7 +303,7 @@ const NodeList: React.FC<NodeListProps> = props => {
412
303
onMotionEnd = { onMotionEnd }
413
304
treeNodeRequiredProps = { treeNodeRequiredProps }
414
305
onMouseMove = { ( ) => {
415
- setActiveKey ( null ) ;
306
+ onActiveChange ( null ) ;
416
307
} }
417
308
/>
418
309
) ;
0 commit comments