This repository was archived by the owner on Mar 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
225 lines (209 loc) · 6.68 KB
/
index.js
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import { css } from '@emotion/react';
import {
Combobox as ReachCombobox,
ComboboxInput as ReachComboboxInput,
ComboboxPopover as ReachComboboxPopover,
ComboboxList as ReachComboboxList,
} from '@reach/combobox';
import PropTypes from 'prop-types';
import React, { Children, useCallback, useMemo, useRef, useState, useEffect } from 'react';
import { interfaceUI, toUnits } from '../../../styles';
import { useTheme } from '../../../themes';
import List from '../List';
import TextField from '../TextField';
import Option from './Option';
import Heading from './Heading';
export const ComboBox = (props) => {
const {
autocomplete,
children,
disabled,
error,
hint,
id,
label,
labelId,
noMargin,
onBlur,
onChange,
onFocus,
openOnFocus,
optional,
placeholder,
unstyled,
value,
...otherProps
} = props;
const [focus, setFocus] = useState(otherProps.autofocus);
const [popoverOpen, setPopoverOpen] = useState(otherProps.autofocus);
const popoverRef = useRef();
const inputRef = useRef();
// eslint-disable-next-line react-hooks/exhaustive-deps
useEffect(() => {
if (popoverRef.current) {
setPopoverOpen(!popoverRef.current.hidden);
}
});
const theme = useTheme();
// Memoise our handlers as they don't need to be re-created on every render.
const onBlurHandler = useCallback(
(...args) => {
setFocus(false);
if (onBlur) {
onBlur(...args);
}
},
[onBlur],
);
const onFocusHandler = useCallback(
(...args) => {
setFocus(true);
if (onFocus) {
onFocus(...args);
}
},
[onFocus],
);
// Create an array of all our options.
const options = useMemo(() => Children.toArray(children).filter((child) => child), [children]);
return (
<div
css={css`
:root {
--reach-combobox: 1;
}
[data-reach-combobox-list] {
margin: 0;
padding: 0;
user-select: none;
}
[data-reach-combobox-option] {
margin: 0;
padding: 0;
}
[data-suggested-value] {
font-weight: bold;
}
`}
>
<ReachCombobox aria-label={label} openOnFocus={openOnFocus} {...otherProps}>
<ReachComboboxInput
as={TextField}
disabled={disabled}
error={error}
placeholder={placeholder}
label={label}
labelId={labelId}
hint={hint}
noMargin={noMargin}
optional={optional}
unstyled={unstyled}
onBlur={onBlurHandler}
onChange={onChange}
onFocus={onFocusHandler}
ref={inputRef}
signifierIcon={autocomplete ? 'search' : undefined}
actionIcon={focus && popoverOpen ? 'chevron-up' : 'chevron-down'}
actionIconOnClick={
focus && popoverOpen
? () => {
if (popoverRef.current) {
popoverRef.current.hidden = true;
}
}
: undefined
}
actionIconTitle={popoverOpen ? 'Hide options' : 'Show options'}
autocomplete={autocomplete}
value={value}
/>
<ReachComboboxPopover ref={popoverRef}>
<ReachComboboxList
as={List}
// persistSelection={autocomplete}
unstyled
css={css`
${interfaceUI.medium(theme)};
background: ${theme.colors.buttons.neutral};
border: 2px solid ${theme.colors.text.default};
border-top: 0;
color: ${theme.colors.text.default};
list-style-type: none;
margin: 0;
margin-top: -2px;
max-height: 60vh;
overflow-y: scroll;
overflow: hidden;
padding: 0;
padding-top: ${toUnits(theme.spacing.padding.small)};
transition: height 200ms;
z-index: 100;
`}
>
{options}
</ReachComboboxList>
</ReachComboboxPopover>
</ReachCombobox>
</div>
);
};
ComboBox.defaultProps = {
autocomplete: true,
children: undefined,
disabled: false,
error: undefined,
hint: undefined,
id: undefined,
labelId: undefined,
noMargin: false,
openOnFocus: true,
onBlur: undefined,
onChange: undefined,
onFocus: undefined,
optional: false,
placeholder: undefined,
unstyled: false,
value: undefined,
};
ComboBox.propTypes = {
/** A component to place at the bottom of the option list, */
/** Determines whether the component shows an autocomplete interface or not. When set to true, this component will behave more like a `select` element. */
autocomplete: PropTypes.bool,
/** @ignore */
children: PropTypes.node,
/** @ignore */
onBlur: PropTypes.func,
/** @ignore */
onFocus: PropTypes.func,
/** Disables this input; this applies a disabled style and disables user input/interaction with this element. This is useful if you have inputs that are conditionally allowed based on other states in your UI. */
disabled: PropTypes.bool,
/** An error message (either a simple string or a component) used to output an error message related to this component's value. If provided, an `aria-errormessage` will be set on the input component that will tell users of assistive technology the error message relates to this input. */
error: PropTypes.node,
/** HTML `id` attribute of the `input` element. Used for both the input's `id` attribute and the `<label>` `for` attribute. */
id: PropTypes.string,
/** Additional context to help users understand the purpose of the input. */
hint: PropTypes.node,
/** Visible text that serves to introduce the input. */
label: PropTypes.node.isRequired,
/** HTML `id` attribute for the `<label>` tag used to label the text input component. */
labelId: PropTypes.string,
/** Remove any outer margins from component. */
noMargin: PropTypes.bool,
/** `onChange` handler for the `TextField` component. */
onChange: PropTypes.func,
/** Open this combobox's list of options when it is focused. */
openOnFocus: PropTypes.bool,
/** Used to mark this input as optional. Will output text in `theme.components.ComboBox.optionalMessage`, if set. */
optional: PropTypes.bool,
/** Placeholder text, used only for examples. */
placeholder: PropTypes.string,
/* @ignore Don't output any CSS styles. */
unstyled: PropTypes.bool,
/* If supplied, this will be the selected value of the ComboBox. */
value: PropTypes.string,
};
// Export child components.
ComboBox.Option = Option;
ComboBox.Heading = Heading;
export const { defaultProps, propTypes } = ComboBox;
export default ComboBox;