|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @flow |
| 8 | + */ |
| 9 | + |
| 10 | +importtype{KeyboardEvent}from'react-events/src/dom/Keyboard'; |
| 11 | + |
| 12 | +importReactfrom'react'; |
| 13 | +import{tabFocusableImpl}from'./TabbableScope'; |
| 14 | +import{useKeyboard}from'react-events/keyboard'; |
| 15 | + |
| 16 | +typeGridComponentProps={ |
| 17 | +children: React.Node, |
| 18 | +}; |
| 19 | + |
| 20 | +const{useRef}=React; |
| 21 | + |
| 22 | +functionfocusCell(cell){ |
| 23 | +consttabbableNodes=cell.getScopedNodes(); |
| 24 | +if(tabbableNodes!==null&&tabbableNodes.length>0){ |
| 25 | +tabbableNodes[0].focus(); |
| 26 | +} |
| 27 | +} |
| 28 | + |
| 29 | +functionfocusCellByRowIndex(row,rowIndex){ |
| 30 | +constcells=row.getChildren(); |
| 31 | +constcell=cells[rowIndex]; |
| 32 | +if(cell){ |
| 33 | +focusCell(cell); |
| 34 | +} |
| 35 | +} |
| 36 | + |
| 37 | +functiongetRowCells(currentCell){ |
| 38 | +constrow=currentCell.getParent(); |
| 39 | +if(parent!==null){ |
| 40 | +constcells=row.getChildren(); |
| 41 | +constrowIndex=cells.indexOf(currentCell); |
| 42 | +return[cells,rowIndex]; |
| 43 | +} |
| 44 | +return[null,0]; |
| 45 | +} |
| 46 | + |
| 47 | +functiongetColumns(currentCell){ |
| 48 | +constrow=currentCell.getParent(); |
| 49 | +if(parent!==null){ |
| 50 | +constgrid=row.getParent(); |
| 51 | +constcolumns=grid.getChildren(); |
| 52 | +constcolumnIndex=columns.indexOf(row); |
| 53 | +return[columns,columnIndex]; |
| 54 | +} |
| 55 | +return[null,0]; |
| 56 | +} |
| 57 | + |
| 58 | +exportfunctioncreateFocusGrid(): Array<React.Component>{ |
| 59 | +constGridScope=React.unstable_createScope(tabFocusableImpl); |
| 60 | + |
| 61 | +functionGridContainer({children}): GridComponentProps{ |
| 62 | +return<GridScope>{children}</GridScope>; |
| 63 | +} |
| 64 | + |
| 65 | +functionGridRow({children}): GridComponentProps{ |
| 66 | +return<GridScope>{children}</GridScope>; |
| 67 | +} |
| 68 | + |
| 69 | +functionGridCell({children}): GridComponentProps{ |
| 70 | +constscopeRef=useRef(null); |
| 71 | +constkeyboard=useKeyboard({ |
| 72 | +onKeyDown(event: KeyboardEvent): boolean{ |
| 73 | +constcurrentCell=scopeRef.current; |
| 74 | +switch(event.key){ |
| 75 | +case'UpArrow': { |
| 76 | +const[cells,rowIndex]=getRowCells(currentCell); |
| 77 | +if(cells!==null){ |
| 78 | +const[columns,columnIndex]=getColumns(currentCell); |
| 79 | +if(columns!==null){ |
| 80 | +if(columnIndex>0){ |
| 81 | +constcolumn=columns[columnIndex-1]; |
| 82 | +focusCellByRowIndex(column,rowIndex); |
| 83 | +} |
| 84 | +} |
| 85 | +} |
| 86 | +returnfalse; |
| 87 | +} |
| 88 | +case'DownArrow': { |
| 89 | +const[cells,rowIndex]=getRowCells(currentCell); |
| 90 | +if(cells!==null){ |
| 91 | +const[columns,columnIndex]=getColumns(currentCell); |
| 92 | +if(columns!==null){ |
| 93 | +if(columnIndex!==-1&&columnIndex!==columns.length-1){ |
| 94 | +constcolumn=columns[columnIndex+1]; |
| 95 | +focusCellByRowIndex(column,rowIndex); |
| 96 | +} |
| 97 | +} |
| 98 | +} |
| 99 | +returnfalse; |
| 100 | +} |
| 101 | +case'LeftArrow': { |
| 102 | +const[cells,rowIndex]=getRowCells(currentCell); |
| 103 | +if(cells!==null){ |
| 104 | +if(rowIndex>0){ |
| 105 | +focusCell(cells[rowIndex-1]); |
| 106 | +} |
| 107 | +} |
| 108 | +returnfalse; |
| 109 | +} |
| 110 | +case'RightArrow': { |
| 111 | +const[cells,rowIndex]=getRowCells(currentCell); |
| 112 | +if(cells!==null){ |
| 113 | +if(rowIndex!==-1&&rowIndex!==cells.length-1){ |
| 114 | +focusCell(cells[rowIndex+1]); |
| 115 | +} |
| 116 | +} |
| 117 | +returnfalse; |
| 118 | +} |
| 119 | +} |
| 120 | +returntrue; |
| 121 | +}, |
| 122 | +}); |
| 123 | +return( |
| 124 | +<GridScopelisteners={keyboard}ref={scopeRef}> |
| 125 | +{children} |
| 126 | +</GridScope> |
| 127 | +); |
| 128 | +} |
| 129 | + |
| 130 | +return[GridContainer,GridRow,GridCell]; |
| 131 | +} |
0 commit comments