Skip to content

Commit

Permalink
Components: Use withFocusOutside utility for Autocomplete dismiss
Browse files Browse the repository at this point in the history
As implemented, Popover portaled content will still fire focus onto root Autocomplete node, so blur trigger will cancel.

See: https://reactjs.org/docs/portals.html#event-bubbling-through-portals
  • Loading branch information
aduth committed Nov 7, 2017
1 parent 092becd commit d652ed8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
16 changes: 11 additions & 5 deletions components/autocomplete/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import classnames from 'classnames';
import { escapeRegExp, find, filter, map } from 'lodash';
import { escapeRegExp, find, filter, map, flowRight } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -14,11 +14,12 @@ import { keycodes } from '@wordpress/utils';
* Internal dependencies
*/
import './style.scss';
import withFocusOutside from '../higher-order/with-focus-outside';
import Button from '../button';
import Popover from '../popover';
import withInstanceId from '../higher-order/with-instance-id';

const { ENTER, ESCAPE, UP, DOWN, LEFT, RIGHT, TAB } = keycodes;
const { ENTER, ESCAPE, UP, DOWN, LEFT, RIGHT } = keycodes;

/**
* Recursively select the firstChild until hitting a leaf node.
Expand Down Expand Up @@ -136,6 +137,10 @@ export class Autocomplete extends Component {
this.setState( this.constructor.getInitialState() );
}

handleFocusOutside() {
this.reset();
}

// this method is separate so it can be overrided in tests
getCursor( container ) {
const selection = window.getSelection();
Expand Down Expand Up @@ -333,7 +338,6 @@ export class Autocomplete extends Component {

case LEFT:
case RIGHT:
case TAB:
this.reset();
return;

Expand Down Expand Up @@ -407,7 +411,6 @@ export class Autocomplete extends Component {
<Popover
isOpen={ isExpanded }
focusOnOpen={ false }
onClose={ this.reset }
position="top right"
className={ classes }
getAnchorRect={ this.getWordRect }
Expand Down Expand Up @@ -439,4 +442,7 @@ export class Autocomplete extends Component {
}
}

export default withInstanceId( Autocomplete );
export default flowRight( [
withInstanceId,
withFocusOutside,
] )( Autocomplete );
22 changes: 18 additions & 4 deletions components/autocomplete/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import { keycodes } from '@wordpress/utils';
/**
* Internal dependencies
*/
import { Autocomplete } from '../';
import EnhancedAutocomplete, { Autocomplete } from '../';

const { ENTER, ESCAPE, UP, DOWN, SPACE } = keycodes;

jest.useFakeTimers();

class FakeEditor extends Component {
// we want to change the editor contents manually so don't let react update it
shouldComponentUpdate() {
Expand All @@ -35,9 +37,9 @@ class FakeEditor extends Component {
}
}

function makeAutocompleter( completers ) {
function makeAutocompleter( completers, AutocompleteComponent = Autocomplete ) {
return mount(
<Autocomplete instanceId="1" completers={ completers }>{
<AutocompleteComponent instanceId="1" completers={ completers }>{
( { isExpanded, listBoxId, activeId } ) => (
<FakeEditor
aria-autocomplete="list"
Expand All @@ -46,7 +48,7 @@ function makeAutocompleter( completers ) {
aria-activedescendant={ activeId }
/>
)
}</Autocomplete>
}</AutocompleteComponent>
);
}

Expand Down Expand Up @@ -420,6 +422,18 @@ describe( 'Autocomplete', () => {
} );
} );

it( 'closes by blur', () => {
jest.spyOn( Autocomplete.prototype, 'handleFocusOutside' );

const wrapper = makeAutocompleter( [], EnhancedAutocomplete );
simulateInput( wrapper, [ par( tx( '/' ) ) ] );
wrapper.find( '.fake-editor' ).simulate( 'blur' );

jest.runAllTimers();

expect( Autocomplete.prototype.handleFocusOutside ).toHaveBeenCalled();
} );

it( 'selects by enter', ( done ) => {
const onSelect = jest.fn();
const wrapper = makeAutocompleter( [ { ...slashCompleter, onSelect } ] );
Expand Down

0 comments on commit d652ed8

Please sign in to comment.