This repository has been archived by the owner on Mar 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
feat(AutoFocusZone): added simple AutoFocusZone component for focusing the first focusable element or the element with the provided selector #1015
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2088c2d
-initial commit
315d75f
-addressed comments
ad2f55a
Update AutoFocusZone.tsx
mnajdova e8bb2c7
-changed comments
6b68d02
Merge remote-tracking branch 'origin/feat/auto-focus-zone' into feat/…
8970207
-updated changelog
67d47ce
Merge branch 'master' into feat/auto-focus-zone
mnajdova 7ce4e1a
Merge branch 'master' into feat/auto-focus-zone
mnajdova 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
This file contains 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
This file contains 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
This file contains 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
18 changes: 18 additions & 0 deletions
18
packages/react/src/lib/accessibility/Behaviors/Popup/popupAutoFocusBehavior.ts
This file contains 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,18 @@ | ||
import { Accessibility } from '../../types' | ||
import popupBehavior from './popupBehavior' | ||
|
||
/** | ||
* @description | ||
* Adds role='button' to 'trigger' component's part, if it is not focusable element and no role attribute provided. | ||
* Adds tabIndex='0' to 'trigger' component's part, if it is not tabbable element and no tabIndex attribute provided. | ||
* | ||
* @specification | ||
* Adds attribute 'aria-disabled=true' to 'trigger' component's part if 'disabled' property is true. Does not set the attribute otherwise. | ||
* Automatically focus the first focusable element inside component. | ||
*/ | ||
const popupAutoFocusBehavior: Accessibility = (props: any) => ({ | ||
...popupBehavior(props), | ||
autoFocus: true, | ||
}) | ||
|
||
export default popupAutoFocusBehavior |
63 changes: 63 additions & 0 deletions
63
packages/react/src/lib/accessibility/FocusZone/AutoFocusZone.tsx
This file contains 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,63 @@ | ||
import * as React from 'react' | ||
import * as PropTypes from 'prop-types' | ||
import * as _ from 'lodash' | ||
|
||
import { getNextElement, focusAsync } from './focusUtilities' | ||
|
||
import { AutoFocusZoneProps } from './AutoFocusZone.types' | ||
import getUnhandledProps from '../../getUnhandledProps' | ||
import getElementType from '../../getElementType' | ||
import * as customPropTypes from '../../customPropTypes' | ||
import callable from '../../callable' | ||
import Ref from '../../../components/Ref/Ref' | ||
|
||
/** AutoFocusZone is used to focus inner element on mount. */ | ||
export class AutoFocusZone extends React.Component<AutoFocusZoneProps> { | ||
private root = React.createRef<HTMLElement>() | ||
|
||
static propTypes = { | ||
as: customPropTypes.as, | ||
firstFocusableSelector: PropTypes.oneOfType([PropTypes.func, PropTypes.string]), | ||
} | ||
|
||
static handledProps = _.keys(AutoFocusZone.propTypes) | ||
|
||
public componentDidMount(): void { | ||
this.findElementAndFocusAsync() | ||
} | ||
|
||
public render(): JSX.Element { | ||
const unhandledProps = getUnhandledProps( | ||
{ handledProps: AutoFocusZone.handledProps }, | ||
this.props, | ||
) | ||
|
||
const ElementType = getElementType({}, this.props) as React.ComponentClass<AutoFocusZoneProps> | ||
|
||
return ( | ||
<Ref innerRef={this.root}> | ||
<ElementType {...unhandledProps}>{this.props.children}</ElementType> | ||
</Ref> | ||
) | ||
} | ||
|
||
private findElementAndFocusAsync = () => { | ||
if (!this.root.current) return | ||
const { firstFocusableSelector } = this.props | ||
|
||
const focusSelector = callable(firstFocusableSelector)() | ||
|
||
const firstFocusableChild = focusSelector | ||
? (this.root.current.querySelector(`.${focusSelector}`) as HTMLElement) | ||
: getNextElement( | ||
this.root.current, | ||
this.root.current.firstChild as HTMLElement, | ||
true, | ||
false, | ||
false, | ||
true, | ||
) | ||
|
||
firstFocusableChild && focusAsync(firstFocusableChild) | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
packages/react/src/lib/accessibility/FocusZone/AutoFocusZone.types.tsx
This file contains 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,13 @@ | ||
import * as React from 'react' | ||
|
||
export interface AutoFocusZoneProps extends React.HTMLAttributes<HTMLDivElement> { | ||
/** | ||
* Element type the root element will use. Default is "div". | ||
*/ | ||
as?: React.ReactType | ||
|
||
/** | ||
* Indicates the selector for first focusable item. | ||
*/ | ||
firstFocusableSelector?: string | (() => string) | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,112 @@ | ||
import * as React from 'react' | ||
import * as ReactTestUtils from 'react-dom/test-utils' | ||
|
||
import { FocusZone, AutoFocusZone } from '../../../src/lib/accessibility/FocusZone' | ||
|
||
// rAF does not exist in node - let's mock it | ||
window.requestAnimationFrame = (callback: FrameRequestCallback) => { | ||
const r = window.setTimeout(callback, 0) | ||
jest.runAllTimers() | ||
return r | ||
} | ||
|
||
const animationFrame = () => new Promise(resolve => window.requestAnimationFrame(resolve)) | ||
jest.useFakeTimers() | ||
|
||
describe('AutoFocusZone', () => { | ||
let lastFocusedElement: HTMLElement | undefined | ||
|
||
const _onFocus = (ev: any): void => (lastFocusedElement = ev.target) | ||
|
||
const 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 | ||
}) | ||
|
||
describe('Focusing the ATZ', () => { | ||
function setupTest(firstFocusableSelector?: string) { | ||
let autoFocusZoneRef: AutoFocusZone | null = null | ||
const topLevelDiv = ReactTestUtils.renderIntoDocument( | ||
<div onFocusCapture={_onFocus}> | ||
<AutoFocusZone | ||
data-is-focusable={true} | ||
firstFocusableSelector={firstFocusableSelector} | ||
ref={ftz => { | ||
autoFocusZoneRef = ftz | ||
}} | ||
> | ||
<button className={'f'}>f</button> | ||
<FocusZone> | ||
<button className={'a'}>a</button> | ||
<button className={'b'}>b</button> | ||
</FocusZone> | ||
</AutoFocusZone> | ||
<button className={'z'}>z</button> | ||
</div>, | ||
) as HTMLElement | ||
|
||
const buttonF = topLevelDiv.querySelector('.f') as HTMLElement | ||
const buttonA = topLevelDiv.querySelector('.a') as HTMLElement | ||
const buttonB = topLevelDiv.querySelector('.b') as HTMLElement | ||
const buttonZ = topLevelDiv.querySelector('.z') as HTMLElement | ||
|
||
// Assign bounding locations to buttons. | ||
setupElement(buttonF, { clientRect: { top: 0, bottom: 10, left: 0, right: 10 } }) | ||
setupElement(buttonA, { clientRect: { top: 10, bottom: 20, left: 0, right: 10 } }) | ||
setupElement(buttonB, { clientRect: { top: 20, bottom: 30, left: 0, right: 10 } }) | ||
setupElement(buttonZ, { clientRect: { top: 30, bottom: 40, left: 0, right: 10 } }) | ||
|
||
return { autoFocusZone: autoFocusZoneRef, buttonF, buttonA, buttonB, buttonZ } | ||
} | ||
|
||
it('goes to first focusable element when focusing the ATZ', async () => { | ||
expect.assertions(1) | ||
|
||
const { autoFocusZone, buttonF } = setupTest() | ||
|
||
// By calling `componentDidMount`, AFZ will behave as just initialized and focus needed element | ||
// Focus within should go to 1st focusable inner element. | ||
autoFocusZone.componentDidMount() | ||
await animationFrame() | ||
expect(lastFocusedElement).toBe(buttonF) | ||
}) | ||
|
||
it('goes to the element with containing the firstFocusableSelector if provided when focusing the ATZ', async () => { | ||
expect.assertions(1) | ||
const { autoFocusZone, buttonB } = setupTest('b') | ||
|
||
// By calling `componentDidMount`, AFZ will behave as just initialized and focus needed element | ||
// Focus within should go to the element containing the selector. | ||
autoFocusZone.componentDidMount() | ||
await animationFrame() | ||
expect(lastFocusedElement).toBe(buttonB) | ||
}) | ||
}) | ||
}) |
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.
Can we use enzyme for tests?
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.
I was copying and trying to keep these tests consistent with the other focus zone tests.. If we decide later to move all of them to enzyme we will do it for all of them.