Back to Ant Design

Checkbox

components/checkbox/index.en-US.md

6.3.73.9 KB
Original Source

When To Use

  • Used for selecting multiple values from several options.
  • If you use only one checkbox, it is the same as using Switch to toggle between two states. The difference is that Switch will trigger the state change directly, but Checkbox just marks the state as changed and this needs to be submitted.

Examples

<!-- prettier-ignore -->

<code src="./demo/basic.tsx">Basic</code> <code src="./demo/disabled.tsx">Disabled</code> <code src="./demo/controller.tsx">Controlled Checkbox</code> <code src="./demo/group.tsx">Checkbox Group</code> <code src="./demo/check-all.tsx">Check all</code> <code src="./demo/layout.tsx">Use with Grid</code> <code src="./demo/style-class.tsx" version="6.0.0">Custom semantic dom styling</code> <code src="./demo/debug-line.tsx" debug>Same line</code> <code src="./demo/debug-disable-popover.tsx" debug>Disabled to show Tooltip</code> <code src="./demo/debug-group-width.tsx" debug>Group same width</code> <code src="./demo/custom-line-width.tsx" debug>customize lineWidth</code>

API

Common props ref:Common props

Checkbox

PropertyDescriptionTypeDefaultVersion
checkedSpecifies whether the checkbox is selectedbooleanfalse
classNamesCustomize class for each semantic structure inside the component. Supports object or function.Record<SemanticDOM, string> | (info: { props })=> Record<SemanticDOM, string>-
defaultCheckedSpecifies the initial state: whether or not the checkbox is selectedbooleanfalse
disabledIf disable checkboxbooleanfalse
indeterminateThe indeterminate checked state of checkboxbooleanfalse
onChangeThe callback function that is triggered when the state changes(e: CheckboxChangeEvent) => void-
onBlurCalled when leaving the componentfunction()-
onFocusCalled when entering the componentfunction()-
stylesCustomize inline style for each semantic structure inside the component. Supports object or function.Record<SemanticDOM, CSSProperties> | (info: { props })=> Record<SemanticDOM, CSSProperties>-

Checkbox.Group

PropertyDescriptionTypeDefaultVersion
defaultValueDefault selected value(string | number)[][]
disabledIf disable all checkboxesbooleanfalse
nameThe name property of all input[type="checkbox"] childrenstring-
optionsSpecifies optionsstring[] | number[] | Option[][]
valueUsed for setting the currently selected value(string | number | boolean)[][]
titletitle of the optionstring-
classNameclassName of the optionstring-5.25.0
stylestyles of the optionReact.CSSProperties-
onChangeThe callback function that is triggered when the state changes(checkedValue: T[]) => void-
Option
typescript
interface Option {
  label: string;
  value: string;
  disabled?: boolean;
}

Methods

Checkbox

NameDescriptionVersion
blur()Remove focus
focus()Get focus
nativeElementReturns the DOM node of the Checkbox5.17.3

Semantic DOM

<code src="./demo/_semantic.tsx" simplify="true"></code>

Design Token

<ComponentTokenTable component="Checkbox"></ComponentTokenTable>

FAQ

Why not work in Form.Item? {#faq-form-item-limitations}

Form.Item default bind value to value property, but Checkbox value property is checked. You can use valuePropName to change bind property.

tsx
<Form.Item name="fieldA" valuePropName="checked">
  <Checkbox />
</Form.Item>