@leafygreen-ui/combobox 1.0.2 → 1.2.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.
Files changed (49) hide show
  1. package/CHANGELOG.md +65 -0
  2. package/README.md +2 -2
  3. package/dist/Chip.d.ts.map +1 -1
  4. package/dist/Combobox.d.ts +7 -1
  5. package/dist/Combobox.d.ts.map +1 -1
  6. package/dist/Combobox.styles.d.ts +7 -3
  7. package/dist/Combobox.styles.d.ts.map +1 -1
  8. package/dist/Combobox.types.d.ts +33 -6
  9. package/dist/Combobox.types.d.ts.map +1 -1
  10. package/dist/ComboboxContext.d.ts +1 -1
  11. package/dist/ComboboxContext.d.ts.map +1 -1
  12. package/dist/ComboboxOption.d.ts.map +1 -1
  13. package/dist/ComboboxTestUtils.d.ts +3 -4
  14. package/dist/ComboboxTestUtils.d.ts.map +1 -1
  15. package/dist/esm/index.js +1 -1
  16. package/dist/esm/index.js.map +1 -1
  17. package/dist/index.js +1 -1
  18. package/dist/index.js.map +1 -1
  19. package/dist/utils/OptionObjectUtils.d.ts +5 -0
  20. package/dist/utils/OptionObjectUtils.d.ts.map +1 -0
  21. package/dist/utils/flattenChildren.d.ts +11 -0
  22. package/dist/utils/flattenChildren.d.ts.map +1 -0
  23. package/dist/utils/getNameAndValue.d.ts +14 -0
  24. package/dist/utils/getNameAndValue.d.ts.map +1 -0
  25. package/dist/utils/index.d.ts +5 -0
  26. package/dist/utils/index.d.ts.map +1 -0
  27. package/dist/utils/wrapJSX.d.ts +14 -0
  28. package/dist/utils/wrapJSX.d.ts.map +1 -0
  29. package/package.json +22 -12
  30. package/src/Chip.tsx +16 -9
  31. package/src/Combobox.spec.tsx +336 -164
  32. package/src/Combobox.story.tsx +274 -248
  33. package/src/Combobox.styles.ts +94 -24
  34. package/src/Combobox.tsx +456 -279
  35. package/src/Combobox.types.ts +46 -8
  36. package/src/ComboboxContext.tsx +2 -2
  37. package/src/ComboboxOption.tsx +36 -11
  38. package/src/ComboboxTestUtils.tsx +22 -8
  39. package/src/utils/ComboboxUtils.spec.tsx +227 -0
  40. package/src/utils/OptionObjectUtils.ts +26 -0
  41. package/src/utils/flattenChildren.tsx +47 -0
  42. package/src/utils/getNameAndValue.ts +23 -0
  43. package/src/utils/index.ts +8 -0
  44. package/src/utils/wrapJSX.tsx +54 -0
  45. package/tsconfig.json +3 -0
  46. package/tsconfig.tsbuildinfo +1 -3977
  47. package/dist/util.d.ts +0 -53
  48. package/dist/util.d.ts.map +0 -1
  49. package/src/util.tsx +0 -117
@@ -0,0 +1,8 @@
1
+ export { wrapJSX } from './wrapJSX';
2
+ export { getNameAndValue } from './getNameAndValue';
3
+ export {
4
+ getOptionObjectFromValue,
5
+ getDisplayNameForValue,
6
+ getValueForDisplayName,
7
+ } from './OptionObjectUtils';
8
+ export { flattenChildren } from './flattenChildren';
@@ -0,0 +1,54 @@
1
+ import React, { ReactChild } from 'react';
2
+
3
+ /**
4
+ *
5
+ * Wraps every instance of `wrap` found in `str` in the provided `element`.
6
+ *
7
+ * E.g. `wrapJSX('Apple', 'ap', 'em') => <em>Ap</em>ple`
8
+ *
9
+ * @param str
10
+ * @param wrap
11
+ * @param element
12
+ * @returns `JSX.Element`
13
+ */
14
+ export const wrapJSX = (
15
+ str: string,
16
+ wrap?: string,
17
+ element?: keyof HTMLElementTagNameMap,
18
+ ): JSX.Element => {
19
+ if (wrap && element) {
20
+ const regex = new RegExp(wrap, 'gi');
21
+ const matches = str.matchAll(regex);
22
+
23
+ if (matches) {
24
+ const outArray = str.split('') as Array<ReactChild>;
25
+
26
+ /**
27
+ * For every match, splice it into the "string",
28
+ * wrapped in the React element
29
+ */
30
+ // Consider adding --downlevelIteration TS flag so we don't need Array.from
31
+ for (const match of Array.from(matches)) {
32
+ const matchIndex = match.index ?? -1;
33
+ const matchContent = match[0];
34
+ const matchLength = matchContent.length;
35
+ const key = matchIndex + matchContent + matchLength;
36
+
37
+ // We create a replacement array that's
38
+ // the same length as the match we're deleting,
39
+ // in order to keep the matchIndexes aligned
40
+ // with the indexes of the output array
41
+ const replacement = new Array<ReactChild>(matchLength).fill('');
42
+ replacement[0] = React.createElement(element, { key }, matchContent);
43
+
44
+ outArray.splice(matchIndex, matchLength, ...replacement);
45
+ }
46
+
47
+ return <>{outArray}</>;
48
+ }
49
+
50
+ return <>{str}</>;
51
+ }
52
+
53
+ return <>{str}</>;
54
+ };
package/tsconfig.json CHANGED
@@ -40,6 +40,9 @@
40
40
  {
41
41
  "path": "../tooltip"
42
42
  },
43
+ {
44
+ "path": "../tokens"
45
+ },
43
46
  {
44
47
  "path": "../typography"
45
48
  }