@ssa-ui-kit/core 1.0.8 → 1.0.10

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ssa-ui-kit/core",
3
- "version": "1.0.8",
3
+ "version": "1.0.10",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "private": false,
@@ -63,6 +63,9 @@ export const Basic: StoryObj = (args: TypeaheadProps) => {
63
63
  <Typeahead
64
64
  initialSelectedItems={[items[2].id]}
65
65
  isDisabled={args.isDisabled}
66
+ onEmptyChange={(isEmpty) => {
67
+ console.log('>>>onEmptyChange event', isEmpty);
68
+ }}
66
69
  name={'typeahead-dropdown'}
67
70
  label="Label"
68
71
  helperText="Helper Text"
@@ -124,6 +127,9 @@ export const Multiple: StoryObj = (args: TypeaheadProps) => {
124
127
  initialSelectedItems={[items[2].id, items[1].id]}
125
128
  isMultiple
126
129
  isDisabled={args.isDisabled}
130
+ onEmptyChange={(isEmpty) => {
131
+ console.log('>>>onEmptyChange event', isEmpty);
132
+ }}
127
133
  label="Label"
128
134
  helperText="Helper Text"
129
135
  register={register}
@@ -45,6 +45,7 @@ export const Typeahead = ({
45
45
  setValue,
46
46
  register,
47
47
  onChange,
48
+ onEmptyChange,
48
49
  renderOption,
49
50
  }: TypeaheadProps) => {
50
51
  const theme = useTheme();
@@ -67,6 +68,7 @@ export const Typeahead = ({
67
68
  setValue,
68
69
  register,
69
70
  onChange,
71
+ onEmptyChange,
70
72
  renderOption,
71
73
  });
72
74
 
@@ -34,6 +34,7 @@ export interface TypeaheadProps {
34
34
  setValue?: UseFormSetValue<FieldValues>;
35
35
  register?: UseFormReturn['register'];
36
36
  onChange?: (selectedItem: TypeaheadValue, isSelected: boolean) => void;
37
+ onEmptyChange?: (isEmpty?: boolean) => void;
37
38
  renderOption?: (data: {
38
39
  value: string | number;
39
40
  input: string;
@@ -48,6 +49,7 @@ export type UseTypeaheadProps = Pick<
48
49
  | 'children'
49
50
  | 'isMultiple'
50
51
  | 'onChange'
52
+ | 'onEmptyChange'
51
53
  | 'renderOption'
52
54
  | 'isOpen'
53
55
  | 'className'
@@ -29,6 +29,7 @@ export const useTypeahead = ({
29
29
  register,
30
30
  setValue,
31
31
  onChange,
32
+ onEmptyChange,
32
33
  renderOption,
33
34
  }: UseTypeaheadProps) => {
34
35
  const inputName = `${name}-text`;
@@ -39,6 +40,7 @@ export const useTypeahead = ({
39
40
  const [optionsWithKey, setOptionsWithKey] = useState<
40
41
  Record<number | string, Record<string, string | number>>
41
42
  >({});
43
+ const [isEmpty, setIsEmpty] = useState<boolean>();
42
44
  const [isFirstRender, setFirstRender] = useState<boolean>(true);
43
45
  const [items, setItems] = useState<Array<React.ReactElement> | undefined>();
44
46
  const [inputValue, setInputValue] = useState<string>('');
@@ -69,8 +71,16 @@ export const useTypeahead = ({
69
71
  shouldDirty: !isFirstRender,
70
72
  });
71
73
  }
74
+
75
+ if (!isFirstRender) {
76
+ setIsEmpty(!selected.length);
77
+ }
72
78
  }, [selected]);
73
79
 
80
+ useEffect(() => {
81
+ onEmptyChange?.(isEmpty);
82
+ }, [isEmpty]);
83
+
74
84
  useEffect(() => {
75
85
  if (isDisabled && isOpen) {
76
86
  setIsOpen(false);
@@ -261,7 +271,11 @@ export const useTypeahead = ({
261
271
  inputRef.current?.focus();
262
272
  event.stopPropagation();
263
273
  event.preventDefault();
264
- } else if (['Tab', 'Enter'].includes(event.code) && firstSuggestion) {
274
+ } else if (
275
+ ['Tab', 'Enter'].includes(event.code) &&
276
+ firstSuggestion &&
277
+ firstSuggestion !== inputValue
278
+ ) {
265
279
  const foundItem = Object.values(optionsWithKey).find(
266
280
  (item) =>
267
281
  `${item.label}`.toLowerCase() === firstSuggestion.toLowerCase(),
@@ -281,7 +295,7 @@ export const useTypeahead = ({
281
295
  handleChange(selected[selected.length - 1]);
282
296
  event.preventDefault();
283
297
  return false;
284
- } else if (!isOpen) {
298
+ } else if (!isOpen && firstSuggestion !== inputValue) {
285
299
  setIsOpen(true);
286
300
  }
287
301
  };