@choc-ui/chakra-autocomplete 4.9.0 → 4.10.2

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/README.md CHANGED
@@ -147,7 +147,7 @@ function App() {
147
147
  <AutoCompleteInput variant="filled" />
148
148
  <AutoCompleteList>
149
149
  {Object.entries(continents).map(([continent, countries], co_id) => (
150
- <AutoCompleteGroup key={co_id} id={continent} showDivider>
150
+ <AutoCompleteGroup key={co_id} showDivider>
151
151
  <AutoCompleteGroupTitle textTransform="capitalize">
152
152
  {continent}
153
153
  </AutoCompleteGroupTitle>
@@ -155,7 +155,6 @@ function App() {
155
155
  <AutoCompleteItem
156
156
  key={c_id}
157
157
  value={country}
158
- groupId={continent}
159
158
  textTransform="capitalize"
160
159
  >
161
160
  {country}
@@ -178,7 +177,7 @@ export default App;
178
177
 
179
178
  ## Accessing the internal state
180
179
 
181
- To access the internal state of the `AutoComplete`, use a function as children (commonly known as a render prop). You'll get access to the internal state `isOpen`, with the `onOpen`, `onClose`, `removeItem`, `resetItems` methods and the `tags` in multiple mode.
180
+ To access the internal state of the `AutoComplete`, use a function as children (commonly known as a render prop). You'll get access to the internal state `isOpen`, with the `onOpen` and `onClose` methods.
182
181
 
183
182
  ```js
184
183
  import {
@@ -386,6 +385,13 @@ Then add the `AutoCompleteCreatable` component to the bottom of the list. Refer
386
385
 
387
386
  <img width="517" alt="CleanShot 2021-07-29 at 02 29 20@2x" src="https://user-images.githubusercontent.com/30869823/127417453-e78b9b48-26e8-4ff0-a264-1d6bb4717ab0.png">
388
387
 
388
+ ### Autocomplete methods
389
+ Assign a ref to the `AutoComplete` component and call the available methods with:
390
+ ```js
391
+ ref.current?.resetItems()
392
+ ref.current?.removeItem(itemValue)
393
+ ```
394
+
389
395
  ### Codesandbox Link [Here](https://githubbox.com/anubra266/choc-autocomplete/tree/main/demo)
390
396
 
391
397
  ## API Reference
@@ -560,6 +566,17 @@ boolean | MaybeRenderProp<{ value: Item["value"] }>
560
566
  <td>method to call whenever a suggestion is focused</td>
561
567
  <td>&mdash;&mdash;&mdash;</td>
562
568
  </tr>
569
+ <tr>
570
+ <td>onReady</td>
571
+ <td>
572
+
573
+ ```ts
574
+ (props:{tags:ItemTag[]}) => void
575
+ ```
576
+ </td>
577
+ <td>method that exposes variables used in component</td>
578
+ <td>&mdash;&mdash;&mdash;</td>
579
+ </tr>
563
580
  <tr>
564
581
  <td>onTagRemoved</td>
565
582
  <td>
@@ -794,13 +811,6 @@ val => val;
794
811
 
795
812
  </td>
796
813
  </tr>
797
- <tr>
798
- <td>groupId</td>
799
- <td>string</td>
800
- <td>The id of the group, if Item belongs to one. It's required if you need to wrap the items in an HOC.</td>
801
- <td>no<br></td>
802
- <td>&mdash;&mdash;&mdash;</td>
803
- </tr>
804
814
  <tr>
805
815
  <td>label</td>
806
816
  <td>string</td>
@@ -1013,7 +1023,6 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
1013
1023
  <td align="center"><a href="https://anubra266.tk"><img src="https://avatars.githubusercontent.com/u/30869823?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Abraham</b></sub></a><br /><a href="https://github.com/anubra266/choc-autocomplete/commits?author=anubra266" title="Code">💻</a></td>
1014
1024
  <td align="center"><a href="http://margalit.com.au"><img src="https://avatars.githubusercontent.com/u/2268424?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Sam Margalit</b></sub></a><br /><a href="https://github.com/anubra266/choc-autocomplete/commits?author=margalit" title="Documentation">📖</a></td>
1015
1025
  <td align="center"><a href="https://github.com/gepd"><img src="https://avatars.githubusercontent.com/u/7091609?v=4?s=100" width="100px;" alt=""/><br /><sub><b>gepd</b></sub></a><br /><a href="https://github.com/anubra266/choc-autocomplete/commits?author=gepd" title="Code">💻</a> <a href="https://github.com/anubra266/choc-autocomplete/issues?q=author%3Agepd" title="Bug reports">🐛</a></td>
1016
- <td align="center"><a href="https://github.com/Buupu"><img src="https://avatars.githubusercontent.com/u/26443341?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Sammy Fattah</b></sub></a><br /><a href="https://github.com/anubra266/choc-autocomplete/commits?author=Buupu" title="Code">💻</a></td>
1017
1026
  </tr>
1018
1027
  </table>
1019
1028
 
@@ -7,7 +7,6 @@ export interface AutoCompleteItemProps extends FlexProps {
7
7
  disabled?: boolean;
8
8
  _fixed?: CSSObject;
9
9
  getValue?: (item: AutoCompleteItemProps["value"]) => any;
10
- groupId?: string;
11
10
  }
12
11
  export declare const AutoCompleteItem: import("@chakra-ui/react").ComponentWithAs<"div", AutoCompleteItemProps>;
13
12
  export declare const baseItemStyles: FlexProps;
@@ -1,5 +1,13 @@
1
- import { UseAutoCompleteProps, UseAutoCompleteReturn } from "./types";
1
+ import React from "react";
2
+ import { MaybeRenderProp } from "@chakra-ui/react-utils";
3
+ import { AutoCompleteRefMethods, UseAutoCompleteProps } from "./types";
4
+ export declare type AutoCompleteChildProps = {
5
+ isOpen: boolean;
6
+ onClose: () => void;
7
+ onOpen: () => void;
8
+ };
2
9
  export interface AutoCompleteProps extends UseAutoCompleteProps {
3
- children: UseAutoCompleteReturn["children"];
10
+ children: MaybeRenderProp<AutoCompleteChildProps>;
11
+ ref?: React.RefObject<AutoCompleteRefMethods>;
4
12
  }
5
13
  export declare const AutoComplete: import("@chakra-ui/react").ComponentWithAs<"div", AutoCompleteProps>;
@@ -9,12 +9,13 @@ var React__default = _interopDefault(React);
9
9
  var reactUtils = require('@chakra-ui/react-utils');
10
10
  var react = require('@chakra-ui/react');
11
11
  var utils = require('@chakra-ui/utils');
12
+ var reactNanny = require('react-nanny');
12
13
  var layout = require('@chakra-ui/layout');
13
14
  var tag = require('@chakra-ui/tag');
14
15
 
15
16
  var _createContext = /*#__PURE__*/reactUtils.createContext({
16
17
  name: "AutoCompleteContext",
17
- errorMessage: "useAutoCompleteContext: `context` is undefined. Seems you forgot to wrap all autocomplete components within `<AutoComplete />`"
18
+ errorMessage: "useAutoCompleteContext: `context` is undefined. Seems you forgot to wrap all autoomplete components within `<AutoComplete />`"
18
19
  }),
19
20
  AutoCompleteProvider = _createContext[0],
20
21
  useAutoCompleteContext = _createContext[1];
@@ -136,6 +137,9 @@ function fuzzyScore(str1, str2, gramSize) {
136
137
  return hits / total;
137
138
  }
138
139
 
140
+ var getDefItemValue = function getDefItemValue(item) {
141
+ return (typeof item === "string" ? item : item[Object.keys(item)[0]]).toString();
142
+ };
139
143
  var setEmphasis = function setEmphasis(children, query) {
140
144
  if (typeof children !== "string" || utils.isEmpty(query)) {
141
145
  return children;
@@ -146,6 +150,25 @@ var setEmphasis = function setEmphasis(children, query) {
146
150
  });
147
151
  return newChildren;
148
152
  };
153
+ var getItemList = function getItemList(children) {
154
+ var itemChildren = reactNanny.getChildrenDeep(children, function (child) {
155
+ var _child$type;
156
+
157
+ return (child == null ? void 0 : (_child$type = child.type) == null ? void 0 : _child$type.displayName) === "AutoCompleteItem";
158
+ });
159
+ return itemChildren.map(function (item) {
160
+ var itemObj = utils.pick(item.props, ["value", "label", "fixed", "disabled"]);
161
+ var _item$props$getValue = item.props.getValue,
162
+ getValue = _item$props$getValue === void 0 ? getDefItemValue : _item$props$getValue;
163
+ var value = getValue(itemObj.value);
164
+ var finObj = utils.isDefined(itemObj.label) ? itemObj : _extends({}, itemObj, {
165
+ label: value
166
+ });
167
+ return _extends({}, finObj, {
168
+ value: value
169
+ });
170
+ });
171
+ };
149
172
  var getFocusedStyles = function getFocusedStyles() {
150
173
  return {
151
174
  bg: "whiteAlpha.100",
@@ -176,16 +199,30 @@ var getMultipleWrapStyles = function getMultipleWrapStyles(themeInput, multiple)
176
199
  });
177
200
  };
178
201
 
179
- var hasFirstItem = function hasFirstItem(props, firstItem) {
180
- return utils.isDefined(firstItem == null ? void 0 : firstItem.groupId) && utils.isDefined(props.id) && (firstItem == null ? void 0 : firstItem.groupId) === props.id;
181
- };
182
- var hasLastItem = function hasLastItem(props, lastItem) {
183
- return utils.isDefined(lastItem == null ? void 0 : lastItem.groupId) && utils.isDefined(props.id) && (lastItem == null ? void 0 : lastItem.groupId) === props.id;
202
+ var hasFirstItem = function hasFirstItem(children, firstItem) {
203
+ var result = reactNanny.getChildDeep(children, function (child) {
204
+ var _child$type;
205
+
206
+ return (child == null ? void 0 : (_child$type = child.type) == null ? void 0 : _child$type.displayName) === "AutoCompleteItem" && child.props.value === (firstItem == null ? void 0 : firstItem.value);
207
+ });
208
+ return result;
184
209
  };
185
- var hasChildren = function hasChildren(props, filteredList) {
186
- return filteredList.some(function (item) {
187
- return utils.isDefined(item.groupId) && utils.isDefined(props.id) && item.groupId === props.id;
210
+ var hasLastItem = function hasLastItem(children, lastItem) {
211
+ var result = reactNanny.getChildDeep(children, function (child) {
212
+ var _child$type2;
213
+
214
+ return (child == null ? void 0 : (_child$type2 = child.type) == null ? void 0 : _child$type2.displayName) === "AutoCompleteItem" && child.props.value === (lastItem == null ? void 0 : lastItem.value);
188
215
  });
216
+ return result;
217
+ };
218
+ var hasChildren = function hasChildren(children, filteredList) {
219
+ return utils.isDefined(reactNanny.getChildDeep(children, function (child) {
220
+ return filteredList.findIndex(function (i) {
221
+ var _child$props;
222
+
223
+ return i.value === ((_child$props = child.props) == null ? void 0 : _child$props.value);
224
+ }) >= 0;
225
+ }));
189
226
  };
190
227
 
191
228
  var _excluded = ["onBlur", "onChange", "onFocus", "onKeyDown", "variant", "wrapStyles"],
@@ -213,6 +250,7 @@ function useAutoComplete(autoCompleteProps) {
213
250
  listAllValuesOnFocus = autoCompleteProps.listAllValuesOnFocus,
214
251
  maxSuggestions = autoCompleteProps.maxSuggestions,
215
252
  multiple = autoCompleteProps.multiple,
253
+ onReady = autoCompleteProps.onReady,
216
254
  defaultIsOpen = autoCompleteProps.defaultIsOpen,
217
255
  _autoCompleteProps$sh = autoCompleteProps.shouldRenderSuggestions,
218
256
  shouldRenderSuggestions = _autoCompleteProps$sh === void 0 ? function () {
@@ -229,22 +267,24 @@ function useAutoComplete(autoCompleteProps) {
229
267
  onClose = _useDisclosure.onClose,
230
268
  onOpen = _useDisclosure.onOpen;
231
269
 
232
- var _useState = React.useState([]),
233
- itemList = _useState[0],
234
- setItemList = _useState[1];
235
-
270
+ var children = utils.runIfFn(autoCompleteProps.children, {
271
+ isOpen: isOpen,
272
+ onClose: onClose,
273
+ onOpen: onOpen
274
+ });
275
+ var itemList = getItemList(children);
236
276
  var inputRef = React.useRef(null);
237
277
  var inputWrapperRef = React.useRef(null);
238
278
  var listRef = React.useRef(null);
239
279
  var interactionRef = React.useRef(null);
240
280
 
241
- var _useState2 = React.useState(""),
242
- query = _useState2[0],
243
- setQuery = _useState2[1];
281
+ var _useState = React.useState(""),
282
+ query = _useState[0],
283
+ setQuery = _useState[1];
244
284
 
245
- var _useState3 = React.useState(defaultValues),
246
- values = _useState3[0],
247
- setValues = _useState3[1];
285
+ var _useState2 = React.useState(defaultValues),
286
+ values = _useState2[0],
287
+ setValues = _useState2[1];
248
288
 
249
289
  React.useEffect(function () {
250
290
  if (!multiple && !utils.isEmpty(defaultValues)) {
@@ -252,13 +292,13 @@ function useAutoComplete(autoCompleteProps) {
252
292
  }
253
293
  }, []);
254
294
 
255
- var _useState4 = React.useState((_itemList$ = itemList[0]) == null ? void 0 : _itemList$.value),
256
- focusedValue = _useState4[0],
257
- setFocusedValue = _useState4[1];
295
+ var _useState3 = React.useState((_itemList$ = itemList[0]) == null ? void 0 : _itemList$.value),
296
+ focusedValue = _useState3[0],
297
+ setFocusedValue = _useState3[1];
258
298
 
259
- var _useState5 = React.useState(false),
260
- listAll = _useState5[0],
261
- setListAll = _useState5[1];
299
+ var _useState4 = React.useState(false),
300
+ listAll = _useState4[0],
301
+ setListAll = _useState4[1];
262
302
 
263
303
  var maxSelections = autoCompleteProps.maxSelections || values.length + 1;
264
304
  var filteredResults = itemList.filter(function (i) {
@@ -370,14 +410,11 @@ function useAutoComplete(autoCompleteProps) {
370
410
  }
371
411
  };
372
412
  }) : [];
373
- var children = utils.runIfFn(autoCompleteProps.children, {
374
- isOpen: isOpen,
375
- onClose: onClose,
376
- onOpen: onOpen,
377
- tags: tags,
378
- removeItem: removeItem,
379
- resetItems: resetItems
380
- });
413
+ React.useEffect(function () {
414
+ utils.runIfFn(onReady, {
415
+ tags: tags
416
+ });
417
+ }, [values]);
381
418
 
382
419
  var getInputProps = function getInputProps(props, themeInput) {
383
420
  var _onBlur = props.onBlur,
@@ -401,7 +438,7 @@ function useAutoComplete(autoCompleteProps) {
401
438
  input: _extends({
402
439
  isReadOnly: isReadOnly,
403
440
  onFocus: function onFocus(e) {
404
- utils.runIfFn(_onFocus);
441
+ utils.runIfFn(_onFocus, e);
405
442
  if (autoCompleteProps.openOnFocus && !isReadOnly) onOpen();
406
443
  if (autoCompleteProps.selectOnFocus) e.target.select();
407
444
  if (listAllValuesOnFocus) setListAll(true);
@@ -409,7 +446,7 @@ function useAutoComplete(autoCompleteProps) {
409
446
  onBlur: function onBlur(e) {
410
447
  var _inputWrapperRef$curr;
411
448
 
412
- utils.runIfFn(_onBlur);
449
+ utils.runIfFn(_onBlur, e);
413
450
  var listIsFocused = e.relatedTarget === (listRef == null ? void 0 : listRef.current);
414
451
  var inputWrapperIsFocused = (_inputWrapperRef$curr = inputWrapperRef.current) == null ? void 0 : _inputWrapperRef$curr.contains(e.relatedTarget);
415
452
 
@@ -491,11 +528,9 @@ function useAutoComplete(autoCompleteProps) {
491
528
  };
492
529
  };
493
530
 
494
- var getDefItemValue = function getDefItemValue(item) {
495
- return (typeof item === "string" ? item : item[Object.keys(item)[0]]).toString();
496
- };
497
-
498
531
  var getItemProps = function getItemProps(props) {
532
+ var _getValue;
533
+
499
534
  var _fixed = props._fixed,
500
535
  _focus = props._focus,
501
536
  itemChild = props.children,
@@ -510,7 +545,7 @@ function useAutoComplete(autoCompleteProps) {
510
545
  sx = props.sx,
511
546
  rest = _objectWithoutPropertiesLoose(props, _excluded2);
512
547
 
513
- var value = getValue(valueProp).toString();
548
+ var value = (_getValue = getValue(valueProp)) == null ? void 0 : _getValue.toString();
514
549
  var isFocused = value === focusedValue;
515
550
  var isValidSuggestion = filteredList.findIndex(function (i) {
516
551
  return i.value === value;
@@ -553,21 +588,19 @@ function useAutoComplete(autoCompleteProps) {
553
588
  }, isFocused && (_focus || getFocusedStyles()), fixed && _fixed, rest),
554
589
  root: {
555
590
  isValidSuggestion: isValidSuggestion,
556
- setItemList: setItemList,
557
591
  value: value
558
592
  }
559
593
  };
560
594
  };
561
595
 
562
596
  var getGroupProps = function getGroupProps(props) {
563
- var hasItems = hasChildren(props, filteredList);
564
- var lastItem = utils.getLastItem(filteredList.filter(function (i) {
565
- return utils.isUndefined(i == null ? void 0 : i.noFilter);
566
- }));
597
+ var hasItems = hasChildren(props.children, filteredList);
567
598
  return {
568
599
  divider: {
569
- hasFirstChild: hasFirstItem(props, firstItem),
570
- hasLastChild: hasLastItem(props, lastItem)
600
+ hasFirstChild: hasFirstItem(props.children, firstItem),
601
+ hasLastChild: hasLastItem(props.children, utils.getLastItem(filteredList.filter(function (i) {
602
+ return utils.isUndefined(i == null ? void 0 : i.noFilter);
603
+ })))
571
604
  },
572
605
  group: {
573
606
  display: hasItems ? "initial" : "none"
@@ -606,6 +639,8 @@ function useAutoComplete(autoCompleteProps) {
606
639
  onClose: onClose,
607
640
  onOpen: onOpen,
608
641
  query: query,
642
+ removeItem: removeItem,
643
+ resetItems: resetItems,
609
644
  setQuery: setQuery,
610
645
  tags: tags,
611
646
  values: values
@@ -617,7 +652,15 @@ var AutoComplete = /*#__PURE__*/react.forwardRef(function (props, ref) {
617
652
  var children = context.children,
618
653
  isOpen = context.isOpen,
619
654
  onClose = context.onClose,
620
- onOpen = context.onOpen;
655
+ onOpen = context.onOpen,
656
+ resetItems = context.resetItems,
657
+ removeItem = context.removeItem;
658
+ React.useImperativeHandle(ref, function () {
659
+ return {
660
+ resetItems: resetItems,
661
+ removeItem: removeItem
662
+ };
663
+ });
621
664
  return React__default.createElement(AutoCompleteProvider, {
622
665
  value: context
623
666
  }, React__default.createElement(react.Popover, {
@@ -652,7 +695,6 @@ var AutoCompleteItem = /*#__PURE__*/react.forwardRef(function (props, forwardedR
652
695
  var itemProps = getItemProps(props);
653
696
  var _itemProps$root = itemProps.root,
654
697
  isValidSuggestion = _itemProps$root.isValidSuggestion,
655
- setItemList = _itemProps$root.setItemList,
656
698
  value = _itemProps$root.value;
657
699
  var isFocused = focusedValue === value;
658
700
  React.useEffect(function () {
@@ -664,14 +706,6 @@ var AutoCompleteItem = /*#__PURE__*/react.forwardRef(function (props, forwardedR
664
706
  });
665
707
  }, [isFocused, interactionRef]);
666
708
  React.useEffect(function () {
667
- var item = _extends({}, utils.pick(props, ["label", "fixed", "disabled", "groupId"]), {
668
- itemVal: props.value,
669
- value: value
670
- });
671
-
672
- setItemList(function (itemList) {
673
- return [].concat(itemList, [item]);
674
- });
675
709
  if (typeof value !== "string") console.warn("wow");
676
710
  if (typeof value !== "string" && utils.isUndefined(props.getValue)) console.error("You must define the `getValue` prop, when an Item's value is not a string");
677
711
  }, []);
@@ -693,7 +727,7 @@ var baseItemStyles = {
693
727
  mx: "2",
694
728
  px: "2",
695
729
  py: "2",
696
- borderRadius: "md",
730
+ rounded: "md",
697
731
  cursor: "pointer"
698
732
  };
699
733
 
@@ -873,10 +907,10 @@ var baseStyles = {
873
907
  py: "4",
874
908
  opacity: "0",
875
909
  bg: "#232934",
876
- borderRadius: "md",
910
+ rounded: "md",
877
911
  maxH: "350px",
878
912
  border: "none",
879
- boxShadow: "base",
913
+ shadow: "base",
880
914
  pos: "absolute",
881
915
  zIndex: "popover",
882
916
  overflowY: "auto",