1<Checkboxid="test-checkbox"onChange={()=>{}}>This is a checkbox</Checkbox>
Setup with State
1const[checkedOne, setCheckedOne]=useState(false)
2constonChange=()=>{
3setCheckedOne(!checkedOne)
4}
5
6<Checkboxid="test-checkbox"onChange={handleOnChange}checked={checked}value="test-checkbox">This is a checkbox</Checkbox>
With checkbox checked
Code
1<Checkbox
2id="test-checkbox-2"
3onChange={()=>{}}
4checked
5>This is a checkbox</Checkbox>
With a different form control size applied
Code
1<Checkboxid="test-checkbox-3"onChange={()=>{}}size="small">This is a checkbox</Checkbox>
2<Checkboxid="test-checkbox-4"onChange={()=>{}}>This is a checkbox</Checkbox>
3<Checkboxid="test-checkbox-5"onChange={()=>{}}size="large">This is a checkbox</Checkbox>
With label context applied
Code
1<Checkbox
2id="test-checkbox-6"
3onChange={()=>{}}
4labelContext="Hitting checkbox will display new content"
5>This is a checkbox</Checkbox>
With error validation
an error has occured
Code
1<Checkbox
2id="test-checkbox-7"
3onChange={()=>{}}
4error
5errorText="an error has occured"
6>This is a checkbox</Checkbox>
As a disabled form control
Code
1<Checkboxid="test-checkbox-8"onChange={()=>{}}disabled>This is a checkbox</Checkbox>
2<Checkboxid="test-checkbox-10"onChange={()=>{}}checkeddisabled>This is a checkbox</Checkbox>
Design
Checkboxes are standard HTML elements, based on physical paper forms and as such are well understood by customers. Checkboxes allow customers to select the state of a binary option.
Problem
As a customer, I wish to select something or confirm a decision.
As a designer, you want to ask the customer to opt-in or opt-out of something or select something.
Solution
Present the user with a single checkbox, clearly labelled with the information required.
Guidance
Best practice guidance
Use to opt in to or select an option (e.g. terms and conditions)
by default the checkbox should not be selected.
Use short, meaningful labels.
Provide sufficient details for the customer without being too verbose.
Use additional help text to support the form label if this will help the
user enter the data correctly.
Use sentence case for all labels and help text, with the exception of
titles or names.
Use reverse pyramid writing, starting with the most important
information first.
Start your label with an active descriptive verb such as ‘Agree’ or
‘Opt’.
Consider a Switch for actions which take effect immediately (not on
submit of a form)
Consider a Switch for actions which turn a whole feature on or off
Consider a Checkbox list for multi-select non-mutually exclusive
selections.
Consider a Radio list for selecting a single option from a list of
mutually exclusive options.
Don’t use double negatives to trick customers into things they don’t
want.
The primary content to describe the form control, the label must display a meaningful description to ensure the form control is corrently understood by users.
id
string
true
The unique id of the form control.
onChange
function
true
The onChange event of the form control.
className
string
false
Allows for specific classNames to be set.
name
string
false
The name of the form control.
value
string
false
The value of the form control.
checked
boolean
false
The checked state of the form control.
size
oneOf: default | small | large
default
false
Sets the size of the form control and the associated label text, this prop controls the font size and padding of the form control. If not set "default" is output as the default setting.
Sets the width of the form control, this props controls the width of the form control. If not set width defaults to 100%.
containerWidth
boolean
true
Controls where the width is defined - true is for the width applied to the component parent container which then limits the width of the label, form control, error and support text. The default is false which sets the width only on the form control.
disabled
boolean
false
The disabled state of the form control.
error
boolean
false
The error state of the form control.
errorText
string
false
Provides the error message to be presented to users to help them correct the erroneous form control. This value will only be output when the "error" boolean props is set to true.
labelContext
string
false
Allows for additional instructions to be inserted into the label element, this content is visually hidden and can be used to provide additional context to a form control for assistive technolgy users.