-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathComboBox.jsx
134 lines (117 loc) · 3.27 KB
/
ComboBox.jsx
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
131
132
133
134
/**
* @component
*/
import PropTypes from 'prop-types';
import React, { useMemo } from 'react';
import isInIframeDialog from '../utils/isInIframeDialog';
import './ComboBox.scss';
import DialogSelectComboBox from './DialogSelectComboBox';
import HTMLSelectComboBox from './HTMLSelectComboBox';
/**
* A button with a dropdown that contains a scrollable list of distinct values
* from which people can choose.
*/
const ComboBox = ({
className,
label,
list,
disabled = false,
listValue,
listKey,
stopPropagation = false,
defaultValue,
parent,
onSelect,
style,
value,
}) => {
const iframeDialogView = useMemo(isInIframeDialog, []);
return iframeDialogView ? (
<HTMLSelectComboBox
className={className}
label={label}
list={list}
disabled={disabled}
listValue={listValue}
listKey={listKey}
stopPropagation={stopPropagation}
defaultValue={defaultValue}
value={value}
onSelect={onSelect}
/>
) : (
<DialogSelectComboBox
className={className}
label={label}
list={list}
disabled={disabled}
listValue={listValue}
listKey={listKey}
stopPropagation={stopPropagation}
defaultValue={defaultValue}
parent={parent}
onSelect={onSelect}
style={style}
value={value}
/>
);
};
export default ComboBox;
ComboBox.propTypes = {
/**
* This callback is called when an item is selected.
*/
onSelect: PropTypes.func,
/**
* Disables any interactions and styles the combobox with a disabled style.
*/
disabled: PropTypes.bool,
/**
* A placeholder value shown inside the combobox.
*/
label: PropTypes.string,
/**
* An array of values to select from.
*/
list: PropTypes.array.isRequired, // eslint-disable-line react/forbid-prop-types
/**
* The name of the property on the list objects that uniquely identifies an
* item.
*/
listKey: PropTypes.string.isRequired,
/**
* The name of the property on the list objects that is shown as its name.
*/
listValue: PropTypes.string.isRequired,
/**
* A classname string that will be applied to the Button component and the
* overlay.
*/
className: PropTypes.string,
/**
* The default value of the combobox. This does not work in combination with
* the `label` or `value` props.
*/
defaultValue: PropTypes.string,
/**
* Wether to stop the propagation of click events.
*/
stopPropagation: PropTypes.bool,
/**
* The parent component of the combobox overlay.
*/
parent: PropTypes.oneOfType([PropTypes.func, PropTypes.node]),
/**
* A React style object that will be applied to the Button component and the
* overlay.
*/
style: PropTypes.objectOf(
PropTypes.oneOfType([PropTypes.string, PropTypes.number])
),
/**
* The value of the combobox. This does not work in combination with the `
* label`-prop.
*/
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
};
ComboBox.displayName = 'ComboBox';