Skip to content

Commit

Permalink
Update ref on index.js [closes #25070] (#25073)
Browse files Browse the repository at this point in the history
Co-authored-by: gatsbybot <mathews.kyle+gatsbybot@gmail.com>
  • Loading branch information
samajammin and gatsbybot authored Jul 14, 2020
1 parent 4ca8f05 commit 1fa5d0f
Showing 1 changed file with 24 additions and 23 deletions.
47 changes: 24 additions & 23 deletions docs/docs/adding-search-with-algolia.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ const Stats = connectStateResults(
const useClickOutside = (ref, handler, events) => {
if (!events) events = [`mousedown`, `touchstart`]
const detectClickOutside = event =>
!ref.current.contains(event.target) && handler()
ref.current && !ref.current.contains(event.target) && handler()
useEffect(() => {
for (const event of events)
document.addEventListener(event, detectClickOutside)
Expand All @@ -226,28 +226,29 @@ export default function Search({ indices, collapse, hitsAsGrid }) {
)
useClickOutside(ref, () => setFocus(false))
return (
<InstantSearch
searchClient={searchClient}
indexName={indices[0].name}
onSearchStateChange={({ query }) => setQuery(query)}
root={{ Root, props: { ref } }}
>
<Input onFocus={() => setFocus(true)} {...{ collapse, focus }} />
<HitsWrapper show={query.length > 0 && focus} asGrid={hitsAsGrid}>
{indices.map(({ name, title, hitComp }) => (
<Index key={name} indexName={name}>
<header>
<h3>{title}</h3>
<Stats />
</header>
<Results>
<Hits hitComponent={hitComps[hitComp](() => setFocus(false))} />
</Results>
</Index>
))}
<PoweredBy />
</HitsWrapper>
</InstantSearch>
<Root ref={ref}>
<InstantSearch
searchClient={searchClient}
indexName={indices[0].name}
onSearchStateChange={({ query }) => setQuery(query)}
>
<Input onFocus={() => setFocus(true)} {...{ collapse, focus }} />
<HitsWrapper show={query.length > 0 && focus} asGrid={hitsAsGrid}>
{indices.map(({ name, title, hitComp }) => (
<Index key={name} indexName={name}>
<header>
<h3>{title}</h3>
<Stats />
</header>
<Results>
<Hits hitComponent={hitComps[hitComp](() => setFocus(false))} />
</Results>
</Index>
))}
<PoweredBy />
</HitsWrapper>
</InstantSearch>
</Root>
)
}
```
Expand Down

0 comments on commit 1fa5d0f

Please sign in to comment.