-
Notifications
You must be signed in to change notification settings - Fork 0
/
WithObjects.tsx
73 lines (67 loc) · 1.87 KB
/
WithObjects.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
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
import { useCombobox } from 'downshift'
import { useState } from 'react'
import { FzfHighlight, useFzf } from 'react-fzf'
import { posts } from '../utils/data'
import { Example } from '../utils/Example'
function postToString(post: typeof posts[number] | null): string {
return post?.title ?? ''
}
export function WithObjects() {
const [query, setQuery] = useState('')
const { getFzfHighlightProps, results } = useFzf({
items: posts,
itemToString: postToString,
query,
})
const {
getInputProps,
getItemProps,
getLabelProps,
getMenuProps,
getToggleButtonProps,
highlightedIndex,
isOpen,
selectedItem,
} = useCombobox({
items: results,
itemToString: postToString,
onInputValueChange: ({ inputValue }) => {
setQuery(inputValue ?? '')
},
})
return (
<Example title="with objects">
<Example.Input>
<ul>
{posts.map((post) => (
<li key={post.title}>{JSON.stringify(post)}</li>
))}
</ul>
</Example.Input>
<Example.Output>
<label {...getLabelProps()}>
query:
<input {...getInputProps()} />
<button type="button" {...getToggleButtonProps()} aria-label="toggle menu">
↓
</button>
</label>
<ul {...getMenuProps({ className: 'menu' })}>
{isOpen &&
results.map((item, index) => (
<li
key={item.id}
style={{
...(highlightedIndex === index ? { backgroundColor: 'lightblue' } : {}),
...(selectedItem === item ? { color: 'red' } : {}),
}}
{...getItemProps({ item, index })}
>
<FzfHighlight {...getFzfHighlightProps({ index, item })} />
</li>
))}
</ul>
</Example.Output>
</Example>
)
}