-
Notifications
You must be signed in to change notification settings - Fork 0
/
WithStrings.tsx
64 lines (59 loc) · 1.7 KB
/
WithStrings.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
import { useCombobox } from 'downshift'
import { useState } from 'react'
import { FzfHighlight, useFzf } from 'react-fzf'
import { colors } from '../utils/data'
import { Example } from '../utils/Example'
export function WithStrings() {
const [query, setQuery] = useState('')
const { getFzfHighlightProps, results } = useFzf({ items: colors, query })
const {
getInputProps,
getItemProps,
getLabelProps,
getMenuProps,
getToggleButtonProps,
highlightedIndex,
isOpen,
selectedItem,
} = useCombobox({
items: results,
onInputValueChange: ({ inputValue }) => {
setQuery(inputValue ?? '')
},
})
return (
<Example title="with strings">
<Example.Input>
<ul>
{colors.map((color) => (
<li key={color}>{color}</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}${index}`}
style={{
...(highlightedIndex === index ? { backgroundColor: 'lightblue' } : {}),
...(selectedItem === item ? { color: 'red' } : {}),
}}
{...getItemProps({ item, index })}
>
<FzfHighlight {...getFzfHighlightProps({ index, item })} />
</li>
))}
</ul>
</Example.Output>
</Example>
)
}