12.0.0 • Published 14 days ago

@leafygreen-ui/select v12.0.0

Weekly downloads
27
License
Apache-2.0
Repository
github
Last release
14 days ago

Select

npm (scoped)

View on MongoDB.design

Installation

Yarn

yarn add @leafygreen-ui/select

NPM

npm install @leafygreen-ui/select

Example

import { Option, OptionGroup, Select, Size } from '@leafygreen-ui/select';

<Select
  label="Label"
  description="Description"
  placeholder="Placeholder"
  name="Name"
  size={Size.Default}
  defaultValue="cat"
>
  <Option value="dog" description="Bark">
    Dog
  </Option>
  <Option value="cat">Cat</Option>
  <OptionGroup label="Less common">
    <Option value="hamster">Hamster</Option>
    <Option value="parrot">Parrot</Option>
  </OptionGroup>
</Select>;

Select Properties

PropTypeDescriptionDefault
childrennode<Option /> and <OptionGroup /> elements.
classNamestringAdds a className to the outermost element.
darkModebooleanDetermines whether or not the component will appear in dark mode.false
size'xsmall', 'small', 'default', 'large'Sets the size of the component's elements.'default'
idstringid associated with the Select component.
namestringThe name that will be used when submitted as part of a form.
labelstringText shown in bold above the input element.
aria-labelledbystringMust be provided if and only if neither label nor aria-label is not provided.
aria-labelstringMust be provided if and only if neither label nor aria-labelledby is not provided.
descriptionReact.ReactNodeText that gives more detail about the requirements for the input.
placeholderstringThe placeholder text shown in the input element when an option is not selected.'Select'
disabledbooleanDisables the component from being edited.false
valuestringSets the <Option /> that will appear selected and makes the component a controlled component.''
defaultValuestringSets the <Option /> that will appear selected on page load when the component is uncontrolled.''
onChangefunctionA function that gets called when the selected value changes. Receives the value string as the first argument.() => {}
readOnlybooleanDisables the console warning when the component is controlled and no onChange prop is provided.false
allowDeselectbooleanEnables or disables the option for a user to select a null default value.true
usePortalbooleanDetermines if Select dropdown will be rendered inside a portal.true
portalContainerHTMLElement | nullSets the container used for the popover's portal. NOTE: If using a scrollContainer make sure that the portalContainer is contained within the scrollContainer. E.g, passing the same refrence to scrollContainer and portalContainer.
scrollContainerHTMLElement | nullIf the popover portal has a scrollable ancestor other than the window, this prop allows passing a reference to that lement to allow the portal to position properly.
portalClassNamestringPasses the given className to the popover's portal container if the default portal container is being used.
popoverZIndexnumberSets the z-index CSS property for the popover.
state'error''none''valid'Determines the state of the <select>'none'
errorMessagestringText that shows when the state is set to error.'This input needs your attention'
successMessagestringText that shows when the state is set to valid.'Success'
baseFontSize'13', '16'Determines the base font size if sizeVariant is set to default'13'
dropdownWidthBasis'option' | 'trigger'Determines the width of the dropdown. trigger will make the dropdown width the width of the menu trigger. option will make the dropdown width as wide as the widest option.trigger

Option

PropTypeDescriptionDefault
childrenstring, numberContent to appear inside of the component.
classNamestringAdds a className to the outermost element.
glyphReact.ReactElementIcon to display next to the option text.
valuestringCorresponds to the value passed into the onChange prop of <Select /> when the option is selected.text contents of children
descriptionstringOptional descriptive text under the value
disabledbooleanPrevents the option from being selectable.false

OptionGroup

PropTypeDescriptionDefault
childrennode<Option /> elements
classNamestringAdds a className to the outermost element.
labelstringText shown above the group's options.
disabledbooleanPrevents all the contained options from being selectable.false

Test Harnesses

getTestUtils()

getTestUtils() is a util that allows consumers to reliably interact with LG Select in a product test suite. If the Select component cannot be found, an error will be thrown.

Usage

import { Select, getTestUtils } from '@leafygreen-ui/select';

const utils = getTestUtils(lgId?: string); // lgId refers to the custom `data-lgid` attribute passed to `Select`. It defaults to 'lg-select' if left empty.

Single Select

import { render } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { Select, getTestUtils } from '@leafygreen-ui/select';

...

test('select', () => {
  render(
    <Select
      label="Label"
      description="Description"
    >
      <Option value="dog" description="Bark">
        Dog
      </Option>
      <Option value="cat">Cat</Option>
      <OptionGroup label="Less common">
        <Option value="hamster">Hamster</Option>
        <Option value="parrot">Parrot</Option>
      </OptionGroup>
    </Select>
  );

  const { getInputValue, getInput, getOptions } = getTestUtils();

  expect(getInput()).toBeInTheDocument();
  expect(getInputValue()).toBe('Select');

  // opens the select
  userEvent.click(getInput());
  // `select` is an option
  expect(getOptions()).toHaveLength(5);
});

Multiple Select's

When testing multiple Select's it is recommended to add the custom data-lgid attribute to each Select.

import { render } from '@testing-library/react';
import { Select, getTestUtils } from '@leafygreen-ui/select';

...

test('select', () => {
  render(
    <>
      <Select
        label="Label 1"
        description="Description 1"
        data-lgid="select-1"
      >
        <Option value="dog" description="Bark">
          Dog
        </Option>
        <Option value="cat">Cat</Option>
        <OptionGroup label="Less common">
          <Option value="hamster">Hamster</Option>
          <Option value="parrot">Parrot</Option>
        </OptionGroup>
      </Select>
      <Select
        label="Label 2"
        description="Description 2"
        data-lgid="select-2"
        defaultValue="sad cat"
      >
        <Option value="sad dog" description="Sad Bark Bark">
          Sad Dog
        </Option>
        <Option value="sad cat">Sad Cat</Option>
        <OptionGroup label="Less common">
          <Option value="sad hamster">Sad Hamster</Option>
          <Option value="sad parrot">Sad Parrot</Option>
        </OptionGroup>
      </Select>
    </>,
  );
  const lgUtilsSelect1 = getTestUtils('select-1'); // data-lgid
  const lgUtilsSelect2 = getTestUtils('select-2'); // data-lgid

  // First Select
  expect(lgUtilsSelect1.getInput()).toBeInTheDocument();
  expect(lgUtilsSelect1.getInputValue()).toBe('Select');

  // Second Select
  expect(lgUtilsSelect2.getInput()).toBeInTheDocument();
  expect(lgUtilsSelect2.getInputValue()).toBe('sad cat');
});

Select with other LG elements

import { render } from '@testing-library/react';
import TextInput, { getTestUtils as getTextInputTestUtils } from '@leafygreen-ui/text-input';
import { Select, getTestUtils as getSelectTestUtils } from '@leafygreen-ui/select';

...

test('Form', () => {
  render(
    <Form>
      <TextInput label="TextInput label" />
      <Select
        label="Label 1"
        description="Description 1"
      >
        <Option value="dog" description="Bark">
          Dog
        </Option>
        <Option value="cat">Cat</Option>
        <OptionGroup label="Less common">
          <Option value="hamster">Hamster</Option>
          <Option value="parrot">Parrot</Option>
        </OptionGroup>
      </Select>
    </Form>,
  );

  const lgUtilsTextInput = getTextInputTestUtils();
  const lgUtilsSelect = getSelectTestUtils();

  // LG TextInput
  expect(lgUtilsTextInput.getInput()).toBeInTheDocument();
  expect(lgUtilsTextInput.getInputValue()).toBe('');

  // LG Select
  expect(lgUtilsSelect.getInput()).toBeInTheDocument();
  expect(lgUtilsSelect.getInputValue()).toBe('Select');
});

Test Utils

Elements

const {
  getInput,
  getLabel,
  getDescription,
  getErrorMessage,
  getOptions,
  getOptionByValue,
  getPopover,
  getInputValue,
  isDisabled,
  isValid,
  isError,
} = getTestUtils();
UtilDescriptionReturns
getInput()Returns the input nodeHTMLButtonElement
getLabel()Returns the label nodeHTMLButtonElement | null
getDescription()Returns the description nodeHTMLButtonElement | null
getErrorMessage()Returns the error message nodeHTMLButtonElement | null
getOptions()Returns an array of optionsArray<HTMLLIElement>
getOptionByValue(string)Returns an individual optionHTMLLIElement | null
getPopover()Returns the dropdown popoverHTMLDivElement | null
getInputValue()Returns the input valuestring
isDisabled()Returns whether the input is disabledboolean
isError()Returns whether the input state is errorboolean
12.0.0

14 days ago

11.3.2

25 days ago

11.3.1

1 month ago

11.3.0

1 month ago

11.2.3

1 month ago

11.2.2

2 months ago

11.2.1

2 months ago

11.2.0

3 months ago

11.1.3-popover.0

4 months ago

11.1.2

4 months ago

11.0.0

7 months ago

11.0.1

7 months ago

10.3.8

10 months ago

10.3.9

10 months ago

10.3.14

9 months ago

10.3.13

9 months ago

10.3.16

8 months ago

10.3.15

9 months ago

10.3.10

9 months ago

10.3.12

9 months ago

10.3.11

9 months ago

10.3.18

8 months ago

11.0.0-alpha.1

10 months ago

10.3.17

8 months ago

11.0.0-alpha.0

10 months ago

11.1.1

6 months ago

11.1.0

6 months ago

10.3.5-next.0

12 months ago

10.3.5-next.4

12 months ago

10.3.5-next.3

12 months ago

10.3.5-next.2

12 months ago

10.3.5-next.1

12 months ago

10.3.5-next.7

12 months ago

10.3.5-next.6

12 months ago

10.3.5-next.5

12 months ago

10.3.3

12 months ago

10.3.4

12 months ago

10.3.5

11 months ago

10.3.6

11 months ago

10.3.7

11 months ago

10.2.3

1 year ago

10.3.2

1 year ago

10.3.0

1 year ago

10.3.1

1 year ago

10.2.0

1 year ago

10.2.1

1 year ago

10.2.2

1 year ago

10.0.0

1 year ago

10.1.0

1 year ago

10.1.1

1 year ago

7.1.2

2 years ago

7.1.1

2 years ago

7.1.0

2 years ago

8.0.0

2 years ago

9.0.0

2 years ago

7.1.0-next.1

2 years ago

7.1.0-next.0

2 years ago

7.1.0-next.2

2 years ago

7.0.2

2 years ago

7.0.1

2 years ago

7.1.0-test.0

2 years ago

6.1.0

2 years ago

6.0.0

2 years ago

7.0.0

2 years ago

5.0.3

2 years ago

5.0.2

2 years ago

5.0.1

2 years ago

5.0.0

2 years ago

4.0.2

2 years ago

4.0.1

2 years ago

3.1.0

2 years ago

4.0.0

2 years ago

3.0.8

2 years ago

3.0.7

3 years ago

3.0.6

3 years ago

3.0.5

3 years ago

3.0.4

3 years ago

3.0.3

3 years ago

3.0.2

3 years ago

3.0.1

3 years ago

3.0.0

3 years ago

2.0.5

3 years ago

2.1.0

3 years ago

2.0.4

3 years ago

2.0.3

3 years ago

2.0.2

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.0

3 years ago