Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
### Fixes
- Require `name` prop in `Icon` component @lucivpav ([#1723](https://github.com/stardust-ui/react/pull/1723))
- Export `broadcast` icon in Teams theme @miroslavstastny ([#1737](https://github.com/stardust-ui/react/pull/1737))
- `FocusZone` should respect elements with `contenteditable` attribute on Home/End key press @sophieH29 ([#1749](https://github.com/stardust-ui/react/pull/1749))

### Features
- Expose `isFromKeyboard` in `Grid` component @sophieH29 ([#1729](https://github.com/stardust-ui/react/pull/1729))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ This is a list of changes made to this Stardust copy of FocusZone in comparison
- Fix `FocusZone` - add `shouldResetActiveElementWhenTabFromZone` prop @sophieH29 ([#614](https://github.com/stardust-ui/react/pull/614))
- Make `FocusZoneTabbableElements` a usual enum @layershifter ([#867](https://github.com/stardust-ui/react/pull/867))
- Update tabindexes and focus alignment when item is focused programatically @sophieH29 ([#1098](https://github.com/stardust-ui/react/pull/1098))
- `FocusZone` should respect elements with `contenteditable` attribute on Home/End key press @sophieH29 ([#1749](https://github.com/stardust-ui/react/pull/1749))

### Features
- Add embed mode for FocusZone and new Chat behavior ([#233](https://github.com/stardust-ui/react/pull/233))
Expand Down
14 changes: 10 additions & 4 deletions packages/react/src/lib/accessibility/FocusZone/FocusZone.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -501,8 +501,9 @@ export default class FocusZone extends React.Component<FocusZoneProps> implement

case keyboardKey.Home:
if (
this.isElementInput(ev.target as HTMLElement) &&
!this.shouldInputLoseFocus(ev.target as HTMLInputElement, false)
this.isContentEditableElement(ev.target as HTMLElement) ||
(this.isElementInput(ev.target as HTMLElement) &&
!this.shouldInputLoseFocus(ev.target as HTMLInputElement, false))
) {
return false
}
Expand All @@ -519,8 +520,9 @@ export default class FocusZone extends React.Component<FocusZoneProps> implement

case keyboardKey.End:
if (
this.isElementInput(ev.target as HTMLElement) &&
!this.shouldInputLoseFocus(ev.target as HTMLInputElement, true)
this.isContentEditableElement(ev.target as HTMLElement) ||
(this.isElementInput(ev.target as HTMLElement) &&
!this.shouldInputLoseFocus(ev.target as HTMLInputElement, false))
) {
return false
}
Expand Down Expand Up @@ -957,6 +959,10 @@ export default class FocusZone extends React.Component<FocusZoneProps> implement
}
}

isContentEditableElement(element: HTMLElement): boolean {
return element && element.getAttribute('contenteditable') === 'true'
}

isElementInput(element: HTMLElement): boolean {
if (
element &&
Expand Down
47 changes: 47 additions & 0 deletions packages/react/test/specs/lib/FocusZone-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1420,4 +1420,51 @@ describe('FocusZone', () => {
expect(buttonB.tabIndex).toBe(0)
expect(buttonA.tabIndex).toBe(-1)
})

it('remains focus in element with "contenteditable=true" attribute on Home/End keys', () => {
const component = ReactTestUtils.renderIntoDocument<{}, React.Component>(
<div {...{ onFocusCapture: onFocus }}>
<FocusZone>
<div contentEditable={true} id="a" />
<button id="b">b</button>
</FocusZone>
</div>,
)

const focusZone = ReactDOM.findDOMNode(component)!.firstChild as Element

const contentEditableA = focusZone.querySelector('#a') as HTMLElement
const buttonB = focusZone.querySelector('#b') as HTMLElement

setupElement(contentEditableA, {
clientRect: {
top: 0,
bottom: 20,
left: 20,
right: 40,
},
})

setupElement(buttonB, {
clientRect: {
top: 0,
bottom: 20,
left: 20,
right: 40,
},
})

// contentEditableA should be focused.
contentEditableA.focus()
expect(lastFocusedElement).toBe(contentEditableA)

ReactTestUtils.Simulate.keyDown(contentEditableA, { which: keyboardKey.Home })
expect(lastFocusedElement).toBe(contentEditableA)
ReactTestUtils.Simulate.keyDown(contentEditableA, { which: keyboardKey.End })
expect(lastFocusedElement).toBe(contentEditableA)

// change focus to buttonB
buttonB.focus()
expect(lastFocusedElement).toBe(buttonB)
})
})