Skip to content
This repository has been archived by the owner on Sep 19, 2021. It is now read-only.

don't jump to top when navigating within a page #510

Merged
merged 2 commits into from
Jul 9, 2018
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
19 changes: 17 additions & 2 deletions src/components/Main/App.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react'
import { withRouter } from 'react-router'
import { i18n } from '../../config'
import { SectionTitle, ProgressBar, ScoreCard, Navigation, NavigationToggle } from '..'
import { Introduction, Show } from '../Form'
Expand Down Expand Up @@ -38,6 +39,20 @@ class App extends React.Component {
}
this.showInstructions = this.showInstructions.bind(this)
this.dismissInstructions = this.dismissInstructions.bind(this)

// workaround for not having React.createRef(), introduced in React 16.3
// https://reactjs.org/docs/refs-and-the-dom.html#dont-overuse-refs
this.sectionFocusEl = null
this.setSectionFocusEl = (el) => {
this.sectionFocusEl = el
}
}

componentDidUpdate(prevProps) {
// for keyboard navigation accessbility, focus on the main content area after a new section is navigated to
if (this.props.location.pathname !== prevProps.location.pathname) {
this.sectionFocusEl.focus()
}
}

showInstructions (event) {
Expand Down Expand Up @@ -146,7 +161,7 @@ class App extends React.Component {
<button onClick={this.showInstructions} className="instructions mobile-visible"><span>{i18n.t('app.instructions')}</span></button>
&nbsp;
</div>
<a href="javascript:;;;" className="eapp-section-focus" title="Main content. Please press TAB to go to the next question"></a>
<a href="javascript:;;;" className="eapp-section-focus" title="Main content. Please press TAB to go to the next question" ref={this.setSectionFocusEl}></a>
<div id="main-content" className={klassCore}>
{this.props.children}
&nbsp;
Expand Down Expand Up @@ -179,4 +194,4 @@ function mapStateToProps (state) {

// Wraps the the App component with connect() which adds the dispatch()
// function to the props property for this component
export default connect(mapStateToProps)(App)
export default withRouter(connect(mapStateToProps)(App))
9 changes: 6 additions & 3 deletions src/components/Main/App.test.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react'
import { MemoryRouter } from 'react-router'
import App from './App'
import renderer from 'react-test-renderer'
import configureMockStore from 'redux-mock-store'
Expand All @@ -19,9 +20,11 @@ test('Renders homepage', () => {
})

const component = renderer.create(
<Provider store={store}>
<App/>
</Provider>
<MemoryRouter>
<Provider store={store}>
<App/>
</Provider>
</MemoryRouter>
)

let tree = component.toJSON()
Expand Down
10 changes: 0 additions & 10 deletions src/components/Section/Section.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,6 @@ class Section extends React.Component {
this.update(this.props)
}

componentDidUpdate () {
// Once a section updates then attempt to focus on the first form element
const el = window.document.querySelector('.eapp-section-focus')
if (el) {
window.setTimeout(() => {
el.focus()
}, 200)
}
}

update (props) {
const subsection = props.subsection || 'intro'
const path = `/form/${props.section}/${subsection}`
Expand Down