| 1 |
import { useRef } from '@wordpress/element'; |
| 2 |
import { __ } from '@wordpress/i18n'; |
| 3 |
import { search as sIcon, Icon, closeSmall } from '@wordpress/icons'; |
| 4 |
import { KB_HOST } from '@constants'; |
| 5 |
import classNames from 'classnames'; |
| 6 |
import { useKnowledgeBaseStore } from '@help-center/state/knowledge-base.js'; |
| 7 |
|
| 8 |
export const SearchForm = ({ onChange }) => { |
| 9 |
const { searchTerm, clearSearchTerm, reset } = useKnowledgeBaseStore(); |
| 10 |
const warmed = useRef(false); |
| 11 |
const searchRef = useRef(); |
| 12 |
|
| 13 |
return ( |
| 14 |
<form |
| 15 |
method="get" |
| 16 |
onSubmit={(e) => e.preventDefault()} |
| 17 |
className="relative h-10 w-full"> |
| 18 |
<label htmlFor="ext-help-center-search" className="sr-only"> |
| 19 |
{__('Search for articles', 'extendify-local')} |
| 20 |
</label> |
| 21 |
<input |
| 22 |
ref={searchRef} |
| 23 |
name="ext-kb-search" |
| 24 |
autoFocus |
| 25 |
autoCapitalize="off" |
| 26 |
id="ext-help-center-search" |
| 27 |
type="text" |
| 28 |
value={searchTerm ?? ''} |
| 29 |
onChange={(e) => onChange(e.target.value)} |
| 30 |
onFocus={() => { |
| 31 |
if (warmed.current) return; |
| 32 |
warmed.current = true; |
| 33 |
fetch(`${KB_HOST}/api/posts?boot=true`, { method: 'POST' }); |
| 34 |
}} |
| 35 |
placeholder={__('What do you need help with?', 'extendify-local')} |
| 36 |
className="input border-text-800 h-10 w-full border px-3 text-sm placeholder-gray-600" |
| 37 |
/> |
| 38 |
<div className="absolute inset-y-5 right-2 flex items-center justify-center text-gray-400 rtl:left-2 rtl:right-auto"> |
| 39 |
<Icon |
| 40 |
icon={!searchTerm ? sIcon : closeSmall} |
| 41 |
className={classNames('fill-current', { |
| 42 |
'cursor-pointer': searchTerm, |
| 43 |
})} |
| 44 |
onClick={() => { |
| 45 |
reset(); |
| 46 |
clearSearchTerm(); |
| 47 |
searchRef.current?.focus(); |
| 48 |
}} |
| 49 |
size={24} |
| 50 |
/> |
| 51 |
</div> |
| 52 |
</form> |
| 53 |
); |
| 54 |
}; |
| 55 |
|