GeezerVison is showing github.com — [react-ui] Add preventDefault+stopPropagation to Keyboard + update Fo… · react/react@57a5805
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/react-ui/accessibility/src/FocusTable.js +6-6Lines changed: 6 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -146,7 +146,7 @@ export function createFocusTable(): Array<React.Component> { | 146 | 146 | function Cell({children}): FocusCellProps { | 147 | 147 | const scopeRef = useRef(null); | 148 | 148 | const keyboard = useKeyboard({ | 149 | | - onKeyDown(event: KeyboardEvent): boolean { | | 149 | + onKeyDown(event: KeyboardEvent): void { | 150 | 150 | const currentCell = scopeRef.current; | 151 | 151 | switch (event.key) { | 152 | 152 | case 'UpArrow': { | @@ -162,7 +162,7 @@ export function createFocusTable(): Array<React.Component> { | 162 | 162 | } | 163 | 163 | } | 164 | 164 | } | 165 | | - return false; | | 165 | + return; | 166 | 166 | } | 167 | 167 | case 'DownArrow': { | 168 | 168 | const [cells, rowIndex] = getRowCells(currentCell); | @@ -179,7 +179,7 @@ export function createFocusTable(): Array<React.Component> { | 179 | 179 | } | 180 | 180 | } | 181 | 181 | } | 182 | | - return false; | | 182 | + return; | 183 | 183 | } | 184 | 184 | case 'LeftArrow': { | 185 | 185 | const [cells, rowIndex] = getRowCells(currentCell); | @@ -190,7 +190,7 @@ export function createFocusTable(): Array<React.Component> { | 190 | 190 | triggerNavigateOut(currentCell, 'left'); | 191 | 191 | } | 192 | 192 | } | 193 | | - return false; | | 193 | + return; | 194 | 194 | } | 195 | 195 | case 'RightArrow': { | 196 | 196 | const [cells, rowIndex] = getRowCells(currentCell); | @@ -203,10 +203,10 @@ export function createFocusTable(): Array<React.Component> { | 203 | 203 | } | 204 | 204 | } | 205 | 205 | } | 206 | | - return false; | | 206 | + return; | 207 | 207 | } | 208 | 208 | } | 209 | | - return true; | | 209 | + event.continuePropagation(); | 210 | 210 | }, | 211 | 211 | }); | 212 | 212 | return ( | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-ui/accessibility/src/TabFocus.js +43-20Lines changed: 43 additions & 20 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -53,10 +53,11 @@ function focusElem(elem: null | HTMLElement): void { | 53 | 53 | } | 54 | 54 | } | 55 | 55 |
| 56 | | -export function focusNext( | | 56 | +function internalFocusNext( | 57 | 57 | scope: ReactScopeMethods, | | 58 | + event?: KeyboardEvent, | 58 | 59 | contain?: boolean, | 59 | | -): boolean { | | 60 | +): void { | 60 | 61 | const [ | 61 | 62 | tabbableNodes, | 62 | 63 | firstTabbableElem, | @@ -66,23 +67,31 @@ export function focusNext( | 66 | 67 | ] = getTabbableNodes(scope); | 67 | 68 |
| 68 | 69 | if (focusedElement === null) { | 69 | | - focusElem(firstTabbableElem); | | 70 | + if (event) { | | 71 | + event.continuePropagation(); | | 72 | + } | 70 | 73 | } else if (focusedElement === lastTabbableElem) { | 71 | | - if (contain === true) { | | 74 | + if (contain) { | 72 | 75 | focusElem(firstTabbableElem); | 73 | | - } else { | 74 | | - return true; | | 76 | + if (event) { | | 77 | + event.preventDefault(); | | 78 | + } | | 79 | + } else if (event) { | | 80 | + event.continuePropagation(); | 75 | 81 | } | 76 | 82 | } else { | 77 | 83 | focusElem((tabbableNodes: any)[currentIndex + 1]); | | 84 | + if (event) { | | 85 | + event.preventDefault(); | | 86 | + } | 78 | 87 | } | 79 | | - return false; | 80 | 88 | } | 81 | 89 |
| 82 | | -export function focusPrevious( | | 90 | +function internalFocusPrevious( | 83 | 91 | scope: ReactScopeMethods, | | 92 | + event?: KeyboardEvent, | 84 | 93 | contain?: boolean, | 85 | | -): boolean { | | 94 | +): void { | 86 | 95 | const [ | 87 | 96 | tabbableNodes, | 88 | 97 | firstTabbableElem, | @@ -92,17 +101,32 @@ export function focusPrevious( | 92 | 101 | ] = getTabbableNodes(scope); | 93 | 102 |
| 94 | 103 | if (focusedElement === null) { | 95 | | - focusElem(firstTabbableElem); | | 104 | + if (event) { | | 105 | + event.continuePropagation(); | | 106 | + } | 96 | 107 | } else if (focusedElement === firstTabbableElem) { | 97 | | - if (contain === true) { | | 108 | + if (contain) { | 98 | 109 | focusElem(lastTabbableElem); | 99 | | - } else { | 100 | | - return true; | | 110 | + if (event) { | | 111 | + event.preventDefault(); | | 112 | + } | | 113 | + } else if (event) { | | 114 | + event.continuePropagation(); | 101 | 115 | } | 102 | 116 | } else { | 103 | 117 | focusElem((tabbableNodes: any)[currentIndex - 1]); | | 118 | + if (event) { | | 119 | + event.preventDefault(); | | 120 | + } | 104 | 121 | } | 105 | | - return false; | | 122 | +} | | 123 | + | | 124 | +export function focusPrevious(scope: ReactScopeMethods): void { | | 125 | + internalFocusPrevious(scope); | | 126 | +} | | 127 | + | | 128 | +export function focusNext(scope: ReactScopeMethods): void { | | 129 | + internalFocusNext(scope); | 106 | 130 | } | 107 | 131 |
| 108 | 132 | export function getNextController( | @@ -137,21 +161,20 @@ export const TabFocusController = React.forwardRef( | 137 | 161 | ({children, contain}: TabFocusControllerProps, ref): React.Node => { | 138 | 162 | const scopeRef = useRef(null); | 139 | 163 | const keyboard = useKeyboard({ | 140 | | - onKeyDown(event: KeyboardEvent): boolean { | | 164 | + onKeyDown(event: KeyboardEvent): void { | 141 | 165 | if (event.key !== 'Tab') { | 142 | | - return true; | | 166 | + event.continuePropagation(); | | 167 | + return; | 143 | 168 | } | 144 | 169 | const scope = scopeRef.current; | 145 | 170 | if (scope !== null) { | 146 | 171 | if (event.shiftKey) { | 147 | | - return focusPrevious(scope, contain); | | 172 | + internalFocusPrevious(scope, event, contain); | 148 | 173 | } else { | 149 | | - return focusNext(scope, contain); | | 174 | + internalFocusNext(scope, event, contain); | 150 | 175 | } | 151 | 176 | } | 152 | | - return true; | 153 | 177 | }, | 154 | | - preventKeys: ['Tab', ['Tab', {shiftKey: true}]], | 155 | 178 | }); | 156 | 179 |
| 157 | 180 | return ( | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-ui/accessibility/src/__tests__/TabFocus-test.internal.js +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -255,14 +255,14 @@ describe('TabFocusController', () => { | 255 | 255 | firstFocusController, | 256 | 256 | ); | 257 | 257 | expect(nextController).toBe(secondFocusController); | 258 | | - ReactTabFocus.focusNext(nextController); | | 258 | + ReactTabFocus.focusFirst(nextController); | 259 | 259 | expect(document.activeElement).toBe(divRef.current); | 260 | 260 |
| 261 | 261 | const previousController = ReactTabFocus.getPreviousController( | 262 | 262 | nextController, | 263 | 263 | ); | 264 | 264 | expect(previousController).toBe(firstFocusController); | 265 | | - ReactTabFocus.focusNext(previousController); | | 265 | + ReactTabFocus.focusFirst(previousController); | 266 | 266 | expect(document.activeElement).toBe(buttonRef.current); | 267 | 267 | }); | 268 | 268 | }); | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-ui/events/src/dom/Keyboard.js +24-85Lines changed: 24 additions & 85 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -24,15 +24,12 @@ type KeyboardEventType = | 24 | 24 |
| 25 | 25 | type KeyboardProps = {| | 26 | 26 | disabled?: boolean, | 27 | | - onClick?: (e: KeyboardEvent) => ?boolean, | 28 | | - onKeyDown?: (e: KeyboardEvent) => ?boolean, | 29 | | - onKeyUp?: (e: KeyboardEvent) => ?boolean, | 30 | | - preventClick?: boolean, | 31 | | - preventKeys?: PreventKeysArray, | | 27 | + onClick?: (e: KeyboardEvent) => void, | | 28 | + onKeyDown?: (e: KeyboardEvent) => void, | | 29 | + onKeyUp?: (e: KeyboardEvent) => void, | 32 | 30 | |}; | 33 | 31 |
| 34 | 32 | type KeyboardState = {| | 35 | | - defaultPrevented: boolean, | 36 | 33 | isActive: boolean, | 37 | 34 | |}; | 38 | 35 |
| @@ -48,20 +45,11 @@ export type KeyboardEvent = {| | 48 | 45 | target: Element | Document, | 49 | 46 | type: KeyboardEventType, | 50 | 47 | timeStamp: number, | | 48 | + continuePropagation: () => void, | | 49 | + preventDefault: () => void, | 51 | 50 | |}; | 52 | 51 |
| 53 | | -type ModifiersObject = {| | 54 | | - altKey?: boolean, | 55 | | - ctrlKey?: boolean, | 56 | | - metaKey?: boolean, | 57 | | - shiftKey?: boolean, | 58 | | -|}; | 59 | | - | 60 | | -type PreventKeysArray = Array<string | Array<string | ModifiersObject>>; | 61 | | - | 62 | | -const isArray = Array.isArray; | 63 | 52 | const targetEventTypes = ['click_active', 'keydown_active', 'keyup']; | 64 | | -const modifiers = ['altKey', 'ctrlKey', 'metaKey', 'shiftKey']; | 65 | 53 |
| 66 | 54 | /** | 67 | 55 | * Normalization of deprecated HTML5 `key` values | @@ -146,20 +134,31 @@ function createKeyboardEvent( | 146 | 134 | event: ReactDOMResponderEvent, | 147 | 135 | context: ReactDOMResponderContext, | 148 | 136 | type: KeyboardEventType, | 149 | | - defaultPrevented: boolean, | 150 | 137 | ): KeyboardEvent { | 151 | 138 | const nativeEvent = (event: any).nativeEvent; | 152 | 139 | const {altKey, ctrlKey, metaKey, shiftKey} = nativeEvent; | 153 | 140 | let keyboardEvent = { | 154 | 141 | altKey, | 155 | 142 | ctrlKey, | 156 | | - defaultPrevented, | | 143 | + defaultPrevented: nativeEvent.defaultPrevented === true, | 157 | 144 | metaKey, | 158 | 145 | pointerType: 'keyboard', | 159 | 146 | shiftKey, | 160 | 147 | target: event.target, | 161 | 148 | timeStamp: context.getTimeStamp(), | 162 | 149 | type, | | 150 | + // We don't use stopPropagation, as the default behavior | | 151 | + // is to not propagate. Plus, there might be confusion | | 152 | + // using stopPropagation as we don't actually stop | | 153 | + // native propagation from working, but instead only | | 154 | + // allow propagation to the others keyboard responders. | | 155 | + continuePropagation() { | | 156 | + context.continuePropagation(); | | 157 | + }, | | 158 | + preventDefault() { | | 159 | + keyboardEvent.defaultPrevented = true; | | 160 | + nativeEvent.preventDefault(); | | 161 | + }, | 163 | 162 | }; | 164 | 163 | if (type !== 'keyboard:click') { | 165 | 164 | const key = getEventKey(nativeEvent); | @@ -171,32 +170,18 @@ function createKeyboardEvent( | 171 | 170 |
| 172 | 171 | function dispatchKeyboardEvent( | 173 | 172 | event: ReactDOMResponderEvent, | 174 | | - listener: KeyboardEvent => ?boolean, | | 173 | + listener: KeyboardEvent => void, | 175 | 174 | context: ReactDOMResponderContext, | 176 | 175 | type: KeyboardEventType, | 177 | | - defaultPrevented: boolean, | 178 | 176 | ): void { | 179 | | - const syntheticEvent = createKeyboardEvent( | 180 | | - event, | 181 | | - context, | 182 | | - type, | 183 | | - defaultPrevented, | 184 | | - ); | 185 | | - let shouldPropagate; | 186 | | - const listenerWithReturnValue = e => { | 187 | | - shouldPropagate = listener(e); | 188 | | - }; | 189 | | - context.dispatchEvent(syntheticEvent, listenerWithReturnValue, DiscreteEvent); | 190 | | - if (shouldPropagate) { | 191 | | - context.continuePropagation(); | 192 | | - } | | 177 | + const syntheticEvent = createKeyboardEvent(event, context, type); | | 178 | + context.dispatchEvent(syntheticEvent, listener, DiscreteEvent); | 193 | 179 | } | 194 | 180 |
| 195 | 181 | const keyboardResponderImpl = { | 196 | 182 | targetEventTypes, | 197 | 183 | getInitialState(): KeyboardState { | 198 | 184 | return { | 199 | | - defaultPrevented: false, | 200 | 185 | isActive: false, | 201 | 186 | }; | 202 | 187 | }, | @@ -207,82 +192,36 @@ const keyboardResponderImpl = { | 207 | 192 | state: KeyboardState, | 208 | 193 | ): void { | 209 | 194 | const {type} = event; | 210 | | - const nativeEvent: any = event.nativeEvent; | 211 | 195 |
| 212 | 196 | if (props.disabled) { | 213 | 197 | return; | 214 | 198 | } | 215 | 199 |
| 216 | 200 | if (type === 'keydown') { | 217 | | - state.defaultPrevented = nativeEvent.defaultPrevented === true; | 218 | | - | 219 | | - const preventKeys = ((props.preventKeys: any): PreventKeysArray); | 220 | | - if (!state.defaultPrevented && isArray(preventKeys)) { | 221 | | - preventKeyLoop: for (let i = 0; i < preventKeys.length; i++) { | 222 | | - const preventKey = preventKeys[i]; | 223 | | - let key = preventKey; | 224 | | - | 225 | | - if (isArray(preventKey)) { | 226 | | - key = preventKey[0]; | 227 | | - const config = ((preventKey[1]: any): Object); | 228 | | - for (let s = 0; s < modifiers.length; s++) { | 229 | | - const modifier = modifiers[s]; | 230 | | - const configModifier = config[modifier]; | 231 | | - const eventModifier = nativeEvent[modifier]; | 232 | | - if ( | 233 | | - (configModifier && !eventModifier) || | 234 | | - (!configModifier && eventModifier) | 235 | | - ) { | 236 | | - continue preventKeyLoop; | 237 | | - } | 238 | | - } | 239 | | - } | 240 | | - | 241 | | - if (key === getEventKey(nativeEvent)) { | 242 | | - state.defaultPrevented = true; | 243 | | - nativeEvent.preventDefault(); | 244 | | - break; | 245 | | - } | 246 | | - } | 247 | | - } | 248 | 201 | state.isActive = true; | 249 | 202 | const onKeyDown = props.onKeyDown; | 250 | 203 | if (onKeyDown != null) { | 251 | 204 | dispatchKeyboardEvent( | 252 | 205 | event, | 253 | | - ((onKeyDown: any): (e: KeyboardEvent) => ?boolean), | | 206 | + ((onKeyDown: any): (e: KeyboardEvent) => void), | 254 | 207 | context, | 255 | 208 | 'keyboard:keydown', | 256 | | - state.defaultPrevented, | 257 | 209 | ); | 258 | 210 | } | 259 | 211 | } else if (type === 'click' && isVirtualClick(event)) { | 260 | | - if (props.preventClick !== false) { | 261 | | - // 'click' occurs before or after 'keyup', and may need native | 262 | | - // behavior prevented | 263 | | - nativeEvent.preventDefault(); | 264 | | - state.defaultPrevented = true; | 265 | | - } | 266 | 212 | const onClick = props.onClick; | 267 | 213 | if (onClick != null) { | 268 | | - dispatchKeyboardEvent( | 269 | | - event, | 270 | | - onClick, | 271 | | - context, | 272 | | - 'keyboard:click', | 273 | | - state.defaultPrevented, | 274 | | - ); | | 214 | + dispatchKeyboardEvent(event, onClick, context, 'keyboard:click'); | 275 | 215 | } | 276 | 216 | } else if (type === 'keyup') { | 277 | 217 | state.isActive = false; | 278 | 218 | const onKeyUp = props.onKeyUp; | 279 | 219 | if (onKeyUp != null) { | 280 | 220 | dispatchKeyboardEvent( | 281 | 221 | event, | 282 | | - ((onKeyUp: any): (e: KeyboardEvent) => ?boolean), | | 222 | + ((onKeyUp: any): (e: KeyboardEvent) => void), | 283 | 223 | context, | 284 | 224 | 'keyboard:keyup', | 285 | | - state.defaultPrevented, | 286 | 225 | ); | 287 | 226 | } | 288 | 227 | } | |
|