Allows the user to search for Resources and include these as an Element.
({ query, setElement }: WidgetProps)
| 291 | |
| 292 | /** Allows the user to search for Resources and include these as an Element. */ |
| 293 | function SearchWidget({ query, setElement }: WidgetProps) { |
| 294 | const { results } = useServerSearch(query); |
| 295 | // The currently selected result |
| 296 | const [index, setIndex] = useState(0); |
| 297 | |
| 298 | useHotkeys( |
| 299 | 'tab,enter', |
| 300 | e => { |
| 301 | e.preventDefault(); |
| 302 | setElement(results[index]); |
| 303 | }, |
| 304 | { enableOnTags: ['TEXTAREA'] }, |
| 305 | [], |
| 306 | ); |
| 307 | |
| 308 | useHotkeys( |
| 309 | 'left', |
| 310 | e => { |
| 311 | e.preventDefault(); |
| 312 | let next = index - 1; |
| 313 | if (next < 0) { |
| 314 | next = results.length - 1; |
| 315 | } |
| 316 | setIndex(index - 1); |
| 317 | }, |
| 318 | { enableOnTags: ['TEXTAREA'] }, |
| 319 | [index], |
| 320 | ); |
| 321 | |
| 322 | useHotkeys( |
| 323 | 'right', |
| 324 | e => { |
| 325 | e.preventDefault(); |
| 326 | let next = index + 1; |
| 327 | if (next > results.length - 1) { |
| 328 | next = 0; |
| 329 | } |
| 330 | setIndex(index + 1); |
| 331 | }, |
| 332 | { enableOnTags: ['TEXTAREA'] }, |
| 333 | [index], |
| 334 | ); |
| 335 | |
| 336 | if (query == '') { |
| 337 | return ( |
| 338 | <WidgetWrapper> |
| 339 | <p>Search something...</p> |
| 340 | </WidgetWrapper> |
| 341 | ); |
| 342 | } |
| 343 | |
| 344 | return ( |
| 345 | <WidgetWrapper> |
| 346 | <p> (press tab to select, left / right to browse)</p> |
| 347 | <p> |
| 348 | <ResourceInline subject={results[index]} /> |
| 349 | </p> |
| 350 | </WidgetWrapper> |
nothing calls this directly
no test coverage detected