• /
  • EnglishEspañol日本語한국어Português
  • 로그인지금 시작하기

Switch

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

ariaLabel

string

Provide a descriptive label for this control, e.g. "Switch view".

checked

boolean

Determines the checked state of the switch, where true corresponds to the switch appearing to be switched "on", and a false value to the switch appearing to be "off".When checked is defined, the Switch component will behave like a controlled component.

className

string

Appends class names to the component. Should be used only for positioning and spacing purposes.

defaultChecked

boolean

Sets the default checked value for the switch when operating as an uncontrolled component. ie. Where the checked prop is not already stipulated.

description

string

Message with instructions on how to fill the form field.

disabled

boolean

Determines whether the user can interact with the switch, or if it should be rendered in a disabled state.

info

string

Additional information can be displayed in an info tooltip next to the Label.

invalid

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 boolean field or a string. When it is a string, as well as the invalid state being shown, the text will be shown below.

label

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.

loading

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.

onChange

function

Callback fired any time the user toggles the switch. The current checked state can be evaluated in the callback via event.target.checked.

function (
event: React.ChangeEvent
)

spacingType

enum[]

Spacing property. Spacing is defined as a tuple of zero to four values, which follow the same conventions as CSS properties like margin or padding. To omit a value, use SPACING_TYPE.OMIT.

<Array of
<One of

Switch.SPACING_TYPE.EXTRA_LARGE, Switch.SPACING_TYPE.LARGE, Switch.SPACING_TYPE.MEDIUM, Switch.SPACING_TYPE.NONE, Switch.SPACING_TYPE.OMIT, Switch.SPACING_TYPE.SMALL

>
>

style

object

Inline style for custom styling. Should be used only for positioning and spacing purposes.

testId

string

Adds a data-test-id attribute. Use it to target the component in unit and E2E tests.For a test id to be valid, prefix it with your nerdpack id, followed up by a dot.For example, my-nerdpack.some-element. Note: You might not see data-test-id attributes as they are removed from the DOM, to debug them pass a e2e-test query parameter to the URL.

Copyright © 2024 New Relic Inc.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.