2018-08-28 19:15:24 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __, sprintf } from '@wordpress/i18n';
|
2018-10-10 03:58:34 +00:00
|
|
|
import { Component, createRef } from '@wordpress/element';
|
|
|
|
import { withInstanceId } from '@wordpress/compose';
|
2018-08-28 19:15:24 +00:00
|
|
|
import { findIndex, noop } from 'lodash';
|
2018-09-19 17:16:52 +00:00
|
|
|
import Gridicon from 'gridicons';
|
2018-08-28 19:15:24 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2018-10-10 03:58:34 +00:00
|
|
|
import classnames from 'classnames';
|
2018-08-28 19:15:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import Autocomplete from './autocomplete';
|
2018-10-31 00:04:00 +00:00
|
|
|
import { product, productCategory, coupons, variations } from './autocompleters';
|
2018-08-28 19:15:24 +00:00
|
|
|
import Tag from 'components/tag';
|
|
|
|
import './style.scss';
|
|
|
|
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* A search box which autocompletes results while typing, allowing for the user to select an existing object
|
|
|
|
* (product, order, customer, etc). Currently only products are supported.
|
|
|
|
*/
|
2018-08-28 19:15:24 +00:00
|
|
|
class Search extends Component {
|
|
|
|
constructor( props ) {
|
|
|
|
super( props );
|
|
|
|
this.state = {
|
|
|
|
value: '',
|
2018-10-10 03:58:34 +00:00
|
|
|
isActive: false,
|
2018-08-28 19:15:24 +00:00
|
|
|
};
|
|
|
|
|
2018-10-10 03:58:34 +00:00
|
|
|
this.input = createRef();
|
|
|
|
|
2018-08-28 19:15:24 +00:00
|
|
|
this.selectResult = this.selectResult.bind( this );
|
|
|
|
this.removeResult = this.removeResult.bind( this );
|
|
|
|
this.updateSearch = this.updateSearch.bind( this );
|
2018-10-10 03:58:34 +00:00
|
|
|
this.onFocus = this.onFocus.bind( this );
|
|
|
|
this.onBlur = this.onBlur.bind( this );
|
2018-08-28 19:15:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
selectResult( value ) {
|
2018-09-03 02:08:13 +00:00
|
|
|
const { selected, onChange } = this.props;
|
2018-08-28 19:15:24 +00:00
|
|
|
// Check if this is already selected
|
2018-09-03 02:08:13 +00:00
|
|
|
const isSelected = findIndex( selected, { id: value.id } );
|
2018-08-28 19:15:24 +00:00
|
|
|
if ( -1 === isSelected ) {
|
2018-09-03 02:08:13 +00:00
|
|
|
this.setState( { value: '' } );
|
|
|
|
onChange( [ ...selected, value ] );
|
2018-08-28 19:15:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
removeResult( id ) {
|
|
|
|
return () => {
|
2018-09-03 02:08:13 +00:00
|
|
|
const { selected, onChange } = this.props;
|
|
|
|
const i = findIndex( selected, { id } );
|
|
|
|
onChange( [ ...selected.slice( 0, i ), ...selected.slice( i + 1 ) ] );
|
2018-08-28 19:15:24 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
updateSearch( onChange ) {
|
|
|
|
return event => {
|
|
|
|
const value = event.target.value || '';
|
|
|
|
this.setState( { value } );
|
|
|
|
onChange( event );
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
getAutocompleter() {
|
|
|
|
switch ( this.props.type ) {
|
|
|
|
case 'products':
|
|
|
|
return product;
|
2018-09-07 19:39:04 +00:00
|
|
|
case 'product_cats':
|
|
|
|
return productCategory;
|
2018-09-17 02:26:34 +00:00
|
|
|
case 'coupons':
|
|
|
|
return coupons;
|
2018-10-31 00:04:00 +00:00
|
|
|
case 'variations':
|
|
|
|
return variations;
|
2018-08-28 19:15:24 +00:00
|
|
|
default:
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-10 03:58:34 +00:00
|
|
|
renderTags() {
|
|
|
|
const { selected } = this.props;
|
|
|
|
return selected.length ? (
|
|
|
|
<div className="woocommerce-search__token-list">
|
|
|
|
{ selected.map( ( item, i ) => {
|
|
|
|
const screenReaderLabel = sprintf(
|
|
|
|
__( '%1$s (%2$s of %3$s)', 'wc-admin' ),
|
|
|
|
item.label,
|
|
|
|
i + 1,
|
|
|
|
selected.length
|
|
|
|
);
|
|
|
|
return (
|
|
|
|
<Tag
|
|
|
|
key={ item.id }
|
|
|
|
id={ item.id }
|
|
|
|
label={ item.label }
|
|
|
|
remove={ this.removeResult }
|
|
|
|
screenReaderLabel={ screenReaderLabel }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
} ) }
|
|
|
|
</div>
|
|
|
|
) : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
onFocus() {
|
|
|
|
this.setState( { isActive: true } );
|
|
|
|
}
|
|
|
|
|
|
|
|
onBlur() {
|
|
|
|
this.setState( { isActive: false } );
|
|
|
|
}
|
|
|
|
|
2018-08-28 19:15:24 +00:00
|
|
|
render() {
|
|
|
|
const autocompleter = this.getAutocompleter();
|
2018-09-20 05:07:37 +00:00
|
|
|
const { placeholder, inlineTags, selected, instanceId, className } = this.props;
|
2018-10-10 03:58:34 +00:00
|
|
|
const { value = '', isActive } = this.state;
|
2018-09-26 17:13:28 +00:00
|
|
|
const aria = {
|
|
|
|
'aria-labelledby': this.props[ 'aria-labelledby' ],
|
|
|
|
'aria-label': this.props[ 'aria-label' ],
|
|
|
|
};
|
2018-08-28 19:15:24 +00:00
|
|
|
return (
|
2018-09-20 05:07:37 +00:00
|
|
|
<div className={ classnames( 'woocommerce-search', className ) }>
|
2018-10-10 14:12:00 +00:00
|
|
|
<Gridicon className="woocommerce-search__icon" icon="search" size={ 18 } />
|
2018-08-28 19:15:24 +00:00
|
|
|
<Autocomplete completer={ autocompleter } onSelect={ this.selectResult }>
|
2018-10-10 03:58:34 +00:00
|
|
|
{ ( { listBoxId, activeId, onChange } ) =>
|
|
|
|
// Disable reason: The div below visually simulates an input field. Its
|
|
|
|
// child input is the actual input and responds accordingly to all keyboard
|
|
|
|
// events, but click events need to be passed onto the child input. There
|
|
|
|
// is no appropriate aria role for describing this situation, which is only
|
|
|
|
// for the benefit of sighted users.
|
|
|
|
/* eslint-disable jsx-a11y/no-static-element-interactions, jsx-a11y/click-events-have-key-events */
|
|
|
|
inlineTags ? (
|
|
|
|
<div
|
|
|
|
className={ classnames( 'woocommerce-search__inline-container', {
|
|
|
|
'is-active': isActive,
|
|
|
|
} ) }
|
|
|
|
onClick={ () => {
|
|
|
|
this.input.current.focus();
|
|
|
|
} }
|
|
|
|
>
|
|
|
|
{ this.renderTags() }
|
|
|
|
<input
|
|
|
|
ref={ this.input }
|
|
|
|
type="text"
|
|
|
|
size={
|
|
|
|
( ( value.length === 0 && placeholder && placeholder.length ) ||
|
|
|
|
value.length ) + 1
|
|
|
|
}
|
|
|
|
value={ value }
|
|
|
|
placeholder={ ( selected.length === 0 && placeholder ) || '' }
|
|
|
|
className="woocommerce-search__inline-input"
|
|
|
|
onChange={ this.updateSearch( onChange ) }
|
|
|
|
aria-owns={ listBoxId }
|
|
|
|
aria-activedescendant={ activeId }
|
|
|
|
onFocus={ this.onFocus }
|
|
|
|
onBlur={ this.onBlur }
|
|
|
|
aria-describedby={
|
|
|
|
selected.length ? `search-inline-input-${ instanceId }` : null
|
|
|
|
}
|
|
|
|
{ ...aria }
|
2018-08-28 19:15:24 +00:00
|
|
|
/>
|
2018-10-10 03:58:34 +00:00
|
|
|
<span id={ `search-inline-input-${ instanceId }` } className="screen-reader-text">
|
|
|
|
{ __( 'Move backward for selected items' ) }
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<input
|
|
|
|
type="search"
|
|
|
|
value={ value }
|
|
|
|
placeholder={ placeholder }
|
|
|
|
className="woocommerce-search__input"
|
|
|
|
onChange={ this.updateSearch( onChange ) }
|
|
|
|
aria-owns={ listBoxId }
|
|
|
|
aria-activedescendant={ activeId }
|
|
|
|
{ ...aria }
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
</Autocomplete>
|
|
|
|
{ ! inlineTags && this.renderTags() }
|
2018-08-28 19:15:24 +00:00
|
|
|
</div>
|
|
|
|
);
|
2018-10-10 03:58:34 +00:00
|
|
|
/* eslint-enable jsx-a11y/no-static-element-interactions, jsx-a11y/click-events-have-key-events */
|
2018-08-28 19:15:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Search.propTypes = {
|
2018-09-20 05:07:37 +00:00
|
|
|
/**
|
|
|
|
* Class name applied to parent div.
|
|
|
|
*/
|
|
|
|
className: PropTypes.string,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* Function called when selected results change, passed result list.
|
|
|
|
*/
|
2018-08-28 19:15:24 +00:00
|
|
|
onChange: PropTypes.func,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* The object type to be used in searching.
|
|
|
|
*/
|
2018-10-31 00:04:00 +00:00
|
|
|
type: PropTypes.oneOf( [
|
|
|
|
'products',
|
|
|
|
'product_cats',
|
|
|
|
'orders',
|
|
|
|
'customers',
|
|
|
|
'coupons',
|
|
|
|
'variations',
|
|
|
|
] ).isRequired,
|
2018-09-19 17:16:52 +00:00
|
|
|
/**
|
|
|
|
* A placeholder for the search input.
|
|
|
|
*/
|
|
|
|
placeholder: PropTypes.string,
|
2018-09-03 02:08:13 +00:00
|
|
|
/**
|
2018-09-03 02:08:13 +00:00
|
|
|
* An array of objects describing selected values.
|
2018-09-03 02:08:13 +00:00
|
|
|
*/
|
|
|
|
selected: PropTypes.arrayOf(
|
|
|
|
PropTypes.shape( {
|
|
|
|
id: PropTypes.number.isRequired,
|
|
|
|
label: PropTypes.string.isRequired,
|
|
|
|
} )
|
|
|
|
),
|
2018-10-10 03:58:34 +00:00
|
|
|
/**
|
|
|
|
* Render tags inside input, otherwise render below input
|
|
|
|
*/
|
|
|
|
inlineTags: PropTypes.bool,
|
2018-08-28 19:15:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Search.defaultProps = {
|
|
|
|
onChange: noop,
|
2018-09-03 02:08:13 +00:00
|
|
|
selected: [],
|
2018-10-10 03:58:34 +00:00
|
|
|
inlineTags: false,
|
2018-08-28 19:15:24 +00:00
|
|
|
};
|
|
|
|
|
2018-10-10 03:58:34 +00:00
|
|
|
export default withInstanceId( Search );
|