When a user chooses one or more options provided
The checkbox state can be controlled through the checked
and indeterminate
property. If neither of those is provided the checkbox will manage its own state.
You can listen to the checkbox state changes through the onChange
prop.
Note: Setting checked
will override defaultChecked
as it puts the component into a controlled state. defaultChecked
should only be used for uncontrolled checkboxes.
Usage
import { Checkbox } from 'nr1'
Examples
Basic
<div className="nr1-Docs-prettify"> <Checkbox onChange={(event) => alert('Foo')} label="Foo" /> <Checkbox indeterminate label="Bar" /> <Checkbox checked disabled label="Baz" /></div>
With info
<Checkbox onChange={(event) => alert('Foo')} info="Info value" label="Foo" />
With description
<Checkbox onChange={(event) => alert('Foo')} description="Description value" label="Foo"/>
With invalid message
<Checkbox onChange={(event) => alert('Foo')} invalid="Invalid message value" label="Foo"/>
Controlled component
class MyNerdlet extends React.PureComponent { constructor(props) { super(props);
this.state = { isChecked: false, };
this.onChange = this.onChange.bind(this); }
onChange(event) { console.log(event); const isChecked = event.target.checked;
this.setState({ isChecked }); }
render() { return ( <Checkbox checked={this.state.isChecked} onChange={this.onChange} label="Foo" /> ); }}
Props
boolean | If |
string | Appends class names to the component.Should be used only for positioning and spacing purposes. |
boolean | If |
string | Message with instructions on how to fill the form field. |
boolean | If |
boolean | If |
string | Additional information can be displayed in an info tooltip next to the Label. |
boolean|string | When true, sets the field in an invalid state, in order to notify the user attention is needed over this particular field. This property can be a |
string | Text to display as label. |
function | Callback fired any time the selected state of the checkbox changes. The event source of the callback. You can access the new value with |
enum[] | Spacing property. Spacing is defined as a tuple of zero to four values, which follow the same conventions as CSS properties like |
object | Inline style for custom styling.Should be used only for positioning and spacing purposes. |
string | Adds a |
string | The value of the component. This goes into the inner checkbox input, not the component itself, and is returned as part of the |