Switches are used as toggles in content filters, or to provide a way for a user to select a binary preference.
Usage
import { Switch } from 'nr1'
Examples
Basic
<div className="nr1-Docs-prettify"> <Switch onChange={(e) => alert(`Toggle to: ${e.target.checked}`)} /> <Switch defaultChecked onChange={(e) => alert(`Toggle to: ${e.target.checked}`)} /></div>
Disabled
<div className="nr1-Docs-prettify"> <Switch disabled /> <Switch defaultChecked disabled /></div>
Loading state
<div className="nr1-Docs-prettify"> <Switch loading /> <Switch defaultChecked loading /></div>
With Label
<div className="nr1-Docs-prettify"> <Switch label="This is a label" /> <Switch disabled label="This is a label" /></div>
With label and info
<Switch label="This is a label" info="Info value" />
With description
<Switch label="This is a label" description="Description value" />
With invalid message
<Switch label="This is a label" invalid="Invalid message value" />
Controlled component set to loading while performing a request
class Example extends React.Component { constructor() { super(...arguments);
this.state = { checked: false, loading: false, };
this._onChange = this._onChange.bind(this); }
_requestMock() { return new Promise((resolve, reject) => { setTimeout(resolve, 1000); }); }
_onChange() { let initialChecked;
this.setState(({ checked }) => { initialChecked = checked;
return { checked: !checked, loading: true, }; });
this._requestMock() .then(() => { // do some stuff }) .catch(() => { this.setState({ checked: initialChecked, }); }) .finally(() => { this.setState({ loading: false, }); }); }
render() { const { checked, loading } = this.state;
return ( <Switch checked={checked} label="This is a label" loading={loading} onChange={this._onChange} /> ); }}
Controlled component set to loading while performing a request that toggles back to unchecked if request fails
class Example extends React.Component { constructor() { super(...arguments);
this.state = { checked: false, loading: false, };
this._onChange = this._onChange.bind(this); }
_requestMock() { return new Promise((resolve, reject) => { setTimeout(reject, 1000); }); }
_onChange() { let initialChecked;
this.setState(({ checked }) => { initialChecked = checked;
return { checked: !checked, loading: true, }; });
this._requestMock() .then(() => { // do some stuff }) .catch(() => { this.setState({ checked: initialChecked, }); }) .finally(() => { this.setState({ loading: false, }); }); }
render() { const { checked, loading } = this.state;
return ( <Switch checked={checked} label="This is a label" loading={loading} onChange={this._onChange} /> ); }}
Props
string | Provide a descriptive label for this control, e.g. "Switch view". |
boolean | Determines the checked state of the switch, where |
string | Appends class names to the component. Should be used only for positioning and spacing purposes. |
boolean | Sets the default checked value for the switch when operating as an uncontrolled component. ie. Where the |
string | Message with instructions on how to fill the form field. |
boolean | Determines whether the user can interact with the switch, or if it should be rendered in a disabled state. |
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 | Associate a text label to provide users a brief title or explanation for the setting controlled by the switch. The label will be positioned on the left side of the switch. |
boolean | To indicate whether an action is in progress, especially in the case that it takes more than 1 second to complete, you should display the loading state. The switch is not interactive while loading.If the action associated to the loading state fails, the checked property should be set back to its previous value. |
function | Callback fired any time the user toggles the switch. The current checked state can be evaluated in the callback via function ( |
enum[] | Spacing property. Spacing is defined as a tuple of zero to four values, which follow the same conventions as CSS properties like <Array of |
object | Inline style for custom styling. Should be used only for positioning and spacing purposes. |
string | Adds a |