Components

Checkbox(React)

Boxes for checking and unchecking multiple values in forms.

Version:14.18.2 View sourceChangelogReport issue
Install:yarn add @thumbtack/thumbprint-react
Import:import { Checkbox } from '@thumbtack/thumbprint-react';

Basic checkbox

Checkboxes can be checked, unchecked, or in an indeterminate state.

The isChecked prop determines if a checkbox is checked.

Multiple checkboxes

This an example of how to use many Checkbox components at once.

Indeterminate checkboxes

Indeterminate checkboxes are used when not all items in a field are selected.

Disabled checkboxes

The isDisabled prop visually and functionally disabled the radio button. It also visually disables the related label.

Checkbox with an error

The hasError prop can be used to visually represent an error.

This prop only changes the checkbox’s color. It should be used alongside an error message that helps users advance through the form.

Multi-column content

It’s possible to provide complex UIs to the children prop. Clicking on the content will select the related checkbox.

This example puts the label content within children. It’s also possible to not provide children as undefined and use a custom label instead. In that case, you must use Checkbox’s id prop.

Checkbox input by default is vertically center-aligned with children. If children prop spans over multiple lines and you want it to align at the top, it is possible to provide checkboxVerticalAlign prop as top.

Using classes

These example use React Hooks, but if you want to use the old class-based components, you can do that.

Props

Checkbox

  • onChange
    required

    Function that runs when a checkbox value changes. It receives the new boolean value, the provided id, and the underlying event as such: props.onChange(event.target.checked, props.id, event).

    Type
    ( value: boolean, id: string | undefined, event: React.ChangeEvent<HTMLInputElement>, ) => void
  • isDisabled

    Disables the input and the label.

    Type
    boolean
    Default
    false
  • isChecked

    Determines if the checkbox is checked.

    Type
    boolean
    Default
    false
  • hasError

    Makes the radio and text color red.

    Type
    boolean
    Default
    false
  • children

    Text or elements that appear within the label. If children is not provided, the developer must use the Radio’s id prop to associate it with a custom <label> element.

    Type
    React.ReactNode
  • id

    The id is added to the checkbox as an HTML attribute and passed to the onChange function.

    Type
    string
  • isRequired

    Adds the required HTML attribute.

    Type
    boolean
    Default
    false
  • name

    Checkboxes on a page with the same name will be grouped together when sent to the server. The browser will only send the value of checkboxes that are checked.

    Type
    string
  • labelPadding

    Determine that padding that gets applied to the label. This can be used to increase the label’s click target. The value supplied should be a string with a unit such as 8px or 8px 16px.

    Type
    string
    Default
    '14px 0'
  • onKeyDown

    Function that is called when the user presses a key while focused on the Checkbox.

    Type
    (event: React.KeyboardEvent<HTMLInputElement>) => void
    Default
    (): void => {}
  • isIndeterminate

    Shows a horizontal line to represent an indeterminate input.

    Type
    boolean
    Default
    false
  • dataTestId

    A selector hook into the React component for use in automated testing environments. It is applied internally to the <input /> element.

    Type
    string
  • dataTest
    deprecated

    A selector hook into the React component for use in automated testing environments. It is applied internally to the <input /> element.

    Note: Deprecated in favor of the `dataTestId` prop

    Type
    string
  • checkboxVerticalAlign

    Determines how the checkbox input will be vertically aligned relative to props.children.

    Type

    This prop can be one of the following 2 types:

    • 'top'
    • 'center'
    Default
    'center'
  • value

    Determines the value that will be submitted if the checkbox is checked. The default value is 'on'.

    Type

    This prop can be one of the following 3 types:

    • string
    • string[]
    • number