@ssa-ui-kit/core 2.15.0-canary-6bf2bcc-20250506 → 2.15.0-canary-138a5b1-20250506
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.
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import { OpenChangeReason } from '@floating-ui/react';
|
|
3
|
-
import { TypeaheadOptionProps, UseTypeaheadProps } from './types';
|
|
4
|
-
export declare const useTypeahead: ({ name, isOpen: isInitOpen, selectedItems, defaultSelectedItems, isDisabled, isMultiple, children, className, startIcon, endIcon, startIconClassName, endIconClassName, validationSchema, error, success, placeholder, filterOptions, autoSelect, onChange, onClearAll, onRemoveSelectedClick, onEmptyChange, renderOption, }: UseTypeaheadProps) => {
|
|
3
|
+
import { TypeaheadOptionProps, TypeaheadValue, UseTypeaheadProps } from './types';
|
|
4
|
+
export declare const useTypeahead: ({ name, isOpen: isInitOpen, selectedItems: providedSelectedItems, defaultSelectedItems, isDisabled, isMultiple, children, className, startIcon, endIcon, startIconClassName, endIconClassName, validationSchema, error, success, placeholder, filterOptions, autoSelect, onChange, onClearAll, onRemoveSelectedClick, onEmptyChange, renderOption, }: UseTypeaheadProps) => {
|
|
5
5
|
isOpen: boolean;
|
|
6
6
|
isDisabled: boolean | undefined;
|
|
7
7
|
optionsWithKey: Record<string | number, TypeaheadOptionProps>;
|
|
8
|
-
selectedItems:
|
|
8
|
+
selectedItems: TypeaheadValue[];
|
|
9
9
|
inputRef: React.RefObject<HTMLInputElement>;
|
|
10
10
|
firstSuggestion: string;
|
|
11
11
|
isMultiple: boolean | undefined;
|
|
@@ -27,7 +27,7 @@ export declare const useTypeahead: ({ name, isOpen: isInitOpen, selectedItems, d
|
|
|
27
27
|
useFormResult: import("react-hook-form").UseFormReturn<import("react-hook-form").FieldValues, any, undefined>;
|
|
28
28
|
register: import("react-hook-form").UseFormRegister<import("react-hook-form").FieldValues>;
|
|
29
29
|
setValue: import("react-hook-form").UseFormSetValue<import("react-hook-form").FieldValues>;
|
|
30
|
-
onChange: ((selectedItem:
|
|
30
|
+
onChange: ((selectedItem: TypeaheadValue, isSelected: boolean) => void) | undefined;
|
|
31
31
|
handleClearAll: (e: React.MouseEvent) => void;
|
|
32
32
|
handleOpenChange: (open: boolean, event: Event, reason?: OpenChangeReason) => void;
|
|
33
33
|
handleInputChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
|
package/dist/index.js
CHANGED
|
@@ -17559,7 +17559,7 @@ const findExactMatch = (input, options) => {
|
|
|
17559
17559
|
const useTypeahead = ({
|
|
17560
17560
|
name = 'typeahead-input',
|
|
17561
17561
|
isOpen: isInitOpen,
|
|
17562
|
-
selectedItems,
|
|
17562
|
+
selectedItems: providedSelectedItems,
|
|
17563
17563
|
defaultSelectedItems,
|
|
17564
17564
|
isDisabled,
|
|
17565
17565
|
isMultiple,
|
|
@@ -17582,12 +17582,33 @@ const useTypeahead = ({
|
|
|
17582
17582
|
renderOption
|
|
17583
17583
|
}) => {
|
|
17584
17584
|
const inputName = `${name}-text`;
|
|
17585
|
+
const defaultForm = (0,external_react_hook_form_namespaceObject.useForm)();
|
|
17586
|
+
const form = (0,external_react_hook_form_namespaceObject.useFormContext)() ?? defaultForm;
|
|
17587
|
+
const {
|
|
17588
|
+
register,
|
|
17589
|
+
setValue,
|
|
17590
|
+
watch
|
|
17591
|
+
} = form;
|
|
17592
|
+
const formFieldValue = watch(name);
|
|
17593
|
+
const controlledValue = (() => {
|
|
17594
|
+
if (providedSelectedItems) {
|
|
17595
|
+
return providedSelectedItems;
|
|
17596
|
+
}
|
|
17597
|
+
// controlledValue should be undefined if defaultSelectedItems is provided
|
|
17598
|
+
if (defaultSelectedItems) {
|
|
17599
|
+
return;
|
|
17600
|
+
}
|
|
17601
|
+
if (isMultiple) {
|
|
17602
|
+
return Array.isArray(formFieldValue) ? formFieldValue : [];
|
|
17603
|
+
}
|
|
17604
|
+
return ['number', 'string'].includes(typeof formFieldValue) ? [formFieldValue] : [];
|
|
17605
|
+
})();
|
|
17585
17606
|
const [isOpen, _setIsOpen] = (0,hooks_namespaceObject.useUncontrolled)({
|
|
17586
17607
|
defaultValue: isInitOpen,
|
|
17587
17608
|
finalValue: false
|
|
17588
17609
|
});
|
|
17589
|
-
const [
|
|
17590
|
-
value:
|
|
17610
|
+
const [selectedItems, setSelected] = (0,hooks_namespaceObject.useUncontrolled)({
|
|
17611
|
+
value: controlledValue,
|
|
17591
17612
|
defaultValue: defaultSelectedItems,
|
|
17592
17613
|
finalValue: []
|
|
17593
17614
|
});
|
|
@@ -17596,22 +17617,16 @@ const useTypeahead = ({
|
|
|
17596
17617
|
ref: inputRef
|
|
17597
17618
|
} = (0,hooks_namespaceObject.useElementSize)();
|
|
17598
17619
|
const triggerRef = (0,external_react_namespaceObject.useRef)(null);
|
|
17599
|
-
const defaultForm = (0,external_react_hook_form_namespaceObject.useForm)();
|
|
17600
|
-
const form = (0,external_react_hook_form_namespaceObject.useFormContext)() ?? defaultForm;
|
|
17601
|
-
const {
|
|
17602
|
-
register,
|
|
17603
|
-
setValue
|
|
17604
|
-
} = form;
|
|
17605
17620
|
(0,external_react_namespaceObject.useEffect)(() => {
|
|
17606
|
-
if (!
|
|
17621
|
+
if (!selectedItems.length) {
|
|
17607
17622
|
return;
|
|
17608
17623
|
}
|
|
17609
17624
|
if (isMultiple) {
|
|
17610
|
-
setValue?.(name,
|
|
17625
|
+
setValue?.(name, selectedItems, {
|
|
17611
17626
|
shouldDirty: false
|
|
17612
17627
|
});
|
|
17613
17628
|
} else {
|
|
17614
|
-
setValue?.(name,
|
|
17629
|
+
setValue?.(name, selectedItems[0], {
|
|
17615
17630
|
shouldDirty: false
|
|
17616
17631
|
});
|
|
17617
17632
|
}
|
|
@@ -17629,8 +17644,8 @@ const useTypeahead = ({
|
|
|
17629
17644
|
const inputValue = (0,external_react_namespaceObject.useMemo)(() => {
|
|
17630
17645
|
if (isMultiple) return rawInput ?? '';
|
|
17631
17646
|
if (rawInput != null) return rawInput;
|
|
17632
|
-
return
|
|
17633
|
-
}, [isMultiple, rawInput,
|
|
17647
|
+
return selectedItems.length === 1 ? optionsWithKey[selectedItems[0]]?.label?.toString() || '' : '';
|
|
17648
|
+
}, [isMultiple, rawInput, selectedItems, optionsWithKey]);
|
|
17634
17649
|
const filteredChildren = (0,external_react_namespaceObject.useMemo)(() => {
|
|
17635
17650
|
// if filtering is disabled, or there's no input, show all
|
|
17636
17651
|
if (!filterOptions || !inputValue) return external_react_default().Children.toArray(children);
|
|
@@ -17648,7 +17663,7 @@ const useTypeahead = ({
|
|
|
17648
17663
|
const items = (0,external_react_namespaceObject.useMemo)(() => {
|
|
17649
17664
|
return filteredChildren.map((child, index) => {
|
|
17650
17665
|
if (! /*#__PURE__*/external_react_default().isValidElement(child)) return null;
|
|
17651
|
-
const isActive =
|
|
17666
|
+
const isActive = selectedItems.includes(child.props.value);
|
|
17652
17667
|
const {
|
|
17653
17668
|
value,
|
|
17654
17669
|
label,
|
|
@@ -17680,7 +17695,7 @@ const useTypeahead = ({
|
|
|
17680
17695
|
}) ?? child.props.children ?? label ?? value
|
|
17681
17696
|
});
|
|
17682
17697
|
});
|
|
17683
|
-
}, [children,
|
|
17698
|
+
}, [children, selectedItems, inputValue]);
|
|
17684
17699
|
const firstSuggestion = (0,external_react_namespaceObject.useMemo)(() => {
|
|
17685
17700
|
if (!inputValue) return '';
|
|
17686
17701
|
const needle = inputValue.toLowerCase();
|
|
@@ -17705,8 +17720,8 @@ const useTypeahead = ({
|
|
|
17705
17720
|
resetInput = true
|
|
17706
17721
|
}) => {
|
|
17707
17722
|
if (isDisabled || value == null) return;
|
|
17708
|
-
const alreadySelected =
|
|
17709
|
-
const updatedSelected = isMultiple ? alreadySelected ?
|
|
17723
|
+
const alreadySelected = selectedItems.includes(value);
|
|
17724
|
+
const updatedSelected = isMultiple ? alreadySelected ? selectedItems.filter(item => item !== value) : [...selectedItems, value] : alreadySelected ? [] : [value];
|
|
17710
17725
|
const fieldValue = isMultiple ? updatedSelected : updatedSelected[0];
|
|
17711
17726
|
setSelected(updatedSelected);
|
|
17712
17727
|
setValue?.(name, fieldValue);
|
|
@@ -17747,9 +17762,9 @@ const useTypeahead = ({
|
|
|
17747
17762
|
return;
|
|
17748
17763
|
}
|
|
17749
17764
|
// unset selected value if not fully matched
|
|
17750
|
-
if (!isMultiple &&
|
|
17765
|
+
if (!isMultiple && selectedItems.length > 0) {
|
|
17751
17766
|
handleChange({
|
|
17752
|
-
value:
|
|
17767
|
+
value: selectedItems[0],
|
|
17753
17768
|
shouldClose: false,
|
|
17754
17769
|
resetInput: false
|
|
17755
17770
|
});
|
|
@@ -17769,15 +17784,14 @@ const useTypeahead = ({
|
|
|
17769
17784
|
const match = findExactMatch(firstSuggestion, optionsWithKey);
|
|
17770
17785
|
if (match) {
|
|
17771
17786
|
handleChange({
|
|
17772
|
-
value: match.value
|
|
17773
|
-
shouldClose: false
|
|
17787
|
+
value: match.value
|
|
17774
17788
|
});
|
|
17775
17789
|
}
|
|
17776
17790
|
e.preventDefault();
|
|
17777
17791
|
return;
|
|
17778
17792
|
}
|
|
17779
|
-
if (isMultiple && e.code === 'Backspace' &&
|
|
17780
|
-
const lastSelected =
|
|
17793
|
+
if (isMultiple && e.code === 'Backspace' && selectedItems.length && !inputValue) {
|
|
17794
|
+
const lastSelected = selectedItems[selectedItems.length - 1];
|
|
17781
17795
|
handleChange({
|
|
17782
17796
|
value: lastSelected,
|
|
17783
17797
|
shouldClose: false
|
|
@@ -17802,8 +17816,8 @@ const useTypeahead = ({
|
|
|
17802
17816
|
return;
|
|
17803
17817
|
}
|
|
17804
17818
|
setIsOpen(open);
|
|
17805
|
-
if (!isMultiple &&
|
|
17806
|
-
const selectedValue =
|
|
17819
|
+
if (!isMultiple && selectedItems.length > 0) {
|
|
17820
|
+
const selectedValue = selectedItems[0];
|
|
17807
17821
|
const label = optionsWithKey[selectedValue]?.label;
|
|
17808
17822
|
setRawInput(label ? String(label) : null);
|
|
17809
17823
|
return;
|
|
@@ -17820,7 +17834,7 @@ const useTypeahead = ({
|
|
|
17820
17834
|
isOpen,
|
|
17821
17835
|
isDisabled,
|
|
17822
17836
|
optionsWithKey,
|
|
17823
|
-
selectedItems
|
|
17837
|
+
selectedItems,
|
|
17824
17838
|
inputRef,
|
|
17825
17839
|
firstSuggestion,
|
|
17826
17840
|
isMultiple,
|
|
@@ -18013,11 +18027,6 @@ const MultipleTrigger = () => {
|
|
|
18013
18027
|
disabled: context.isDisabled,
|
|
18014
18028
|
className: ['typeahead-input', TypeaheadInput(theme), TypeaheadInputPlaceholder].join(' '),
|
|
18015
18029
|
...typeaheadInputAdditionalProps
|
|
18016
|
-
}), (0,jsx_runtime_namespaceObject.jsx)("input", {
|
|
18017
|
-
type: "hidden",
|
|
18018
|
-
readOnly: true,
|
|
18019
|
-
value: context.selectedItems,
|
|
18020
|
-
...context.register?.(context.name, context.validationSchema)
|
|
18021
18030
|
})]
|
|
18022
18031
|
}), !context.isDisabled && context.selectedItems.length ? (0,jsx_runtime_namespaceObject.jsx)(Button_Button, {
|
|
18023
18032
|
variant: "tertiary",
|
|
@@ -18073,11 +18082,6 @@ const SingleTrigger = () => {
|
|
|
18073
18082
|
tabIndex: -1,
|
|
18074
18083
|
className: ['typeahead-input', TypeaheadInput(theme), TypeaheadInputPlaceholder].join(' '),
|
|
18075
18084
|
...typeaheadInputAdditionalProps
|
|
18076
|
-
}), (0,jsx_runtime_namespaceObject.jsx)("input", {
|
|
18077
|
-
type: "hidden",
|
|
18078
|
-
readOnly: true,
|
|
18079
|
-
value: context.selectedItems[0] || '',
|
|
18080
|
-
...context.register?.(context.name, context.validationSchema)
|
|
18081
18085
|
}), !context.isDisabled && context.selectedItems.length ? (0,jsx_runtime_namespaceObject.jsx)(Button_Button, {
|
|
18082
18086
|
variant: "tertiary",
|
|
18083
18087
|
"data-testid": "remove-all-button",
|