2019-08-21 06:41:42 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { Button } from '@wordpress/components';
|
|
|
|
import classnames from 'classnames';
|
|
|
|
import { Component, createRef } from '@wordpress/element';
|
|
|
|
import { isEqual } from 'lodash';
|
2020-01-06 15:15:40 +00:00
|
|
|
import { ENTER, ESCAPE, UP, DOWN, LEFT, RIGHT, TAB } from '@wordpress/keycodes';
|
2019-08-21 06:41:42 +00:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A list box that displays filtered options after search.
|
|
|
|
*/
|
|
|
|
class List extends Component {
|
|
|
|
constructor() {
|
|
|
|
super( ...arguments );
|
|
|
|
|
|
|
|
this.handleKeyDown = this.handleKeyDown.bind( this );
|
|
|
|
this.select = this.select.bind( this );
|
|
|
|
this.optionRefs = {};
|
|
|
|
this.listbox = createRef();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate( prevProps ) {
|
2019-10-07 22:42:32 +00:00
|
|
|
const { options, selectedIndex } = this.props;
|
2019-08-21 06:41:42 +00:00
|
|
|
|
|
|
|
// Remove old option refs to avoid memory leaks.
|
2019-10-07 22:42:32 +00:00
|
|
|
if ( ! isEqual( options, prevProps.options ) ) {
|
2019-08-21 06:41:42 +00:00
|
|
|
this.optionRefs = {};
|
|
|
|
}
|
2019-10-07 22:42:32 +00:00
|
|
|
|
|
|
|
if ( selectedIndex !== prevProps.selectedIndex ) {
|
|
|
|
this.scrollToOption( selectedIndex );
|
|
|
|
}
|
2019-08-21 06:41:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getOptionRef( index ) {
|
|
|
|
if ( ! this.optionRefs.hasOwnProperty( index ) ) {
|
|
|
|
this.optionRefs[ index ] = createRef();
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.optionRefs[ index ];
|
|
|
|
}
|
|
|
|
|
|
|
|
select( option ) {
|
|
|
|
const { onSelect } = this.props;
|
|
|
|
|
|
|
|
if ( option.isDisabled ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
onSelect( option );
|
|
|
|
}
|
|
|
|
|
|
|
|
scrollToOption( index ) {
|
|
|
|
const listbox = this.listbox.current;
|
|
|
|
|
2019-10-07 22:42:32 +00:00
|
|
|
if ( listbox.scrollHeight <= listbox.clientHeight ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! this.optionRefs[ index ] ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const option = this.optionRefs[ index ].current;
|
|
|
|
const scrollBottom = listbox.clientHeight + listbox.scrollTop;
|
|
|
|
const elementBottom = option.offsetTop + option.offsetHeight;
|
|
|
|
if ( elementBottom > scrollBottom ) {
|
|
|
|
listbox.scrollTop = elementBottom - listbox.clientHeight;
|
|
|
|
} else if ( option.offsetTop < listbox.scrollTop ) {
|
|
|
|
listbox.scrollTop = option.offsetTop;
|
2019-08-21 06:41:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleKeyDown( event ) {
|
2019-10-07 22:42:32 +00:00
|
|
|
const {
|
|
|
|
decrementSelectedIndex,
|
|
|
|
incrementSelectedIndex,
|
|
|
|
options,
|
|
|
|
onSearch,
|
|
|
|
selectedIndex,
|
|
|
|
setExpanded,
|
|
|
|
} = this.props;
|
|
|
|
if ( options.length === 0 ) {
|
2019-08-21 06:41:42 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ( event.keyCode ) {
|
|
|
|
case UP:
|
2019-10-07 22:42:32 +00:00
|
|
|
decrementSelectedIndex();
|
2019-08-21 06:41:42 +00:00
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DOWN:
|
2019-10-07 22:42:32 +00:00
|
|
|
incrementSelectedIndex();
|
2019-08-21 06:41:42 +00:00
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ENTER:
|
2019-10-07 22:42:32 +00:00
|
|
|
this.select( options[ selectedIndex ] );
|
2019-08-21 06:41:42 +00:00
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LEFT:
|
|
|
|
case RIGHT:
|
2019-10-07 22:42:32 +00:00
|
|
|
setExpanded( false );
|
2019-08-21 06:41:42 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ESCAPE:
|
2019-10-07 22:42:32 +00:00
|
|
|
setExpanded( false );
|
2019-08-21 06:41:42 +00:00
|
|
|
onSearch( null );
|
|
|
|
return;
|
|
|
|
|
2020-01-06 15:15:40 +00:00
|
|
|
case TAB:
|
|
|
|
this.select( options[ selectedIndex ] );
|
|
|
|
setExpanded( false );
|
|
|
|
onSearch( null );
|
|
|
|
return;
|
|
|
|
|
2019-08-21 06:41:42 +00:00
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
toggleKeyEvents( isListening ) {
|
|
|
|
const { node } = this.props;
|
|
|
|
// This exists because we must capture ENTER key presses before RichText.
|
|
|
|
// It seems that react fires the simulated capturing events after the
|
|
|
|
// native browser event has already bubbled so we can't stopPropagation
|
|
|
|
// and avoid RichText getting the event from TinyMCE, hence we must
|
|
|
|
// register a native event handler.
|
|
|
|
const handler = isListening ? 'addEventListener' : 'removeEventListener';
|
|
|
|
node[ handler ]( 'keydown', this.handleKeyDown, true );
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.toggleKeyEvents( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.toggleKeyEvents( false );
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2019-10-07 22:42:32 +00:00
|
|
|
const { instanceId, listboxId, options, selectedIndex, staticList } = this.props;
|
|
|
|
const listboxClasses = classnames( 'woocommerce-select-control__listbox', {
|
2019-08-21 06:41:42 +00:00
|
|
|
'is-static': staticList,
|
|
|
|
} );
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div ref={ this.listbox } id={ listboxId } role="listbox" className={ listboxClasses }>
|
2019-10-07 22:42:32 +00:00
|
|
|
{ options.map( ( option, index ) => (
|
|
|
|
<Button
|
|
|
|
ref={ this.getOptionRef( index ) }
|
|
|
|
key={ option.key }
|
|
|
|
id={ `woocommerce-select-control__option-${ instanceId }-${ option.key }` }
|
|
|
|
role="option"
|
|
|
|
aria-selected={ index === selectedIndex }
|
|
|
|
disabled={ option.isDisabled }
|
|
|
|
className={ classnames( 'woocommerce-select-control__option', {
|
|
|
|
'is-selected': index === selectedIndex,
|
|
|
|
} ) }
|
|
|
|
onClick={ () => this.select( option ) }
|
|
|
|
tabIndex="-1"
|
|
|
|
>
|
|
|
|
{ option.label }
|
|
|
|
</Button>
|
|
|
|
) ) }
|
2019-08-21 06:41:42 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
List.propTypes = {
|
|
|
|
/**
|
2019-10-07 22:42:32 +00:00
|
|
|
* ID of the main SelectControl instance.
|
2019-08-21 06:41:42 +00:00
|
|
|
*/
|
|
|
|
instanceId: PropTypes.number,
|
|
|
|
/**
|
|
|
|
* ID used for a11y in the listbox.
|
|
|
|
*/
|
|
|
|
listboxId: PropTypes.string,
|
|
|
|
/**
|
|
|
|
* Parent node to bind keyboard events to.
|
|
|
|
*/
|
|
|
|
node: PropTypes.instanceOf( Element ).isRequired,
|
|
|
|
/**
|
|
|
|
* Function to execute when an option is selected.
|
|
|
|
*/
|
|
|
|
onSelect: PropTypes.func,
|
2019-10-07 22:42:32 +00:00
|
|
|
/**
|
|
|
|
* Array of options to display.
|
|
|
|
*/
|
|
|
|
options: PropTypes.arrayOf(
|
|
|
|
PropTypes.shape( {
|
|
|
|
isDisabled: PropTypes.bool,
|
|
|
|
key: PropTypes.oneOfType( [ PropTypes.number, PropTypes.string ] ).isRequired,
|
2019-10-16 04:00:20 +00:00
|
|
|
keywords: PropTypes.arrayOf( PropTypes.oneOfType( [ PropTypes.string, PropTypes.number ] ) ),
|
|
|
|
label: PropTypes.oneOfType( [ PropTypes.string, PropTypes.object ] ),
|
2019-10-07 22:42:32 +00:00
|
|
|
value: PropTypes.any,
|
|
|
|
} )
|
|
|
|
).isRequired,
|
2019-08-21 06:41:42 +00:00
|
|
|
/**
|
|
|
|
* Integer for the currently selected item.
|
|
|
|
*/
|
|
|
|
selectedIndex: PropTypes.number,
|
|
|
|
/**
|
|
|
|
* Bool to determine if the list should be positioned absolutely or staticly.
|
|
|
|
*/
|
|
|
|
staticList: PropTypes.bool,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default List;
|