Skip to content

Commit

Permalink
Fix the focus not returning to the button that caused the MovieEditor…
Browse files Browse the repository at this point in the history
… modal to open after its been closed
  • Loading branch information
Erin Doyle committed Sep 2, 2018
1 parent a855e2e commit e82845e
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/getActiveElement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function getActiveElement() {
return typeof document !== 'undefined' && document.activeElement;
}
1 change: 1 addition & 0 deletions src/primitives/MovieToolbarButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const MovieToolbarButton = ({

return (
<button
id={`${buttonText}-${movieTitle}-btn`}
className="btn btn-secondary"
aria-label={ariaLabel}
onClick={clickHandler}
Expand Down
4 changes: 2 additions & 2 deletions src/wishlist/MovieEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class MovieEditor extends Component {
labelledby: "modal-title"
}}
shouldFocusAfterRender={true}
shouldCloseOnOverlayClick={true}
shouldReturnFocusAfterClose={false} // override default behavior which returns focus to wrong element
>
<div className="modal-dialog modal-dialog-centered">
<div className="modal-content">
Expand All @@ -66,7 +66,7 @@ class MovieEditor extends Component {
<div className="modal-body">
<div className="form-group">
<label htmlFor="notes">Notes:
<textarea id="notes" className="form-control" value={notes} onChange={this.handleChangeNotes} />
<textarea id="notes" className="form-control" value={notes} onBlur={this.handleChangeNotes} />
</label>
</div>
</div>
Expand Down
19 changes: 16 additions & 3 deletions src/wishlist/MovieWishlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class MovieWishlist extends Component {

this.state = {
showEditor: false,
movieIdInEdit: null
movieIdInEdit: null,
onEditorClose: null
};

this.addSomeMoviesLink = null;
Expand All @@ -33,14 +34,26 @@ class MovieWishlist extends Component {
}
}

componentDidUpdate(prevProps, prevState) {
const { showEditor, onEditorClose } = this.state;

// If the Movie Editor was just closed set the focus back to the Edit button that opened it
if (prevState.showEditor === true && showEditor === false) {
if (onEditorClose) {
onEditorClose();
}
}
}

setAddSomeMoviesLinkRef(element) {
this.addSomeMoviesLink = element;
}

handleShowEditor(movieId) {
handleShowEditor(movieId, editorCloseCallback) {
this.setState({
showEditor: true,
movieIdInEdit: movieId
movieIdInEdit: movieId,
onEditorClose: editorCloseCallback
});
}

Expand Down
14 changes: 13 additions & 1 deletion src/wishlist/getWishlistActions.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
import React from 'react';

import MovieToolbar from '../primitives/MovieToolbar';
import getActiveElement from '../getActiveElement';


const getMovieActions = (showEditor, setAsWatched, setAsUnwatched, handleRemove) =>
(movieId, movieTitle, watched) => {
const watchButtonText = watched ? 'Unwatch' : 'Watched';
const watchClickHandler = () => watched ? setAsUnwatched(movieId) : setAsWatched(movieId);
const editClickHandler = () => showEditor(movieId);
const editClickHandler = () => {
const currentActiveElement = getActiveElement();

const onEditorClose = () => {
if (currentActiveElement) {
currentActiveElement.focus();
}
};

showEditor(movieId, onEditorClose);
};
const removeClickHandler = () => handleRemove(movieId);

const movieButtonList = [
Expand Down

0 comments on commit e82845e

Please sign in to comment.