React interruptor
El componente Switch Ignite UI for React es un componente de selección de opciones binarias que se comporta de manera similar al componente Switch en iOS.
React Switch Example
Usage
At its core, the IgrSwitch component allows for toggling between on/off states. The default styling is done according to the selection controls specification in the Material Design guidelines.
Primero, debes instalar el paquete npm Ignite UI for React correspondiente ejecutando el siguiente comando:
npm install igniteui-react
Entonces tendrás que importar elIgrSwitch CSS necesario y su contenido, así:
import { IgrSwitch } from 'igniteui-react';
import 'igniteui-webcomponents/themes/light/bootstrap.css';
<IgrSwitch></IgrSwitch>
[!WARNING] The
IgrSwitchcomponent doesn't work with the standard<form>element. UseForminstead.
Examples
Label
Para proporcionar una etiqueta significativa para el cambio, simplemente coloque algo de texto entre las etiquetas de apertura y cierre:
<IgrSwitch><span>Label</span></IgrSwitch>
You can specify if the label should be positioned before or after the switch toggle by setting the labelPosition attribute of the switch. Allowed values are before and after(default):
<IgrSwitch aria-labelledby="switchLabel" labelPosition="before" ><span id="switch-label">Label</span></IgrSwitch>
El interruptor también puede estar etiquetado por elementos externos al interruptor. En este caso, el usuario tiene control total para colocar y diseñar la etiqueta de acuerdo con sus necesidades.
<span id="switch-label">Label</span>
<IgrSwitch aria-labelledby="switchLabel"></IgrSwitch>
Checked
You can use the checked attribute to toggle on the switch.
<IgrSwitch checked={true}></IgrSwitch>
Required
You can use the required attribute to mark the switch as required.
<IgrSwitch required={true}></IgrSwitch>
Invalid
You can use the invalid attribute to mark the switch as invalid.
Disabled
You can use the disabled attribute to disable the switch.
<IgrSwitch disabled="true"></IgrSwitch>
Forms
You can use the name and value attributes when using the switch with Form.
<IgrSwitch name="wifi" value="enabled"></IgrSwitch>
Styling
ElIgrSwitch componente expone varias partes CSS para darte control total sobre su estilo:
| Nombre | Descripción |
|---|---|
base |
El envoltorio base del switch. |
control |
El elemento de entrada del interruptor. |
thumb |
El indicador de posición del interruptor. |
label |
La etiqueta del interruptor. |
igc-switch::part(thumb) {
background-color: var(--ig-success-500);
box-shadow: none;
}
igc-switch::part(thumb checked) {
background-color: var(--ig-gray-50);
}
igc-switch::part(control checked) {
background-color: var(--ig-success-500);
}