@vev/silke 1.1.3 → 1.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +26 -0
- package/dist/index.js +37 -35
- package/dist/index.js.LEGAL.txt +12 -0
- package/dist/styles.css +3673 -3509
- package/dist/types/components/index.d.ts +2 -0
- package/dist/types/components/silke-action-list/index.d.ts +1 -0
- package/dist/types/components/silke-action-list/silke-action-list.d.ts +11 -0
- package/dist/types/components/silke-autocomplete-field/autocomplete-types.d.ts +4 -0
- package/dist/types/components/silke-autocomplete-field/index.d.ts +1 -0
- package/dist/types/components/silke-autocomplete-field/silke-autocomplete-field-native.d.ts +60 -0
- package/dist/types/components/silke-autocomplete-field/silke-autocomplete-field.d.ts +3 -0
- package/dist/types/components/silke-autocomplete-field/silke-autocomplete-popup-native.d.ts +15 -0
- package/dist/types/components/silke-conditions-input/utils.d.ts +1 -1
- package/dist/types/components/silke-popover-native/index.d.ts +1 -0
- package/dist/types/components/silke-popover-native/silke-popover-native.d.ts +35 -0
- package/dist/types/components/silke-popover-native/types.d.ts +6 -0
- package/dist/types/components/silke-popover-native/use-popover-positioning.d.ts +9 -0
- package/dist/types/components/silke-select-field/index.d.ts +1 -0
- package/dist/types/components/silke-select-field/silke-select-field-native.d.ts +4 -0
- package/dist/types/icons.d.ts +2 -0
- package/package.json +3 -2
- package/src/components/index.ts +2 -0
- package/src/components/silke-action-list/index.ts +1 -0
- package/src/components/silke-action-list/silke-action-list.mdx +48 -0
- package/src/components/silke-action-list/silke-action-list.scss +21 -0
- package/src/components/silke-action-list/silke-action-list.tsx +85 -0
- package/src/components/silke-autocomplete-field/autocomplete-types.ts +4 -0
- package/src/components/silke-autocomplete-field/index.ts +1 -0
- package/src/components/silke-autocomplete-field/silke-autocomplete-field-native.tsx +346 -0
- package/src/components/silke-autocomplete-field/silke-autocomplete-field.mdx +39 -0
- package/src/components/silke-autocomplete-field/silke-autocomplete-field.tsx +3 -0
- package/src/components/silke-autocomplete-field/silke-autocomplete-popup-native.tsx +541 -0
- package/src/components/silke-autocomplete-field/silke-autocomplete-popup.tsx +54 -22
- package/src/components/silke-button/silke-button.scss +3 -3
- package/src/components/silke-form-schema/silke-schema-field.tsx +1 -1
- package/src/components/silke-popover-native/index.ts +1 -0
- package/src/components/silke-popover-native/silke-popover-native.mdx +207 -0
- package/src/components/silke-popover-native/silke-popover-native.scss +112 -0
- package/src/components/silke-popover-native/silke-popover-native.tsx +172 -0
- package/src/components/silke-popover-native/types.ts +21 -0
- package/src/components/silke-popover-native/use-popover-positioning.ts +159 -0
- package/src/components/silke-select-field/index.ts +1 -0
- package/src/components/silke-select-field/silke-select-field-native.tsx +17 -0
- package/src/components/silke-select-field/silke-select-field.mdx +65 -0
- package/src/components/silke-text-field/silke-text-field.tsx +2 -1
- package/src/icons.tsx +2 -0
- package/tsconfig.build.tsbuildinfo +1 -1
|
@@ -0,0 +1,541 @@
|
|
|
1
|
+
import { getSearchReg } from '@vev/utils';
|
|
2
|
+
import { isEqual, isUndefined } from 'lodash';
|
|
3
|
+
import React, {
|
|
4
|
+
Children,
|
|
5
|
+
ReactNode,
|
|
6
|
+
useCallback,
|
|
7
|
+
useEffect,
|
|
8
|
+
useMemo,
|
|
9
|
+
useRef,
|
|
10
|
+
useState,
|
|
11
|
+
} from 'react';
|
|
12
|
+
import { useFormField, useInterval, useParentScroll, useWindowResize } from '../../hooks';
|
|
13
|
+
import { SilkeBox } from '../silke-box';
|
|
14
|
+
import { SilkeDivider } from '../silke-divider';
|
|
15
|
+
import { FormValidator } from '../silke-form';
|
|
16
|
+
import { SilkeIcon } from '../silke-icon';
|
|
17
|
+
import { PopoverOrigin } from '../silke-popover';
|
|
18
|
+
import { SilkePopoverNative } from '../silke-popover-native';
|
|
19
|
+
import type { PopoverPlacement } from '../silke-popover-native/types';
|
|
20
|
+
import { SilkeTextSmall } from '../silke-text';
|
|
21
|
+
import { SilkeTextField, SilkeTextFieldItem } from '../silke-text-field';
|
|
22
|
+
import { useTextFieldContext } from '../silke-text-field/text-field-context';
|
|
23
|
+
import AutocompleteItem from './autocomplete-item';
|
|
24
|
+
import { Data, Entry } from './autocomplete-types';
|
|
25
|
+
import styles from './silke-autocomplete-field.scss';
|
|
26
|
+
import { SilkeAutocompleteFieldProps } from './silke-autocomplete-field';
|
|
27
|
+
import { SilkeButton } from '../silke-button';
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Converts PopoverOrigin to PopoverPlacement
|
|
31
|
+
*/
|
|
32
|
+
function convertOriginToPlacement(
|
|
33
|
+
anchorOrigin?: PopoverOrigin,
|
|
34
|
+
targetOrigin?: PopoverOrigin,
|
|
35
|
+
): PopoverPlacement {
|
|
36
|
+
// Default to bottom-start (equivalent to bottom-left anchor, top-left target)
|
|
37
|
+
if (!anchorOrigin && !targetOrigin) return 'bottom-start';
|
|
38
|
+
|
|
39
|
+
// Map common combinations
|
|
40
|
+
if (anchorOrigin?.startsWith('bottom-')) {
|
|
41
|
+
if (anchorOrigin === 'bottom-left') return 'bottom-start';
|
|
42
|
+
if (anchorOrigin === 'bottom-center') return 'bottom';
|
|
43
|
+
if (anchorOrigin === 'bottom-right') return 'bottom-end';
|
|
44
|
+
}
|
|
45
|
+
if (anchorOrigin?.startsWith('top-')) {
|
|
46
|
+
if (anchorOrigin === 'top-left') return 'top-start';
|
|
47
|
+
if (anchorOrigin === 'top-center') return 'top';
|
|
48
|
+
if (anchorOrigin === 'top-right') return 'top-end';
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return 'bottom-start';
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Props for autocomplete popup
|
|
56
|
+
*/
|
|
57
|
+
type SilkeAutocompletePopupNativeProps<T = any> = SilkeAutocompleteFieldProps<T> & {
|
|
58
|
+
containerRef?: React.RefObject<HTMLDivElement>;
|
|
59
|
+
searchPlaceholder?: string;
|
|
60
|
+
onRequestClose?: () => void;
|
|
61
|
+
hide?: boolean;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const autocompleteValidator: FormValidator<SilkeAutocompletePopupNativeProps> = (props, value) => {
|
|
65
|
+
if (props.multiple) {
|
|
66
|
+
const len = value?.length || 0;
|
|
67
|
+
if (len > (props.maxItems || 1000)) return `To many item, max ${props.maxItems}`;
|
|
68
|
+
if (len < (props.minItems || 0)) return `To few item, min ${props.minItems}`;
|
|
69
|
+
}
|
|
70
|
+
if (props.required && !value && value !== 0) return 'Required';
|
|
71
|
+
|
|
72
|
+
return undefined;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Checks if any of an `Entry`'s fields match the query
|
|
77
|
+
*/
|
|
78
|
+
const filter = ({ label, subLabel, value }: Entry<any>, search: RegExp): boolean =>
|
|
79
|
+
(value && value.toString && search.test(value.toString() + '')) ||
|
|
80
|
+
(label ? search.test(label) : false) ||
|
|
81
|
+
(subLabel ? search.test(subLabel) : false);
|
|
82
|
+
|
|
83
|
+
const normalizeString = (str?: string | number): string =>
|
|
84
|
+
str ? str.toString().toLowerCase() : '';
|
|
85
|
+
|
|
86
|
+
const filterString = ({ label, subLabel, value }: Entry<any>, searchString: string): boolean => {
|
|
87
|
+
const normalizedSearchString = normalizeString(searchString);
|
|
88
|
+
return (
|
|
89
|
+
normalizeString(label).startsWith(normalizedSearchString) ||
|
|
90
|
+
normalizeString(value).startsWith(normalizedSearchString) ||
|
|
91
|
+
normalizeString(subLabel).startsWith(normalizedSearchString)
|
|
92
|
+
);
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const sorter: SilkeAutocompletePopupNativeProps<any>['sort'] = () => 0;
|
|
96
|
+
|
|
97
|
+
const putMatchingItemsOnTop = (filteredItems: Entry<any>[], searchString: string): Entry<any>[] => {
|
|
98
|
+
const itemsWithMatch = filteredItems.filter((item) => filterString(item, searchString));
|
|
99
|
+
const itemsWithNoMatch = filteredItems.filter((item) => !filterString(item, searchString));
|
|
100
|
+
return [...itemsWithMatch, ...itemsWithNoMatch];
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
export const useMatchingItemsOnTop = (
|
|
104
|
+
filteredItems: Entry<any>[],
|
|
105
|
+
searchString?: string,
|
|
106
|
+
selectedLabel?: string,
|
|
107
|
+
): Entry<any>[] => {
|
|
108
|
+
return useMemo(
|
|
109
|
+
() =>
|
|
110
|
+
!searchString || searchString === selectedLabel
|
|
111
|
+
? filteredItems
|
|
112
|
+
: putMatchingItemsOnTop(filteredItems, searchString),
|
|
113
|
+
[searchString, filteredItems, selectedLabel],
|
|
114
|
+
);
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
export function SilkeAutocompletePopupNative<T>(props: SilkeAutocompletePopupNativeProps<T>) {
|
|
118
|
+
const {
|
|
119
|
+
disableSearch,
|
|
120
|
+
disableAddFromList,
|
|
121
|
+
selectedVariableKey,
|
|
122
|
+
containerRef,
|
|
123
|
+
items,
|
|
124
|
+
onAdd,
|
|
125
|
+
renderItem: ItemRender,
|
|
126
|
+
sort = sorter,
|
|
127
|
+
label,
|
|
128
|
+
anchorOrigin,
|
|
129
|
+
targetOrigin,
|
|
130
|
+
maxDisplayedItems = 150,
|
|
131
|
+
dropDownMinWidth,
|
|
132
|
+
dropDownMaxWidth,
|
|
133
|
+
onSearch,
|
|
134
|
+
onClose,
|
|
135
|
+
onRequestClose,
|
|
136
|
+
buttonRight,
|
|
137
|
+
searchPlaceholder,
|
|
138
|
+
hide = false,
|
|
139
|
+
...form
|
|
140
|
+
} = useFormField(props, autocompleteValidator);
|
|
141
|
+
const context = useTextFieldContext();
|
|
142
|
+
const size = form.size || context.size || 'base';
|
|
143
|
+
|
|
144
|
+
const inputField = useRef<HTMLDivElement>();
|
|
145
|
+
const isSelected = (value: T): boolean => {
|
|
146
|
+
if (form.multiple) {
|
|
147
|
+
const values = Array.isArray(form.value) ? form.value : [];
|
|
148
|
+
for (const v of values) {
|
|
149
|
+
if (isEqual(v, value) || isEqual(value, selectedVariableKey)) return true;
|
|
150
|
+
}
|
|
151
|
+
return false;
|
|
152
|
+
}
|
|
153
|
+
return isEqual(value, form.value) || isEqual(value, selectedVariableKey);
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
function getIndexInDir(
|
|
157
|
+
currentIndex: number,
|
|
158
|
+
listLength: number,
|
|
159
|
+
dir: 'ArrowUp' | 'ArrowDown',
|
|
160
|
+
): number {
|
|
161
|
+
if (dir === 'ArrowUp') {
|
|
162
|
+
return currentIndex === 0 ? listLength - 1 : currentIndex - 1;
|
|
163
|
+
}
|
|
164
|
+
return currentIndex === listLength - 1 ? 0 : currentIndex + 1;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const [selectedLabel] = useMemo((): [label?: string] => {
|
|
168
|
+
if (form.multiple) return [];
|
|
169
|
+
const entry = items.find((entry) => !entry.divider && isEqual(entry.value, form.value));
|
|
170
|
+
return [entry?.label || (form.value && form.value.toString()) || ''];
|
|
171
|
+
}, [items, form.multiple, form.value]);
|
|
172
|
+
|
|
173
|
+
const [searchText, _setSearchText] = useState<undefined | string>();
|
|
174
|
+
const [prevSearchText, setPrevSearchText] = useState(searchText);
|
|
175
|
+
const inputRef = useRef<HTMLInputElement>();
|
|
176
|
+
const dropdownRef = useRef<HTMLElement>();
|
|
177
|
+
const [highlight, setHighlight] = useState<undefined | number>(undefined);
|
|
178
|
+
|
|
179
|
+
const before: ReactNode[] = [];
|
|
180
|
+
const after: ReactNode[] = [];
|
|
181
|
+
Children.forEach(form.children, (child) => {
|
|
182
|
+
if (React.isValidElement(child) && child.type === SilkeTextFieldItem && child.props.before) {
|
|
183
|
+
before.push(child);
|
|
184
|
+
} else {
|
|
185
|
+
after.push(child);
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
const setSearchText = useCallback((value: string | undefined) => {
|
|
190
|
+
_setSearchText((previousValue) => {
|
|
191
|
+
setPrevSearchText(previousValue);
|
|
192
|
+
return value;
|
|
193
|
+
});
|
|
194
|
+
}, []);
|
|
195
|
+
|
|
196
|
+
const resetSearchText = useCallback(() => {
|
|
197
|
+
setSearchText('');
|
|
198
|
+
}, []);
|
|
199
|
+
|
|
200
|
+
useEffect(() => {
|
|
201
|
+
if (!form.multiple && !isUndefined(form.value)) {
|
|
202
|
+
resetSearchText();
|
|
203
|
+
}
|
|
204
|
+
}, [form.multiple, resetSearchText, form.value]);
|
|
205
|
+
|
|
206
|
+
useEffect(() => {
|
|
207
|
+
if (highlight !== undefined) {
|
|
208
|
+
dropdownRef.current?.children.item(highlight)?.scrollIntoView(false);
|
|
209
|
+
}
|
|
210
|
+
}, [highlight]);
|
|
211
|
+
|
|
212
|
+
const handleChange = (nuValue: T | undefined) => {
|
|
213
|
+
if (form.multiple) {
|
|
214
|
+
form.onChange([...(Array.isArray(form.value) ? form.value : []), nuValue]);
|
|
215
|
+
} else {
|
|
216
|
+
form.onChange(nuValue as any);
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
const handleRemoveSingle = (remove: T) => {
|
|
221
|
+
if (form.multiple && form.value) {
|
|
222
|
+
const value = Array.isArray(form.value) ? form.value.filter((v: T) => remove !== v) : [];
|
|
223
|
+
if (!isEqual(value, form.value)) form.onChange(value);
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
const searchReg = useMemo(() => {
|
|
228
|
+
return searchText && searchText !== selectedLabel ? getSearchReg(searchText) : undefined;
|
|
229
|
+
}, [searchText, selectedLabel]);
|
|
230
|
+
|
|
231
|
+
const filteredItems = useMemo(() => {
|
|
232
|
+
return searchReg ? items.filter((entry) => filter(entry, searchReg)) : items;
|
|
233
|
+
}, [searchReg, items]);
|
|
234
|
+
|
|
235
|
+
const itemsWithMatchingOnTop = useMatchingItemsOnTop(filteredItems, searchText, selectedLabel);
|
|
236
|
+
|
|
237
|
+
const [displayedItems, isHidingResults]: [Entry<T>[], boolean] = useMemo(() => {
|
|
238
|
+
return [
|
|
239
|
+
itemsWithMatchingOnTop.slice(0, maxDisplayedItems),
|
|
240
|
+
itemsWithMatchingOnTop.length > maxDisplayedItems,
|
|
241
|
+
];
|
|
242
|
+
}, [itemsWithMatchingOnTop, maxDisplayedItems]);
|
|
243
|
+
|
|
244
|
+
const filteredDataEntries = displayedItems.filter((entry) => !entry.divider) as Data<T>[];
|
|
245
|
+
|
|
246
|
+
const isItemSelected = (item: string): boolean => {
|
|
247
|
+
return (
|
|
248
|
+
Array.isArray(form.value) &&
|
|
249
|
+
form.value.map((s) => String(s).toUpperCase()).includes(item.trim().toUpperCase())
|
|
250
|
+
);
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
const findItem = (item: string) => {
|
|
254
|
+
return filteredItems.find((listItem) => {
|
|
255
|
+
return (listItem.label || listItem.value).toUpperCase() === item.toUpperCase().trim();
|
|
256
|
+
});
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
const [overridePlacement, setOverridePlacement] = useState<PopoverPlacement | undefined>();
|
|
260
|
+
|
|
261
|
+
const updatePlacement = () => {
|
|
262
|
+
if (containerRef && containerRef.current) {
|
|
263
|
+
const rect = containerRef.current.getBoundingClientRect();
|
|
264
|
+
const height = window.innerHeight;
|
|
265
|
+
const spaceUnderElement = height - rect.top - rect.height;
|
|
266
|
+
if (spaceUnderElement < 300) {
|
|
267
|
+
setOverridePlacement('top-start');
|
|
268
|
+
} else {
|
|
269
|
+
setOverridePlacement(undefined);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
useInterval(updatePlacement, 1000);
|
|
275
|
+
useParentScroll(containerRef, updatePlacement);
|
|
276
|
+
useWindowResize(updatePlacement);
|
|
277
|
+
|
|
278
|
+
const getIndexOfFirstSelectedItem = () => {
|
|
279
|
+
for (let i = 0; i < filteredItems.length; i++) {
|
|
280
|
+
if (isSelected(filteredItems[i].value)) {
|
|
281
|
+
return i;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
return 0;
|
|
285
|
+
};
|
|
286
|
+
|
|
287
|
+
useEffect(() => {
|
|
288
|
+
if (highlight === undefined) {
|
|
289
|
+
const indexOfFirstSelectedItem = getIndexOfFirstSelectedItem();
|
|
290
|
+
setHighlight(indexOfFirstSelectedItem);
|
|
291
|
+
}
|
|
292
|
+
}, []);
|
|
293
|
+
|
|
294
|
+
const handleSelect = (item?: Entry<T>) => {
|
|
295
|
+
inputField?.current?.focus();
|
|
296
|
+
if (!item) {
|
|
297
|
+
resetSearchText();
|
|
298
|
+
} else if (!item.divider) {
|
|
299
|
+
if (form.multiple) {
|
|
300
|
+
if (isSelected(item.value)) handleRemoveSingle(item.value);
|
|
301
|
+
else handleChange(item.value);
|
|
302
|
+
resetSearchText();
|
|
303
|
+
} else {
|
|
304
|
+
resetSearchText();
|
|
305
|
+
handleChange(item.value);
|
|
306
|
+
onRequestClose && onRequestClose();
|
|
307
|
+
onClose && onClose();
|
|
308
|
+
setHighlight(undefined);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
const handleKeyUp = (e: React.KeyboardEvent) => {
|
|
314
|
+
switch (e.key) {
|
|
315
|
+
case 'Backspace': {
|
|
316
|
+
if (prevSearchText?.length === 1) {
|
|
317
|
+
setPrevSearchText('');
|
|
318
|
+
} else if (form.multiple && form.value && !searchText) {
|
|
319
|
+
if (Array.isArray(form.value)) form.onChange(form.value.slice(0, -1));
|
|
320
|
+
}
|
|
321
|
+
break;
|
|
322
|
+
}
|
|
323
|
+
case 'Enter': {
|
|
324
|
+
if (form.multiple && onAdd && searchText) {
|
|
325
|
+
if (!isItemSelected(searchText)) {
|
|
326
|
+
const item = findItem(searchText);
|
|
327
|
+
if (!item) {
|
|
328
|
+
onAdd(searchText);
|
|
329
|
+
} else {
|
|
330
|
+
handleChange(item.value);
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
setSearchText('');
|
|
334
|
+
setPrevSearchText('');
|
|
335
|
+
} else {
|
|
336
|
+
const item = highlight !== undefined && filteredDataEntries[highlight];
|
|
337
|
+
if (item) {
|
|
338
|
+
handleSelect(item);
|
|
339
|
+
} else {
|
|
340
|
+
if (onAdd && searchText) {
|
|
341
|
+
onAdd(searchText);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
inputField.current?.focus();
|
|
346
|
+
onRequestClose && onRequestClose();
|
|
347
|
+
onClose && onClose();
|
|
348
|
+
break;
|
|
349
|
+
}
|
|
350
|
+
case 'ArrowUp':
|
|
351
|
+
case 'ArrowDown': {
|
|
352
|
+
e.preventDefault();
|
|
353
|
+
e.stopPropagation();
|
|
354
|
+
const { key } = e;
|
|
355
|
+
setHighlight((value) => getIndexInDir(value || 0, filteredDataEntries.length, key));
|
|
356
|
+
break;
|
|
357
|
+
}
|
|
358
|
+
case 'Escape': {
|
|
359
|
+
inputField.current?.focus();
|
|
360
|
+
onRequestClose && onRequestClose();
|
|
361
|
+
onClose && onClose();
|
|
362
|
+
break;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
};
|
|
366
|
+
|
|
367
|
+
const dataCy = props['data-cy'] || undefined;
|
|
368
|
+
const iconFontSize = size === 's' ? '12px' : undefined;
|
|
369
|
+
|
|
370
|
+
const matchAnchorWidth =
|
|
371
|
+
!dropDownMinWidth ||
|
|
372
|
+
(!!containerRef?.current && containerRef?.current?.clientWidth > dropDownMinWidth);
|
|
373
|
+
|
|
374
|
+
const placement = overridePlacement || convertOriginToPlacement(anchorOrigin, targetOrigin);
|
|
375
|
+
|
|
376
|
+
const handleRequestClose = () => {
|
|
377
|
+
onRequestClose && onRequestClose();
|
|
378
|
+
onClose && onClose();
|
|
379
|
+
};
|
|
380
|
+
|
|
381
|
+
return (
|
|
382
|
+
<SilkePopoverNative
|
|
383
|
+
offsetY={1}
|
|
384
|
+
anchor={containerRef}
|
|
385
|
+
placement={placement}
|
|
386
|
+
matchWidth={matchAnchorWidth}
|
|
387
|
+
hideBackdrop
|
|
388
|
+
hide={hide}
|
|
389
|
+
animate
|
|
390
|
+
onRequestClose={handleRequestClose}
|
|
391
|
+
>
|
|
392
|
+
<SilkeBox
|
|
393
|
+
role="listbox"
|
|
394
|
+
rounded={size === 's' ? 'tiny' : 'small'}
|
|
395
|
+
shadow="level3"
|
|
396
|
+
data-cy={dataCy ? dataCy + '-dropdown' : undefined}
|
|
397
|
+
bg="neutral-20"
|
|
398
|
+
column
|
|
399
|
+
style={{
|
|
400
|
+
maxHeight: 300,
|
|
401
|
+
minWidth: dropDownMinWidth,
|
|
402
|
+
maxWidth: dropDownMaxWidth,
|
|
403
|
+
}}
|
|
404
|
+
gap={disableSearch ? undefined : 'xs'}
|
|
405
|
+
vPad="xs"
|
|
406
|
+
value={form.value as any}
|
|
407
|
+
onKeyUp={handleKeyUp}
|
|
408
|
+
ref={dropdownRef}
|
|
409
|
+
>
|
|
410
|
+
<SilkeBox hPad="xs">
|
|
411
|
+
<SilkeTextField
|
|
412
|
+
gap={size === 's' ? 'xxs' : 'xs'}
|
|
413
|
+
inline="label-inside"
|
|
414
|
+
label={<SilkeIcon icon="search" />}
|
|
415
|
+
aria-label={'Search for ' + label}
|
|
416
|
+
autoFocus
|
|
417
|
+
size={size}
|
|
418
|
+
className={disableSearch ? styles.hiddenSearch : undefined}
|
|
419
|
+
placeholder={searchPlaceholder || 'Search'}
|
|
420
|
+
kind="ghost"
|
|
421
|
+
onFocus={(e) => {
|
|
422
|
+
inputRef.current = e.currentTarget as HTMLInputElement;
|
|
423
|
+
if (props.onFocus) props.onFocus(e);
|
|
424
|
+
}}
|
|
425
|
+
onBlur={(e) => {
|
|
426
|
+
if (props.onBlur) props.onBlur(e);
|
|
427
|
+
}}
|
|
428
|
+
onChange={(text: string) => {
|
|
429
|
+
onSearch?.(text);
|
|
430
|
+
setHighlight(0);
|
|
431
|
+
setSearchText(text);
|
|
432
|
+
}}
|
|
433
|
+
onKeyUp={handleKeyUp}
|
|
434
|
+
onKeyDown={(e: React.KeyboardEvent) => {
|
|
435
|
+
if (e.key === 'Tab') {
|
|
436
|
+
inputField.current?.focus();
|
|
437
|
+
}
|
|
438
|
+
if (['Enter', 'ArrowUp', 'ArrowDown', 'Escape'].includes(e.key)) {
|
|
439
|
+
e.preventDefault();
|
|
440
|
+
e.stopPropagation();
|
|
441
|
+
}
|
|
442
|
+
}}
|
|
443
|
+
value={searchText}
|
|
444
|
+
>
|
|
445
|
+
{props.showAddIcon && onAdd && (
|
|
446
|
+
<SilkeTextFieldItem hPad="xs">
|
|
447
|
+
<SilkeButton icon="add" kind="ghost" size="s" onClick={() => onAdd('variable')} />
|
|
448
|
+
</SilkeTextFieldItem>
|
|
449
|
+
)}
|
|
450
|
+
{buttonRight && <div onClick={() => onRequestClose?.()}>{buttonRight}</div>}
|
|
451
|
+
</SilkeTextField>
|
|
452
|
+
</SilkeBox>
|
|
453
|
+
{!disableSearch && <SilkeDivider />}
|
|
454
|
+
<SilkeBox column scroll flex hPad="xs">
|
|
455
|
+
{displayedItems.length === 0 && (
|
|
456
|
+
<SilkeBox hPad="s" height={size} vAlign="center">
|
|
457
|
+
<SilkeTextSmall color="neutral-60">No results</SilkeTextSmall>
|
|
458
|
+
</SilkeBox>
|
|
459
|
+
)}
|
|
460
|
+
{(() => {
|
|
461
|
+
const sortedItems = displayedItems.sort(sort);
|
|
462
|
+
const groupedItems: { groupLabel?: string; items: Entry<T>[] }[] = [];
|
|
463
|
+
let currentGroup: { groupLabel?: string; items: Entry<T>[] } | null = null;
|
|
464
|
+
|
|
465
|
+
sortedItems.forEach((item) => {
|
|
466
|
+
const itemGroupLabel = !item.divider ? (item as Data<T>).groupLabel : undefined;
|
|
467
|
+
if (!currentGroup || currentGroup.groupLabel !== itemGroupLabel) {
|
|
468
|
+
currentGroup = { groupLabel: itemGroupLabel, items: [] };
|
|
469
|
+
groupedItems.push(currentGroup);
|
|
470
|
+
}
|
|
471
|
+
currentGroup.items.push(item);
|
|
472
|
+
});
|
|
473
|
+
|
|
474
|
+
let itemIndex = 0;
|
|
475
|
+
|
|
476
|
+
return groupedItems.map((group, groupIdx) => (
|
|
477
|
+
<React.Fragment key={`group-${groupIdx}`}>
|
|
478
|
+
{group.groupLabel && (
|
|
479
|
+
<SilkeBox hPad="s" vPad="s">
|
|
480
|
+
<SilkeTextSmall color="neutral-60" weight="strong">
|
|
481
|
+
{group.groupLabel}
|
|
482
|
+
</SilkeTextSmall>
|
|
483
|
+
</SilkeBox>
|
|
484
|
+
)}
|
|
485
|
+
{group.items.map((item) => {
|
|
486
|
+
const currentItemIndex = itemIndex++;
|
|
487
|
+
return item.divider ? (
|
|
488
|
+
<SilkeDivider dark key={`hr-${currentItemIndex}`} />
|
|
489
|
+
) : (
|
|
490
|
+
<AutocompleteItem
|
|
491
|
+
ItemRender={ItemRender}
|
|
492
|
+
key={currentItemIndex}
|
|
493
|
+
size={size}
|
|
494
|
+
iconFontSize={iconFontSize}
|
|
495
|
+
item={item}
|
|
496
|
+
highlight={currentItemIndex === highlight}
|
|
497
|
+
selected={isSelected(item.value)}
|
|
498
|
+
multiple={form.multiple}
|
|
499
|
+
onMouseDown={(e) => {
|
|
500
|
+
e.preventDefault();
|
|
501
|
+
e.stopPropagation();
|
|
502
|
+
if (!item.divider && !item.disabled) handleSelect(item);
|
|
503
|
+
}}
|
|
504
|
+
/>
|
|
505
|
+
);
|
|
506
|
+
})}
|
|
507
|
+
</React.Fragment>
|
|
508
|
+
));
|
|
509
|
+
})()}
|
|
510
|
+
{onAdd && !disableAddFromList && searchText && (
|
|
511
|
+
<AutocompleteItem
|
|
512
|
+
size={size}
|
|
513
|
+
iconFontSize={iconFontSize}
|
|
514
|
+
highlight={!displayedItems.length}
|
|
515
|
+
item={{
|
|
516
|
+
label: `+ Add item "${searchText}"`,
|
|
517
|
+
value: undefined,
|
|
518
|
+
}}
|
|
519
|
+
onMouseDown={(e) => {
|
|
520
|
+
e.preventDefault();
|
|
521
|
+
onAdd(searchText);
|
|
522
|
+
setSearchText('');
|
|
523
|
+
}}
|
|
524
|
+
/>
|
|
525
|
+
)}
|
|
526
|
+
{isHidingResults
|
|
527
|
+
? props.maxDisplayedItemsLabel || (
|
|
528
|
+
<>
|
|
529
|
+
<SilkeDivider dark />
|
|
530
|
+
<SilkeBox pad="l">
|
|
531
|
+
Currently displaying the {maxDisplayedItems} first results, please enter a more
|
|
532
|
+
specific query to see the rest
|
|
533
|
+
</SilkeBox>
|
|
534
|
+
</>
|
|
535
|
+
)
|
|
536
|
+
: null}
|
|
537
|
+
</SilkeBox>
|
|
538
|
+
</SilkeBox>
|
|
539
|
+
</SilkePopoverNative>
|
|
540
|
+
);
|
|
541
|
+
}
|
|
@@ -189,7 +189,7 @@ export function SilkeAutocompletePopup<T>(props: SilkeAutocompletePopupProps<T>)
|
|
|
189
189
|
if (!form.multiple && !isUndefined(form.value)) {
|
|
190
190
|
resetSearchText();
|
|
191
191
|
}
|
|
192
|
-
}, [
|
|
192
|
+
}, [form.multiple, resetSearchText, form.value]);
|
|
193
193
|
|
|
194
194
|
useEffect(() => {
|
|
195
195
|
if (highlight !== undefined) {
|
|
@@ -452,27 +452,59 @@ export function SilkeAutocompletePopup<T>(props: SilkeAutocompletePopupProps<T>)
|
|
|
452
452
|
<SilkeTextSmall color="neutral-60">No results</SilkeTextSmall>
|
|
453
453
|
</SilkeBox>
|
|
454
454
|
)}
|
|
455
|
-
{
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
)
|
|
475
|
-
|
|
455
|
+
{(() => {
|
|
456
|
+
const sortedItems = displayedItems.sort(sort);
|
|
457
|
+
const groupedItems: { groupLabel?: string; items: Entry<T>[] }[] = [];
|
|
458
|
+
let currentGroup: { groupLabel?: string; items: Entry<T>[] } | null = null;
|
|
459
|
+
|
|
460
|
+
// Group items by groupLabel
|
|
461
|
+
sortedItems.forEach((item) => {
|
|
462
|
+
const itemGroupLabel = !item.divider ? (item as Data<T>).groupLabel : undefined;
|
|
463
|
+
|
|
464
|
+
if (!currentGroup || currentGroup.groupLabel !== itemGroupLabel) {
|
|
465
|
+
currentGroup = { groupLabel: itemGroupLabel, items: [] };
|
|
466
|
+
groupedItems.push(currentGroup);
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
currentGroup.items.push(item);
|
|
470
|
+
});
|
|
471
|
+
|
|
472
|
+
let itemIndex = 0;
|
|
473
|
+
|
|
474
|
+
return groupedItems.map((group, groupIdx) => (
|
|
475
|
+
<React.Fragment key={`group-${groupIdx}`}>
|
|
476
|
+
{group.groupLabel && (
|
|
477
|
+
<SilkeBox hPad="s" vPad="s">
|
|
478
|
+
<SilkeTextSmall color="neutral-60" weight="strong">
|
|
479
|
+
{group.groupLabel}
|
|
480
|
+
</SilkeTextSmall>
|
|
481
|
+
</SilkeBox>
|
|
482
|
+
)}
|
|
483
|
+
{group.items.map((item) => {
|
|
484
|
+
const currentItemIndex = itemIndex++;
|
|
485
|
+
return item.divider ? (
|
|
486
|
+
<SilkeDivider dark key={`hr-${currentItemIndex}`} />
|
|
487
|
+
) : (
|
|
488
|
+
<AutocompleteItem
|
|
489
|
+
ItemRender={ItemRender}
|
|
490
|
+
key={currentItemIndex}
|
|
491
|
+
size={size}
|
|
492
|
+
iconFontSize={iconFontSize}
|
|
493
|
+
item={item}
|
|
494
|
+
highlight={currentItemIndex === highlight}
|
|
495
|
+
selected={isSelected(item.value)}
|
|
496
|
+
multiple={form.multiple}
|
|
497
|
+
onMouseDown={(e) => {
|
|
498
|
+
e.preventDefault();
|
|
499
|
+
e.stopPropagation();
|
|
500
|
+
if (!item.divider && !item.disabled) handleSelect(item);
|
|
501
|
+
}}
|
|
502
|
+
/>
|
|
503
|
+
);
|
|
504
|
+
})}
|
|
505
|
+
</React.Fragment>
|
|
506
|
+
));
|
|
507
|
+
})()}
|
|
476
508
|
{onAdd && !disableAddFromList && searchText && (
|
|
477
509
|
<AutocompleteItem
|
|
478
510
|
size={size}
|
|
@@ -102,9 +102,9 @@ body {
|
|
|
102
102
|
--silke-button-background-pressed: var(--color-surface-3);
|
|
103
103
|
--silke-button-color-hover: var(--color-neutral-100);
|
|
104
104
|
--silke-button-color-pressed: var(--color-neutral-100);
|
|
105
|
-
--silke-button-border: 1px solid
|
|
106
|
-
--silke-button-border-hover: 1px solid
|
|
107
|
-
--silke-button-border-pressed: 1px solid
|
|
105
|
+
--silke-button-border: 1px solid var(--color-surface-4);
|
|
106
|
+
--silke-button-border-hover: 1px solid var(--color-surface-3);
|
|
107
|
+
--silke-button-border-pressed: 1px solid var(--color-surface-3);
|
|
108
108
|
}
|
|
109
109
|
|
|
110
110
|
&.danger {
|
|
@@ -99,7 +99,7 @@ export function SilkeSchemaField({
|
|
|
99
99
|
return (
|
|
100
100
|
<>
|
|
101
101
|
{['object', 'array', 'image', 'divider'].includes(field?.type) && !isFirst && (
|
|
102
|
-
<SilkeBox column flex vPad={field?.type === 'divider' ? 'xs' : '
|
|
102
|
+
<SilkeBox column flex vPad={field?.type === 'divider' ? 'xs' : 's'}>
|
|
103
103
|
<SilkeDivider dark />
|
|
104
104
|
</SilkeBox>
|
|
105
105
|
)}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './silke-popover-native';
|