UNPKG

@material-ui/core

Version:

React components that implement Google's Material Design.

244 lines (210 loc) 6.15 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray"; import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties"; import React from 'react'; import PropTypes from 'prop-types'; import clsx from 'clsx'; import useFormControl from '../FormControl/useFormControl'; import withStyles from '../styles/withStyles'; import IconButton from '../IconButton'; export var styles = { root: { padding: 9 }, checked: {}, disabled: {}, input: { cursor: 'inherit', position: 'absolute', opacity: 0, width: '100%', height: '100%', top: 0, left: 0, margin: 0, padding: 0 } }; /** * @ignore - internal component. */ var SwitchBase = React.forwardRef(function SwitchBase(props, ref) { var autoFocus = props.autoFocus, checkedProp = props.checked, checkedIcon = props.checkedIcon, classes = props.classes, classNameProp = props.className, defaultChecked = props.defaultChecked, disabledProp = props.disabled, icon = props.icon, id = props.id, inputProps = props.inputProps, inputRef = props.inputRef, name = props.name, onBlur = props.onBlur, onChange = props.onChange, onFocus = props.onFocus, readOnly = props.readOnly, required = props.required, tabIndex = props.tabIndex, type = props.type, value = props.value, other = _objectWithoutProperties(props, ["autoFocus", "checked", "checkedIcon", "classes", "className", "defaultChecked", "disabled", "icon", "id", "inputProps", "inputRef", "name", "onBlur", "onChange", "onFocus", "readOnly", "required", "tabIndex", "type", "value"]); var _React$useRef = React.useRef(checkedProp != null), isControlled = _React$useRef.current; var _React$useState = React.useState(Boolean(defaultChecked)), _React$useState2 = _slicedToArray(_React$useState, 2), checkedState = _React$useState2[0], setCheckedState = _React$useState2[1]; var muiFormControl = useFormControl(); var handleFocus = function handleFocus(event) { if (onFocus) { onFocus(event); } if (muiFormControl && muiFormControl.onFocus) { muiFormControl.onFocus(event); } }; var handleBlur = function handleBlur(event) { if (onBlur) { onBlur(event); } if (muiFormControl && muiFormControl.onBlur) { muiFormControl.onBlur(event); } }; var handleInputChange = function handleInputChange(event) { var checked = event.target.checked; if (!isControlled) { setCheckedState(checked); } if (onChange) { onChange(event, checked); } }; var disabled = disabledProp; if (muiFormControl) { if (typeof disabled === 'undefined') { disabled = muiFormControl.disabled; } } var checked = isControlled ? checkedProp : checkedState; var hasLabelFor = type === 'checkbox' || type === 'radio'; return React.createElement(IconButton, _extends({ component: "span", className: clsx(classes.root, classNameProp, checked && classes.checked, disabled && classes.disabled), disabled: disabled, tabIndex: null, role: undefined, onFocus: handleFocus, onBlur: handleBlur, ref: ref }, other), checked ? checkedIcon : icon, React.createElement("input", _extends({ autoFocus: autoFocus, checked: checkedProp, defaultChecked: defaultChecked, className: classes.input, disabled: disabled, id: hasLabelFor && id, name: name, onChange: handleInputChange, readOnly: readOnly, ref: inputRef, required: required, tabIndex: tabIndex, type: type, value: value }, inputProps))); }); // NB: If changed, please update Checkbox, Switch and Radio // so that the API documentation is updated. process.env.NODE_ENV !== "production" ? SwitchBase.propTypes = { /** * If `true`, the `input` element will be focused during the first mount. */ autoFocus: PropTypes.bool, /** * If `true`, the component is checked. */ checked: PropTypes.bool, /** * The icon to display when the component is checked. */ checkedIcon: PropTypes.node.isRequired, /** * Override or extend the styles applied to the component. * See [CSS API](#css) below for more details. */ classes: PropTypes.object.isRequired, /** * @ignore */ className: PropTypes.string, /** * @ignore */ defaultChecked: PropTypes.bool, /** * If `true`, the switch will be disabled. */ disabled: PropTypes.bool, /** * The icon to display when the component is unchecked. */ icon: PropTypes.node.isRequired, /** * The id of the `input` element. */ id: PropTypes.string, /** * [Attributes](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#Attributes) applied to the `input` element. */ inputProps: PropTypes.object, /** * This prop can be used to pass a ref callback to the `input` element. */ inputRef: PropTypes.oneOfType([PropTypes.func, PropTypes.object]), /* * @ignore */ name: PropTypes.string, /** * @ignore */ onBlur: PropTypes.func, /** * Callback fired when the state is changed. * * @param {object} event The event source of the callback. * You can pull out the new value by accessing `event.target.checked`. * @param {boolean} checked The `checked` value of the switch */ onChange: PropTypes.func, /** * @ignore */ onFocus: PropTypes.func, /** * It prevents the user from changing the value of the field * (not from interacting with the field). */ readOnly: PropTypes.bool, /** * If `true`, the `input` element will be required. */ required: PropTypes.bool, /** * @ignore */ tabIndex: PropTypes.oneOfType([PropTypes.number, PropTypes.string]), /** * The input component prop `type`. */ type: PropTypes.string.isRequired, /** * The value of the component. */ value: PropTypes.any } : void 0; export default withStyles(styles, { name: 'PrivateSwitchBase' })(SwitchBase);