Skip to content

Commit

Permalink
Merge pull request #949 from open-sausages/pulls/1/escape-back-to-target
Browse files Browse the repository at this point in the history
BUG When using the esc key to exist a popover, refocus to the target element
  • Loading branch information
robbieaverill authored Aug 28, 2019
2 parents 430f27c + 10c10d5 commit 657cd1e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
2 changes: 1 addition & 1 deletion client/dist/js/bundle.js

Large diffs are not rendered by default.

33 changes: 30 additions & 3 deletions client/src/components/PopoverOptionSet/PopoverOptionSet.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Button, Input, InputGroup, InputGroupAddon, Popover } from 'reactstrap';
import { Button, Input, InputGroup, InputGroupAddon, Popover, Util as reactstrapUtil } from 'reactstrap';
import classNames from 'classnames';
import i18n from 'i18n';

Expand All @@ -16,20 +16,47 @@ class PopoverOptionSet extends Component {
this.handleSearchValueClear = this.handleSearchValueClear.bind(this);
this.handleSearchValueChange = this.handleSearchValueChange.bind(this);
this.handleKeyDown = this.handleKeyDown.bind(this);
this.doToggle = this.doToggle.bind(this);
this.focusOnTarget = this.focusOnTarget.bind(this);

this.state = {
searchValue: ''
};
}

/**
* Pass toggle to parent (props requires a toggle function) and clear the search input
* Handle the toggle from the underlying react strap component.
*/
handleToggle() {
this.doToggle(false);
}

/**
* Pass toggle to parent (props requires a toggle function) and clear the search input
* @param {bool} focusOnTarget Whether we should give the focus back to the popover target.
*/
doToggle(focusOnTarget) {
const { toggle } = this.props;

toggle();
this.handleSearchValueClear();

if (focusOnTarget) {
this.focusOnTarget();
}
}

/**
* Move the focus back to the popover target
*/
focusOnTarget() {
const { target } = this.props;
if (target) {
const el = reactstrapUtil.getTarget(target);
if (el) {
el.focus();
}
}
}

/**
Expand Down Expand Up @@ -58,7 +85,7 @@ class PopoverOptionSet extends Component {
*/
handleKeyDown(event) {
if (event.key === 'Escape') {
this.handleToggle();
this.doToggle(true);
}
}

Expand Down

0 comments on commit 657cd1e

Please sign in to comment.