@bbl-digital/snorre 3.0.10 → 3.0.14

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/bundle.js CHANGED
@@ -31850,41 +31850,45 @@
31850
31850
  const [inputDirty, setInputDirty] = React.useState(false);
31851
31851
  const debouncedValue = useDebounce(value, props.debounceDelay ?? 0);
31852
31852
  const [inputValues, setInputValues] = React.useState(props.values);
31853
+ const labelKey = props.labelFromValues ?? 'label';
31853
31854
 
31854
31855
  const handleValueClick = value => {
31855
- setShowValues(false);
31856
31856
  props.onSelectItem?.(value);
31857
+ if (props.fuzzy) setValueChanged(value[labelKey]);
31858
+ setShowValues(false);
31857
31859
  };
31858
31860
 
31859
31861
  const clearSelectedItem = () => handleValueClick({
31860
- [props.labelFromValues ?? 'label']: '',
31862
+ [labelKey]: '',
31861
31863
  [props.keyFromValues ?? 'key']: ''
31862
31864
  });
31863
31865
 
31864
31866
  const onInputChange = e => {
31865
31867
  setInputDirty(true);
31866
- if (inputValues) setHighlightedIndex(0);
31868
+ if (renderedValues) setHighlightedIndex(0);
31867
31869
  setValueChanged(e.target.value); // Should not use onChange whenever we have fuzzy search on
31868
31870
 
31869
- if (props.fuzzy) return;
31871
+ if (props.fuzzy) {
31872
+ setShowValues(Boolean(value));
31873
+ return;
31874
+ }
31875
+
31870
31876
  if (props.onChange) props.onChange(e);
31871
31877
  };
31872
31878
 
31873
31879
  const handleOnInputClick = () => {
31874
- if (inputValues?.length) setHighlightedIndex(0);
31880
+ if (renderedValues?.length) setHighlightedIndex(0);
31875
31881
  setShowValues(true);
31876
31882
  };
31877
31883
 
31878
31884
  const onFuzzyBlur = e => {
31879
- // If the value of the input is changed, and does not match the value of the values array,
31880
- // we should revert back the input value to the original value
31881
31885
  if (e.target.value === props.value) return; // If target value is the same as a value from the values array, we should set the value for the user
31882
31886
 
31883
31887
  if (inputValues?.length) {
31884
- const valueInInputValues = inputValues.find(item => item[props.labelFromValues ?? 'label'].length && item[props.labelFromValues ?? 'label'].toLowerCase() === e.target.value.toLowerCase());
31888
+ const valueInInputValues = inputValues.find(item => item[labelKey].length && item[labelKey].toLowerCase() === e.target.value.toLowerCase());
31885
31889
 
31886
31890
  if (valueInInputValues) {
31887
- const val = valueInInputValues[props.labelFromValues ?? 'label'];
31891
+ const val = valueInInputValues[labelKey];
31888
31892
  setValueChanged(val);
31889
31893
  handleValueClick(valueInInputValues);
31890
31894
  return;
@@ -31892,14 +31896,18 @@
31892
31896
  }
31893
31897
  };
31894
31898
 
31899
+ const onFuzzyFocus = () => {
31900
+ setValueChanged('');
31901
+ if (props.values?.length) setShowValues(true);
31902
+ };
31903
+
31895
31904
  const renderedValues = React.useMemo(() => {
31896
31905
  if (!props.values?.length) return [];
31897
31906
 
31898
31907
  if (props.fuzzy) {
31899
31908
  const fuzzyOptions = {
31900
- keys: [props.labelFromValues ?? 'label']
31909
+ keys: [labelKey]
31901
31910
  };
31902
- setShowValues(Boolean(value));
31903
31911
  return matchSorter.matchSorter(props.values, value ?? '', fuzzyOptions);
31904
31912
  }
31905
31913
 
@@ -31928,11 +31936,11 @@
31928
31936
  });
31929
31937
  }
31930
31938
 
31931
- if (highlightedIndex === null || !inputValues?.length) {
31939
+ if (highlightedIndex === null || !renderedValues?.length) {
31932
31940
  return;
31933
31941
  }
31934
31942
 
31935
- const item = inputValues[highlightedIndex];
31943
+ const item = renderedValues[highlightedIndex];
31936
31944
  handleValueClick(item);
31937
31945
  setShowValues(false);
31938
31946
  };
@@ -31942,7 +31950,7 @@
31942
31950
  e.stopPropagation();
31943
31951
  if (!showValues) return;
31944
31952
 
31945
- if (highlightedIndex === null || !inputValues?.length) {
31953
+ if (highlightedIndex === null || !renderedValues?.length) {
31946
31954
  return;
31947
31955
  }
31948
31956
 
@@ -31962,13 +31970,13 @@
31962
31970
  e.preventDefault();
31963
31971
  e.stopPropagation();
31964
31972
 
31965
- if (highlightedIndex === null || !inputValues?.length) {
31973
+ if (highlightedIndex === null || !renderedValues?.length) {
31966
31974
  return;
31967
31975
  }
31968
31976
 
31969
31977
  const newHighlightIndex = highlightedIndex + 1;
31970
31978
 
31971
- if (newHighlightIndex === inputValues.length) {
31979
+ if (newHighlightIndex === renderedValues.length) {
31972
31980
  setHighlightedIndex(null);
31973
31981
  setShowValues(false);
31974
31982
  return;
@@ -31996,7 +32004,7 @@
31996
32004
  key
31997
32005
  } = e;
31998
32006
 
31999
- if (showValues && inputValues?.length) {
32007
+ if (showValues && renderedValues?.length) {
32000
32008
  setHighlightedIndex(0);
32001
32009
  } else {
32002
32010
  setHighlightedIndex(null);
@@ -32058,6 +32066,7 @@
32058
32066
  handleCustomOnKeyDown,
32059
32067
  handleOnInputClick,
32060
32068
  onFuzzyBlur,
32069
+ onFuzzyFocus,
32061
32070
  value,
32062
32071
  highlightedIndex,
32063
32072
  showValues,
@@ -32092,7 +32101,8 @@
32092
32101
  handleOnKeyDown,
32093
32102
  handleValueClick,
32094
32103
  onInputChange,
32095
- onFuzzyBlur
32104
+ onFuzzyBlur,
32105
+ onFuzzyFocus
32096
32106
  } = useAutocomplete(props);
32097
32107
  React.useEffect(() => {
32098
32108
  const handleClickOutside = e => {
@@ -32137,7 +32147,7 @@
32137
32147
  disabled: props.disabled,
32138
32148
  autoFocus: props.focus,
32139
32149
  onBlur: props.fuzzy ? onFuzzyBlur : props.onBlur,
32140
- onFocus: props.onFocus,
32150
+ onFocus: props.fuzzy ? onFuzzyFocus : props.onFocus,
32141
32151
  onChange: onInputChange,
32142
32152
  onKeyDown: props.onKeyDown ? handleCustomOnKeyDown : handleOnKeyDown,
32143
32153
  onClick: handleOnInputClick,
@@ -10,41 +10,45 @@ const useAutocomplete = props => {
10
10
  const [inputDirty, setInputDirty] = useState(false);
11
11
  const debouncedValue = useDebounce(value, props.debounceDelay ?? 0);
12
12
  const [inputValues, setInputValues] = useState(props.values);
13
+ const labelKey = props.labelFromValues ?? 'label';
13
14
 
14
15
  const handleValueClick = value => {
15
- setShowValues(false);
16
16
  props.onSelectItem?.(value);
17
+ if (props.fuzzy) setValueChanged(value[labelKey]);
18
+ setShowValues(false);
17
19
  };
18
20
 
19
21
  const clearSelectedItem = () => handleValueClick({
20
- [props.labelFromValues ?? 'label']: '',
22
+ [labelKey]: '',
21
23
  [props.keyFromValues ?? 'key']: ''
22
24
  });
23
25
 
24
26
  const onInputChange = e => {
25
27
  setInputDirty(true);
26
- if (inputValues) setHighlightedIndex(0);
28
+ if (renderedValues) setHighlightedIndex(0);
27
29
  setValueChanged(e.target.value); // Should not use onChange whenever we have fuzzy search on
28
30
 
29
- if (props.fuzzy) return;
31
+ if (props.fuzzy) {
32
+ setShowValues(Boolean(value));
33
+ return;
34
+ }
35
+
30
36
  if (props.onChange) props.onChange(e);
31
37
  };
32
38
 
33
39
  const handleOnInputClick = () => {
34
- if (inputValues?.length) setHighlightedIndex(0);
40
+ if (renderedValues?.length) setHighlightedIndex(0);
35
41
  setShowValues(true);
36
42
  };
37
43
 
38
44
  const onFuzzyBlur = e => {
39
- // If the value of the input is changed, and does not match the value of the values array,
40
- // we should revert back the input value to the original value
41
45
  if (e.target.value === props.value) return; // If target value is the same as a value from the values array, we should set the value for the user
42
46
 
43
47
  if (inputValues?.length) {
44
- const valueInInputValues = inputValues.find(item => item[props.labelFromValues ?? 'label'].length && item[props.labelFromValues ?? 'label'].toLowerCase() === e.target.value.toLowerCase());
48
+ const valueInInputValues = inputValues.find(item => item[labelKey].length && item[labelKey].toLowerCase() === e.target.value.toLowerCase());
45
49
 
46
50
  if (valueInInputValues) {
47
- const val = valueInInputValues[props.labelFromValues ?? 'label'];
51
+ const val = valueInInputValues[labelKey];
48
52
  setValueChanged(val);
49
53
  handleValueClick(valueInInputValues);
50
54
  return;
@@ -52,14 +56,18 @@ const useAutocomplete = props => {
52
56
  }
53
57
  };
54
58
 
59
+ const onFuzzyFocus = () => {
60
+ setValueChanged('');
61
+ if (props.values?.length) setShowValues(true);
62
+ };
63
+
55
64
  const renderedValues = useMemo(() => {
56
65
  if (!props.values?.length) return [];
57
66
 
58
67
  if (props.fuzzy) {
59
68
  const fuzzyOptions = {
60
- keys: [props.labelFromValues ?? 'label']
69
+ keys: [labelKey]
61
70
  };
62
- setShowValues(Boolean(value));
63
71
  return matchSorter(props.values, value ?? '', fuzzyOptions);
64
72
  }
65
73
 
@@ -88,11 +96,11 @@ const useAutocomplete = props => {
88
96
  });
89
97
  }
90
98
 
91
- if (highlightedIndex === null || !inputValues?.length) {
99
+ if (highlightedIndex === null || !renderedValues?.length) {
92
100
  return;
93
101
  }
94
102
 
95
- const item = inputValues[highlightedIndex];
103
+ const item = renderedValues[highlightedIndex];
96
104
  handleValueClick(item);
97
105
  setShowValues(false);
98
106
  };
@@ -102,7 +110,7 @@ const useAutocomplete = props => {
102
110
  e.stopPropagation();
103
111
  if (!showValues) return;
104
112
 
105
- if (highlightedIndex === null || !inputValues?.length) {
113
+ if (highlightedIndex === null || !renderedValues?.length) {
106
114
  return;
107
115
  }
108
116
 
@@ -122,13 +130,13 @@ const useAutocomplete = props => {
122
130
  e.preventDefault();
123
131
  e.stopPropagation();
124
132
 
125
- if (highlightedIndex === null || !inputValues?.length) {
133
+ if (highlightedIndex === null || !renderedValues?.length) {
126
134
  return;
127
135
  }
128
136
 
129
137
  const newHighlightIndex = highlightedIndex + 1;
130
138
 
131
- if (newHighlightIndex === inputValues.length) {
139
+ if (newHighlightIndex === renderedValues.length) {
132
140
  setHighlightedIndex(null);
133
141
  setShowValues(false);
134
142
  return;
@@ -156,7 +164,7 @@ const useAutocomplete = props => {
156
164
  key
157
165
  } = e;
158
166
 
159
- if (showValues && inputValues?.length) {
167
+ if (showValues && renderedValues?.length) {
160
168
  setHighlightedIndex(0);
161
169
  } else {
162
170
  setHighlightedIndex(null);
@@ -221,6 +229,7 @@ const useAutocomplete = props => {
221
229
  handleCustomOnKeyDown,
222
230
  handleOnInputClick,
223
231
  onFuzzyBlur,
232
+ onFuzzyFocus,
224
233
  value,
225
234
  highlightedIndex,
226
235
  showValues,
@@ -37,7 +37,8 @@ const Autocomplete = /*#__PURE__*/React.forwardRef(({
37
37
  handleOnKeyDown,
38
38
  handleValueClick,
39
39
  onInputChange,
40
- onFuzzyBlur
40
+ onFuzzyBlur,
41
+ onFuzzyFocus
41
42
  } = useAutocomplete(props);
42
43
  useEffect(() => {
43
44
  const handleClickOutside = e => {
@@ -82,7 +83,7 @@ const Autocomplete = /*#__PURE__*/React.forwardRef(({
82
83
  disabled: props.disabled,
83
84
  autoFocus: props.focus,
84
85
  onBlur: props.fuzzy ? onFuzzyBlur : props.onBlur,
85
- onFocus: props.onFocus,
86
+ onFocus: props.fuzzy ? onFuzzyFocus : props.onFocus,
86
87
  onChange: onInputChange,
87
88
  onKeyDown: props.onKeyDown ? handleCustomOnKeyDown : handleOnKeyDown,
88
89
  onClick: handleOnInputClick,
@@ -11,6 +11,7 @@ declare const useAutocomplete: (props: IProps) => {
11
11
  handleCustomOnKeyDown: (e: React.KeyboardEvent<HTMLInputElement>) => void;
12
12
  handleOnInputClick: () => void;
13
13
  onFuzzyBlur: (e: React.FocusEvent<HTMLInputElement, Element>) => void;
14
+ onFuzzyFocus: () => void;
14
15
  value: string;
15
16
  highlightedIndex: number | null;
16
17
  showValues: boolean;
@@ -1 +1 @@
1
- {"version":3,"file":"useAutocomplete.d.ts","sourceRoot":"","sources":["../../../../src/packages/core/Autocomplete/hooks/useAutocomplete.ts"],"names":[],"mappings":";AAIA,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAA;AAE1D,QAAA,MAAM,eAAe,UAAW,MAAM;;;;;;uBAuBV,MAAM,WAAW,CAAC,gBAAgB,CAAC;yBA6IjC,mBAAmB,CAAC,gBAAgB,CAAC;+BAZ/B,mBAAmB,CAAC,gBAAgB,CAAC;;qBAhH/C,gBAAgB,CAAC,gBAAgB,EAAE,OAAO,CAAC;;;;;CAyMpE,CAAA;AAED,eAAe,eAAe,CAAA"}
1
+ {"version":3,"file":"useAutocomplete.d.ts","sourceRoot":"","sources":["../../../../src/packages/core/Autocomplete/hooks/useAutocomplete.ts"],"names":[],"mappings":";AAIA,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAA;AAE1D,QAAA,MAAM,eAAe,UAAW,MAAM;;;;;;uBA2BV,MAAM,WAAW,CAAC,gBAAgB,CAAC;yBA6IjC,mBAAmB,CAAC,gBAAgB,CAAC;+BAZ/B,mBAAmB,CAAC,gBAAgB,CAAC;;qBA7G/C,gBAAgB,CAAC,gBAAgB,EAAE,OAAO,CAAC;;;;;;CAuMpE,CAAA;AAED,eAAe,eAAe,CAAA"}
@@ -10,41 +10,45 @@ const useAutocomplete = props => {
10
10
  const [inputDirty, setInputDirty] = useState(false);
11
11
  const debouncedValue = useDebounce(value, props.debounceDelay ?? 0);
12
12
  const [inputValues, setInputValues] = useState(props.values);
13
+ const labelKey = props.labelFromValues ?? 'label';
13
14
 
14
15
  const handleValueClick = value => {
15
- setShowValues(false);
16
16
  props.onSelectItem?.(value);
17
+ if (props.fuzzy) setValueChanged(value[labelKey]);
18
+ setShowValues(false);
17
19
  };
18
20
 
19
21
  const clearSelectedItem = () => handleValueClick({
20
- [props.labelFromValues ?? 'label']: '',
22
+ [labelKey]: '',
21
23
  [props.keyFromValues ?? 'key']: ''
22
24
  });
23
25
 
24
26
  const onInputChange = e => {
25
27
  setInputDirty(true);
26
- if (inputValues) setHighlightedIndex(0);
28
+ if (renderedValues) setHighlightedIndex(0);
27
29
  setValueChanged(e.target.value); // Should not use onChange whenever we have fuzzy search on
28
30
 
29
- if (props.fuzzy) return;
31
+ if (props.fuzzy) {
32
+ setShowValues(Boolean(value));
33
+ return;
34
+ }
35
+
30
36
  if (props.onChange) props.onChange(e);
31
37
  };
32
38
 
33
39
  const handleOnInputClick = () => {
34
- if (inputValues?.length) setHighlightedIndex(0);
40
+ if (renderedValues?.length) setHighlightedIndex(0);
35
41
  setShowValues(true);
36
42
  };
37
43
 
38
44
  const onFuzzyBlur = e => {
39
- // If the value of the input is changed, and does not match the value of the values array,
40
- // we should revert back the input value to the original value
41
45
  if (e.target.value === props.value) return; // If target value is the same as a value from the values array, we should set the value for the user
42
46
 
43
47
  if (inputValues?.length) {
44
- const valueInInputValues = inputValues.find(item => item[props.labelFromValues ?? 'label'].length && item[props.labelFromValues ?? 'label'].toLowerCase() === e.target.value.toLowerCase());
48
+ const valueInInputValues = inputValues.find(item => item[labelKey].length && item[labelKey].toLowerCase() === e.target.value.toLowerCase());
45
49
 
46
50
  if (valueInInputValues) {
47
- const val = valueInInputValues[props.labelFromValues ?? 'label'];
51
+ const val = valueInInputValues[labelKey];
48
52
  setValueChanged(val);
49
53
  handleValueClick(valueInInputValues);
50
54
  return;
@@ -52,14 +56,18 @@ const useAutocomplete = props => {
52
56
  }
53
57
  };
54
58
 
59
+ const onFuzzyFocus = () => {
60
+ setValueChanged('');
61
+ if (props.values?.length) setShowValues(true);
62
+ };
63
+
55
64
  const renderedValues = useMemo(() => {
56
65
  if (!props.values?.length) return [];
57
66
 
58
67
  if (props.fuzzy) {
59
68
  const fuzzyOptions = {
60
- keys: [props.labelFromValues ?? 'label']
69
+ keys: [labelKey]
61
70
  };
62
- setShowValues(Boolean(value));
63
71
  return matchSorter(props.values, value ?? '', fuzzyOptions);
64
72
  }
65
73
 
@@ -88,11 +96,11 @@ const useAutocomplete = props => {
88
96
  });
89
97
  }
90
98
 
91
- if (highlightedIndex === null || !inputValues?.length) {
99
+ if (highlightedIndex === null || !renderedValues?.length) {
92
100
  return;
93
101
  }
94
102
 
95
- const item = inputValues[highlightedIndex];
103
+ const item = renderedValues[highlightedIndex];
96
104
  handleValueClick(item);
97
105
  setShowValues(false);
98
106
  };
@@ -102,7 +110,7 @@ const useAutocomplete = props => {
102
110
  e.stopPropagation();
103
111
  if (!showValues) return;
104
112
 
105
- if (highlightedIndex === null || !inputValues?.length) {
113
+ if (highlightedIndex === null || !renderedValues?.length) {
106
114
  return;
107
115
  }
108
116
 
@@ -122,13 +130,13 @@ const useAutocomplete = props => {
122
130
  e.preventDefault();
123
131
  e.stopPropagation();
124
132
 
125
- if (highlightedIndex === null || !inputValues?.length) {
133
+ if (highlightedIndex === null || !renderedValues?.length) {
126
134
  return;
127
135
  }
128
136
 
129
137
  const newHighlightIndex = highlightedIndex + 1;
130
138
 
131
- if (newHighlightIndex === inputValues.length) {
139
+ if (newHighlightIndex === renderedValues.length) {
132
140
  setHighlightedIndex(null);
133
141
  setShowValues(false);
134
142
  return;
@@ -156,7 +164,7 @@ const useAutocomplete = props => {
156
164
  key
157
165
  } = e;
158
166
 
159
- if (showValues && inputValues?.length) {
167
+ if (showValues && renderedValues?.length) {
160
168
  setHighlightedIndex(0);
161
169
  } else {
162
170
  setHighlightedIndex(null);
@@ -221,6 +229,7 @@ const useAutocomplete = props => {
221
229
  handleCustomOnKeyDown,
222
230
  handleOnInputClick,
223
231
  onFuzzyBlur,
232
+ onFuzzyFocus,
224
233
  value,
225
234
  highlightedIndex,
226
235
  showValues,
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/packages/core/Autocomplete/index.tsx"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,OAAO,KAAK,EAAE,EAAa,YAAY,EAAE,MAAM,OAAO,CAAA;AAQtD,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAA;AAG3C,oBAAY,GAAG,GAAG,YAAY,CAAC,gBAAgB,CAAC,CAAA;AAEhD,QAAA,MAAM,YAAY,EAAE,KAAK,CAAC,EAAE,CAAC,MAAM,CAoLjC,CAAA;AAEF,eAAe,YAAY,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/packages/core/Autocomplete/index.tsx"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,OAAO,KAAK,EAAE,EAAa,YAAY,EAAE,MAAM,OAAO,CAAA;AAQtD,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAA;AAG3C,oBAAY,GAAG,GAAG,YAAY,CAAC,gBAAgB,CAAC,CAAA;AAEhD,QAAA,MAAM,YAAY,EAAE,KAAK,CAAC,EAAE,CAAC,MAAM,CAqLjC,CAAA;AAEF,eAAe,YAAY,CAAA"}
@@ -37,7 +37,8 @@ const Autocomplete = /*#__PURE__*/React.forwardRef(({
37
37
  handleOnKeyDown,
38
38
  handleValueClick,
39
39
  onInputChange,
40
- onFuzzyBlur
40
+ onFuzzyBlur,
41
+ onFuzzyFocus
41
42
  } = useAutocomplete(props);
42
43
  useEffect(() => {
43
44
  const handleClickOutside = e => {
@@ -82,7 +83,7 @@ const Autocomplete = /*#__PURE__*/React.forwardRef(({
82
83
  disabled: props.disabled,
83
84
  autoFocus: props.focus,
84
85
  onBlur: props.fuzzy ? onFuzzyBlur : props.onBlur,
85
- onFocus: props.onFocus,
86
+ onFocus: props.fuzzy ? onFuzzyFocus : props.onFocus,
86
87
  onChange: onInputChange,
87
88
  onKeyDown: props.onKeyDown ? handleCustomOnKeyDown : handleOnKeyDown,
88
89
  onClick: handleOnInputClick,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bbl-digital/snorre",
3
- "version": "3.0.10",
3
+ "version": "3.0.14",
4
4
  "description": "Design library for BBL Digital",
5
5
  "license": "MIT",
6
6
  "main": "./lib/index.js",