-
Notifications
You must be signed in to change notification settings - Fork 11.7k
/
Seg.tsx
225 lines (202 loc) · 6.24 KB
/
Seg.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
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 { cx, css } from '@emotion/css';
import debouncePromise from 'debounce-promise';
import React, { useState, useEffect } from 'react';
import { useAsyncFn } from 'react-use';
import { SelectableValue } from '@grafana/data';
import { InlineLabel, Select, AsyncSelect, Input } from '@grafana/ui';
import { useShadowedState } from '../useShadowedState';
// this file is a simpler version of `grafana-ui / SegmentAsync.tsx`
// with some changes:
// 1. click-outside does not select the value. i think it's better to be explicit here.
// 2. we set a min-width on the select-element to handle cases where the `value`
// is very short, like "x", and then you click on it and the select opens,
// and it tries to be as short as "x" and it does not work well.
// NOTE: maybe these changes could be migrated into the SegmentAsync later
type SelVal = SelectableValue<string>;
// when allowCustomValue is true, there is no way to enforce the selectableValue
// enum-type, so i just go with `string`
type LoadOptions = (filter: string) => Promise<SelVal[]>;
type Props = {
value: string;
buttonClassName?: string;
loadOptions?: LoadOptions;
// if filterByLoadOptions is false,
// loadOptions is only executed once,
// when the select-box opens,
// and as you write, the list gets filtered
// by the select-box.
// if filterByLoadOptions is true,
// as you write the loadOptions is executed again and again,
// and it is relied on to filter the results.
filterByLoadOptions?: boolean;
onChange: (v: SelVal) => void;
allowCustomValue?: boolean;
};
const selectClass = css({
minWidth: '160px',
});
type SelProps = {
loadOptions: LoadOptions;
filterByLoadOptions?: boolean;
onClose: () => void;
onChange: (v: SelVal) => void;
allowCustomValue?: boolean;
};
type SelReloadProps = {
loadOptions: (filter: string) => Promise<SelVal[]>;
onClose: () => void;
onChange: (v: SelVal) => void;
allowCustomValue?: boolean;
};
// when a custom value is written into a select-box,
// by default the new value is prefixed with "Create:",
// and that sounds confusing because here we do not create
// anything. we change this to just be the entered string.
const formatCreateLabel = (v: string) => v;
const SelReload = ({ loadOptions, allowCustomValue, onChange, onClose }: SelReloadProps): JSX.Element => {
// here we rely on the fact that writing text into the <AsyncSelect/>
// does not cause a re-render of the current react component.
// this way there is only a single render-call,
// so there is only a single `debouncedLoadOptions`.
// if we want ot make this "re-render safe,
// we will have to put the debounced call into an useRef,
// and probably have an useEffect
const debouncedLoadOptions = debouncePromise(loadOptions, 1000, { leading: true });
return (
<div className={selectClass}>
<AsyncSelect
formatCreateLabel={formatCreateLabel}
defaultOptions
autoFocus
isOpen
onCloseMenu={onClose}
allowCustomValue={allowCustomValue}
loadOptions={debouncedLoadOptions}
onChange={onChange}
/>
</div>
);
};
type SelSingleLoadProps = {
loadOptions: (filter: string) => Promise<SelVal[]>;
onClose: () => void;
onChange: (v: SelVal) => void;
allowCustomValue?: boolean;
};
const SelSingleLoad = ({ loadOptions, allowCustomValue, onChange, onClose }: SelSingleLoadProps): JSX.Element => {
const [loadState, doLoad] = useAsyncFn(loadOptions, [loadOptions]);
useEffect(() => {
doLoad('');
}, [doLoad, loadOptions]);
return (
<div className={selectClass}>
<Select
isLoading={loadState.loading}
formatCreateLabel={formatCreateLabel}
autoFocus
isOpen={!loadState.loading}
onCloseMenu={onClose}
allowCustomValue={allowCustomValue}
options={loadState.value ?? []}
onChange={onChange}
/>
</div>
);
};
const Sel = ({ loadOptions, filterByLoadOptions, allowCustomValue, onChange, onClose }: SelProps): JSX.Element => {
// unfortunately <Segment/> and <SegmentAsync/> have somewhat different behavior,
// so the simplest approach was to just create two separate wrapper-components
return filterByLoadOptions ? (
<SelReload loadOptions={loadOptions} allowCustomValue={allowCustomValue} onChange={onChange} onClose={onClose} />
) : (
<SelSingleLoad
loadOptions={loadOptions}
allowCustomValue={allowCustomValue}
onChange={onChange}
onClose={onClose}
/>
);
};
type InpProps = {
initialValue: string;
onChange: (newVal: string) => void;
onClose: () => void;
};
const Inp = ({ initialValue, onChange, onClose }: InpProps): JSX.Element => {
const [currentValue, setCurrentValue] = useShadowedState(initialValue);
return (
<Input
autoFocus
type="text"
spellCheck={false}
onBlur={onClose}
onKeyDown={(e) => {
if (e.key === 'Enter') {
onChange(currentValue);
}
}}
onChange={(e) => {
setCurrentValue(e.currentTarget.value);
}}
value={currentValue}
/>
);
};
const defaultButtonClass = css({
width: 'auto',
cursor: 'pointer',
});
export const Seg = ({
value,
buttonClassName,
loadOptions,
filterByLoadOptions,
allowCustomValue,
onChange,
}: Props): JSX.Element => {
const [isOpen, setOpen] = useState(false);
if (!isOpen) {
const className = cx(defaultButtonClass, buttonClassName);
return (
<InlineLabel
as="button"
className={className}
onClick={() => {
setOpen(true);
}}
>
{value}
</InlineLabel>
);
} else {
if (loadOptions !== undefined) {
return (
<Sel
loadOptions={loadOptions}
filterByLoadOptions={filterByLoadOptions ?? false}
allowCustomValue={allowCustomValue}
onChange={(v) => {
setOpen(false);
onChange(v);
}}
onClose={() => {
setOpen(false);
}}
/>
);
} else {
return (
<Inp
initialValue={value}
onClose={() => {
setOpen(false);
}}
onChange={(v) => {
setOpen(false);
onChange({ value: v, label: v });
}}
/>
);
}
}
};