@vev/silke 1.1.3 → 1.1.7
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 +34 -0
- package/dist/index.js +40 -38
- package/dist/index.js.LEGAL.txt +12 -0
- package/dist/styles.css +1596 -1036
- package/dist/types/components/index.d.ts +4 -1
- 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-button/silke-dropdown-button.d.ts +8 -13
- package/dist/types/components/silke-card/silke-card-grid.d.ts +33 -1
- package/dist/types/components/silke-command-text-field/image-utils.d.ts +20 -0
- package/dist/types/components/silke-command-text-field/index.d.ts +4 -0
- package/dist/types/components/silke-command-text-field/silke-command-parser.d.ts +3 -0
- package/dist/types/components/silke-command-text-field/silke-command-parser.spec.d.ts +1 -0
- package/dist/types/components/silke-command-text-field/silke-command-text-field.d.ts +46 -0
- package/dist/types/components/silke-command-text-field/utils.d.ts +61 -0
- package/dist/types/components/silke-command-text-field/utils.spec.d.ts +1 -0
- package/dist/types/components/silke-conditions-input/utils.d.ts +1 -1
- package/dist/types/components/silke-form-schema/silke-schema-fields.d.ts +1 -0
- package/dist/types/components/silke-image/silke-image.d.ts +2 -1
- package/dist/types/components/silke-list/index.d.ts +1 -0
- package/dist/types/components/silke-list/silke-list-item.d.ts +24 -6
- package/dist/types/components/silke-list/silke-list.d.ts +9 -9
- package/dist/types/components/silke-list/types.d.ts +19 -0
- package/dist/types/components/silke-list/utils.d.ts +3 -0
- package/dist/types/components/silke-navigation-link/silke-navigation-link.d.ts +1 -0
- package/dist/types/components/silke-overflow-menu/silke-overflow-menu.d.ts +6 -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 +37 -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/components/silke-shortcut-button/index.d.ts +1 -0
- package/dist/types/components/silke-shortcut-button/silke-shortcut-button.d.ts +15 -0
- package/dist/types/components/silke-tag/silke-tag.d.ts +1 -1
- package/dist/types/icons.d.ts +32 -0
- package/jest.config.js +2 -3
- package/package.json +9 -3
- package/src/_variables.scss +125 -129
- package/src/components/index.ts +4 -1
- package/src/components/silke-action-box/silke-action-box.scss +2 -3
- package/src/components/silke-alert-banner/silke-alert-banner.scss +9 -10
- package/src/components/silke-autocomplete-field/autocomplete-item.tsx +2 -2
- 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.scss +18 -12
- package/src/components/silke-autocomplete-field/silke-autocomplete-field.tsx +3 -0
- package/src/components/silke-autocomplete-field/silke-autocomplete-popup-native.tsx +542 -0
- package/src/components/silke-autocomplete-field/silke-autocomplete-popup.tsx +92 -41
- package/src/components/silke-box/silke-box.mdx +321 -21
- package/src/components/silke-box/silke-box.scss +5 -5
- package/src/components/silke-breadcrumbs/silke-breadcrumb.mdx +147 -9
- package/src/components/silke-breadcrumbs/silke-breadcrumb.scss +3 -3
- package/src/components/silke-button/silke-big-button.scss +1 -1
- package/src/components/silke-button/silke-button.scss +27 -18
- package/src/components/silke-button/silke-dropdown-button.mdx +67 -0
- package/src/components/silke-button/silke-dropdown-button.tsx +40 -122
- package/src/components/silke-card/silke-card-grid.tsx +5 -3
- package/src/components/silke-card/silke-card.scss +8 -7
- package/src/components/silke-card/silke-card.tsx +7 -1
- package/src/components/silke-checkbox/silke-checkbox.mdx +139 -14
- package/src/components/silke-checkbox/silke-checkbox.scss +11 -11
- package/src/components/silke-color-picker/color-picker.scss +8 -8
- package/src/components/silke-color-swatches/silke-color-swatches.mdx +130 -0
- package/src/components/silke-command-text-field/image-utils.ts +71 -0
- package/src/components/silke-command-text-field/index.ts +12 -0
- package/src/components/silke-command-text-field/silke-command-parser.spec.ts +89 -0
- package/src/components/silke-command-text-field/silke-command-parser.ts +47 -0
- package/src/components/silke-command-text-field/silke-command-text-field.mdx +301 -0
- package/src/components/silke-command-text-field/silke-command-text-field.module.scss +119 -0
- package/src/components/silke-command-text-field/silke-command-text-field.tsx +665 -0
- package/src/components/silke-command-text-field/utils.spec.ts +366 -0
- package/src/components/silke-command-text-field/utils.ts +287 -0
- package/src/components/silke-conditions-input/silke-conditions-input.scss +4 -4
- package/src/components/silke-content-column/silke-content-column.scss +1 -1
- package/src/components/silke-context-menu/silke-context-menu.scss +3 -4
- package/src/components/silke-context-menu/silke-context-menu.tsx +1 -1
- package/src/components/silke-css-number-field/silke-css-number-field.scss +14 -12
- package/src/components/silke-divider/silke-divider.mdx +154 -32
- package/src/components/silke-divider/silke-divider.scss +3 -2
- package/src/components/silke-drawer/silke-drawer.scss +6 -2
- package/src/components/silke-form/silke-form.mdx +254 -27
- package/src/components/silke-form-schema/fields/array-field-inline.tsx +1 -0
- package/src/components/silke-form-schema/fields/array-field-modal.tsx +1 -0
- package/src/components/silke-form-schema/fields/styles.scss +34 -35
- package/src/components/silke-form-schema/silke-schema-field.tsx +1 -1
- package/src/components/silke-form-schema/silke-schema-fields.tsx +4 -3
- package/src/components/silke-gradient-picker/silke-gradient-picker.module.scss +2 -2
- package/src/components/silke-icon/silke-icon.mdx +212 -8
- package/src/components/silke-icon/silke-icon.scss +10 -2
- package/src/components/silke-icon/silke-icon.tsx +1 -0
- package/src/components/silke-icon/silke-stacked-icon.scss +42 -45
- package/src/components/silke-icon-browser/silke-icon-browser.mdx +143 -0
- package/src/components/silke-image/silke-image-color-backdrop.scss +6 -6
- package/src/components/silke-image/silke-image.mdx +189 -41
- package/src/components/silke-image/silke-image.scss +3 -3
- package/src/components/silke-image/silke-image.tsx +14 -1
- package/src/components/silke-link/silke-link.scss +3 -5
- package/src/components/silke-list/index.ts +1 -0
- package/src/components/silke-list/silke-list-item.mdx +83 -0
- package/src/components/silke-list/silke-list-item.scss +119 -0
- package/src/components/silke-list/silke-list-item.tsx +137 -12
- package/src/components/silke-list/silke-list.mdx +108 -13
- package/src/components/silke-list/silke-list.scss +8 -15
- package/src/components/silke-list/silke-list.tsx +63 -9
- package/src/components/silke-list/types.ts +22 -0
- package/src/components/silke-list/utils.ts +24 -0
- package/src/components/silke-loader/silke-loader.mdx +139 -5
- package/src/components/silke-loader/silke-loader.scss +1 -1
- package/src/components/silke-modal/silke-modal.scss +9 -9
- package/src/components/silke-navigation-link/silke-navigation-link.mdx +27 -2
- package/src/components/silke-navigation-link/silke-navigation-link.scss +34 -40
- package/src/components/silke-navigation-link/silke-navigation-link.tsx +7 -6
- package/src/components/silke-overflow-menu/silke-overflow-menu.mdx +228 -31
- package/src/components/silke-overflow-menu/silke-overflow-menu.scss +8 -8
- package/src/components/silke-overflow-menu/silke-overflow-menu.tsx +14 -8
- package/src/components/silke-popover/silke-popover.scss +5 -5
- 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 +127 -0
- package/src/components/silke-popover-native/silke-popover-native.tsx +176 -0
- package/src/components/silke-popover-native/types.ts +21 -0
- package/src/components/silke-popover-native/use-popover-positioning.ts +157 -0
- package/src/components/silke-progress-indicator/silke-progress-indicator.scss +1 -1
- package/src/components/silke-radio-button/silke-radio-button.scss +18 -11
- package/src/components/silke-search-field/silke-search-field.mdx +161 -7
- 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-shortcut-button/index.ts +1 -0
- package/src/components/silke-shortcut-button/silke-shortcut-button.mdx +89 -0
- package/src/components/silke-shortcut-button/silke-shortcut-button.scss +145 -0
- package/src/components/silke-shortcut-button/silke-shortcut-button.tsx +55 -0
- package/src/components/silke-skeleton/silke-skeleton.mdx +109 -0
- package/src/components/silke-skeleton/silke-skeleton.scss +1 -1
- package/src/components/silke-slider/silke-slider.module.scss +19 -19
- package/src/components/silke-sortable-list/silke-nested-sortable-list.scss +3 -3
- package/src/components/silke-sortable-list/silke-sortable-list.scss +4 -5
- package/src/components/silke-table/silke-table.scss +1 -1
- package/src/components/silke-tabs/silke-tabs.scss +3 -3
- package/src/components/silke-tag/silke-tag.mdx +179 -43
- package/src/components/silke-tag/silke-tag.scss +49 -12
- package/src/components/silke-tag/silke-tag.tsx +17 -7
- package/src/components/silke-text/silke-text.scss +6 -4
- package/src/components/silke-text/silke-title.scss +7 -14
- package/src/components/silke-text-field/silke-text-field.scss +9 -9
- package/src/components/silke-text-field/silke-text-field.tsx +3 -2
- package/src/components/silke-tile/silke-tile.scss +7 -3
- package/src/components/silke-toast-notification/silke-toast-notification.scss +4 -5
- package/src/components/silke-toggle/silke-toggle.mdx +163 -33
- package/src/components/silke-toggle/silke-toggle.scss +4 -4
- package/src/components/silke-toolbar/silke-toolbar.scss +1 -1
- package/src/components/silke-tooltip/silke-shortcuts.tsx +3 -3
- package/src/components/silke-tooltip/silke-tooltip.scss +11 -16
- package/src/components/silke-variables-wrapper/silke-variables-wrapper.scss +9 -7
- package/src/components/silke-virtual/silke-virtual.mdx +161 -0
- package/src/components/silke-virtual/silke-virtual.scss +8 -8
- package/src/icons.tsx +45 -1
- package/src/styles/core.scss +42 -45
- package/src/styles/math.scss +66 -66
- package/tsconfig.build.json +2 -6
- package/tsconfig.build.tsbuildinfo +1 -1
- package/tsconfig.json +3 -14
- package/dist/types/components/silke-list-card/index.d.ts +0 -1
- package/dist/types/components/silke-list-card/silke-list-card.d.ts +0 -14
- package/src/components/silke-list-card/index.ts +0 -1
- package/src/components/silke-list-card/silke-list-card.mdx +0 -46
- package/src/components/silke-list-card/silke-list-card.scss +0 -57
- package/src/components/silke-list-card/silke-list-card.tsx +0 -63
|
@@ -0,0 +1,542 @@
|
|
|
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-10"
|
|
398
|
+
column
|
|
399
|
+
style={{
|
|
400
|
+
maxHeight: 300,
|
|
401
|
+
minWidth: dropDownMinWidth,
|
|
402
|
+
maxWidth: dropDownMaxWidth,
|
|
403
|
+
border: '1px solid var(--color-surface-2)',
|
|
404
|
+
}}
|
|
405
|
+
gap={disableSearch ? undefined : 'xs'}
|
|
406
|
+
vPad="xs"
|
|
407
|
+
value={form.value as any}
|
|
408
|
+
onKeyUp={handleKeyUp}
|
|
409
|
+
ref={dropdownRef}
|
|
410
|
+
>
|
|
411
|
+
<SilkeBox hPad="xs">
|
|
412
|
+
<SilkeTextField
|
|
413
|
+
gap={size === 's' ? 'xxs' : 'xs'}
|
|
414
|
+
inline="label-inside"
|
|
415
|
+
label={<SilkeIcon icon="search" />}
|
|
416
|
+
aria-label={'Search for ' + label}
|
|
417
|
+
autoFocus
|
|
418
|
+
size={size}
|
|
419
|
+
className={disableSearch ? styles.hiddenSearch : undefined}
|
|
420
|
+
placeholder={searchPlaceholder || 'Search'}
|
|
421
|
+
kind="ghost"
|
|
422
|
+
onFocus={(e) => {
|
|
423
|
+
inputRef.current = e.currentTarget as HTMLInputElement;
|
|
424
|
+
if (props.onFocus) props.onFocus(e);
|
|
425
|
+
}}
|
|
426
|
+
onBlur={(e) => {
|
|
427
|
+
if (props.onBlur) props.onBlur(e);
|
|
428
|
+
}}
|
|
429
|
+
onChange={(text: string) => {
|
|
430
|
+
onSearch?.(text);
|
|
431
|
+
setHighlight(0);
|
|
432
|
+
setSearchText(text);
|
|
433
|
+
}}
|
|
434
|
+
onKeyUp={handleKeyUp}
|
|
435
|
+
onKeyDown={(e: React.KeyboardEvent) => {
|
|
436
|
+
if (e.key === 'Tab') {
|
|
437
|
+
inputField.current?.focus();
|
|
438
|
+
}
|
|
439
|
+
if (['Enter', 'ArrowUp', 'ArrowDown', 'Escape'].includes(e.key)) {
|
|
440
|
+
e.preventDefault();
|
|
441
|
+
e.stopPropagation();
|
|
442
|
+
}
|
|
443
|
+
}}
|
|
444
|
+
value={searchText}
|
|
445
|
+
>
|
|
446
|
+
{props.showAddIcon && onAdd && (
|
|
447
|
+
<SilkeTextFieldItem hPad="xs">
|
|
448
|
+
<SilkeButton icon="add" kind="ghost" size="s" onClick={() => onAdd('variable')} />
|
|
449
|
+
</SilkeTextFieldItem>
|
|
450
|
+
)}
|
|
451
|
+
{buttonRight && <div onClick={() => onRequestClose?.()}>{buttonRight}</div>}
|
|
452
|
+
</SilkeTextField>
|
|
453
|
+
</SilkeBox>
|
|
454
|
+
{!disableSearch && <SilkeDivider dark />}
|
|
455
|
+
<SilkeBox column scroll flex hPad="xs">
|
|
456
|
+
{displayedItems.length === 0 && (
|
|
457
|
+
<SilkeBox hPad="s" height={size} vAlign="center">
|
|
458
|
+
<SilkeTextSmall color="neutral-60">No results</SilkeTextSmall>
|
|
459
|
+
</SilkeBox>
|
|
460
|
+
)}
|
|
461
|
+
{(() => {
|
|
462
|
+
const sortedItems = displayedItems.sort(sort);
|
|
463
|
+
const groupedItems: { groupLabel?: string; items: Entry<T>[] }[] = [];
|
|
464
|
+
let currentGroup: { groupLabel?: string; items: Entry<T>[] } | null = null;
|
|
465
|
+
|
|
466
|
+
sortedItems.forEach((item) => {
|
|
467
|
+
const itemGroupLabel = !item.divider ? (item as Data<T>).groupLabel : undefined;
|
|
468
|
+
if (!currentGroup || currentGroup.groupLabel !== itemGroupLabel) {
|
|
469
|
+
currentGroup = { groupLabel: itemGroupLabel, items: [] };
|
|
470
|
+
groupedItems.push(currentGroup);
|
|
471
|
+
}
|
|
472
|
+
currentGroup.items.push(item);
|
|
473
|
+
});
|
|
474
|
+
|
|
475
|
+
let itemIndex = 0;
|
|
476
|
+
|
|
477
|
+
return groupedItems.map((group, groupIdx) => (
|
|
478
|
+
<React.Fragment key={`group-${groupIdx}`}>
|
|
479
|
+
{group.groupLabel && (
|
|
480
|
+
<SilkeBox hPad="s" vPad="s">
|
|
481
|
+
<SilkeTextSmall color="neutral-60" weight="strong">
|
|
482
|
+
{group.groupLabel}
|
|
483
|
+
</SilkeTextSmall>
|
|
484
|
+
</SilkeBox>
|
|
485
|
+
)}
|
|
486
|
+
{group.items.map((item) => {
|
|
487
|
+
const currentItemIndex = itemIndex++;
|
|
488
|
+
return item.divider ? (
|
|
489
|
+
<SilkeDivider dark key={`hr-${currentItemIndex}`} />
|
|
490
|
+
) : (
|
|
491
|
+
<AutocompleteItem
|
|
492
|
+
ItemRender={ItemRender}
|
|
493
|
+
key={currentItemIndex}
|
|
494
|
+
size={size}
|
|
495
|
+
iconFontSize={iconFontSize}
|
|
496
|
+
item={item}
|
|
497
|
+
highlight={currentItemIndex === highlight}
|
|
498
|
+
selected={isSelected(item.value)}
|
|
499
|
+
multiple={form.multiple}
|
|
500
|
+
onMouseDown={(e) => {
|
|
501
|
+
e.preventDefault();
|
|
502
|
+
e.stopPropagation();
|
|
503
|
+
if (!item.divider && !item.disabled) handleSelect(item);
|
|
504
|
+
}}
|
|
505
|
+
/>
|
|
506
|
+
);
|
|
507
|
+
})}
|
|
508
|
+
</React.Fragment>
|
|
509
|
+
));
|
|
510
|
+
})()}
|
|
511
|
+
{onAdd && !disableAddFromList && searchText && (
|
|
512
|
+
<AutocompleteItem
|
|
513
|
+
size={size}
|
|
514
|
+
iconFontSize={iconFontSize}
|
|
515
|
+
highlight={!displayedItems.length}
|
|
516
|
+
item={{
|
|
517
|
+
label: `+ Add item "${searchText}"`,
|
|
518
|
+
value: undefined,
|
|
519
|
+
}}
|
|
520
|
+
onMouseDown={(e) => {
|
|
521
|
+
e.preventDefault();
|
|
522
|
+
onAdd(searchText);
|
|
523
|
+
setSearchText('');
|
|
524
|
+
}}
|
|
525
|
+
/>
|
|
526
|
+
)}
|
|
527
|
+
{isHidingResults
|
|
528
|
+
? props.maxDisplayedItemsLabel || (
|
|
529
|
+
<>
|
|
530
|
+
<SilkeDivider dark />
|
|
531
|
+
<SilkeBox pad="l">
|
|
532
|
+
Currently displaying the {maxDisplayedItems} first results, please enter a more
|
|
533
|
+
specific query to see the rest
|
|
534
|
+
</SilkeBox>
|
|
535
|
+
</>
|
|
536
|
+
)
|
|
537
|
+
: null}
|
|
538
|
+
</SilkeBox>
|
|
539
|
+
</SilkeBox>
|
|
540
|
+
</SilkePopoverNative>
|
|
541
|
+
);
|
|
542
|
+
}
|