-
Notifications
You must be signed in to change notification settings - Fork 2.9k
FocusUtil: fix getPreviousElement to include previous sibling elements correctly #3928
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dzearing
merged 6 commits into
microsoft:master
from
jspurlin:jspurlin/FocusFixFindPrevElement
Feb 16, 2018
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4d4427b
getPreviousChild would skip over siblings if a childmatch was found b…
jspurlin da83645
rush change
jspurlin ffe2f4e
Merge branch 'master' of https://github.com/jspurlin/office-ui-fabric-…
jspurlin d4118f0
add unit test for FocusTrapZone testing the behavior that is being fi…
jspurlin 9303e62
Merge branch 'master' of https://github.com/OfficeDev/office-ui-fabri…
jspurlin de0745e
update comment for the ut
jspurlin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
common/changes/@uifabric/utilities/jspurlin-FocusFixFindPrevElement_2018-02-08-21-02.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "changes": [ | ||
| { | ||
| "packageName": "@uifabric/utilities", | ||
| "comment": "Focus: Fix getPreviousElement to correctly walk across previous siblings if a potential child match was found", | ||
| "type": "patch" | ||
| } | ||
| ], | ||
| "packageName": "@uifabric/utilities", | ||
| "email": "jspurlin@microsoft.com" | ||
| } |
152 changes: 152 additions & 0 deletions
152
packages/office-ui-fabric-react/src/components/FocusTrapZone/FocusTrapZone.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| /* tslint:disable:no-unused-variable */ | ||
| import * as React from 'react'; | ||
| /* tslint:enable:no-unused-variable */ | ||
|
|
||
| import * as ReactDOM from 'react-dom'; | ||
| import * as ReactTestUtils from 'react-dom/test-utils'; | ||
| import { KeyCodes } from '../../Utilities'; | ||
|
|
||
| import { FocusZone, FocusZoneDirection } from '../FocusZone'; | ||
| import { FocusTrapZone } from './FocusTrapZone'; | ||
|
|
||
| describe('FocusTrapZone', () => { | ||
| let lastFocusedElement: HTMLElement | undefined; | ||
| function _onFocus(ev: any) { | ||
| lastFocusedElement = ev.target; | ||
| } | ||
|
|
||
| function setupElement(element: HTMLElement, { | ||
| clientRect, | ||
| isVisible = true | ||
| }: { | ||
| clientRect: { | ||
| top: number; | ||
| left: number; | ||
| bottom: number; | ||
| right: number; | ||
| }; | ||
| isVisible?: boolean; | ||
| }): void { | ||
| element.getBoundingClientRect = () => ({ | ||
| top: clientRect.top, | ||
| left: clientRect.left, | ||
| bottom: clientRect.bottom, | ||
| right: clientRect.right, | ||
| width: clientRect.right - clientRect.left, | ||
| height: clientRect.bottom - clientRect.top | ||
| }); | ||
|
|
||
| element.setAttribute('data-is-visible', String(isVisible)); | ||
|
|
||
| element.focus = () => ReactTestUtils.Simulate.focus(element); | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| lastFocusedElement = undefined; | ||
| }); | ||
|
|
||
| it('can tab across FocusZones with different button structures', () => { | ||
| const component = ReactTestUtils.renderIntoDocument( | ||
| <div { ...{ onFocusCapture: _onFocus } }> | ||
| <FocusTrapZone forceFocusInsideTrap={ false }> | ||
| <FocusZone direction={ FocusZoneDirection.horizontal } data-is-visible={ true }> | ||
| <div data-is-visible={ true }> | ||
| <button className='a'>a</button> | ||
| </div> | ||
| <div data-is-visible={ true }> | ||
| <button className='b'>b</button> | ||
| </div> | ||
| <div data-is-visible={ true }> | ||
| <button className='c'>c</button> | ||
| </div> | ||
| </FocusZone> | ||
| <FocusZone direction={ FocusZoneDirection.horizontal } data-is-visible={ true }> | ||
| <div data-is-visible={ true }> | ||
| <div data-is-visible={ true }> | ||
| <button className='d'>d</button> | ||
| <button className='e'>e</button> | ||
| <button className='f'>f</button> | ||
| </div> | ||
| </div> | ||
| </FocusZone> | ||
| </FocusTrapZone> | ||
| </div> | ||
| ); | ||
|
|
||
| const focusTrapZone = ReactDOM.findDOMNode(component as React.ReactInstance) as Element; | ||
|
|
||
| const buttonA = focusTrapZone.querySelector('.a') as HTMLElement; | ||
| const buttonB = focusTrapZone.querySelector('.b') as HTMLElement; | ||
| const buttonC = focusTrapZone.querySelector('.c') as HTMLElement; | ||
| const buttonD = focusTrapZone.querySelector('.d') as HTMLElement; | ||
| const buttonE = focusTrapZone.querySelector('.e') as HTMLElement; | ||
| const buttonF = focusTrapZone.querySelector('.f') as HTMLElement; | ||
|
|
||
| // Assign bounding locations to buttons. | ||
| setupElement(buttonA, { | ||
| clientRect: { | ||
| top: 0, | ||
| bottom: 30, | ||
| left: 0, | ||
| right: 30 | ||
| } | ||
| }); | ||
|
|
||
| setupElement(buttonB, { | ||
| clientRect: { | ||
| top: 0, | ||
| bottom: 30, | ||
| left: 30, | ||
| right: 60 | ||
| } | ||
| }); | ||
|
|
||
| setupElement(buttonC, { | ||
| clientRect: { | ||
| top: 0, | ||
| bottom: 30, | ||
| left: 60, | ||
| right: 90 | ||
| } | ||
| }); | ||
|
|
||
| setupElement(buttonD, { | ||
| clientRect: { | ||
| top: 30, | ||
| bottom: 60, | ||
| left: 0, | ||
| right: 30 | ||
| } | ||
| }); | ||
|
|
||
| setupElement(buttonE, { | ||
| clientRect: { | ||
| top: 30, | ||
| bottom: 60, | ||
| left: 30, | ||
| right: 60 | ||
| } | ||
| }); | ||
|
|
||
| setupElement(buttonF, { | ||
| clientRect: { | ||
| top: 30, | ||
| bottom: 60, | ||
| left: 60, | ||
| right: 90 | ||
| } | ||
| }); | ||
|
|
||
| // Focus the first button. | ||
| ReactTestUtils.Simulate.focus(buttonA); | ||
| expect(lastFocusedElement).toBe(buttonA); | ||
|
|
||
| // Pressing shift + tab should go to d. | ||
| ReactTestUtils.Simulate.keyDown(buttonA, { which: KeyCodes.tab, shiftKey: true }); | ||
| expect(lastFocusedElement).toBe(buttonD); | ||
|
|
||
| // Pressing tab should go to a. | ||
| ReactTestUtils.Simulate.keyDown(buttonD, { which: KeyCodes.tab }); | ||
| expect(lastFocusedElement).toBe(buttonA); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to write a unit test for this scenario?
Also, is there logic missing on "findNextElement"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getNextElement doesn't need to be updated, the logic is slightly different and doesn't check for tabindex when checking if an element is tabbable. There may be some other bug hidden in here from that difference, but it doesn't need this update
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and yes, I'll look at adding a unit test
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cool ping me once you have a test to cover it (so we don't break it later!) and let's get it merged.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dzearing done, ut added. I also verified that this breaks without my fix and works with it :)