@choc-ui/chakra-autocomplete 4.5.8 → 4.7.0

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
@@ -24,7 +24,9 @@
24
24
  <a><img alt="GitHub Repo stars" src="https://img.shields.io/github/stars/anubra266/choc-autocomplete?logo=github&style=for-the-badge">
25
25
 
26
26
  <!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
27
- [![All Contributors](https://img.shields.io/badge/all_contributors-4-orange.svg?style=flat-square)](#contributors-)
27
+
28
+ [![All Contributors](https://img.shields.io/badge/all_contributors-3-orange.svg?style=flat-square)](#contributors-)
29
+
28
30
  <!-- ALL-CONTRIBUTORS-BADGE:END -->
29
31
 
30
32
  </a>
@@ -145,7 +147,7 @@ function App() {
145
147
  <AutoCompleteInput variant="filled" />
146
148
  <AutoCompleteList>
147
149
  {Object.entries(continents).map(([continent, countries], co_id) => (
148
- <AutoCompleteGroup key={co_id} showDivider>
150
+ <AutoCompleteGroup key={co_id} id={continent} showDivider>
149
151
  <AutoCompleteGroupTitle textTransform="capitalize">
150
152
  {continent}
151
153
  </AutoCompleteGroupTitle>
@@ -153,6 +155,7 @@ function App() {
153
155
  <AutoCompleteItem
154
156
  key={c_id}
155
157
  value={country}
158
+ groupId={continent}
156
159
  textTransform="capitalize"
157
160
  >
158
161
  {country}
@@ -175,7 +178,7 @@ export default App;
175
178
 
176
179
  ## Accessing the internal state
177
180
 
178
- 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.
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` methods and the `tags` in multiple mode.
179
182
 
180
183
  ```js
181
184
  import {
@@ -343,7 +346,7 @@ function App() {
343
346
  tags.map((tag, tid) => (
344
347
  <AutoCompleteTag
345
348
  key={tid}
346
- label={tag.value}
349
+ label={tag.label}
347
350
  onRemove={tag.onRemove}
348
351
  />
349
352
  ))
@@ -773,6 +776,13 @@ This Composes your suggestions
773
776
  </tr>
774
777
  </thead>
775
778
  <tbody>
779
+ <tr>
780
+ <td>groupId</td>
781
+ <td>string</td>
782
+ <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>
783
+ <td>no<br></td>
784
+ <td>&mdash;&mdash;&mdash;</td>
785
+ </tr>
776
786
  <tr>
777
787
  <td>label</td>
778
788
  <td>string</td>
@@ -985,7 +995,6 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
985
995
  <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>
986
996
  <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>
987
997
  <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>
988
- <td align="center"><a href="http://ericc.netlify.app"><img src="https://avatars.githubusercontent.com/u/1885157?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Eric Carboni</b></sub></a><br /><a href="https://github.com/anubra266/choc-autocomplete/commits?author=eric-hc" title="Code">💻</a></td>
989
998
  </tr>
990
999
  </table>
991
1000
 
@@ -6,6 +6,7 @@ export interface AutoCompleteItemProps extends FlexProps {
6
6
  _focus?: CSSObject | any;
7
7
  disabled?: boolean;
8
8
  _fixed?: CSSObject;
9
+ groupId?: string;
9
10
  }
10
11
  export declare const AutoCompleteItem: import("@chakra-ui/react").ComponentWithAs<"div", AutoCompleteItemProps>;
11
12
  export declare const baseItemStyles: FlexProps;
@@ -1,11 +1,5 @@
1
- import { MaybeRenderProp } from "@chakra-ui/react-utils";
2
- import { UseAutoCompleteProps } from "./types";
3
- export declare type AutoCompleteChildProps = {
4
- isOpen: boolean;
5
- onClose: () => void;
6
- onOpen: () => void;
7
- };
1
+ import { UseAutoCompleteProps, UseAutoCompleteReturn } from "./types";
8
2
  export interface AutoCompleteProps extends UseAutoCompleteProps {
9
- children: MaybeRenderProp<AutoCompleteChildProps>;
3
+ children: UseAutoCompleteReturn["children"];
10
4
  }
11
5
  export declare const AutoComplete: import("@chakra-ui/react").ComponentWithAs<"div", AutoCompleteProps>;
@@ -9,13 +9,12 @@ 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');
13
12
  var layout = require('@chakra-ui/layout');
14
13
  var tag = require('@chakra-ui/tag');
15
14
 
16
15
  var _createContext = /*#__PURE__*/reactUtils.createContext({
17
16
  name: "AutoCompleteContext",
18
- errorMessage: "useAutoCompleteContext: `context` is undefined. Seems you forgot to wrap all autoomplete components within `<AutoComplete />`"
17
+ errorMessage: "useAutoCompleteContext: `context` is undefined. Seems you forgot to wrap all autocomplete components within `<AutoComplete />`"
19
18
  }),
20
19
  AutoCompleteProvider = _createContext[0],
21
20
  useAutoCompleteContext = _createContext[1];
@@ -147,16 +146,6 @@ var setEmphasis = function setEmphasis(children, query) {
147
146
  });
148
147
  return newChildren;
149
148
  };
150
- var getItemList = function getItemList(children) {
151
- var itemChildren = reactNanny.getChildrenDeep(children, function (child) {
152
- var _child$type;
153
-
154
- return (child == null ? void 0 : (_child$type = child.type) == null ? void 0 : _child$type.displayName) === "AutoCompleteItem";
155
- });
156
- return itemChildren.map(function (item) {
157
- return utils.pick(item.props, ["value", "label", "fixed", "disabled"]);
158
- });
159
- };
160
149
  var getFocusedStyles = function getFocusedStyles() {
161
150
  return {
162
151
  bg: "whiteAlpha.100",
@@ -187,34 +176,20 @@ var getMultipleWrapStyles = function getMultipleWrapStyles(themeInput, multiple)
187
176
  });
188
177
  };
189
178
 
190
- var hasFirstItem = function hasFirstItem(children, firstItem) {
191
- var result = reactNanny.getChildDeep(children, function (child) {
192
- var _child$type;
193
-
194
- 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);
195
- });
196
- return result;
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;
197
181
  };
198
- var hasLastItem = function hasLastItem(children, lastItem) {
199
- var result = reactNanny.getChildDeep(children, function (child) {
200
- var _child$type2;
201
-
202
- 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);
203
- });
204
- return result;
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;
205
184
  };
206
- var hasChildren = function hasChildren(children, filteredList) {
207
- return utils.isDefined(reactNanny.getChildDeep(children, function (child) {
208
- return filteredList.findIndex(function (i) {
209
- var _child$props;
210
-
211
- return i.value === ((_child$props = child.props) == null ? void 0 : _child$props.value);
212
- }) >= 0;
213
- }));
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;
188
+ });
214
189
  };
215
190
 
216
191
  var _excluded = ["onBlur", "onChange", "onFocus", "onKeyDown", "variant", "wrapStyles"],
217
- _excluded2 = ["_fixed", "_focus", "children", "disabled", "value", "fixed", "onClick", "onMouseOver", "sx"];
192
+ _excluded2 = ["_fixed", "_focus", "children", "disabled", "label", "value", "fixed", "onClick", "onMouseOver", "sx"];
218
193
  /**
219
194
  * useAutoComplete that provides all the state and focus management logic
220
195
  * for the autocomplete component. It is consumed by the `Autocomplete` component
@@ -254,24 +229,22 @@ function useAutoComplete(autoCompleteProps) {
254
229
  onClose = _useDisclosure.onClose,
255
230
  onOpen = _useDisclosure.onOpen;
256
231
 
257
- var children = utils.runIfFn(autoCompleteProps.children, {
258
- isOpen: isOpen,
259
- onClose: onClose,
260
- onOpen: onOpen
261
- });
262
- var itemList = getItemList(children);
232
+ var _useState = React.useState([]),
233
+ itemList = _useState[0],
234
+ setItemList = _useState[1];
235
+
263
236
  var inputRef = React.useRef(null);
264
237
  var inputWrapperRef = React.useRef(null);
265
238
  var listRef = React.useRef(null);
266
239
  var interactionRef = React.useRef(null);
267
240
 
268
- var _useState = React.useState(""),
269
- query = _useState[0],
270
- setQuery = _useState[1];
241
+ var _useState2 = React.useState(""),
242
+ query = _useState2[0],
243
+ setQuery = _useState2[1];
271
244
 
272
- var _useState2 = React.useState(defaultValues),
273
- values = _useState2[0],
274
- setValues = _useState2[1];
245
+ var _useState3 = React.useState(defaultValues),
246
+ values = _useState3[0],
247
+ setValues = _useState3[1];
275
248
 
276
249
  React.useEffect(function () {
277
250
  if (!multiple && !utils.isEmpty(defaultValues)) {
@@ -279,13 +252,13 @@ function useAutoComplete(autoCompleteProps) {
279
252
  }
280
253
  }, []);
281
254
 
282
- var _useState3 = React.useState((_itemList$ = itemList[0]) == null ? void 0 : _itemList$.value),
283
- focusedValue = _useState3[0],
284
- setFocusedValue = _useState3[1];
255
+ var _useState4 = React.useState((_itemList$ = itemList[0]) == null ? void 0 : _itemList$.value),
256
+ focusedValue = _useState4[0],
257
+ setFocusedValue = _useState4[1];
285
258
 
286
- var _useState4 = React.useState(false),
287
- listAll = _useState4[0],
288
- setListAll = _useState4[1];
259
+ var _useState5 = React.useState(false),
260
+ listAll = _useState5[0],
261
+ setListAll = _useState5[1];
289
262
 
290
263
  var maxSelections = autoCompleteProps.maxSelections || values.length + 1;
291
264
  var filteredResults = itemList.filter(function (i) {
@@ -326,9 +299,11 @@ function useAutoComplete(autoCompleteProps) {
326
299
  if (!values.includes(itemValue) && values.length < maxSelections) setValues(function (v) {
327
300
  return multiple ? [].concat(v, [itemValue]) : [itemValue];
328
301
  });
329
- var itemLabel = (_filteredList$find = filteredList.find(function (i) {
302
+ var itemLabelFromValue = (_filteredList$find = filteredList.find(function (i) {
330
303
  return i.value === itemValue;
331
- })) == null ? void 0 : _filteredList$find.label;
304
+ })) == null ? void 0 : _filteredList$find.label; // use value if label is not available
305
+
306
+ var itemLabel = itemLabelFromValue || itemValue;
332
307
  setQuery(itemLabel);
333
308
 
334
309
  if (multiple) {
@@ -357,19 +332,24 @@ function useAutoComplete(autoCompleteProps) {
357
332
  if (query === itemValue) setQuery("");
358
333
  };
359
334
 
360
- var getItemFromValue = function getItemFromValue(value) {
361
- return itemList.find(function (item) {
362
- return item.value === value;
363
- });
364
- };
365
-
366
335
  var tags = multiple ? values.map(function (tag) {
367
- return _extends({}, getItemFromValue(tag), {
336
+ var _itemList$find;
337
+
338
+ return {
339
+ label: ((_itemList$find = itemList.find(function (item) {
340
+ return item.value === tag;
341
+ })) == null ? void 0 : _itemList$find.label) || tag,
368
342
  onRemove: function onRemove() {
369
343
  return removeItem(tag);
370
344
  }
371
- });
345
+ };
372
346
  }) : [];
347
+ var children = utils.runIfFn(autoCompleteProps.children, {
348
+ isOpen: isOpen,
349
+ onClose: onClose,
350
+ onOpen: onOpen,
351
+ tags: tags
352
+ });
373
353
 
374
354
  var getInputProps = function getInputProps(props, themeInput) {
375
355
  var _onBlur = props.onBlur,
@@ -488,6 +468,7 @@ function useAutoComplete(autoCompleteProps) {
488
468
  _focus = props._focus,
489
469
  itemChild = props.children,
490
470
  disabled = props.disabled,
471
+ label = props.label,
491
472
  value = props.value,
492
473
  fixed = props.fixed,
493
474
  _onClick = props.onClick,
@@ -499,12 +480,13 @@ function useAutoComplete(autoCompleteProps) {
499
480
  var isValidSuggestion = filteredList.findIndex(function (i) {
500
481
  return i.value === value;
501
482
  }) >= 0;
483
+ var itemLabel = itemChild || label || value;
502
484
  return {
503
- item: _extends({}, typeof itemChild !== "string" || !emphasize ? {
504
- children: itemChild
485
+ item: _extends({}, typeof itemLabel !== "string" || !emphasize ? {
486
+ children: itemLabel
505
487
  } : {
506
488
  dangerouslySetInnerHTML: {
507
- __html: setEmphasis(itemChild, query)
489
+ __html: setEmphasis(itemLabel, query)
508
490
  }
509
491
  }, {
510
492
  "aria-selected": values.includes(value),
@@ -535,19 +517,21 @@ function useAutoComplete(autoCompleteProps) {
535
517
  })
536
518
  }, isFocused && (_focus || getFocusedStyles()), fixed && _fixed, rest),
537
519
  root: {
538
- isValidSuggestion: isValidSuggestion
520
+ isValidSuggestion: isValidSuggestion,
521
+ setItemList: setItemList
539
522
  }
540
523
  };
541
524
  };
542
525
 
543
526
  var getGroupProps = function getGroupProps(props) {
544
- var hasItems = hasChildren(props.children, filteredList);
527
+ var hasItems = hasChildren(props, filteredList);
528
+ var lastItem = utils.getLastItem(filteredList.filter(function (i) {
529
+ return utils.isUndefined(i == null ? void 0 : i.noFilter);
530
+ }));
545
531
  return {
546
532
  divider: {
547
- hasFirstChild: hasFirstItem(props.children, firstItem),
548
- hasLastChild: hasLastItem(props.children, utils.getLastItem(filteredList.filter(function (i) {
549
- return utils.isUndefined(i == null ? void 0 : i.noFilter);
550
- })))
533
+ hasFirstChild: hasFirstItem(props, firstItem),
534
+ hasLastChild: hasLastItem(props, lastItem)
551
535
  },
552
536
  group: {
553
537
  display: hasItems ? "initial" : "none"
@@ -630,7 +614,9 @@ var AutoCompleteItem = /*#__PURE__*/react.forwardRef(function (props, forwardedR
630
614
  var itemRef = React.useRef();
631
615
  var ref = react.useMergeRefs(forwardedRef, itemRef);
632
616
  var itemProps = getItemProps(props);
633
- var isValidSuggestion = itemProps.root.isValidSuggestion;
617
+ var _itemProps$root = itemProps.root,
618
+ isValidSuggestion = _itemProps$root.isValidSuggestion,
619
+ setItemList = _itemProps$root.setItemList;
634
620
  var isFocused = focusedValue === props.value;
635
621
  React.useEffect(function () {
636
622
  var _itemRef$current;
@@ -640,12 +626,18 @@ var AutoCompleteItem = /*#__PURE__*/react.forwardRef(function (props, forwardedR
640
626
  block: "center"
641
627
  });
642
628
  }, [isFocused, interactionRef]);
629
+ React.useEffect(function () {
630
+ setItemList(function (itemList) {
631
+ return [].concat(itemList, [utils.pick(props, ["value", "label", "fixed", "disabled", "groupId"])]);
632
+ });
633
+ }, []);
643
634
 
644
635
  var _itemProps$item = itemProps.item,
645
636
  children = _itemProps$item.children,
646
637
  dangerouslySetInnerHTML = _itemProps$item.dangerouslySetInnerHTML,
647
- rest = _objectWithoutPropertiesLoose(_itemProps$item, _excluded$1);
638
+ restProps = _objectWithoutPropertiesLoose(_itemProps$item, _excluded$1);
648
639
 
640
+ var rest = utils.omit(restProps, ["groupId"]);
649
641
  return isValidSuggestion ? React__default.createElement(react.Flex, Object.assign({
650
642
  ref: ref
651
643
  }, baseItemStyles, rest), children ? children : React__default.createElement("span", {