-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAutocompleteSearch.tsx
More file actions
113 lines (100 loc) · 3.19 KB
/
Copy pathAutocompleteSearch.tsx
File metadata and controls
113 lines (100 loc) · 3.19 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { FC, useState, useRef, useEffect } from 'react';
import Link from 'next/link';
import algoliasearch from 'algoliasearch/lite';
import {
Configure,
Highlight,
Hits,
InstantSearch,
useSearchBox,
} from 'react-instantsearch';
import type { Hit, BaseHit } from 'instantsearch.js';
import { Card } from '@components/layout';
import classNames from 'classnames';
const searchClient = algoliasearch(
'V0X7Z4KE9D',
'544bec33383dc791bcbca3e1ceaec11b',
);
interface ToolHit extends Hit<BaseHit> {
fields: {
slug: string;
name: string;
};
}
interface SearchResult {
hit: ToolHit;
sendEvent: (eventType: string, hit: ToolHit, eventName?: string) => void;
}
const Hit = (result: SearchResult) => {
return (
<Link href={result.hit.fields.slug}>
<Highlight attribute="name" hit={result.hit} />
</Link>
);
};
const CustomSearchBox: FC<{
onFocus: () => void;
onChange: (query: string) => void;
}> = ({ onFocus, onChange }) => {
const { query, refine } = useSearchBox();
return (
<div className="ais-SearchBox">
<input
type="search"
placeholder="Find analysis tools, formatters, linters.."
value={query}
onFocus={onFocus}
onChange={(e) => {
refine(e.target.value);
onChange(e.target.value);
}}
className="ais-SearchBox-input"
/>
</div>
);
};
const AutocompleteSearch: FC = () => {
const [showResults, setShowResults] = useState(false);
const searchRef = useRef<HTMLDivElement>(null);
useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
if (
searchRef.current &&
!searchRef.current.contains(event.target as Node)
) {
setShowResults(false);
}
};
const handleEscape = (event: KeyboardEvent) => {
if (event.key === 'Escape') {
setShowResults(false);
}
};
document.addEventListener('mousedown', handleClickOutside);
document.addEventListener('keydown', handleEscape);
return () => {
document.removeEventListener('mousedown', handleClickOutside);
document.removeEventListener('keydown', handleEscape);
};
}, []);
return (
<InstantSearch searchClient={searchClient} indexName="tools">
<div className="autocomplete-search" ref={searchRef}>
<Configure hitsPerPage={10} typoTolerance={true} />
<CustomSearchBox
onFocus={() => setShowResults(true)}
onChange={(query) => setShowResults(query.length > 0)}
/>
<div className="relative">
<Card
className={classNames('search-results', {
hidden: !showResults,
})}>
<Hits hitComponent={Hit} />
</Card>
</div>
</div>
</InstantSearch>
);
};
export default AutocompleteSearch;