From 76193e4c6f00f9237f5aabcd33ebd7fee16030c8 Mon Sep 17 00:00:00 2001 From: Nicola Heald Date: Tue, 26 Jun 2018 11:31:02 +0100 Subject: [PATCH 1/3] Dispatch input event on insertCompletion --- components/autocomplete/index.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/components/autocomplete/index.js b/components/autocomplete/index.js index 85e07f4e8d966..b208b2de41a5b 100644 --- a/components/autocomplete/index.js +++ b/components/autocomplete/index.js @@ -243,6 +243,16 @@ export class Autocomplete extends Component { range.setStartAfter( child ); } range.deleteContents(); + + let inputEvent; + if ( undefined !== window.InputEvent ) { + inputEvent = new window.InputEvent( 'input', { bubbles: true, cancelable: false } ); + } else { + // IE11 doesn't provide an InputEvent constructor. + inputEvent = document.createEvent( 'UIEvent' ); + inputEvent.initEvent( 'input', true /* bubbles */, false /* cancelable */ ); + } + range.commonAncestorContainer.closest( '[contenteditable=true]' ).dispatchEvent( inputEvent ); } select( option ) { From fd62aed78eb81b9069be2e3c251ef989d86fda55 Mon Sep 17 00:00:00 2001 From: Nicola Heald Date: Wed, 27 Jun 2018 12:32:11 +0100 Subject: [PATCH 2/3] Import closest polyfill --- components/autocomplete/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/components/autocomplete/index.js b/components/autocomplete/index.js index b208b2de41a5b..6b09b87cabcb2 100644 --- a/components/autocomplete/index.js +++ b/components/autocomplete/index.js @@ -3,6 +3,7 @@ */ import classnames from 'classnames'; import { escapeRegExp, find, filter, map, debounce } from 'lodash'; +import 'element-closest'; /** * WordPress dependencies From 86e6638657d441242dcff42c46d8f68d195ddb0e Mon Sep 17 00:00:00 2001 From: Brandon Payton Date: Tue, 3 Jul 2018 17:46:16 -0700 Subject: [PATCH 3/3] Avoid redisplaying menu due to completion events --- components/autocomplete/index.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/components/autocomplete/index.js b/components/autocomplete/index.js index 6b09b87cabcb2..ee3014683bd2e 100644 --- a/components/autocomplete/index.js +++ b/components/autocomplete/index.js @@ -265,8 +265,6 @@ export class Autocomplete extends Component { return; } - this.reset(); - if ( getOptionCompletion ) { const completion = getOptionCompletion( option.value, range, query ); @@ -289,10 +287,20 @@ export class Autocomplete extends Component { } } } + + // Reset autocomplete state after insertion rather than before + // so insertion events don't cause the completion menu to redisplay. + this.reset(); } reset() { - this.setState( this.constructor.getInitialState() ); + const isMounted = !! this.node; + + // Autocompletions may replace the block containing this component, + // so we make sure it is mounted before resetting the state. + if ( isMounted ) { + this.setState( this.constructor.getInitialState() ); + } } resetWhenSuppressed() {