@sproutsocial/seeds-react-menu 0.5.0 → 0.6.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 (103) hide show
  1. package/.turbo/turbo-build.log +10 -10
  2. package/CHANGELOG.md +15 -0
  3. package/dist/esm/index.js +1486 -440
  4. package/dist/esm/index.js.map +1 -1
  5. package/dist/index.d.mts +248 -92
  6. package/dist/index.d.ts +248 -92
  7. package/dist/index.js +1500 -429
  8. package/dist/index.js.map +1 -1
  9. package/package.json +4 -1
  10. package/src/ActionMenu/ActionMenu.stories.tsx +426 -0
  11. package/src/ActionMenu/ActionMenu.tsx +24 -27
  12. package/src/ActionMenu/ActionMenuTypes.ts +8 -7
  13. package/src/ActionMenu/__tests__/ActionMenu.test.tsx +17 -8
  14. package/src/ActionMenu/__tests__/ActionMenu.typetest.tsx +8 -8
  15. package/src/Autocomplete/Autocomplete.stories.tsx +38 -3
  16. package/src/Autocomplete/Autocomplete.tsx +15 -80
  17. package/src/Autocomplete/AutocompleteInput.tsx +18 -27
  18. package/src/Autocomplete/AutocompleteTokenInput.tsx +50 -0
  19. package/src/Autocomplete/AutocompleteTypes.ts +25 -12
  20. package/src/Autocomplete/MultiAutocomplete.tsx +189 -0
  21. package/src/Autocomplete/SingleAutocomplete.tsx +74 -0
  22. package/src/Autocomplete/__tests__/Autocomplete.test.tsx +171 -3
  23. package/src/Autocomplete/__tests__/Autocomplete.typetest.tsx +90 -8
  24. package/src/MenuContent/MenuContent.stories.tsx +164 -0
  25. package/src/MenuContent/MenuContent.tsx +26 -12
  26. package/src/MenuContent/MenuContentTypes.ts +6 -4
  27. package/src/MenuContent/__tests__/MenuContent.typetest.tsx +8 -8
  28. package/src/MenuContext.tsx +191 -37
  29. package/src/MenuFooter/MenuFooter.stories.tsx +202 -0
  30. package/src/MenuFooter/__tests__/MenuFooter.test.tsx +16 -11
  31. package/src/MenuGroup/MenuGroup.feature +16 -16
  32. package/src/MenuGroup/MenuGroup.stories.tsx +248 -0
  33. package/src/MenuGroup/MenuGroup.tsx +8 -7
  34. package/src/MenuGroup/MenuGroupTypes.ts +6 -2
  35. package/src/MenuGroup/__tests__/MenuGroup.test.tsx +6 -6
  36. package/src/MenuGroup/__tests__/MenuGroup.typetest.tsx +7 -7
  37. package/src/MenuGroup/styles.tsx +4 -2
  38. package/src/MenuHeader/MenuHeader.stories.tsx +276 -0
  39. package/src/MenuHeader/MenuHeader.tsx +3 -1
  40. package/src/MenuHeader/__tests__/MenuHeader.test.tsx +23 -13
  41. package/src/MenuHeader/styles.tsx +18 -14
  42. package/src/MenuItem/MenuItem.stories.tsx +507 -0
  43. package/src/MenuItem/MenuItem.tsx +18 -8
  44. package/src/MenuItem/MenuItemTypes.ts +9 -4
  45. package/src/MenuItem/__tests__/MenuItem.test.tsx +6 -6
  46. package/src/MenuItem/styles.tsx +21 -5
  47. package/src/MenuItem/useOptionProps.tsx +10 -8
  48. package/src/MenuLabel.tsx +2 -2
  49. package/src/MenuRoot/MenuRoot.tsx +37 -23
  50. package/src/MenuRoot/__tests__/MenuRoot.test.tsx +59 -32
  51. package/src/MenuSearchInput/MenuSearchInput.tsx +63 -0
  52. package/src/MenuSearchInput/__tests__/MenuSearchInput.test.tsx +157 -0
  53. package/src/MenuSearchInput/__tests__/MenuSearchInput.typetest.tsx +31 -0
  54. package/src/MenuSearchInput/index.ts +1 -0
  55. package/src/MenuSearchTokenInput/MenuSearchTokenInput.tsx +98 -0
  56. package/src/MenuSearchTokenInput/__tests__/MenuSearchTokenInput.test.tsx +247 -0
  57. package/src/MenuSearchTokenInput/__tests__/MenuSearchTokenInput.typetest.tsx +33 -0
  58. package/src/MenuSearchTokenInput/index.ts +1 -0
  59. package/src/MenuToggleButton/MenuToggleButton.stories.tsx +72 -0
  60. package/src/{MenuToggleButton.tsx → MenuToggleButton/MenuToggleButton.tsx} +6 -5
  61. package/src/MenuToggleButton/index.ts +1 -0
  62. package/src/MenuTokenInput.tsx +104 -0
  63. package/src/MultiSelectMenu/MultiSelectMenu.stories.tsx +214 -0
  64. package/src/MultiSelectMenu/MultiSelectMenu.tsx +38 -68
  65. package/src/MultiSelectMenu/MultiSelectMenuTypes.ts +8 -6
  66. package/src/MultiSelectMenu/__tests__/MultiSelectMenu.test.tsx +46 -17
  67. package/src/MultiSelectMenu/__tests__/MultiSelectMenu.typetest.tsx +6 -6
  68. package/src/NestedMenu/NestedMenu.feature +109 -0
  69. package/src/NestedMenu/NestedMenu.stories.tsx +157 -0
  70. package/src/NestedMenu/NestedMenu.tsx +102 -0
  71. package/src/NestedMenu/NestedMenuContent.tsx +71 -0
  72. package/src/NestedMenu/NestedMenuContext.tsx +56 -0
  73. package/src/NestedMenu/NestedMenuHeader.tsx +25 -0
  74. package/src/NestedMenu/NestedMenuItem.tsx +37 -0
  75. package/src/NestedMenu/NestedMenuTypes.ts +47 -0
  76. package/src/NestedMenu/__tests__/NestedMenu.test.tsx +31 -0
  77. package/src/NestedMenu/__tests__/NestedMenu.typetest.tsx +134 -0
  78. package/src/NestedMenu/index.ts +5 -0
  79. package/src/SingleSelectMenu/SingleSelectMenu.stories.tsx +227 -0
  80. package/src/SingleSelectMenu/SingleSelectMenu.tsx +23 -26
  81. package/src/SingleSelectMenu/SingleSelectMenuTypes.ts +7 -5
  82. package/src/SingleSelectMenu/__tests__/SingleSelectMenu.typetest.tsx +6 -6
  83. package/src/SubmenuItem/SubmenuItem.feature +82 -0
  84. package/src/SubmenuItem/SubmenuItem.stories.tsx +163 -0
  85. package/src/SubmenuItem/SubmenuItem.tsx +254 -0
  86. package/src/SubmenuItem/SubmenuItemTypes.ts +24 -0
  87. package/src/SubmenuItem/__tests__/SubmenuItem.test.tsx +3 -0
  88. package/src/SubmenuItem/__tests__/SubmenuItem.typetest.tsx +112 -0
  89. package/src/SubmenuItem/index.ts +2 -0
  90. package/src/hooks/useDownshiftDefaults.tsx +107 -0
  91. package/src/hooks/useItemFiltering.tsx +66 -0
  92. package/src/hooks/useMenuChildren.tsx +197 -128
  93. package/src/index.ts +6 -0
  94. package/src/menuTestingUtils.tsx +21 -10
  95. package/src/reducers/nestedMenuStateReducer.tsx +23 -0
  96. package/src/reducers/useComboboxStateReducer.tsx +26 -8
  97. package/src/reducers/useSelectStateReducer.tsx +24 -6
  98. package/src/storybookUtilities/menu-storybook-components.tsx +5 -3
  99. package/src/types.ts +38 -30
  100. package/src/utils/downshiftDefaults.ts +9 -0
  101. package/src/utils/getMultiSelectOverrides.ts +21 -0
  102. package/src/utils/reduceReducers.ts +19 -0
  103. package/src/utils/itemToString.ts +0 -5
@@ -0,0 +1,66 @@
1
+ import React from "react";
2
+ import type { TypeMenuContext } from "../MenuContext";
3
+ import type { TypeInternalItemProps } from "../types";
4
+ import { itemToString as defaultItemToString } from "../utils/downshiftDefaults";
5
+
6
+ export type TypeGetIsItemVisibleFn = (
7
+ item: TypeMenuContext["items"][number],
8
+ inputValue: string
9
+ ) => boolean;
10
+
11
+ /**
12
+ * @description A function to get the default getIsItemVisible function
13
+ *
14
+ * @param itemToString - A function to convert an item to a string
15
+ *
16
+ * @returns The default getIsItemVisible function
17
+ */
18
+ export const getDefaultGetIsItemVisible =
19
+ (
20
+ itemToString: TypeMenuContext["itemToString"] = defaultItemToString
21
+ ): TypeGetIsItemVisibleFn =>
22
+ (item, inputValue) =>
23
+ itemToString(item).toLowerCase().includes(inputValue.toLowerCase());
24
+
25
+ export type TypeGetShouldFilterFn = (arg: { inputValue?: string }) => boolean;
26
+
27
+ export const defaultGetShouldFilter: TypeGetShouldFilterFn = ({ inputValue }) =>
28
+ Boolean(inputValue);
29
+
30
+ export interface TypeItemFilteringProps {
31
+ allMenuItems: TypeInternalItemProps[];
32
+ itemToString?: TypeMenuContext["itemToString"];
33
+ getIsItemVisible?: TypeGetIsItemVisibleFn;
34
+ getShouldFilter?: TypeGetShouldFilterFn;
35
+ setHiddenItemsMap?: TypeMenuContext["setHiddenItemsMap"];
36
+ }
37
+
38
+ export const useItemFiltering = ({
39
+ allMenuItems,
40
+ itemToString,
41
+ getIsItemVisible = getDefaultGetIsItemVisible(itemToString),
42
+ getShouldFilter = defaultGetShouldFilter,
43
+ setHiddenItemsMap: setHiddenItemsMapProp,
44
+ }: TypeItemFilteringProps) => {
45
+ const [hiddenItemsMap, setHiddenItemsMap] = React.useState<
46
+ Required<TypeMenuContext>["hiddenItemsMap"]
47
+ >({});
48
+ const updateFilteredItems = React.useCallback(
49
+ ({ inputValue }) => {
50
+ const newHiddenItems = getShouldFilter({ inputValue })
51
+ ? allMenuItems.reduce((map, item) => {
52
+ const isItemVisible = getIsItemVisible(item, inputValue);
53
+ return isItemVisible ? map : { ...map, [item.id]: true };
54
+ }, {} as typeof hiddenItemsMap)
55
+ : {};
56
+ setHiddenItemsMap(newHiddenItems);
57
+ setHiddenItemsMapProp?.(newHiddenItems);
58
+ },
59
+ [allMenuItems, getIsItemVisible, getShouldFilter]
60
+ );
61
+
62
+ return {
63
+ getIsItemVisible,
64
+ updateFilteredItems,
65
+ };
66
+ };
@@ -1,29 +1,40 @@
1
1
  import React, { type ReactNode, useMemo } from "react";
2
2
  import { MenuItem, type TypeMenuItemProps } from "../MenuItem";
3
+ import type {
4
+ TypeSubmenuItemProps,
5
+ TypeSubmenuValidMenuComponentProps,
6
+ } from "../SubmenuItem";
3
7
  import type { TypeMenuContext } from "../MenuContext";
4
- import type { TypeMenuGroupProps } from "../MenuGroup/index";
5
- import type { TypeNotEmptyChildren } from "../types";
6
- import { MenuContent, type TypeMenuContentProps } from "../MenuContent/index";
8
+ import type { TypeMenuGroupProps, TypeMenuGroupBaseProps } from "../MenuGroup";
9
+ import type { TypeMenuRootProps } from "../MenuRoot";
10
+ import type {
11
+ TypeNotEmptyChildren,
12
+ TypeChildrenOrItems,
13
+ TypeInternalItemProps,
14
+ } from "../types";
15
+ import { MenuContent, type TypeMenuContentProps } from "../MenuContent";
7
16
 
8
- export interface TypeUseMenuChildrenProps {
9
- children?: ReactNode;
10
- items?: TypeMenuItemProps[];
11
- MenuItemComponent?: React.ComponentType<TypeMenuItemProps>;
17
+ export interface TypeMapMenuItemsProps<
18
+ I extends TypeMenuItemProps = TypeMenuItemProps
19
+ > {
20
+ menuItems: I[];
21
+ MenuItemComponent?: React.ComponentType<I>;
12
22
  }
13
- export interface TypeMapMenuItemsProps {
14
- items: TypeMenuItemProps[];
15
- MenuItemComponent: React.ComponentType<TypeMenuItemProps>;
16
- }
17
- export type TypeItemElement = React.ReactElement<TypeMenuItemProps>;
18
- export type TypeMenuGroupElement = React.ReactElement<TypeMenuGroupProps>;
19
- export type TypeMenuContentElement = React.ReactElement<TypeMenuContentProps>;
23
+ export type TypeItemElement<I extends TypeMenuItemProps = TypeMenuItemProps> =
24
+ React.ReactElement<
25
+ | TypeMenuItemProps
26
+ | TypeSubmenuItemProps<I, TypeSubmenuValidMenuComponentProps<I>>
27
+ >;
28
+ export type TypeMenuGroupElement<
29
+ I extends TypeMenuItemProps = TypeMenuItemProps
30
+ > = React.ReactElement<TypeMenuGroupProps<I>>;
31
+ export type TypeMenuContentElement<
32
+ I extends TypeMenuItemProps = TypeMenuItemProps
33
+ > = React.ReactElement<TypeMenuContentProps<I>>;
20
34
 
21
35
  export interface TypeMenuChildren {
22
36
  type: "item" | "group" | "content";
23
- element:
24
- | React.ReactElement<TypeMenuGroupProps>
25
- | React.ReactElement<TypeMenuItemProps>
26
- | React.ReactElement<TypeMenuContentProps>;
37
+ element: TypeItemElement | TypeMenuGroupElement | TypeMenuContentElement;
27
38
  }
28
39
 
29
40
  // https://stackoverflow.com/questions/69119016/find-specific-children-components-regardless-of-depth-in-react
@@ -58,153 +69,211 @@ const getComponentName = (element: ReactNode): string | undefined => {
58
69
  }
59
70
  };
60
71
 
61
- const getAllMenuItems = (children: ReactNode) => {
62
- const orderedElements: TypeMenuChildren[] = [];
72
+ const addMenuItem = ({
73
+ itemProps,
74
+ allMenuItems,
75
+ menuItemsMap,
76
+ }: {
77
+ itemProps: Omit<TypeInternalItemProps, "index">;
78
+ allMenuItems: TypeMenuContext["items"];
79
+ menuItemsMap: TypeMenuContext["itemsMap"];
80
+ }) => {
81
+ const item = {
82
+ ...itemProps,
83
+ index: allMenuItems.length,
84
+ };
85
+ allMenuItems.push(item);
86
+ menuItemsMap[`${item.id}`] = item;
87
+ };
88
+
89
+ const addItemsFromGroup = <I extends TypeMenuItemProps = TypeMenuItemProps>({
90
+ groupElement: menuGroup,
91
+ allMenuItems,
92
+ menuItemsMap,
93
+ }: {
94
+ groupElement: TypeMenuGroupElement<I>;
95
+ allMenuItems: TypeMenuContext["items"];
96
+ menuItemsMap: TypeMenuContext["itemsMap"];
97
+ }) => {
98
+ const menuItems = menuGroup.props.menuItems;
99
+ if (menuItems) {
100
+ menuItems.forEach((menuItem) => {
101
+ addMenuItem({
102
+ itemProps: {
103
+ id: menuItem.id,
104
+ disabled: menuItem.disabled,
105
+ menuItemProps: menuItem,
106
+ menuGroupProps: menuGroup.props,
107
+ },
108
+ allMenuItems,
109
+ menuItemsMap,
110
+ });
111
+ });
112
+ } else if (menuGroup.props.children) {
113
+ let children = menuGroup.props.children as TypeItemElement[];
114
+ // If there is only one child, it will be an object. Make it an array.
115
+ if (!Array.isArray(children)) {
116
+ children = [children];
117
+ }
118
+ children.forEach((menuItem) => {
119
+ addMenuItem({
120
+ itemProps: {
121
+ id: menuItem.props.id,
122
+ disabled: menuItem.props.disabled,
123
+ menuItemProps: menuItem.props,
124
+ menuGroupProps: menuGroup.props,
125
+ },
126
+ allMenuItems,
127
+ menuItemsMap,
128
+ });
129
+ });
130
+ }
131
+ };
132
+
133
+ const addItemsFromContent = <I extends TypeMenuItemProps = TypeMenuItemProps>({
134
+ contentElement: menuContent,
135
+ allMenuItems,
136
+ menuItemsMap,
137
+ }: {
138
+ contentElement: TypeMenuContentElement<I>;
139
+ allMenuItems: TypeMenuContext["items"];
140
+ menuItemsMap: TypeMenuContext["itemsMap"];
141
+ }) => {
142
+ /**
143
+ * No need to add items inside of MenuContent if it does not have menuItems
144
+ * since we already traversed the children and menuContentProps are not needed.
145
+ */
146
+ if (!("menuItems" in menuContent.props)) return;
147
+ // Add items from menuItems prop if it isn't empty
148
+ if (menuContent.props.menuItems) {
149
+ menuContent.props.menuItems.forEach((menuItem) => {
150
+ addMenuItem({
151
+ itemProps: {
152
+ id: menuItem.id,
153
+ disabled: menuItem.disabled,
154
+ menuItemProps: menuItem,
155
+ },
156
+ allMenuItems,
157
+ menuItemsMap,
158
+ });
159
+ });
160
+ }
161
+ };
162
+
163
+ const setMenuItemsFromChildren = ({
164
+ children,
165
+ allMenuItems,
166
+ menuItemsMap,
167
+ }: {
168
+ children: ReactNode;
169
+ allMenuItems: TypeMenuContext["items"];
170
+ menuItemsMap: TypeMenuContext["itemsMap"];
171
+ }) => {
63
172
  walkAllChildren(children, (element) => {
64
173
  switch (getComponentName(element)) {
65
174
  case "MenuItem":
66
- orderedElements.push({
67
- type: "item",
68
- element: element as TypeItemElement,
175
+ case "SubmenuItem":
176
+ case "NestedMenuItem":
177
+ // Cast element to the TypeItemElement since we know it's one of the three.
178
+ // This is needed for the addMenuItem function.
179
+ const menuItem = element as TypeItemElement;
180
+ // Items in groups would have already been added to the map.
181
+ if (menuItemsMap[`${menuItem.props.id}`]) return;
182
+ addMenuItem({
183
+ itemProps: {
184
+ id: menuItem.props.id,
185
+ disabled: menuItem.props.disabled,
186
+ menuItemProps: menuItem.props,
187
+ },
188
+ allMenuItems,
189
+ menuItemsMap,
69
190
  });
70
191
  break;
71
192
  case "MenuGroup":
72
- orderedElements.push({
73
- type: "group",
74
- element: element as TypeMenuGroupElement,
193
+ addItemsFromGroup({
194
+ // Cast element to the TypeMenuGroupElement since we know it's a group and is needed for addItemsFromGroup.
195
+ groupElement: element as TypeMenuGroupElement,
196
+ allMenuItems,
197
+ menuItemsMap,
75
198
  });
76
199
  break;
77
200
  case "MenuContent":
78
- orderedElements.push({
79
- type: "content",
80
- element: element as TypeMenuContentElement,
201
+ case "NestedMenuContent":
202
+ addItemsFromContent({
203
+ // Cast element to the TypeMenuContentElement since we know it's a content and is needed for addItemsFromContent.
204
+ contentElement: element as TypeMenuContentElement,
205
+ allMenuItems,
206
+ menuItemsMap,
81
207
  });
82
208
  break;
83
209
  default:
84
210
  break;
85
211
  }
86
212
  });
87
- return { orderedElements };
88
213
  };
89
214
 
90
- // Returns a fragment containing a `MenuItemComponent` for each item in `items`.
91
- export const mapMenuItems = ({
92
- items,
93
- MenuItemComponent,
94
- }: TypeMapMenuItemsProps) => {
215
+ // Returns a fragment containing a `MenuItemComponent` for each item in `menuItems`.
216
+ export const mapMenuItems = <I extends TypeMenuItemProps = TypeMenuItemProps>({
217
+ menuItems,
218
+ MenuItemComponent = MenuItem,
219
+ }: TypeMapMenuItemsProps<I>) => {
95
220
  return (
96
221
  <>
97
- {items.map((menuItem) => (
98
- <MenuItemComponent key={menuItem.id} {...menuItem} />
222
+ {menuItems.map((menuItemProps) => (
223
+ <MenuItemComponent key={menuItemProps.id} {...menuItemProps} />
99
224
  ))}
100
225
  </>
101
226
  );
102
227
  };
103
228
 
104
- export const useMenuChildren = ({
229
+ type TypeUseMenuChildrenProps<I extends TypeMenuItemProps = TypeMenuItemProps> =
230
+ {
231
+ children?: TypeMenuRootProps["children"];
232
+ menuItems?: I[];
233
+ MenuItemComponent?: React.ComponentType<I>;
234
+ };
235
+
236
+ export const getChildrenFromMenuItems = <
237
+ I extends TypeMenuItemProps = TypeMenuItemProps
238
+ >({
239
+ menuItems,
240
+ MenuItemComponent,
241
+ }: {
242
+ menuItems: I[];
243
+ MenuItemComponent?: React.ComponentType<I>;
244
+ }) => (
245
+ <MenuContent menuItems={menuItems} MenuItemComponent={MenuItemComponent} />
246
+ );
247
+
248
+ export const useMenuChildren = <
249
+ I extends TypeMenuItemProps = TypeMenuItemProps
250
+ >({
105
251
  children: childrenProp,
106
- items,
107
- MenuItemComponent = MenuItem,
108
- }: TypeUseMenuChildrenProps) => {
252
+ menuItems,
253
+ MenuItemComponent,
254
+ }: TypeUseMenuChildrenProps<I>) => {
109
255
  const { allMenuItems, menuItemsMap, children } = useMemo(() => {
110
256
  let children: TypeNotEmptyChildren = <></>;
111
- let index = 0;
112
257
  const menuItemsMap: TypeMenuContext["itemsMap"] = {};
113
258
  const allMenuItems: TypeMenuContext["items"] = [];
114
259
 
115
- // If an items prop was passed to the Menu, we can bypass the children traversal.
116
- if (items && items.length) {
117
- items.forEach((menuItem) => {
118
- const item = {
119
- id: menuItem.id,
120
- index: index++,
121
- disabled: menuItem.disabled,
122
- menuItemProps: menuItem,
123
- };
124
- allMenuItems.push(item);
125
- menuItemsMap[`${item.id}`] = item;
260
+ // If an menuItems prop was passed to the Menu, we can bypass the children traversal.
261
+ if (menuItems && menuItems.length) {
262
+ menuItems.forEach((menuItem) => {
263
+ addMenuItem({ itemProps: menuItem, allMenuItems, menuItemsMap });
126
264
  });
127
- children = (
128
- <MenuContent>{mapMenuItems({ items, MenuItemComponent })}</MenuContent>
129
- );
265
+ children = getChildrenFromMenuItems({ menuItems, MenuItemComponent });
130
266
  } else if (childrenProp) {
131
267
  children = childrenProp as TypeNotEmptyChildren;
132
- const { orderedElements } = getAllMenuItems(childrenProp);
133
-
134
- orderedElements.forEach(({ type, element }) => {
135
- /* If MenuContent has items, we need to add them to the menuItemsMap.
136
- We do not need to worry about checking its children prop because
137
- we are already traversing the children, and we do not need to add any
138
- menuContentProps to add to the item object*/
139
- if (type === "content") {
140
- const menuContent = element as TypeMenuContentElement;
141
- const items = menuContent.props.items;
142
- if (items) {
143
- items.forEach((menuItem) => {
144
- const item = {
145
- id: menuItem.id,
146
- index: index++,
147
- children: menuItem.children,
148
- disabled: menuItem.disabled,
149
- menuItemProps: menuItem,
150
- };
151
- allMenuItems.push(item);
152
- menuItemsMap[`${item.id}`] = item;
153
- });
154
- }
155
- } else if (type === "group") {
156
- const menuGroup = element as TypeMenuGroupElement;
157
- const items = menuGroup.props.items;
158
- if (items) {
159
- items.forEach((menuItem) => {
160
- const item = {
161
- id: menuItem.id,
162
- index: index++,
163
- children: menuItem.children,
164
- disabled: menuItem.disabled,
165
- menuItemProps: menuItem,
166
- menuGroupProps: element.props as TypeMenuGroupProps,
167
- };
168
- allMenuItems.push(item);
169
- menuItemsMap[`${item.id}`] = item;
170
- });
171
- } else if (menuGroup.props.children) {
172
- let children = menuGroup.props.children as TypeItemElement[];
173
- // If there is only one child, it will be an object. Make it an array.
174
- if (!Array.isArray(children)) {
175
- children = [children];
176
- }
177
- children.forEach((menuItem) => {
178
- const item = {
179
- id: menuItem.props.id,
180
- index: index++,
181
- children: menuItem.props.children,
182
- disabled: menuItem.props.disabled,
183
- menuItemProps: menuItem.props,
184
- menuGroupProps: element.props as TypeMenuGroupProps,
185
- };
186
- allMenuItems.push(item);
187
- menuItemsMap[`${item.id}`] = item;
188
- });
189
- }
190
- } else {
191
- // Items in groups would have already been added to the map.
192
- if (menuItemsMap[`${element.props.id}`]) return;
193
- const menuItem = element as TypeItemElement;
194
- const item = {
195
- id: menuItem.props.id,
196
- index: index++,
197
- disabled: menuItem.props.disabled,
198
- menuItemProps: menuItem.props,
199
- };
200
- allMenuItems.push(item);
201
- menuItemsMap[`${item.id}`] = item;
202
- }
268
+ setMenuItemsFromChildren({
269
+ children: childrenProp,
270
+ allMenuItems,
271
+ menuItemsMap,
203
272
  });
204
273
  }
205
274
 
206
275
  return { allMenuItems, menuItemsMap, children };
207
- }, [childrenProp, items, MenuItemComponent]);
276
+ }, [childrenProp, menuItems, MenuItemComponent]);
208
277
 
209
278
  return { allMenuItems, menuItemsMap, children };
210
279
  };
package/src/index.ts CHANGED
@@ -7,15 +7,21 @@ export * from "./MenuGroup";
7
7
  export * from "./MenuRoot";
8
8
  export * from "./MenuLabel";
9
9
  export * from "./MenuToggleButton";
10
+ export * from "./MenuSearchInput";
11
+ export * from "./MenuTokenInput";
12
+ export * from "./MenuSearchTokenInput";
13
+ export * from "./SubmenuItem";
10
14
 
11
15
  // Menus
12
16
  export * from "./ActionMenu";
13
17
  export * from "./SingleSelectMenu";
14
18
  export * from "./MultiSelectMenu";
15
19
  export * from "./Autocomplete";
20
+ export * from "./NestedMenu";
16
21
 
17
22
  // Utilities
18
23
  export * from "./MenuContext";
24
+ export * from "./NestedMenu/NestedMenuContext";
19
25
  export * from "./hooks/useMenuChildren";
20
26
  export * from "./reducers/useSelectStateReducer";
21
27
  export * from "./types";
@@ -1,15 +1,19 @@
1
1
  import React from "react";
2
2
  import {
3
- MenuContext,
3
+ MenuContentContext,
4
4
  MenuRoot,
5
+ checkIfItemSelected,
6
+ getSelectedItemIds,
7
+ useMenu,
5
8
  type TypeMenuContextProps,
6
9
  type TypeMenuContext,
7
10
  type TypeMenuRootProps,
8
11
  } from "./index";
9
12
 
10
- export const testDefaultContext = (
11
- context: Partial<TypeMenuContextProps> = {}
12
- ): TypeMenuContext => ({
13
+ export const testDefaultContext = ({
14
+ getToggleButtonProps,
15
+ ...context
16
+ }: Partial<TypeMenuContextProps> = {}): TypeMenuContext => ({
13
17
  items: [],
14
18
  itemsMap: context.items
15
19
  ? context.items.reduce((map, item) => {
@@ -17,10 +21,16 @@ export const testDefaultContext = (
17
21
  return map;
18
22
  }, {} as TypeMenuContext["itemsMap"])
19
23
  : {},
20
- // TODO: Make type on MenuContext more agnostic for other use cases
24
+ // TODO: Make type on MenuContentContext more agnostic for other use cases
21
25
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
22
26
  // @ts-expect-error - mocked function does not match real type
23
27
  getItemProps: (props) => ({ role: "option", ...props }),
28
+ // @ts-expect-error - mocked function does not match real type
29
+ getToggleButtonProps: ({ ref, refKey, ...props } = {}) => ({
30
+ ...getToggleButtonProps?.({ ref, refKey, ...props }),
31
+ ...(ref ? { [refKey]: ref } : {}),
32
+ ...props,
33
+ }),
24
34
  isListbox: true,
25
35
  ...context,
26
36
  });
@@ -53,12 +63,12 @@ export const TestWithMenuRoot = ({
53
63
 
54
64
  /**
55
65
  * TestWithMockedMenu
56
- * Given a children and context, render the children with the raw MenuContext with a mocked value.
66
+ * Given a children and context, render the children with the raw MenuContentContext with a mocked value.
57
67
  * Used for unit testing components without the real MenuProvider or MenuRoot.
58
68
  *
59
- * @param children - react children to be loaded inside of the MenuContext.Provider
69
+ * @param children - react children to be loaded inside of the MenuContentContext.Provider
60
70
  * @param context - mocked context props to override the testDefaultContext
61
- * @returns - rendered component with mocked MenuContext
71
+ * @returns - rendered component with mocked MenuContentContext
62
72
  */
63
73
  export const TestWithMockedMenu = ({
64
74
  children,
@@ -67,9 +77,10 @@ export const TestWithMockedMenu = ({
67
77
  children: React.ReactNode;
68
78
  context: Partial<TypeMenuContextProps>;
69
79
  }) => {
80
+ const menuContext = useMenu(testDefaultContext(context));
70
81
  return (
71
- <MenuContext.Provider value={testDefaultContext(context)}>
82
+ <MenuContentContext.Provider value={menuContext}>
72
83
  {children}
73
- </MenuContext.Provider>
84
+ </MenuContentContext.Provider>
74
85
  );
75
86
  };
@@ -0,0 +1,23 @@
1
+ import { useCombobox, useSelect } from "downshift";
2
+
3
+ export const nestedMenuStateReducer = (_state, actionAndChanges) => {
4
+ const { type, changes } = actionAndChanges;
5
+ const hasChildMenu = !!changes?.selectedItem?.menuItemProps?.childMenuId;
6
+ if (!hasChildMenu) {
7
+ return changes;
8
+ }
9
+ switch (type) {
10
+ case useSelect.stateChangeTypes.ToggleButtonBlur:
11
+ case useSelect.stateChangeTypes.ToggleButtonKeyDownEnter:
12
+ case useSelect.stateChangeTypes.ToggleButtonKeyDownSpaceButton:
13
+ case useSelect.stateChangeTypes.ItemClick:
14
+ case useCombobox.stateChangeTypes.ItemClick:
15
+ return {
16
+ ...changes,
17
+ isOpen: true,
18
+ highlightedIndex: 0,
19
+ };
20
+ default:
21
+ return changes;
22
+ }
23
+ };
@@ -1,11 +1,13 @@
1
1
  import { useCombobox } from "downshift";
2
2
  import type { UseComboboxProps } from "downshift";
3
- import type { TypeDefaultItemProps } from "../types";
3
+ import type { TypeInternalItemProps } from "../types";
4
+ import { reduceReducers } from "../utils/reduceReducers";
4
5
 
5
6
  export const useComboboxStateReducer: Required<
6
- UseComboboxProps<TypeDefaultItemProps>
7
+ UseComboboxProps<TypeInternalItemProps>
7
8
  >["stateReducer"] = (state, actionAndChanges) => {
8
9
  const { type, changes } = actionAndChanges;
10
+
9
11
  switch (type) {
10
12
  /** If downshift is blurred by tabbing within the menu, stay open */
11
13
  case useCombobox.stateChangeTypes.InputBlur:
@@ -21,9 +23,25 @@ export const useComboboxStateReducer: Required<
21
23
  }
22
24
  };
23
25
 
24
- export const defaultUseComboboxProps: Partial<
25
- UseComboboxProps<TypeDefaultItemProps>
26
- > = {
27
- // disabling while item.hidden so that hidden items are ignored by downshift
28
- isItemDisabled: (item) => Boolean(item.disabled || item.hidden),
29
- };
26
+ export const useComboboxStateReducerForMulti: Required<
27
+ UseComboboxProps<TypeInternalItemProps>
28
+ >["stateReducer"] = reduceReducers(
29
+ useComboboxStateReducer,
30
+ (state, actionAndChanges) => {
31
+ const { changes, type } = actionAndChanges;
32
+
33
+ switch (type) {
34
+ case useCombobox.stateChangeTypes.InputKeyDownEnter:
35
+ case useCombobox.stateChangeTypes.ItemClick:
36
+ case useCombobox.stateChangeTypes.InputBlur:
37
+ return {
38
+ ...changes,
39
+ ...(changes.selectedItem && {
40
+ inputValue: "",
41
+ }),
42
+ };
43
+ default:
44
+ return changes;
45
+ }
46
+ }
47
+ );
@@ -1,9 +1,10 @@
1
1
  import { useSelect } from "downshift";
2
2
  import type { UseSelectProps } from "downshift";
3
- import type { TypeDefaultItemProps } from "../types";
3
+ import type { TypeInternalItemProps } from "../types";
4
+ import { reduceReducers } from "../utils/reduceReducers";
4
5
 
5
6
  export const useSelectStateReducer: Required<
6
- UseSelectProps<TypeDefaultItemProps>
7
+ UseSelectProps<TypeInternalItemProps>
7
8
  >["stateReducer"] = (state, actionAndChanges) => {
8
9
  const { type, changes } = actionAndChanges;
9
10
  switch (type) {
@@ -21,7 +22,24 @@ export const useSelectStateReducer: Required<
21
22
  }
22
23
  };
23
24
 
24
- export const defaultUseSelectProps = {
25
- stateReducer: useSelectStateReducer,
26
- isItemDisabled: (item) => !!item.disabled,
27
- };
25
+ export const useSelectStateReducerForMulti: Required<
26
+ UseSelectProps<TypeInternalItemProps>
27
+ >["stateReducer"] = reduceReducers(
28
+ useSelectStateReducer,
29
+ (state, actionAndChanges) => {
30
+ const { changes, type } = actionAndChanges;
31
+ switch (type) {
32
+ case useSelect.stateChangeTypes.ToggleButtonKeyDownEnter:
33
+ case useSelect.stateChangeTypes.ToggleButtonKeyDownSpaceButton:
34
+ case useSelect.stateChangeTypes.ItemClick:
35
+ return {
36
+ ...changes,
37
+ isOpen: true, // keep the menu open after selection.
38
+ // TODO: Confirm that keeping highlightedIndex the same is the desired UX
39
+ // highlightedIndex: 0, // with the first option highlighted.
40
+ highlightedIndex: state.highlightedIndex, // keep highlightedIndex the same instead of returning to 0
41
+ };
42
+ }
43
+ return changes;
44
+ }
45
+ );
@@ -1,7 +1,7 @@
1
1
  import styled from "styled-components";
2
2
  import { Box } from "@sproutsocial/seeds-react-box";
3
3
  import { Icon } from "@sproutsocial/seeds-react-icon";
4
- import { useMenuContext, MenuToggleButton } from "../index";
4
+ import { useMenuToggleContext, MenuToggleButton } from "../index";
5
5
 
6
6
  export interface TypeStyledChevronProps {
7
7
  $isOpen?: boolean;
@@ -19,15 +19,17 @@ const StyledChevron = styled.div<TypeStyledChevronProps>`
19
19
 
20
20
  interface StorybookMenuToggleButtonProps {
21
21
  buttonText?: string;
22
+ buttonProps?: Partial<React.ComponentProps<typeof MenuToggleButton>>;
22
23
  }
23
24
 
24
25
  export const StorybookMenuToggleButton = ({
25
26
  buttonText,
27
+ buttonProps,
26
28
  }: StorybookMenuToggleButtonProps) => {
27
- const { isOpen } = useMenuContext();
29
+ const { isOpen } = useMenuToggleContext();
28
30
 
29
31
  return (
30
- <MenuToggleButton appearance="secondary">
32
+ <MenuToggleButton appearance="secondary" {...buttonProps}>
31
33
  <Box display="flex" justifyContent="space-between">
32
34
  {buttonText ? buttonText : <span>Select book</span>}
33
35
  <StyledChevron $isOpen={isOpen}>