({
queryHook,
searchAsYouType = true,
translations,
...props
}: SearchBoxProps)
| 34 | }; |
| 35 | |
| 36 | export function SearchBox({ |
| 37 | queryHook, |
| 38 | searchAsYouType = true, |
| 39 | translations, |
| 40 | ...props |
| 41 | }: SearchBoxProps) { |
| 42 | const { query, refine, isSearchStalled } = useSearchBox( |
| 43 | { queryHook }, |
| 44 | { $$widgetType: 'ais.searchBox' } |
| 45 | ); |
| 46 | const [inputValue, setInputValue] = useState(query); |
| 47 | const inputRef = useRef<HTMLInputElement>(null); |
| 48 | |
| 49 | function setQuery(newQuery: string) { |
| 50 | setInputValue(newQuery); |
| 51 | |
| 52 | if (searchAsYouType) { |
| 53 | refine(newQuery); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | function onReset() { |
| 58 | setQuery(''); |
| 59 | |
| 60 | if (!searchAsYouType) { |
| 61 | refine(''); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | function onChange(event: React.ChangeEvent<HTMLInputElement>) { |
| 66 | setQuery(event.currentTarget.value); |
| 67 | } |
| 68 | |
| 69 | function onSubmit(event: React.FormEvent<HTMLFormElement>) { |
| 70 | if (!searchAsYouType) { |
| 71 | refine(inputValue); |
| 72 | } |
| 73 | |
| 74 | if (props.onSubmit) { |
| 75 | props.onSubmit(event); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | // Track when the InstantSearch query changes to synchronize it with |
| 80 | // the React state. |
| 81 | // We bypass the state update if the input is focused to avoid concurrent |
| 82 | // updates when typing. |
| 83 | if (query !== inputValue && document.activeElement !== inputRef.current) { |
| 84 | setInputValue(query); |
| 85 | } |
| 86 | |
| 87 | const uiProps: UiProps = { |
| 88 | inputRef, |
| 89 | isSearchStalled, |
| 90 | onChange, |
| 91 | onReset, |
| 92 | onSubmit, |
| 93 | value: inputValue, |
nothing calls this directly
no outgoing calls
no test coverage detected