@homecode/ui 4.30.9 → 4.30.11
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/dist/esm/src/components/Autocomplete/Autocomplete.js +35 -6
- package/dist/esm/src/components/Select/Select2.js +2 -2
- package/dist/esm/types/src/components/Autocomplete/Autocomplete.types.d.ts +2 -0
- package/dist/esm/types/src/components/Select/Select.types.d.ts +1 -0
- package/package.json +1 -1
|
@@ -73,7 +73,7 @@ const getTotalCount = (total, newItemsCount = 0, offset = 0, pageSize = 20) => {
|
|
|
73
73
|
return offset + (newItemsCount === pageSize ? 1 : 0);
|
|
74
74
|
};
|
|
75
75
|
function Autocomplete(props) {
|
|
76
|
-
const { className, value, onChange, size = 'm', getOptions, onSelect, items, itemHeight = SIZE_TO_ITEM_HEIGHT[size], pageSize = 20, debounceDelay = 300, round = false, blur = false, selectable = false, inputProps = {}, popupProps = {}, menuProps = {}, scrollProps = {}, loadingPlaceholder, isOpen, } = props;
|
|
76
|
+
const { className, value, onChange, size = 'm', getOptions, onSelect, items, itemHeight = SIZE_TO_ITEM_HEIGHT[size], pageSize = 20, debounceDelay = 300, round = false, blur = false, selectable = false, defaultSelected, scrollToSelected = false, inputProps = {}, popupProps = {}, menuProps = {}, scrollProps = {}, loadingPlaceholder, isOpen, } = props;
|
|
77
77
|
const isMounted = useIsMounted();
|
|
78
78
|
const [filteredItems, setFilteredItems] = useState([]);
|
|
79
79
|
const [itemsWithoutFilter, setItemsWithoutFilter] = useState(() => items ?? []);
|
|
@@ -95,6 +95,7 @@ function Autocomplete(props) {
|
|
|
95
95
|
};
|
|
96
96
|
const currentRequest = useRef('');
|
|
97
97
|
const savedScrollTopRef = useRef(undefined);
|
|
98
|
+
const defaultSelectedAppliedRef = useRef(false);
|
|
98
99
|
// @ts-ignore
|
|
99
100
|
const inputRef = useRef(null);
|
|
100
101
|
const inputDisplayValue = selectable && !isFocused && selectedLabel != null
|
|
@@ -111,10 +112,12 @@ function Autocomplete(props) {
|
|
|
111
112
|
const handleFocus = (e) => {
|
|
112
113
|
isFocusedRef.current = true;
|
|
113
114
|
setIsFocused(true);
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
115
|
+
if (!scrollToSelected || !selectable || !selectedId) {
|
|
116
|
+
const saved = savedScrollTopRef.current;
|
|
117
|
+
if (saved != null && saved > 0) {
|
|
118
|
+
setScrollTop(saved);
|
|
119
|
+
requestAnimationFrame(() => setScrollTop(undefined));
|
|
120
|
+
}
|
|
118
121
|
}
|
|
119
122
|
inputProps?.onFocus?.(e);
|
|
120
123
|
};
|
|
@@ -290,8 +293,22 @@ function Autocomplete(props) {
|
|
|
290
293
|
if (selectable && !value) {
|
|
291
294
|
setSelectedId(null);
|
|
292
295
|
setSelectedLabel(null);
|
|
296
|
+
defaultSelectedAppliedRef.current = false;
|
|
293
297
|
}
|
|
294
298
|
}, [selectable, value]);
|
|
299
|
+
useEffect(() => {
|
|
300
|
+
if (selectable &&
|
|
301
|
+
defaultSelected &&
|
|
302
|
+
!defaultSelectedAppliedRef.current &&
|
|
303
|
+
displayItems.length) {
|
|
304
|
+
const item = displayItems.find(o => o.id === defaultSelected);
|
|
305
|
+
if (item) {
|
|
306
|
+
defaultSelectedAppliedRef.current = true;
|
|
307
|
+
setSelectedId(item.id);
|
|
308
|
+
setSelectedLabel(item.label);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
}, [selectable, defaultSelected, displayItems]);
|
|
295
312
|
useEffect(() => {
|
|
296
313
|
if (!currentFilter && items?.length) {
|
|
297
314
|
setItemsWithoutFilter(items);
|
|
@@ -343,7 +360,16 @@ function Autocomplete(props) {
|
|
|
343
360
|
return !selectable && isLoading ? LoadingPlaceholder : null;
|
|
344
361
|
}
|
|
345
362
|
const computedTotalCount = totalCount > 0 ? totalCount : displayItems.length;
|
|
346
|
-
|
|
363
|
+
const open = isOpen ?? isFocused;
|
|
364
|
+
let initialScrollTop;
|
|
365
|
+
if (open && scrollToSelected && selectable && selectedId) {
|
|
366
|
+
const idx = displayItems.findIndex(o => o.id === selectedId);
|
|
367
|
+
initialScrollTop =
|
|
368
|
+
idx >= 0 ? Math.max(0, idx * itemHeight - itemHeight) : undefined;
|
|
369
|
+
}
|
|
370
|
+
return (jsx(ListScroll, { ...(selectable && { id: selectedId ?? 'none' }), ...(typeof initialScrollTop === 'number' && {
|
|
371
|
+
initialScrollTop,
|
|
372
|
+
}), className: cn(S.options, menuProps.className), scrollProps: {
|
|
347
373
|
y: true,
|
|
348
374
|
...scrollProps,
|
|
349
375
|
className: cn(S.scroll, scrollProps?.className),
|
|
@@ -367,6 +393,9 @@ function Autocomplete(props) {
|
|
|
367
393
|
scrollTop,
|
|
368
394
|
selectable,
|
|
369
395
|
selectedId,
|
|
396
|
+
isOpen,
|
|
397
|
+
isFocused,
|
|
398
|
+
scrollToSelected,
|
|
370
399
|
LoadingPlaceholder,
|
|
371
400
|
]);
|
|
372
401
|
return (jsx(Popup, { className: classes, isOpen: isOpen, focusControl: true, round: round, size: size, blur: blur, direction: "bottom", ...popupProps, trigger: jsx(Input, { ref: inputRef,
|
|
@@ -18,7 +18,7 @@ import useEvent from '../../hooks/useEvent.js';
|
|
|
18
18
|
import S from './Select.styl.js';
|
|
19
19
|
|
|
20
20
|
function Select2(props) {
|
|
21
|
-
const { className, value, onChange, onChipClick, onSearchChange, disableTriggerArrow, inputProps, popupProps, scrollProps, size = 'm', round, optionClassName, additionalOptions = [], options, variant, label, additionalLabel, error, blur, disabled, trigger, required, hideRequiredStar, isSearchable, presets = [], selectAllButton, clearButton, showSelectedCount, disableLabel, selectedChipRemoveTooltip, } = props;
|
|
21
|
+
const { className, value, onChange, onChipClick, onSearchChange, disableTriggerArrow, inputProps, popupProps, scrollProps, size = 'm', round, optionClassName, additionalOptions = [], options, variant, label, additionalLabel, error, blur, disabled, trigger, required, hideRequiredStar, isSearchable, presets = [], selectAllButton, clearButton, showSelectedCount, disableLabel, selectedChipRemoveTooltip, selectedChipIds, } = props;
|
|
22
22
|
const isMultiple$1 = isMultiple(value);
|
|
23
23
|
const closeOnSelect = props.closeOnSelect ?? !isMultiple$1;
|
|
24
24
|
const contentRef = useRef(null);
|
|
@@ -180,7 +180,7 @@ function Select2(props) {
|
|
|
180
180
|
const label = getLabel(id);
|
|
181
181
|
if (!label)
|
|
182
182
|
return null;
|
|
183
|
-
return (jsx(Chip, { className: S.chip, size: size, onRemove: () => onItemToggle(id), onClick: () => onChipClick?.(id), removeTooltip: selectedChipRemoveTooltip, children: label }, id));
|
|
183
|
+
return (jsx(Chip, { className: S.chip, size: size, selected: selectedChipIds?.includes(id), onRemove: () => onItemToggle(id), onClick: () => onChipClick?.(id), removeTooltip: selectedChipRemoveTooltip, children: label }, id));
|
|
184
184
|
});
|
|
185
185
|
};
|
|
186
186
|
const triggerArrow = useMemo(() => {
|
|
@@ -30,6 +30,8 @@ export type Props = FormControl<Value, HTMLInputElement> & {
|
|
|
30
30
|
round?: boolean;
|
|
31
31
|
blur?: boolean;
|
|
32
32
|
selectable?: boolean;
|
|
33
|
+
defaultSelected?: string;
|
|
34
|
+
scrollToSelected?: boolean;
|
|
33
35
|
renderItem?: (props: RenderItemProps) => React.ReactElement;
|
|
34
36
|
loadingPlaceholder?: React.ReactNode;
|
|
35
37
|
};
|