5.1.7 • Published 2 months ago

@highlight-ui/checkbox v5.1.7

Weekly downloads
-
License
MIT
Repository
-
Last release
2 months ago

npm personio.design storybook.personio.design

@highlight-ui/checkbox

A control that allows the user to toggle between checked and unchecked states. Used when there are multiple items available to select, where users can pick zero, one or many items. If you require a single-selection input, please use the RadioButton.

Features

  • Checkbox
    • Controlled component
    • Toggle to disable
    • Optional label to display next to the checkbox input
    • Optional sub-label to display under the main label
    • Optional tooltip placed next to the main input label
  • Checkbox group
    • Renders multiple checkboxes given array of options
    • Controlled component
    • Vertical or horizontal display
    • Toggle to disable all checkboxes within the group
    • Optional name to apply to all checkboxes within the group

Installation

Using npm:

npm install @highlight-ui/checkbox

Using yarn:

yarn add @highlight-ui/checkbox

Using pnpm:

pnpm install @highlight-ui/checkbox

In your (S)CSS file:

@import url('@highlight-ui/checkbox');

Once the package is installed, you can import the library:

import { Checkbox, CheckboxGroup } from '@highlight-ui/checkbox';

Usage

Checkbox

import React, { useState } from 'react';
import { Checkbox } from '@highlight-ui/checkbox';

export default function CheckboxExample() {
  const [checkedStatus, setCheckedStatus] = useState(false);

  return (
    <Checkbox
      id="checkbox"
      label="Click me to tick me"
      subLabel="I am only a checkbox input"
      checked={checkedStatus}
      onClick={() => setCheckedStatus(!checkedStatus)}
    />
  );
}

Checkbox group

import React from 'react';
import { CheckboxGroup } from '@highlight-ui/checkbox';

export default function CheckboxGroupExample() {
  const [value, setValue] = useState<string[]>([]);
  const options = [
    { value: 'option-a', label: 'Option A', subLabel: 'Most important' },
    { value: 'option-b', label: 'Option B', subLabel: 'Average' },
    { value: 'option-c', label: 'Option C', subLabel: 'Lowest priority' },
  ];

  return (
    <CheckboxGroup
      name="checkbox-group"
      orientation="horizontal"
      options={options}
      value={value}
      onChange={(selectedOptions) => {
        const values = selectedOptions.map((v) => v.value) as string[];
        setValue(values);
      }}
    />
  );
}

Props 📜

Checkbox

All HTMLInputElement and PropsWithMetadata(https://gitlab.personio-internal.de/personio/platform/highlight-ui/-/blob/master/packages/utils/commons/src/types.ts#L24) props are accepted with this component. In addition to these, this component has the following props

PropTypeRequiredDefaultDescription
labelstringNonullText displayed next to the checkbox input
subLabelstringNonullText displayed below the main label
tooltipContentReact.ReactNodeNonullThe content of the tooltip pop-out. When supplied, a tooltip will be placed next to the label. This maps to the TooltipProps['content'] prop types.
tooltipMouseOutDelay'none', 'medium', 'long', or numberNonullSpecifies how to to keep the tooltip opened after the mouse has left the tooltip icon. This maps to the TooltipProps['mouseOutDelay'] prop types.
tonedefault or errorNodefaultSets the tone of the checkbox
indeterminatebooleanNonullSets the partial state of the checkbox

CheckboxGroup

Note that the CheckboxGroupOption type matches the Checkbox listed properties above.

PropTypeRequiredDefaultDescription
valueCheckboxGroupOption['value'][]YesCurrently checked values
onChangefunction(option: CheckboxGroupOption[]): voidYesFunction called when the checked status of an option changes
orientationvertical or horizontalNoverticalDisplay of the checkboxes group
optionsCheckboxGroupOption[]YesOptions to render as a group

Accessibility ♿️

Following base accesibility guidelines, the checkbox input currently:

  • Allows clicking checkbox input and main label to check or uncheck
  • Associates the checkbox input with its label through the for prop referring to the id

Potential future improvements to make it more accessible:

  • Adding focus on the whole checkbox and label container
  • Including aria tags to indicate checked and disabled states
  • Tooltip accessibility will be addressed in a future release as it is currently not adequate

Testing

There are a number of unit tests covering the Checkbox and CheckboxGroup components, the snippets below can serve as a base to expand future testing if new behaviours are added.

The relevant render methods provide the option to add or override prop values.

Checkbox

import React from 'react';
import { render } from '@testing-library/react';

import { Checkbox, CheckboxProps } from '@highlight-ui/checkbox';

type Optional<T, K extends keyof T> = Omit<T, K> & Partial<T>;
type CheckboxWrapperProps = Optional<CheckboxProps, 'checked' | 'onClick'>;

function CheckboxWrapper(props: CheckboxWrapperProps) {
  const [checkedStatus, setCheckedStatus] = useState(false);

  return (
    <Checkbox
      checked={checkedStatus}
      onClick={() => setCheckedStatus(!checkedStatus)}
      {...props}
    />
  );
}

describe('CheckboxTest', () => {
  const renderCheckboxWrapper = (props: CheckboxWrapperProps) => {
    return render(<CheckboxWrapper {...props} />);
  };

  it('test description', () => {
    renderCheckboxWrapper({});
    // write your expect here
  });
});

CheckboxGroup

import React from 'react';
import { render } from '@testing-library/react';

import { CheckboxGroup, CheckboxGroupProps, CheckboxGroupOption } from '@highlight-ui/checkbox';

type Optional<T, K extends keyof T> = Omit<T, K> & Partial<T>;
type CheckboxGroupWrapperProps = Optional<
  CheckboxGroupProps<CheckboxGroupOption>,
  'options' | 'value' | 'onChange'
>;

function CheckboxGroupWrapper(props: CheckboxGroupWrapperProps) {
   const [value, setValue] = useState<string[]>([]);

  const options = [
    {
      value: 'earth-day',
      label: 'Earth day',
      subLabel: 'Demonstrate support for environmental protection',
    },
    {
      value: 'water-day',
      label: 'World Water Day',
      subLabel: 'Highlights the importance of fresh water',
    }
  ];

  return (
    <CheckboxGroup
      options={options}
      value={value}
      onChange={(selectedOptions) => {
        const values = selectedOptions.map((v) => v.value) as string[];
        setValue(values);
      }}
      {...props}
    />
  );
}

describe('CheckboxGroupTest', () => {
  const renderCheckboxGroupWrapper = (props: CheckboxGroupWrapperProps) => {
    return render(<CheckboxWrapper {...props} />);
  };

  it('test description', () => {
    renderCheckboxGroupWrapper({});
    // write your expect here
  });
};

Place in design system 💻

The checkbox is used across our library, when there is the existence of multiple choices or bulk selection is available.

Used as part of other components, such as:

When there are mutually exclusive choices or functionalities need to be toggled, please use alternative components:

Contributing 🖌️

Please visit personio.design for usage guidelines and visual examples.

If you're interested in contributing, please visit our contribution page.

5.1.7

2 months ago

5.1.6

3 months ago

5.1.5

5 months ago

5.1.4

5 months ago

5.1.3

5 months ago

5.0.30

9 months ago

5.0.31

9 months ago

5.0.32

9 months ago

5.0.33

9 months ago

5.0.34

9 months ago

5.0.35

9 months ago

5.0.36

8 months ago

5.0.37

8 months ago

5.0.38

8 months ago

5.0.39

8 months ago

5.1.0-r18.0

8 months ago

5.0.20

10 months ago

5.0.21

10 months ago

5.0.22

10 months ago

5.0.23

10 months ago

5.0.24

10 months ago

5.0.25

10 months ago

5.0.26

10 months ago

5.0.27

9 months ago

5.0.28

9 months ago

5.0.29

9 months ago

5.0.13

10 months ago

5.0.14

10 months ago

5.0.15

10 months ago

5.0.16

10 months ago

5.0.17

10 months ago

5.0.18

10 months ago

5.0.19

10 months ago

5.1.2

7 months ago

5.1.1

7 months ago

5.1.0

7 months ago

5.0.40

8 months ago

5.0.41

7 months ago

5.0.42

7 months ago

5.0.43

7 months ago

5.0.44

7 months ago

5.0.9

11 months ago

5.0.8

11 months ago

5.0.7

12 months ago

5.0.6

12 months ago

5.0.5

12 months ago

5.0.4

12 months ago

5.0.3

12 months ago

5.0.10

11 months ago

5.0.11

11 months ago

5.0.12

11 months ago

4.4.0

1 year ago

5.0.2

1 year ago

5.0.1

1 year ago

5.0.0

1 year ago

4.3.31

1 year ago

4.3.35

1 year ago

4.3.34

1 year ago

4.3.33

1 year ago

4.3.32

1 year ago

4.3.36

1 year ago

4.3.30

1 year ago

4.3.28

1 year ago

4.3.27

1 year ago

4.3.26

1 year ago

4.3.25

1 year ago

4.3.29

1 year ago

4.3.2

1 year ago

4.3.1

1 year ago

4.3.4

1 year ago

4.3.3

1 year ago

4.3.0

1 year ago

4.3.9

1 year ago

4.3.6

1 year ago

4.3.5

1 year ago

4.3.8

1 year ago

4.3.7

1 year ago

4.3.13

1 year ago

4.3.12

1 year ago

4.3.11

1 year ago

4.3.10

1 year ago

4.3.17

1 year ago

4.3.16

1 year ago

4.3.15

1 year ago

4.3.14

1 year ago

4.3.19

1 year ago

4.3.18

1 year ago

4.2.3

1 year ago

4.2.2

1 year ago

4.2.1

1 year ago

4.2.0

1 year ago

4.3.20

1 year ago

4.3.24

1 year ago

4.1.0

2 years ago

4.3.23

1 year ago

4.3.22

1 year ago

4.1.2

1 year ago

4.3.21

1 year ago

4.0.19

2 years ago

4.0.16

2 years ago

4.0.17

2 years ago

4.0.15

2 years ago

4.0.14

2 years ago

4.0.0-alpha.1

2 years ago

4.0.5

2 years ago

4.0.4

2 years ago

4.0.10

2 years ago

4.0.7

2 years ago

4.0.6

2 years ago

4.0.1

2 years ago

4.0.0

2 years ago

4.0.3

2 years ago

4.0.2

2 years ago

4.0.12

2 years ago

4.0.11

2 years ago

4.0.13

2 years ago

4.0.9

2 years ago

4.0.0-alpha.0

2 years ago

3.1.12

2 years ago

3.1.14

2 years ago

3.1.13

2 years ago

3.1.11

2 years ago

3.1.10

2 years ago

3.1.9

2 years ago

3.1.8

2 years ago

3.1.7

2 years ago

3.1.6

2 years ago

3.1.5

2 years ago

3.1.3

3 years ago

3.1.2

3 years ago

3.1.1

3 years ago

3.1.4

3 years ago

3.0.18

3 years ago

3.1.0

3 years ago

3.0.17

3 years ago

3.0.16

3 years ago

3.0.15

3 years ago

3.0.14

3 years ago

3.0.13

3 years ago

3.0.12

3 years ago

3.0.11

3 years ago

3.0.10

3 years ago

3.0.9

3 years ago

3.0.8

3 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

2.1.8

3 years ago

2.1.7

3 years ago

2.1.9

3 years ago

3.0.0

3 years ago

2.1.10

3 years ago

2.1.6

3 years ago

2.1.2

3 years ago

2.1.4

3 years ago

2.1.3

3 years ago

2.1.5

3 years ago

2.1.1

3 years ago

2.0.10

3 years ago

2.1.0

3 years ago

2.0.9

3 years ago

2.0.8

3 years ago

2.0.7

3 years ago

2.0.6

3 years ago

2.0.5

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.0.1

3 years ago