Components
Boxes for checking and unchecking multiple values in forms.
Version: | 14.18.2 •View source•Changelog•Report issue | |
---|---|---|
Install: | yarn add @thumbtack/thumbprint-react | |
Import: | import { Checkbox } from '@thumbtack/thumbprint-react'; |
Checkboxes can be checked, unchecked, or in an indeterminate state.
The isChecked
prop determines if a checkbox is checked.
This an example of how to use many Checkbox
components at once.
Indeterminate checkboxes are used when not all items in a field are selected.
The isDisabled
prop visually and functionally disabled the radio button. It also visually disables the related label.
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.
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
.
These example use React Hooks, but if you want to use the old class-based components, you can do that.
onChange
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)
.
(
value: boolean,
id: string | undefined,
event: React.ChangeEvent<HTMLInputElement>,
) => void
isDisabled
Disables the input and the label.
boolean
false
isChecked
Determines if the checkbox is checked.
boolean
false
hasError
Makes the radio and text color red.
boolean
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.
React.ReactNode
id
The id
is added to the checkbox as an HTML attribute and passed to the onChange
function.
string
isRequired
Adds the required
HTML attribute.
boolean
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.
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
.
string
'14px 0'
onKeyDown
Function that is called when the user presses a key while focused on the Checkbox.
(event: React.KeyboardEvent<HTMLInputElement>) => void
(): void => {}
isIndeterminate
Shows a horizontal line to represent an indeterminate input.
boolean
false
dataTestId
A selector hook into the React component for use in automated testing environments. It is
applied internally to the <input />
element.
string
dataTest
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
string
checkboxVerticalAlign
Determines how the checkbox input will be vertically aligned relative to props.children
.
This prop can be one of the following 2 types:
'top'
'center'
'center'
value
Determines the value that will be submitted if the checkbox is checked. The default value is
'on'
.
This prop can be one of the following 3 types:
string
string[]
number