Skip to content
Closed
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
92 changes: 53 additions & 39 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, debounce } from 'lodash';
import { escapeRegExp, find, filter, includes, map, debounce } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -15,6 +15,7 @@ import { __, _n, sprintf } from '@wordpress/i18n';
* Internal dependencies
*/
import './style.scss';
import BackCompatibleOnInput from '../back-compatible-on-input';
import { isDeprecatedCompleter, toCompatibleCompleter } from './completer-compat';
import withFocusOutside from '../higher-order/with-focus-outside';
import Button from '../button';
Expand Down Expand Up @@ -318,8 +319,14 @@ export class Autocomplete extends Component {
getCursor( container ) {
const selection = window.getSelection();
if ( selection.isCollapsed ) {
// Internet Explorer only supports contains() for elements.
// We are simulating and identical condition using textContent and a string includes,
// So we don't throw errors when we should not on IE.
if ( 'production' !== process.env.NODE_ENV ) {
if ( ! container.contains( selection.anchorNode ) ) {
if ( ! container.contains( selection.anchorNode ) &&
selection.anchorNode &&
! container.textContent.includes( selection.anchorNode.textContent.trim() )
) {
throw new Error( 'Invalid assumption: expected selection to be within the autocomplete container' );
}
}
Expand Down Expand Up @@ -466,6 +473,12 @@ export class Autocomplete extends Component {
}

search( event ) {
// if the event contains a key code because of compatibility with browser that don't support onInput,
// ignore keys that we are already catching to manage the autocomplete.
if ( event.keyCode && includes( [ ENTER, ESCAPE, UP, DOWN, LEFT, RIGHT, SPACE ], event.keyCode ) ) {
return;
}

const { completers, open: wasOpen, suppress: wasSuppress, query: wasQuery } = this.state;
const container = event.target;

Expand Down Expand Up @@ -614,44 +627,45 @@ export class Autocomplete extends Component {
// Disable reason: Clicking the editor should reset the autocomplete when the menu is suppressed
/* eslint-disable jsx-a11y/no-static-element-interactions, jsx-a11y/onclick-has-role, jsx-a11y/click-events-have-key-events */
return (
<div
ref={ this.bindNode }
onInput={ this.search }
onClick={ this.resetWhenSuppressed }
className="components-autocomplete"
>
{ children( { isExpanded, listBoxId, activeId } ) }
{ isExpanded && (
<Popover
focusOnMount={ false }
onClose={ this.reset }
position="top right"
className="components-autocomplete__popover"
getAnchorRect={ this.getWordRect }
>
<div
id={ listBoxId }
role="listbox"
className="components-autocomplete__results"
<BackCompatibleOnInput onInput={ this.search } >
<div
className="components-autocomplete"
onClick={ this.resetWhenSuppressed }
ref={ this.bindNode }
>
{ children( { isExpanded, listBoxId, activeId } ) }
{ isExpanded && (
<Popover
focusOnMount={ false }
onClose={ this.reset }
position="top right"
className="components-autocomplete__popover"
getAnchorRect={ this.getWordRect }
>
{ isExpanded && map( filteredOptions, ( option, index ) => (
<Button
key={ option.key }
id={ `components-autocomplete-item-${ instanceId }-${ option.key }` }
role="option"
aria-selected={ index === selectedIndex }
className={ classnames( 'components-autocomplete__result', className, {
'is-selected': index === selectedIndex,
} ) }
onClick={ () => this.select( option ) }
>
{ option.label }
</Button>
) ) }
</div>
</Popover>
) }
</div>
<div
id={ listBoxId }
role="listbox"
className="components-autocomplete__results"
>
{ isExpanded && map( filteredOptions, ( option, index ) => (
<Button
key={ option.key }
id={ `components-autocomplete-item-${ instanceId }-${ option.key }` }
role="option"
aria-selected={ index === selectedIndex }
className={ classnames( 'components-autocomplete__result', className, {
'is-selected': index === selectedIndex,
} ) }
onClick={ () => this.select( option ) }
>
{ option.label }
</Button>
) ) }
</div>
</Popover>
) }
</div>
</BackCompatibleOnInput>
);
/* eslint-enable jsx-a11y/no-static-element-interactions, jsx-a11y/onclick-has-role, jsx-a11y/click-events-have-key-events */
}
Expand Down
2 changes: 1 addition & 1 deletion components/autocomplete/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ describe( 'Autocomplete', () => {
it( 'renders children', () => {
const wrapper = makeAutocompleter( [] );
expect( wrapper.state().open ).toBeUndefined();
expect( wrapper.childAt( 0 ).hasClass( 'components-autocomplete' ) ).toBe( true );
expect( wrapper.find( '.components-autocomplete' ) ).toHaveLength( 1 );
expect( wrapper.find( '.fake-editor' ) ).toHaveLength( 1 );
} );

Expand Down
45 changes: 45 additions & 0 deletions components/back-compatible-on-input/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';

export default class BackCompatibleOnInput extends Component {
constructor() {
super( ...arguments );

this.onInput = this.onInput.bind( this );
this.onKeyUp = this.onKeyUp.bind( this );

this.state = {
isOnInputSupported: false,
};
}

onInput( event ) {
this.setState( {
onInputSupported: true,
} );
this.props.onInput( event );
}

onKeyUp( event ) {
if ( this.state.isOnInputSupported ) {
return;
}
this.props.onInput( event );
}

render() {
const { isOnInputSupported } = this.state;
const { onInput, children, ...props } = this.props;
return (
<div
onInput={ isOnInputSupported ? onInput : this.onInput }
onKeyUp={ isOnInputSupported ? undefined : this.onKeyUp }
{ ...props }
>
{ children }
</div>
);
}
}
36 changes: 19 additions & 17 deletions core-blocks/button/test/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,26 @@ exports[`core/button block edit matches snapshot 1`] = `
>
<div>
<div>
<div
class="components-autocomplete"
>
<span
aria-autocomplete="list"
aria-expanded="false"
aria-label="Add text…"
aria-multiline="false"
class="wp-block-button__link blocks-rich-text__tinymce"
contenteditable="true"
data-is-placeholder-visible="true"
role="textbox"
/>
<span
class="blocks-rich-text__tinymce wp-block-button__link"
<div>
<div
class="components-autocomplete"
>
Add text…
</span>
<span
aria-autocomplete="list"
aria-expanded="false"
aria-label="Add text…"
aria-multiline="false"
class="wp-block-button__link blocks-rich-text__tinymce"
contenteditable="true"
data-is-placeholder-visible="true"
role="textbox"
/>
<span
class="blocks-rich-text__tinymce wp-block-button__link"
>
Add text…
</span>
</div>
</div>
</div>
</div>
Expand Down
36 changes: 19 additions & 17 deletions core-blocks/heading/test/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,26 @@ exports[`core/heading block edit matches snapshot 1`] = `
>
<div>
<div>
<div
class="components-autocomplete"
>
<h2
aria-autocomplete="list"
aria-expanded="false"
aria-label="Write heading…"
aria-multiline="false"
class="blocks-rich-text__tinymce"
contenteditable="true"
data-is-placeholder-visible="true"
role="textbox"
/>
<h2
class="blocks-rich-text__tinymce"
<div>
<div
class="components-autocomplete"
>
Write heading…
</h2>
<h2
aria-autocomplete="list"
aria-expanded="false"
aria-label="Write heading…"
aria-multiline="false"
class="blocks-rich-text__tinymce"
contenteditable="true"
data-is-placeholder-visible="true"
role="textbox"
/>
<h2
class="blocks-rich-text__tinymce"
>
Write heading…
</h2>
</div>
</div>
</div>
</div>
Expand Down
38 changes: 20 additions & 18 deletions core-blocks/list/test/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,27 @@ exports[`core/list block edit matches snapshot 1`] = `
>
<div>
<div>
<div
class="components-autocomplete"
>
<ul
aria-autocomplete="list"
aria-expanded="false"
aria-label="Write list…"
aria-multiline="true"
class="blocks-rich-text__tinymce"
contenteditable="true"
data-is-placeholder-visible="true"
/>
<ul
class="blocks-rich-text__tinymce"
<div>
<div
class="components-autocomplete"
>
<li>
Write list…
</li>
</ul>
<ul
aria-autocomplete="list"
aria-expanded="false"
aria-label="Write list…"
aria-multiline="true"
class="blocks-rich-text__tinymce"
contenteditable="true"
data-is-placeholder-visible="true"
/>
<ul
class="blocks-rich-text__tinymce"
>
<li>
Write list…
</li>
</ul>
</div>
</div>
</div>
</div>
Expand Down
36 changes: 19 additions & 17 deletions core-blocks/paragraph/test/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,26 @@ exports[`core/paragraph block edit matches snapshot 1`] = `
>
<div>
<div>
<div
class="components-autocomplete"
>
<p
aria-autocomplete="list"
aria-expanded="false"
aria-label="Add text or type / to add content"
aria-multiline="false"
class="wp-block-paragraph blocks-rich-text__tinymce"
contenteditable="true"
data-is-placeholder-visible="true"
role="textbox"
/>
<p
class="blocks-rich-text__tinymce wp-block-paragraph"
<div>
<div
class="components-autocomplete"
>
Add text or type / to add content
</p>
<p
aria-autocomplete="list"
aria-expanded="false"
aria-label="Add text or type / to add content"
aria-multiline="false"
class="wp-block-paragraph blocks-rich-text__tinymce"
contenteditable="true"
data-is-placeholder-visible="true"
role="textbox"
/>
<p
class="blocks-rich-text__tinymce wp-block-paragraph"
>
Add text or type / to add content
</p>
</div>
</div>
</div>
</div>
Expand Down
Loading