10.1.1 • Published 1 month ago

@leafygreen-ui/toggle v10.1.1

Weekly downloads
422
License
Apache-2.0
Repository
github
Last release
1 month ago

Toggle

npm (scoped)

View on MongoDB.design

Installation

Yarn

yarn add @leafygreen-ui/toggle

NPM

npm install @leafygreen-ui/toggle

Example

import Toggle from '@leafygreen-ui/toggle';

return (
  <>
    <label id="label" htmlFor="toggle">
      Change setting
    </label>

    <Toggle
      id="toggle"
      aria-labelledby="label"
      onChange={(checked, event) => {
        /* Handle the change event */
      }}
    />
  </>
);

Properties

PropTypeDescriptionDefault
darkModebooleanDetermines if the Toggle will render the dark mode styles.false
size'default', 'small', 'xsmall'Sets the size of the toggle.'default'
checkedbooleanSet's the checked state of the Toggle.
disabledbooleanDisables the Toggle.false
onChange(checked, MouseEvent) => voidonChange fires when the checked state of the component is being updated. Receives the updated checked state of the toggle as its first argument, and the associated mouse event as the second.
classNamestringAdds a className to the outermost element.''
...HTML button attributesAny supported HTML button properties will be applied to the button element.

Accessibility

For the Toggle to be accessible to screen readers, you must pass either aria-label or aria-labelledby to Toggle. Please note, if this is a part of a form, this is in addition to using htmlFor to associate a label with the Toggle. You will see TypeScript and console errors if this isn't done.

Please reach out if you would like further guidance on how to programmatically associate text with the Toggle component.

Test Harnesses

getTestUtils()

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

Usage

import Toggle, { getTestUtils } from '@leafygreen-ui/toggle';

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

Single Toggle

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

...

test('toggle', () => {
  render(<Toggle aria-label="label" />);
  const { getInput, getInputValue } = getTestUtils();

  expect(getInput()).toBeInTheDocument();
  expect(getInputValue()).toBe(false);
});

Multiple Toggle's

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

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

...

test('toggle', () => {
  render(
    <>
      <Toggle data-lgid="toggle-1" aria-label="label 1" />
      <Toggle data-lgid="toggle-2" aria-label="label 2" checked />
    </>,
  );
  const utilsOne = getTestUtils('toggle-1'); // data-lgid
  const utilsTwo = getTestUtils('toggle-2'); // data-lgid

  // First toggle
  expect(utilsOne.getInput()).toBeInTheDocument();
  expect(utilsOne.getInputValue()).toBe(false);

  // Second Toggle
  expect(utilsTwo.getInput()).toBeInTheDocument();
  expect(utilsTwo.getInputValue()).toBe(true);
});

Toggle with other LG form elements

import { render } from '@testing-library/react';
import Toggle, { getTestUtils as getToggleTestUtils } from '@leafygreen-ui/toggle';
import TextInput, { getTestUtils as getTextInputTestUtils } from '@leafygreen-ui/text-input';
import TextArea, { getTestUtils as getTextAreaTestUtils } from '@leafygreen-ui/text-area';

...

test('Form', () => {
  render(
    <Form>
      <Toggle aria-label="Toggle label" />
      <TextInput label="TextInput label" />
      <TextArea label="TextArea label" />
    </Form>,
  );

  const toggleInputUtils = getToggleTestUtils();
  const textInputUtils = getTextInputTestUtils();
  const textAreaUtils = getTextAreaTestUtils();

  // LG Toggle
  expect(toggleInputUtils.getInput()).toBeInTheDocument();
  expect(toggleInputUtils.getInputValue()).toBe(false);

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

  // LG TextArea
  expect(textAreaUtils.getInput()).toBeInTheDocument();
  expect(textAreaUtils.getInputValue()).toBe('');


});

Test Utils

Elements

const { getInput, isDisabled, getInputValue } = getTestUtils();
UtilDescriptionReturns
getInputReturns the input nodeHTMLButtonElement
isDisabledReturns whether the input is disabledboolean
getInputValueReturns the input valueboolean
10.1.1

1 month ago

10.1.0

1 month ago

10.0.20

1 month ago

10.0.19

2 months ago

10.0.18

2 months ago

11.0.0-alpha.1

10 months ago

11.0.0-alpha.0

10 months ago

10.0.13

9 months ago

10.0.12

10 months ago

10.0.17

7 months ago

10.0.16

8 months ago

10.0.15

9 months ago

10.0.14

9 months ago

10.0.8

12 months ago

10.0.9

12 months ago

10.0.9-next.5

12 months ago

10.0.9-next.4

12 months ago

10.0.9-next.3

12 months ago

10.0.9-next.2

12 months ago

10.0.9-next.7

12 months ago

10.0.9-next.6

12 months ago

10.0.11

11 months ago

10.0.10

11 months ago

10.0.9-next.1

12 months ago

10.0.9-next.0

12 months ago

10.0.6

1 year ago

10.0.7

1 year ago

10.0.5

1 year ago

10.0.0

1 year ago

10.0.1

1 year ago

10.0.2

1 year ago

10.0.3

1 year ago

10.0.4

1 year ago

8.0.5

2 years ago

8.0.4

2 years ago

8.0.6

2 years ago

9.0.0

2 years ago

8.0.4-test.0

2 years ago

8.0.4-next.0

2 years ago

8.0.3

2 years ago

8.0.4-next.1

2 years ago

8.0.4-next.2

2 years ago

8.0.2

2 years ago

8.0.1

2 years ago

8.0.0

2 years ago

7.0.5

2 years ago

7.0.4

3 years ago

7.0.3

3 years ago

7.0.2

3 years ago

7.0.1

3 years ago

7.0.0

3 years ago

6.0.0

3 years ago

5.0.2

3 years ago

5.0.1

4 years ago

5.0.0

4 years ago

4.1.1

4 years ago

4.1.0

4 years ago

4.0.0

4 years ago

3.0.4

4 years ago

3.0.3

4 years ago

3.0.2

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.1.8

4 years ago

2.1.7

5 years ago

2.1.6

5 years ago

2.1.5

5 years ago

2.1.4

5 years ago

2.1.3

5 years ago

2.1.2

5 years ago

2.1.1

5 years ago

2.1.0

5 years ago

2.0.4

5 years ago

2.0.3

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.1.2

5 years ago