2023-07-04 12:08:50 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { Icon, search } from '@wordpress/icons';
|
2023-08-09 13:04:35 +00:00
|
|
|
import { useEffect, useState } from '@wordpress/element';
|
|
|
|
import { navigateTo, getNewPath, useQuery } from '@woocommerce/navigation';
|
2023-07-04 12:08:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './search.scss';
|
2023-10-06 08:17:56 +00:00
|
|
|
import { MARKETPLACE_PATH } from '../constants';
|
2023-07-04 12:08:50 +00:00
|
|
|
|
2023-10-13 03:56:38 +00:00
|
|
|
const searchPlaceholder = __(
|
|
|
|
'Search for extensions and themes',
|
|
|
|
'woocommerce'
|
|
|
|
);
|
2023-07-04 12:08:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Search component.
|
|
|
|
*
|
|
|
|
* @return {JSX.Element} Search component.
|
|
|
|
*/
|
2023-08-10 07:16:56 +00:00
|
|
|
function Search(): JSX.Element {
|
2023-07-26 14:58:22 +00:00
|
|
|
const [ searchTerm, setSearchTerm ] = useState( '' );
|
2023-07-04 12:08:50 +00:00
|
|
|
|
2023-08-09 13:04:35 +00:00
|
|
|
const query = useQuery();
|
2023-07-04 12:08:50 +00:00
|
|
|
|
2023-08-09 13:04:35 +00:00
|
|
|
useEffect( () => {
|
|
|
|
if ( query.term ) {
|
|
|
|
setSearchTerm( query.term );
|
2023-09-14 10:01:56 +00:00
|
|
|
} else {
|
|
|
|
setSearchTerm( '' );
|
2023-07-26 14:58:22 +00:00
|
|
|
}
|
2023-08-09 13:04:35 +00:00
|
|
|
}, [ query.term ] );
|
2023-07-04 12:08:50 +00:00
|
|
|
|
2023-10-04 16:59:34 +00:00
|
|
|
useEffect( () => {
|
|
|
|
if ( query.tab !== 'search' ) {
|
|
|
|
setSearchTerm( '' );
|
|
|
|
}
|
|
|
|
}, [ query.tab ] );
|
|
|
|
|
2023-08-09 13:04:35 +00:00
|
|
|
const runSearch = () => {
|
|
|
|
const term = searchTerm.trim();
|
|
|
|
|
2023-10-17 16:32:23 +00:00
|
|
|
const newQuery: { term?: string; tab?: string } = {};
|
|
|
|
if ( term !== '' ) {
|
|
|
|
newQuery.term = term;
|
|
|
|
newQuery.tab = 'search';
|
|
|
|
}
|
|
|
|
|
2023-10-06 08:17:56 +00:00
|
|
|
// When the search term changes, we reset the query string on purpose.
|
2023-08-09 13:04:35 +00:00
|
|
|
navigateTo( {
|
2023-10-17 16:32:23 +00:00
|
|
|
url: getNewPath( newQuery, MARKETPLACE_PATH, {} ),
|
2023-08-09 13:04:35 +00:00
|
|
|
} );
|
2023-08-10 07:16:56 +00:00
|
|
|
|
2023-07-04 12:08:50 +00:00
|
|
|
return [];
|
|
|
|
};
|
|
|
|
|
2023-07-26 14:58:22 +00:00
|
|
|
const handleInputChange = (
|
|
|
|
event: React.ChangeEvent< HTMLInputElement >
|
|
|
|
) => {
|
|
|
|
setSearchTerm( event.target.value );
|
|
|
|
};
|
|
|
|
|
2023-07-04 12:08:50 +00:00
|
|
|
const handleKeyUp = ( event: { key: string } ) => {
|
|
|
|
if ( event.key === 'Enter' ) {
|
|
|
|
runSearch();
|
|
|
|
}
|
2023-08-09 13:04:35 +00:00
|
|
|
|
2023-07-26 14:58:22 +00:00
|
|
|
if ( event.key === 'Escape' ) {
|
|
|
|
setSearchTerm( '' );
|
|
|
|
}
|
2023-07-04 12:08:50 +00:00
|
|
|
};
|
|
|
|
|
2023-07-26 14:58:22 +00:00
|
|
|
return (
|
|
|
|
<div className="woocommerce-marketplace__search">
|
|
|
|
<label
|
|
|
|
className="screen-reader-text"
|
|
|
|
htmlFor="woocommerce-marketplace-search-query"
|
|
|
|
>
|
|
|
|
{ searchPlaceholder }
|
|
|
|
</label>
|
|
|
|
<input
|
|
|
|
id="woocommerce-marketplace-search-query"
|
|
|
|
value={ searchTerm }
|
|
|
|
className="woocommerce-marketplace__search-input"
|
|
|
|
type="search"
|
|
|
|
name="woocommerce-marketplace-search-query"
|
|
|
|
placeholder={ searchPlaceholder }
|
|
|
|
onChange={ handleInputChange }
|
|
|
|
onKeyUp={ handleKeyUp }
|
|
|
|
/>
|
|
|
|
<button
|
|
|
|
id="woocommerce-marketplace-search-button"
|
|
|
|
className="woocommerce-marketplace__search-button"
|
|
|
|
aria-label={ __( 'Search', 'woocommerce' ) }
|
|
|
|
onClick={ runSearch }
|
|
|
|
>
|
|
|
|
<Icon icon={ search } size={ 32 } />
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
);
|
2023-07-04 12:08:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default Search;
|