-
Notifications
You must be signed in to change notification settings - Fork 2
/
ComboBox.tsx
87 lines (82 loc) · 1.99 KB
/
ComboBox.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
import {
Autocomplete as MuiComboBox,
AutocompleteChangeDetails,
AutocompleteChangeReason,
} from '@mui/material';
import { FocusEventHandler, SyntheticEvent } from 'react';
import './combobox.css';
import Textfield from './Textfield';
type ComboBoxProps = {
/**
* Text label title for combobox
*/
title?: string;
/**
* If `true`, the component is disabled.
* @default false
*/
disabled?: boolean;
/**
* True when (input related to) switch is erroneous
* @default false
*/
hasError?: boolean;
/**
* If `true`, the input will take up the full width of its container.
* @default false
*/
fullWidth?: boolean;
/**
* List of available options for the dropdown menu
*/
options?: readonly unknown[];
/**
* Additional css classes to help with unique styling of the button
*/
className?: string[];
/**
* Triggers when content of textfield is changed
*/
onChange?: (
event: SyntheticEvent<Element, Event>,
value: unknown,
reason: AutocompleteChangeReason,
details?: AutocompleteChangeDetails<unknown> | undefined,
) => void;
/**
* Triggers when textfield gets focus
*/
onFocus?: FocusEventHandler<HTMLDivElement>; // Storybook crashes when giving the combo box focus
/**
* Triggers when textfield loses focus
*/
onBlur?: FocusEventHandler<HTMLDivElement>;
};
function ComboBox({
title,
disabled = false,
hasError = false,
fullWidth = false,
options = [],
className,
onChange,
onFocus,
onBlur,
}: ComboBoxProps) {
const errorClass = hasError ? 'error' : '';
const classNameString = className?.join(' ') ?? '';
return (
<MuiComboBox
disablePortal
disabled={disabled}
fullWidth={fullWidth}
options={options}
className={['papi-combo-box', errorClass, classNameString].join(' ')}
onChange={onChange}
onFocus={onFocus}
onBlur={onBlur}
renderInput={(props) => <Textfield {...props} label={title} />}
/>
);
}
export default ComboBox;