-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathindex.jsx
More file actions
70 lines (68 loc) · 2.15 KB
/
Copy pathindex.jsx
File metadata and controls
70 lines (68 loc) · 2.15 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
import { buildClient } from '@datocms/cma-client-browser';
import { useState } from 'react';
import { useSiteSearch } from 'react-datocms';
import ReactPaginate from 'react-paginate';
import './style.css';
const client = buildClient({
apiToken: 'faeb9172e232a75339242faafb9e56de8c8f13b735f7090964',
});
export default function SiteSearchExamples() {
const [query, setQuery] = useState('');
const { state, error, data } = useSiteSearch({
client,
searchIndexId: '34759',
// optional: you can omit it you only have one locale, or you want to find results in every locale
initialState: { locale: 'en' },
// optional: by default fuzzy-search is not active
fuzzySearch: true,
// optional: defaults to 8 search results per page
resultsPerPage: 5,
});
return (
<div className="example" data-title="Basic example">
<form
onSubmit={(e) => {
e.preventDefault();
state.setQuery(query);
}}
>
<input
type="search"
value={query}
onChange={(e) => setQuery(e.target.value)}
placeholder='Search: try something like "react" or "dato"... '
/>
</form>
{!data && !error && <p>Loading...</p>}
{error && <p>Error! {error}</p>}
{data && (
<>
<div className="results">
{data.pageResults.map((result) => (
<div key={result.id} className="result">
<a href={result.url} className="result-title">
{result.title}
</a>
<div className="result-highlights">
<div>{result.bodyExcerpt}</div>
<div>{result.url}</div>
</div>
</div>
))}
</div>
<p>Total results: {data.totalResults}</p>
<ReactPaginate
className="react-paginate"
pageCount={data.totalPages}
forcePage={state.page}
onPageChange={({ selected }) => {
state.setPage(selected);
}}
activeClassName="active"
renderOnZeroPageCount={() => null}
/>
</>
)}
</div>
);
}