<Switch />
Prop | Type | Default | Description |
---|---|---|---|
checked | bool | If true, the component is checked. | |
checkedIcon | node | The icon to display when the component is checked. | |
classes | object | Override or extend the styles applied to the component. See CSS API below for more details. | |
color | 'default' | 'primary' | 'secondary' | 'secondary' | The color of the component. It supports those theme colors that make sense for this component. |
disabled | bool | If true, the switch will be disabled. | |
disableRipple | bool | If true, the ripple effect will be disabled. | |
edge | 'end' | 'start' | false | false | If given, uses a negative margin to counteract the padding on one side (this is often helpful for aligning the left or right side of the icon with content above or below, without ruining the border size and shape). |
icon | node | The icon to display when the component is unchecked. | |
id | string | The id of the input element. | |
inputProps | object | Attributes applied to the input element. | |
inputRef | ref | Pass a ref to the input element. | |
onChange | func | Callback fired when the state is changed. Signature: function(event: object) => void event: The event source of the callback. You can pull out the new value by accessing event.target.value (string). You can pull out the new checked state by accessing event.target.checked (boolean). | |
required | bool | If true, the input element will be required. | |
size | 'medium' | 'small' | 'medium' | The size of the switch. small is equivalent to the dense switch styling. |
value | any | The value of the component. The DOM API casts this to a string. The browser uses "on" as the default value. |
() => {const [state, setState] = React.useState({checkedA: true,checkedB: true,});const handleChange = (event) => {setState({ ...state, [event.target.name]: event.target.checked });};return (<div><Switchchecked={state.checkedA}onChange={handleChange}name="checkedA"inputProps={{ 'aria-label': 'secondary checkbox' }}/><Switchchecked={state.checkedB}onChange={handleChange}color="primary"name="checkedB"inputProps={{ 'aria-label': 'primary checkbox' }}/><Switch inputProps={{ 'aria-label': 'primary checkbox' }} /><Switch disabled inputProps={{ 'aria-label': 'disabled checkbox' }} /><SwitchdisabledcheckedinputProps={{ 'aria-label': 'primary checkbox' }}/></div>);};
() => {const [state, setState] = React.useState({checkedA: true,checkedB: true,});const handleChange = (event) => {setState({ ...state, [event.target.name]: event.target.checked });};return (<FormGroup row><FormControlLabelcontrol={<Switchchecked={state.checkedB}onChange={handleChange}name="checkedB"color="primary"/>}label="Primary"/><FormControlLabel control={<Switch />} label="Uncontrolled" /><FormControlLabel disabled control={<Switch />} label="Disabled" /><FormControlLabeldisabledcontrol={<Switch checked />}label="Disabled"/></FormGroup>);};
() => {const [state, setState] = React.useState({gilad: true,jason: false,antoine: true,});const handleChange = (event) => {setState({ ...state, [event.target.name]: event.target.checked });};return (<FormControl component="fieldset"><FormLabel component="legend">Assign responsibility</FormLabel><FormGroup><FormControlLabelcontrol={<Switchchecked={state.gilad}onChange={handleChange}name="gilad"/>}label="Gilad Gray"/><FormControlLabelcontrol={<Switchchecked={state.jason}onChange={handleChange}name="jason"/>}label="Jason Killian"/><FormControlLabelcontrol={<Switchchecked={state.antoine}onChange={handleChange}name="antoine"/>}label="Antoine Llorca"/></FormGroup><FormHelperText>Be careful</FormHelperText></FormControl>);};