@ssa-ui-kit/core 2.15.0-canary-6bf2bcc-20250506 → 2.15.0-canary-8972259-20250507
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,26 +17617,11 @@ const useTypeahead = ({
|
|
|
17596
17617
|
ref: inputRef
|
|
17597
17618
|
} = (0,hooks_namespaceObject.useElementSize)();
|
|
17598
17619
|
const triggerRef = (0,external_react_namespaceObject.useRef)(null);
|
|
17599
|
-
|
|
17600
|
-
|
|
17601
|
-
|
|
17602
|
-
|
|
17603
|
-
|
|
17604
|
-
} = form;
|
|
17605
|
-
(0,external_react_namespaceObject.useEffect)(() => {
|
|
17606
|
-
if (!selected.length) {
|
|
17607
|
-
return;
|
|
17608
|
-
}
|
|
17609
|
-
if (isMultiple) {
|
|
17610
|
-
setValue?.(name, selected, {
|
|
17611
|
-
shouldDirty: false
|
|
17612
|
-
});
|
|
17613
|
-
} else {
|
|
17614
|
-
setValue?.(name, selected[0], {
|
|
17615
|
-
shouldDirty: false
|
|
17616
|
-
});
|
|
17617
|
-
}
|
|
17618
|
-
}, []);
|
|
17620
|
+
(0,external_react_hook_form_namespaceObject.useController)({
|
|
17621
|
+
name,
|
|
17622
|
+
rules: validationSchema,
|
|
17623
|
+
defaultValue: isMultiple ? selectedItems : selectedItems[0]
|
|
17624
|
+
});
|
|
17619
17625
|
const typeaheadId = (0,external_react_namespaceObject.useId)();
|
|
17620
17626
|
const optionsWithKey = (0,external_react_namespaceObject.useMemo)(() => {
|
|
17621
17627
|
const opts = {};
|
|
@@ -17629,8 +17635,8 @@ const useTypeahead = ({
|
|
|
17629
17635
|
const inputValue = (0,external_react_namespaceObject.useMemo)(() => {
|
|
17630
17636
|
if (isMultiple) return rawInput ?? '';
|
|
17631
17637
|
if (rawInput != null) return rawInput;
|
|
17632
|
-
return
|
|
17633
|
-
}, [isMultiple, rawInput,
|
|
17638
|
+
return selectedItems.length === 1 ? optionsWithKey[selectedItems[0]]?.label?.toString() || '' : '';
|
|
17639
|
+
}, [isMultiple, rawInput, selectedItems, optionsWithKey]);
|
|
17634
17640
|
const filteredChildren = (0,external_react_namespaceObject.useMemo)(() => {
|
|
17635
17641
|
// if filtering is disabled, or there's no input, show all
|
|
17636
17642
|
if (!filterOptions || !inputValue) return external_react_default().Children.toArray(children);
|
|
@@ -17648,7 +17654,7 @@ const useTypeahead = ({
|
|
|
17648
17654
|
const items = (0,external_react_namespaceObject.useMemo)(() => {
|
|
17649
17655
|
return filteredChildren.map((child, index) => {
|
|
17650
17656
|
if (! /*#__PURE__*/external_react_default().isValidElement(child)) return null;
|
|
17651
|
-
const isActive =
|
|
17657
|
+
const isActive = selectedItems.includes(child.props.value);
|
|
17652
17658
|
const {
|
|
17653
17659
|
value,
|
|
17654
17660
|
label,
|
|
@@ -17680,7 +17686,7 @@ const useTypeahead = ({
|
|
|
17680
17686
|
}) ?? child.props.children ?? label ?? value
|
|
17681
17687
|
});
|
|
17682
17688
|
});
|
|
17683
|
-
}, [children,
|
|
17689
|
+
}, [children, selectedItems, inputValue]);
|
|
17684
17690
|
const firstSuggestion = (0,external_react_namespaceObject.useMemo)(() => {
|
|
17685
17691
|
if (!inputValue) return '';
|
|
17686
17692
|
const needle = inputValue.toLowerCase();
|
|
@@ -17705,8 +17711,8 @@ const useTypeahead = ({
|
|
|
17705
17711
|
resetInput = true
|
|
17706
17712
|
}) => {
|
|
17707
17713
|
if (isDisabled || value == null) return;
|
|
17708
|
-
const alreadySelected =
|
|
17709
|
-
const updatedSelected = isMultiple ? alreadySelected ?
|
|
17714
|
+
const alreadySelected = selectedItems.includes(value);
|
|
17715
|
+
const updatedSelected = isMultiple ? alreadySelected ? selectedItems.filter(item => item !== value) : [...selectedItems, value] : alreadySelected ? [] : [value];
|
|
17710
17716
|
const fieldValue = isMultiple ? updatedSelected : updatedSelected[0];
|
|
17711
17717
|
setSelected(updatedSelected);
|
|
17712
17718
|
setValue?.(name, fieldValue);
|
|
@@ -17747,9 +17753,9 @@ const useTypeahead = ({
|
|
|
17747
17753
|
return;
|
|
17748
17754
|
}
|
|
17749
17755
|
// unset selected value if not fully matched
|
|
17750
|
-
if (!isMultiple &&
|
|
17756
|
+
if (!isMultiple && selectedItems.length > 0) {
|
|
17751
17757
|
handleChange({
|
|
17752
|
-
value:
|
|
17758
|
+
value: selectedItems[0],
|
|
17753
17759
|
shouldClose: false,
|
|
17754
17760
|
resetInput: false
|
|
17755
17761
|
});
|
|
@@ -17769,15 +17775,14 @@ const useTypeahead = ({
|
|
|
17769
17775
|
const match = findExactMatch(firstSuggestion, optionsWithKey);
|
|
17770
17776
|
if (match) {
|
|
17771
17777
|
handleChange({
|
|
17772
|
-
value: match.value
|
|
17773
|
-
shouldClose: false
|
|
17778
|
+
value: match.value
|
|
17774
17779
|
});
|
|
17775
17780
|
}
|
|
17776
17781
|
e.preventDefault();
|
|
17777
17782
|
return;
|
|
17778
17783
|
}
|
|
17779
|
-
if (isMultiple && e.code === 'Backspace' &&
|
|
17780
|
-
const lastSelected =
|
|
17784
|
+
if (isMultiple && e.code === 'Backspace' && selectedItems.length && !inputValue) {
|
|
17785
|
+
const lastSelected = selectedItems[selectedItems.length - 1];
|
|
17781
17786
|
handleChange({
|
|
17782
17787
|
value: lastSelected,
|
|
17783
17788
|
shouldClose: false
|
|
@@ -17802,8 +17807,8 @@ const useTypeahead = ({
|
|
|
17802
17807
|
return;
|
|
17803
17808
|
}
|
|
17804
17809
|
setIsOpen(open);
|
|
17805
|
-
if (!isMultiple &&
|
|
17806
|
-
const selectedValue =
|
|
17810
|
+
if (!isMultiple && selectedItems.length > 0) {
|
|
17811
|
+
const selectedValue = selectedItems[0];
|
|
17807
17812
|
const label = optionsWithKey[selectedValue]?.label;
|
|
17808
17813
|
setRawInput(label ? String(label) : null);
|
|
17809
17814
|
return;
|
|
@@ -17820,7 +17825,7 @@ const useTypeahead = ({
|
|
|
17820
17825
|
isOpen,
|
|
17821
17826
|
isDisabled,
|
|
17822
17827
|
optionsWithKey,
|
|
17823
|
-
selectedItems
|
|
17828
|
+
selectedItems,
|
|
17824
17829
|
inputRef,
|
|
17825
17830
|
firstSuggestion,
|
|
17826
17831
|
isMultiple,
|
|
@@ -18013,11 +18018,6 @@ const MultipleTrigger = () => {
|
|
|
18013
18018
|
disabled: context.isDisabled,
|
|
18014
18019
|
className: ['typeahead-input', TypeaheadInput(theme), TypeaheadInputPlaceholder].join(' '),
|
|
18015
18020
|
...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
18021
|
})]
|
|
18022
18022
|
}), !context.isDisabled && context.selectedItems.length ? (0,jsx_runtime_namespaceObject.jsx)(Button_Button, {
|
|
18023
18023
|
variant: "tertiary",
|
|
@@ -18073,11 +18073,6 @@ const SingleTrigger = () => {
|
|
|
18073
18073
|
tabIndex: -1,
|
|
18074
18074
|
className: ['typeahead-input', TypeaheadInput(theme), TypeaheadInputPlaceholder].join(' '),
|
|
18075
18075
|
...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
18076
|
}), !context.isDisabled && context.selectedItems.length ? (0,jsx_runtime_namespaceObject.jsx)(Button_Button, {
|
|
18082
18077
|
variant: "tertiary",
|
|
18083
18078
|
"data-testid": "remove-all-button",
|