@leafygreen-ui/combobox 12.0.8 → 12.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 (36) hide show
  1. package/CHANGELOG.md +38 -0
  2. package/dist/esm/index.js +1 -1
  3. package/dist/esm/index.js.map +1 -1
  4. package/dist/types/Combobox/Combobox.d.ts +1 -1
  5. package/dist/types/Combobox/Combobox.d.ts.map +1 -1
  6. package/dist/types/Combobox/Combobox.types.d.ts +7 -1
  7. package/dist/types/Combobox/Combobox.types.d.ts.map +1 -1
  8. package/dist/types/ComboboxMenu/ComboboxMenu.d.ts +2 -0
  9. package/dist/types/ComboboxMenu/ComboboxMenu.d.ts.map +1 -1
  10. package/dist/types/ComboboxMenu/Menu.styles.d.ts +1 -0
  11. package/dist/types/ComboboxMenu/Menu.styles.d.ts.map +1 -1
  12. package/dist/types/index.d.ts +1 -0
  13. package/dist/types/index.d.ts.map +1 -1
  14. package/dist/types/types/Combobox.types.d.ts +6 -0
  15. package/dist/types/types/Combobox.types.d.ts.map +1 -1
  16. package/dist/types/types/index.d.ts +1 -1
  17. package/dist/types/types/index.d.ts.map +1 -1
  18. package/dist/umd/index.js +1 -1
  19. package/dist/umd/index.js.map +1 -1
  20. package/package.json +15 -15
  21. package/src/Combobox/Combobox.mouse.spec.tsx +1 -1
  22. package/src/Combobox/Combobox.spec.tsx +33 -0
  23. package/src/Combobox/Combobox.tsx +5 -2
  24. package/src/Combobox/Combobox.types.ts +8 -0
  25. package/src/Combobox.stories.tsx +7 -1
  26. package/src/ComboboxMenu/ComboboxMenu.tsx +9 -3
  27. package/src/ComboboxMenu/Menu.styles.ts +12 -5
  28. package/src/ComboboxOption/ComboboxOption.stories.tsx +1 -1
  29. package/src/ComboboxOption/getGlyphs.tsx +2 -2
  30. package/src/index.ts +8 -0
  31. package/src/test-utils/getTestOptions.testutils.tsx +1 -1
  32. package/src/types/Combobox.types.ts +8 -0
  33. package/src/types/index.ts +1 -0
  34. package/src/utils/ComboboxUtils.spec.tsx +1 -1
  35. package/stories.js +2 -2
  36. package/tsdoc.json +44 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leafygreen-ui/combobox",
3
- "version": "12.0.8",
3
+ "version": "12.2.0",
4
4
  "description": "leafyGreen UI Kit Combobox",
5
5
  "main": "./dist/umd/index.js",
6
6
  "module": "./dist/esm/index.js",
@@ -13,25 +13,25 @@
13
13
  "chalk": "^4.1.2",
14
14
  "lodash": "^4.17.21",
15
15
  "polished": "^4.2.2",
16
- "@leafygreen-ui/chip": "^4.0.7",
17
- "@leafygreen-ui/emotion": "^5.0.4",
18
- "@leafygreen-ui/hooks": "^9.2.1",
19
- "@leafygreen-ui/form-field": "^4.0.5",
20
- "@leafygreen-ui/checkbox": "^18.1.1",
21
- "@leafygreen-ui/icon": "^14.6.0",
22
- "@leafygreen-ui/icon-button": "^17.1.1",
23
- "@leafygreen-ui/input-option": "^4.1.1",
24
- "@leafygreen-ui/palette": "^5.0.2",
16
+ "@leafygreen-ui/checkbox": "^18.1.2",
17
+ "@leafygreen-ui/chip": "^4.0.8",
18
+ "@leafygreen-ui/emotion": "^5.1.0",
19
+ "@leafygreen-ui/icon": "^14.7.0",
20
+ "@leafygreen-ui/icon-button": "^17.1.2",
21
+ "@leafygreen-ui/form-field": "^4.0.6",
22
+ "@leafygreen-ui/hooks": "^9.3.0",
23
+ "@leafygreen-ui/input-option": "^4.1.2",
25
24
  "@leafygreen-ui/lib": "^15.6.2",
26
- "@leafygreen-ui/tokens": "^3.2.4",
27
- "@leafygreen-ui/typography": "^22.1.4",
28
- "@leafygreen-ui/popover": "^14.2.0"
25
+ "@leafygreen-ui/palette": "^5.0.2",
26
+ "@leafygreen-ui/popover": "^14.3.0",
27
+ "@leafygreen-ui/tokens": "^4.0.0",
28
+ "@leafygreen-ui/typography": "^22.2.0"
29
29
  },
30
30
  "peerDependencies": {
31
- "@leafygreen-ui/leafygreen-provider": ">=3.2.0"
31
+ "@leafygreen-ui/leafygreen-provider": "^3.2.0 || workspace:^4.0.0 || workspace:^5.0.0"
32
32
  },
33
33
  "devDependencies": {
34
- "@leafygreen-ui/button": "^25.1.1",
34
+ "@leafygreen-ui/button": "^25.1.2",
35
35
  "@lg-tools/build": "^0.8.3"
36
36
  },
37
37
  "homepage": "https://github.com/mongodb/leafygreen-ui/tree/main/packages/combobox",
@@ -10,7 +10,7 @@ import {
10
10
  import userEvent from '@testing-library/user-event';
11
11
  import isUndefined from 'lodash/isUndefined';
12
12
 
13
- import Button from '@leafygreen-ui/button';
13
+ import { Button } from '@leafygreen-ui/button';
14
14
 
15
15
  import {
16
16
  defaultOptions,
@@ -16,6 +16,7 @@ import { RenderMode } from '@leafygreen-ui/popover';
16
16
  import { eventContainingTargetValue } from '@leafygreen-ui/testing-lib';
17
17
 
18
18
  import { OptionObject } from '../ComboboxOption/ComboboxOption.types';
19
+ import { DropdownWidthBasis } from '../types';
19
20
  import {
20
21
  defaultOptions,
21
22
  groupedOptions,
@@ -55,6 +56,38 @@ describe('packages/combobox', () => {
55
56
  test.todo('expand-y');
56
57
  test.todo('scroll-x');
57
58
 
59
+ /**
60
+ * dropdownWidthBasis prop
61
+ */
62
+ describe('dropdownWidthBasis', () => {
63
+ test('applies max-content width when set to "option"', () => {
64
+ const { openMenu } = renderCombobox('single', {
65
+ dropdownWidthBasis: DropdownWidthBasis.Option,
66
+ });
67
+ const { menuContainerEl } = openMenu();
68
+
69
+ expect(menuContainerEl).toBeInTheDocument();
70
+ // The Popover wrapper should have the autoWidthStyles applied
71
+ const popoverWrapper = menuContainerEl?.parentElement?.parentElement;
72
+ expect(popoverWrapper).toHaveStyle({
73
+ width: 'max-content',
74
+ });
75
+ });
76
+
77
+ test('renders without max-content width when set to "trigger"', () => {
78
+ const { openMenu } = renderCombobox('single', {
79
+ dropdownWidthBasis: DropdownWidthBasis.Trigger,
80
+ });
81
+ const { menuContainerEl } = openMenu();
82
+
83
+ expect(menuContainerEl).toBeInTheDocument();
84
+ // The Popover wrapper should not have max-content width
85
+ const popoverWrapper = menuContainerEl?.parentElement?.parentElement;
86
+ const computedStyle = window.getComputedStyle(popoverWrapper!);
87
+ expect(computedStyle.width).not.toBe('max-content');
88
+ });
89
+ });
90
+
58
91
  const tests = [['single'], ['multiple']] as Array<Array<Select>>;
59
92
 
60
93
  describe.each(tests)('%s select', select => {
@@ -28,8 +28,8 @@ import {
28
28
  useIdAllocator,
29
29
  usePrevious,
30
30
  } from '@leafygreen-ui/hooks';
31
- import Icon from '@leafygreen-ui/icon';
32
- import IconButton from '@leafygreen-ui/icon-button';
31
+ import { Icon } from '@leafygreen-ui/icon';
32
+ import { IconButton } from '@leafygreen-ui/icon-button';
33
33
  import LeafyGreenProvider, {
34
34
  PopoverPropsProvider,
35
35
  useDarkMode,
@@ -52,6 +52,7 @@ import {
52
52
  ComboboxElement,
53
53
  ComboboxSize,
54
54
  DiffObject,
55
+ DropdownWidthBasis,
55
56
  getNullSelection,
56
57
  onChangeType,
57
58
  Overflow,
@@ -136,6 +137,7 @@ export function Combobox<M extends boolean>({
136
137
  portalRef,
137
138
  scrollContainer,
138
139
  popoverZIndex,
140
+ dropdownWidthBasis = DropdownWidthBasis.Trigger,
139
141
  ...rest
140
142
  }: ComboboxProps<M>) {
141
143
  const { darkMode, theme } = useDarkMode(darkModeProp);
@@ -1331,6 +1333,7 @@ export function Combobox<M extends boolean>({
1331
1333
  searchLoadingMessage={searchLoadingMessage}
1332
1334
  searchErrorMessage={searchErrorMessage}
1333
1335
  searchEmptyMessage={searchEmptyMessage}
1336
+ dropdownWidthBasis={dropdownWidthBasis}
1334
1337
  >
1335
1338
  {renderedOptionsJSX}
1336
1339
  </ComboboxMenu>
@@ -6,6 +6,7 @@ import { PopoverProps } from '@leafygreen-ui/popover';
6
6
 
7
7
  import {
8
8
  ComboboxSize,
9
+ DropdownWidthBasis,
9
10
  onChangeType,
10
11
  Overflow,
11
12
  SearchState,
@@ -178,6 +179,13 @@ export type BaseComboboxProps = Omit<React.ComponentProps<'div'>, 'onChange'> &
178
179
  * Allows for a controlled text-input value
179
180
  */
180
181
  inputValue?: string;
182
+
183
+ /**
184
+ * Determines the width of the dropdown. `trigger` will make the dropdown width the width of the combobox. `option` will make the dropdown width as wide as the widest option.
185
+ *
186
+ * @default 'trigger'
187
+ */
188
+ dropdownWidthBasis?: DropdownWidthBasis;
181
189
  };
182
190
 
183
191
  export type ComboboxProps<M extends boolean> = Either<
@@ -8,12 +8,13 @@ import {
8
8
  import { StoryFn } from '@storybook/react';
9
9
  import { userEvent, within } from '@storybook/test';
10
10
 
11
- import Button from '@leafygreen-ui/button';
11
+ import { Button } from '@leafygreen-ui/button';
12
12
  import { css } from '@leafygreen-ui/emotion';
13
13
 
14
14
  import { getComboboxOptions } from './test-utils/getTestOptions.testutils';
15
15
  import {
16
16
  ComboboxSize,
17
+ DropdownWidthBasis,
17
18
  Overflow,
18
19
  SearchState,
19
20
  State,
@@ -88,6 +89,7 @@ const meta: StoryMetaType<typeof Combobox> = {
88
89
  darkMode: false,
89
90
  disabled: false,
90
91
  clearable: true,
92
+ dropdownWidthBasis: DropdownWidthBasis.Trigger,
91
93
  errorMessage: 'No Pomegranates!',
92
94
  children: getComboboxOptions(),
93
95
  },
@@ -139,6 +141,10 @@ const meta: StoryMetaType<typeof Combobox> = {
139
141
  control: 'select',
140
142
  if: { arg: 'multiselect' },
141
143
  },
144
+ dropdownWidthBasis: {
145
+ options: Object.values(DropdownWidthBasis),
146
+ control: 'select',
147
+ },
142
148
  },
143
149
  };
144
150
 
@@ -3,16 +3,18 @@ import isUndefined from 'lodash/isUndefined';
3
3
 
4
4
  import { css, cx } from '@leafygreen-ui/emotion';
5
5
  import { useAvailableSpace, useForwardedRef } from '@leafygreen-ui/hooks';
6
- import Icon from '@leafygreen-ui/icon';
6
+ import { Icon } from '@leafygreen-ui/icon';
7
7
  import { useDarkMode } from '@leafygreen-ui/leafygreen-provider';
8
8
  import { palette } from '@leafygreen-ui/palette';
9
- import Popover from '@leafygreen-ui/popover';
9
+ import { Popover } from '@leafygreen-ui/popover';
10
10
  import { Error } from '@leafygreen-ui/typography';
11
11
 
12
12
  import { ComboboxProps } from '../Combobox';
13
13
  import { ComboboxContext } from '../ComboboxContext';
14
+ import { DropdownWidthBasis } from '../types';
14
15
 
15
16
  import {
17
+ autoWidthStyles,
16
18
  getMenuThemeStyles,
17
19
  loadingIconStyle,
18
20
  menuBaseStyle,
@@ -30,6 +32,7 @@ type ComboboxMenuProps = {
30
32
  id: string;
31
33
  labelId: string;
32
34
  menuWidth: number;
35
+ dropdownWidthBasis?: DropdownWidthBasis;
33
36
  } & Pick<
34
37
  ComboboxProps<any>,
35
38
  'searchLoadingMessage' | 'searchErrorMessage' | 'searchEmptyMessage'
@@ -46,6 +49,7 @@ export const ComboboxMenu = React.forwardRef<HTMLDivElement, ComboboxMenuProps>(
46
49
  searchLoadingMessage,
47
50
  searchErrorMessage,
48
51
  searchEmptyMessage,
52
+ dropdownWidthBasis = DropdownWidthBasis.Trigger,
49
53
  }: ComboboxMenuProps,
50
54
  forwardedRef,
51
55
  ) => {
@@ -134,7 +138,9 @@ export const ComboboxMenu = React.forwardRef<HTMLDivElement, ComboboxMenuProps>(
134
138
  justify="middle"
135
139
  refEl={refEl}
136
140
  adjustOnMutation={true}
137
- className={cx(popoverStyle(menuWidth), popoverThemeStyle[theme])}
141
+ className={cx(popoverStyle(menuWidth), popoverThemeStyle[theme], {
142
+ [autoWidthStyles]: dropdownWidthBasis === DropdownWidthBasis.Option,
143
+ })}
138
144
  >
139
145
  <div
140
146
  ref={ref}
@@ -1,9 +1,12 @@
1
- import { transparentize } from 'polished';
2
-
3
1
  import { css, keyframes } from '@leafygreen-ui/emotion';
4
2
  import { Theme } from '@leafygreen-ui/lib';
5
3
  import { palette } from '@leafygreen-ui/palette';
6
- import { color, fontFamilies, spacing } from '@leafygreen-ui/tokens';
4
+ import {
5
+ boxShadows,
6
+ color,
7
+ fontFamilies,
8
+ spacing,
9
+ } from '@leafygreen-ui/tokens';
7
10
 
8
11
  import { fontSize, lineHeight } from '../ComboboxChip/ComboboxChip.styles';
9
12
  import { ComboboxSize as Size } from '../types';
@@ -33,15 +36,19 @@ export const popoverStyle = (width = 384) => css`
33
36
 
34
37
  export const popoverThemeStyle: Record<Theme, string> = {
35
38
  [Theme.Light]: css`
36
- box-shadow: 0px 4px 7px ${transparentize(0.85, palette.black)};
39
+ box-shadow: ${boxShadows[Theme.Light][1]};
37
40
  border-color: ${palette.gray.light2};
38
41
  `,
39
42
  [Theme.Dark]: css`
40
- box-shadow: 0px 4px 7px ${transparentize(0.85, palette.black)};
43
+ box-shadow: ${boxShadows[Theme.Dark][1]};
41
44
  border-color: ${palette.gray.dark2};
42
45
  `,
43
46
  };
44
47
 
48
+ export const autoWidthStyles = css`
49
+ width: max-content;
50
+ `;
51
+
45
52
  export const menuBaseStyle = css`
46
53
  position: relative;
47
54
  width: 100%;
@@ -1,7 +1,7 @@
1
1
  import React from 'react';
2
2
  import { StoryMetaType, StoryType } from '@lg-tools/storybook-utils';
3
3
 
4
- import Icon from '@leafygreen-ui/icon';
4
+ import { Icon } from '@leafygreen-ui/icon';
5
5
  import LeafyGreenProvider from '@leafygreen-ui/leafygreen-provider';
6
6
 
7
7
  import { ComboboxContext, defaultContext } from '../ComboboxContext';
@@ -1,8 +1,8 @@
1
1
  import React, { ReactElement } from 'react';
2
2
 
3
- import Checkbox from '@leafygreen-ui/checkbox';
3
+ import { Checkbox } from '@leafygreen-ui/checkbox';
4
4
  import { cx } from '@leafygreen-ui/emotion';
5
- import Icon, { isComponentGlyph } from '@leafygreen-ui/icon';
5
+ import { Icon, isComponentGlyph } from '@leafygreen-ui/icon';
6
6
  import { Theme } from '@leafygreen-ui/lib';
7
7
 
8
8
  import { ComboboxSize } from '../types';
package/src/index.ts CHANGED
@@ -1,3 +1,11 @@
1
1
  export { Combobox, type ComboboxProps } from './Combobox';
2
2
  export { ComboboxGroup, type ComboboxGroupProps } from './ComboboxGroup';
3
3
  export { ComboboxOption, type ComboboxOptionProps } from './ComboboxOption';
4
+ export {
5
+ ComboboxSize,
6
+ DropdownWidthBasis,
7
+ Overflow,
8
+ SearchState,
9
+ State,
10
+ TruncationLocation,
11
+ } from './types';
@@ -1,6 +1,6 @@
1
1
  import React from 'react';
2
2
 
3
- import Icon from '@leafygreen-ui/icon';
3
+ import { Icon } from '@leafygreen-ui/icon';
4
4
 
5
5
  import { ComboboxGroup, ComboboxOption } from '..';
6
6
 
@@ -57,6 +57,14 @@ export const SearchState = {
57
57
  } as const;
58
58
  export type SearchState = (typeof SearchState)[keyof typeof SearchState];
59
59
 
60
+ /** Determines the width of the dropdown menu */
61
+ export const DropdownWidthBasis = {
62
+ Trigger: 'trigger',
63
+ Option: 'option',
64
+ } as const;
65
+ export type DropdownWidthBasis =
66
+ (typeof DropdownWidthBasis)[keyof typeof DropdownWidthBasis];
67
+
60
68
  /**
61
69
  * The type expected in the `value` or `initialValue` prop.
62
70
  * Also the type passed as an argument to `onChange`.
@@ -2,6 +2,7 @@ export {
2
2
  ComboboxElement,
3
3
  ComboboxSize,
4
4
  type DiffObject,
5
+ DropdownWidthBasis,
5
6
  getNullSelection,
6
7
  type onChangeType,
7
8
  Overflow,
@@ -1,7 +1,7 @@
1
1
  import React from 'react';
2
2
  import { render } from '@testing-library/react';
3
3
 
4
- import Icon from '@leafygreen-ui/icon';
4
+ import { Icon } from '@leafygreen-ui/icon';
5
5
 
6
6
  import { ComboboxGroup, ComboboxOption } from '..';
7
7