-
Notifications
You must be signed in to change notification settings - Fork 10
/
Autocomplete.tsx
39 lines (31 loc) · 1.04 KB
/
Autocomplete.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { autocomplete } from "@algolia/autocomplete-js"
import React, { createElement, Fragment, useEffect, useRef } from "react"
import { createRoot, Root } from "react-dom/client"
function Autocomplete(props) {
const containerRef = useRef<HTMLDivElement | null>(null)
const panelRootRef = useRef<Root | null>(null)
const rootRef = useRef<HTMLElement | null>(null)
useEffect(() => {
if (!containerRef.current) {
return undefined
}
const search = autocomplete({
container: containerRef.current,
renderer: { createElement, Fragment, render: () => {} },
render({ children }, root) {
if (!panelRootRef.current || rootRef.current !== root) {
rootRef.current = root
panelRootRef.current?.unmount()
panelRootRef.current = createRoot(root)
}
panelRootRef.current.render(children)
},
...props,
})
return () => {
search.destroy()
}
}, [props])
return <div className="h-full w-full" ref={containerRef} />
}
export default Autocomplete