Skip to content

Improve focus managmenet #63

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions src/components/__tests__/__snapshots__/checker.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ exports[`render matches snapshot with errors 1`] = `
<Tray
border={false}
contentRef={[Function]}
data-mce-component={true}
defaultFocusElement={null}
insertAt="bottom"
label="Accessibility Checker"
Expand Down Expand Up @@ -452,6 +453,7 @@ exports[`render matches snapshot without errors 1`] = `
<Tray
border={false}
contentRef={[Function]}
data-mce-component={true}
defaultFocusElement={null}
insertAt="bottom"
label="Accessibility Checker"
Expand Down
37 changes: 34 additions & 3 deletions src/components/__tests__/checker.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react"
import Checker from "../checker"
import { shallow } from "enzyme"
import { shallow, mount } from "enzyme"
import util from "util"

const promisify = util.promisify
Expand Down Expand Up @@ -32,7 +32,9 @@ beforeEach(() => {
fakeEditor = {
getContainer: () => ({
querySelector: () => fakeIframe
})
}),
on: jest.fn(),
focus: jest.fn()
}
child = node.appendChild(document.createElement("div"))
child2 = node.appendChild(document.createElement("div"))
Expand Down Expand Up @@ -166,7 +168,7 @@ describe("check", () => {
})

test("does nothing if props.getBody() returns falsy", () => {
component = shallow(<Checker getBody={() => false} />)
component = shallow(<Checker getBody={() => false} editor={fakeEditor} />)
instance = component.instance()
const spy = jest.fn()
instance.check(spy)
Expand All @@ -184,6 +186,35 @@ describe("check", () => {
}).not.toThrow()
})
})

describe("close", () => {
beforeEach(() => jest.useFakeTimers())
afterEach(() => {
jest.useRealTimers()
jest.restoreAllMocks()
component.unmount()
})

it("calls editor.on('Remove') when mounted", () => {
component = mount(<Checker getBody={() => node} editor={fakeEditor} />)
instance = component.instance()
instance.check() // open it
jest.runAllTimers()
expect(fakeEditor.on).toHaveBeenCalled()
expect(fakeEditor.on.mock.calls[0][0]).toEqual("Remove")
})

it("calls focus on the editor on closing the tray", () => {
component = mount(<Checker getBody={() => node} editor={fakeEditor} />)
instance = component.instance()
instance.check() // opens it
jest.runAllTimers()
const closeButton = document.querySelector("[data-mce-component] button")
closeButton.click()
jest.runAllTimers()
expect(instance.props.editor.focus).toHaveBeenCalled()
})
})
})

describe("setErrorIndex", () => {
Expand Down
12 changes: 12 additions & 0 deletions src/components/checker.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ export default class Checker extends React.Component {
additionalRules: []
}

componentDidMount() {
this.props.editor.on("Remove", editor => {
this.setState({ open: false })
})
}

setConfig(config) {
this.setState({ config })
}
Expand Down Expand Up @@ -285,6 +291,10 @@ export default class Checker extends React.Component {
this.setState({ open: false })
}

handleExited() {
this.props.editor.focus(false)
}

render() {
const rule = this.errorRule()
const issueNumberMessage = formatMessage("Issue { num }/{ total }", {
Expand All @@ -295,9 +305,11 @@ export default class Checker extends React.Component {
return (
<LiveAnnouncer>
<Tray
data-mce-component
label={formatMessage("Accessibility Checker")}
open={this.state.open}
onDismiss={() => this.handleClose()}
onExited={() => this.handleExited()}
placement="end"
contentRef={e => (this.trayElement = e)}
>
Expand Down