-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInputWithSuggestions.tsx
60 lines (55 loc) · 2.11 KB
/
InputWithSuggestions.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
import { type FC, type PropsWithChildren, useState } from 'react';
import { Dropdown, FormControl, InputGroup } from 'react-bootstrap';
import { type Suggestion } from 'components/result-search/jsonSchema';
import { truthyOrNoneTag } from './utility';
type InputWithSuggestionsProps = {
setInput: (input: string) => void;
suggestions?: Suggestion[];
placeholder?: string;
value?: string;
};
const InputWithSuggestions: FC<PropsWithChildren<InputWithSuggestionsProps>> = ({
setInput,
suggestions,
placeholder,
value,
children,
}) => {
const [input, setLocalInput] = useState(value);
const updateInput = (newInput: string) => {
setLocalInput(newInput);
setInput(newInput);
};
return (
<Dropdown as={InputGroup} onSelect={(k) => updateInput(k ?? '')} align="end">
<FormControl
placeholder={placeholder}
aria-label={placeholder ?? 'Input field with suggestions'}
value={input}
onChange={(e) => updateInput(e.target.value)}
/>
{suggestions !== undefined && suggestions.length > 0 && (
<>
<Dropdown.Toggle split variant="outline-secondary" />
<Dropdown.Menu>
{suggestions.map((suggestion) => (
<Dropdown.Item key={suggestion.field} eventKey={suggestion.field}>
{suggestion.field}
<br />
<small>
{truthyOrNoneTag(
suggestion.description,
'No description given.'
)}
</small>
</Dropdown.Item>
))}
</Dropdown.Menu>
</>
)}
{/* TODO: clean up, find alternative for this (this is used in filters) */}
{children}
</Dropdown>
);
};
export default InputWithSuggestions;