/
WithOptions.tsx
130 lines (123 loc) · 4.13 KB
/
WithOptions.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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { useState } from 'react'
import { basicMatch, byLengthAsc, extendedMatch, type UseFzfOptions, FzfHighlight, useFzf } from 'react-fzf'
import { names } from '../utils/data'
import { Example } from '../utils/Example'
function nameToLastname(name: string) {
return name.split(' ')[1] ?? ''
}
export function WithOptions() {
const [query, setQuery] = useState('')
const [casing, setCasing] = useState<Casing>('smart-case')
const [forward, setForward] = useState(true)
const [fuzzy, setFuzzy] = useState<Fuzzy>('v2')
const [lastNameOnly, setLastNameOnly] = useState(false)
const [lengthTiebreaker, setLengthTiebreaker] = useState(false)
const [limit, setLimit] = useState(20)
const [normalize, setNormalize] = useState(true)
const [sort, setSort] = useState(true)
const [useExtendedMatch, setUseExtendedMatch] = useState(false)
const { getFzfHighlightProps, results } = useFzf({
casing,
forward,
fuzzy,
items: names,
itemToString: lastNameOnly ? nameToLastname : undefined,
limit,
match: useExtendedMatch ? extendedMatch : basicMatch,
normalize,
query,
sort,
tiebreakers: lengthTiebreaker ? [byLengthAsc] : [],
})
return (
<Example
title="with options"
header={
<>
<label>
query:
<input type="text" placeholder="filter…" value={query} onChange={(event) => setQuery(event.target.value)} />
</label>
<label>
limit:
<input
min={0}
onChange={(event) => setLimit(event.target.valueAsNumber)}
placeholder="limit"
type="number"
value={limit}
/>
</label>
<label>
last name only:
<input onChange={(event) => setLastNameOnly(event.target.checked)} type="checkbox" checked={lastNameOnly} />
</label>
<label>
casing:
<select value={casing} onChange={(event) => setCasing(event.target.value as Casing)}>
<option value="smart-case">smart-case</option>
<option value="case-sensitive">case-sensitive</option>
<option value="case-insensitive">case-insensitive</option>
</select>
</label>
<label>
normalize:
<input onChange={(event) => setNormalize(event.target.checked)} type="checkbox" checked={normalize} />
</label>
<label>
sort:
<input checked={sort} onChange={(event) => setSort(event.target.checked)} type="checkbox" />
</label>
<label>
use length tiebreaker:
<input
checked={lengthTiebreaker}
disabled={!sort}
onChange={(event) => setLengthTiebreaker(event.target.checked)}
type="checkbox"
/>
</label>
<label>
fuzzy:
<select value={fuzzy.toString()} onChange={(event) => setFuzzy(event.target.value as Fuzzy)}>
<option value="v1">v1</option>
<option value="v2">v2</option>
<option value="false">false</option>
</select>
</label>
<label>
use extended match:
<input
checked={useExtendedMatch}
onChange={(event) => setUseExtendedMatch(event.target.checked)}
type="checkbox"
/>
</label>
<label>
forward:
<input checked={forward} onChange={(event) => setForward(event.target.checked)} type="checkbox" />
</label>
</>
}
>
<Example.Input>
<ul>
{names.map((name) => (
<li key={name}>{name}</li>
))}
</ul>
</Example.Input>
<Example.Output>
<ul>
{results.map((item, index) => (
<li key={item}>
<FzfHighlight {...getFzfHighlightProps({ index, item })} />
</li>
))}
</ul>
</Example.Output>
</Example>
)
}
type Casing = NonNullable<UseFzfOptions<string[]>['casing']>
type Fuzzy = NonNullable<UseFzfOptions<string[]>['fuzzy']>