2018-08-28 19:15:24 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __, sprintf } from '@wordpress/i18n';
|
|
|
|
import { Component } from '@wordpress/element';
|
|
|
|
import { findIndex, noop } from 'lodash';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import Autocomplete from './autocomplete';
|
2018-09-07 19:39:04 +00:00
|
|
|
import { product, productCategory } 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: '',
|
|
|
|
};
|
|
|
|
|
|
|
|
this.selectResult = this.selectResult.bind( this );
|
|
|
|
this.removeResult = this.removeResult.bind( this );
|
|
|
|
this.updateSearch = this.updateSearch.bind( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
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-08-28 19:15:24 +00:00
|
|
|
default:
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const autocompleter = this.getAutocompleter();
|
2018-09-03 02:08:13 +00:00
|
|
|
const { selected } = this.props;
|
|
|
|
const { value = '' } = this.state;
|
2018-08-28 19:15:24 +00:00
|
|
|
return (
|
|
|
|
<div className="woocommerce-search">
|
|
|
|
<Autocomplete completer={ autocompleter } onSelect={ this.selectResult }>
|
|
|
|
{ ( { listBoxId, activeId, onChange } ) => (
|
|
|
|
<input
|
|
|
|
type="search"
|
|
|
|
value={ value }
|
|
|
|
className="woocommerce-search__input"
|
|
|
|
onChange={ this.updateSearch( onChange ) }
|
|
|
|
aria-owns={ listBoxId }
|
|
|
|
aria-activedescendant={ activeId }
|
|
|
|
/>
|
|
|
|
) }
|
|
|
|
</Autocomplete>
|
|
|
|
{ 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,
|
|
|
|
selected.length
|
|
|
|
);
|
|
|
|
return (
|
|
|
|
<Tag
|
|
|
|
key={ item.id }
|
|
|
|
id={ item.id }
|
|
|
|
label={ item.label }
|
|
|
|
remove={ this.removeResult }
|
|
|
|
removeLabel={ __( 'Remove product', 'wc-admin' ) }
|
|
|
|
screenReaderLabel={ screenReaderLabel }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
} ) }
|
|
|
|
</div>
|
|
|
|
) : null }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Search.propTypes = {
|
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-09-07 19:39:04 +00:00
|
|
|
type: PropTypes.oneOf( [ 'products', 'product_cats', 'orders', 'customers' ] ).isRequired,
|
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-08-28 19:15:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Search.defaultProps = {
|
|
|
|
onChange: noop,
|
2018-09-03 02:08:13 +00:00
|
|
|
selected: [],
|
2018-08-28 19:15:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default Search;
|