• /
  • Log in
  • Start now

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

ReferenceError: Switch is not defined

Disabled

ReferenceError: Switch is not defined

Loading state

ReferenceError: Switch is not defined

Label

ReferenceError: Switch is not defined

Controlled component set to loading while performing a request

Controlled component set to loading while performing a request that toggles back to unchecked if request fails

Props

checkedboolean

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.

classNamestring

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

defaultCheckedboolean
DEFAULT
false

Sets the default checked value for the switch when operating as an uncontrolled component.

ie. Where the checked prop is not already stipulated.

disabledboolean
DEFAULT
false

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

infostring

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

labelstring

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.

loadingboolean
DEFAULT
false

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.

onChangefunction

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: Event
) => undefined
spacingTypeenum[]

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,
>
>
styleobject

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

testIdstring

Adds a data-test-id.

Used to target the component in unit and e2e testing.

Methods

Switch.render

function () => undefined
Edit this pageCreate an issue
Copyright © 2021 New Relic Inc.

This site uses cookies 🍪

We use cookies and other similar technologies ("Cookies") on our websites and services to enhance your experience and to provide you with relevant content. By using our websites and services you are agreeing to the use of cookies. You can read more here. If you consent to our cookies, please click Yes.