UNPKG

@material-ui/core

Version:

React components that implement Google's Material Design.

194 lines (158 loc) 5.81 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = exports.styles = void 0; var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties")); var _react = _interopRequireDefault(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _clsx = _interopRequireDefault(require("clsx")); var _FormControl = require("../FormControl"); var _withStyles = _interopRequireDefault(require("../styles/withStyles")); var _Typography = _interopRequireDefault(require("../Typography")); var _helpers = require("../utils/helpers"); var styles = function styles(theme) { return { /* Styles applied to the root element. */ root: { display: 'inline-flex', alignItems: 'center', cursor: 'pointer', // For correct alignment with the text. verticalAlign: 'middle', // Remove grey highlight WebkitTapHighlightColor: 'transparent', marginLeft: -11, marginRight: 16, // used for row presentation of radio/checkbox '&$disabled': { cursor: 'default' } }, /* Styles applied to the root element if `labelPlacement="start"`. */ labelPlacementStart: { flexDirection: 'row-reverse', marginLeft: 16, // used for row presentation of radio/checkbox marginRight: -11 }, /* Styles applied to the root element if `labelPlacement="top"`. */ labelPlacementTop: { flexDirection: 'column-reverse', marginLeft: 16 }, /* Styles applied to the root element if `labelPlacement="bottom"`. */ labelPlacementBottom: { flexDirection: 'column', marginLeft: 16 }, /* Pseudo-class applied to the root element if `disabled={true}`. */ disabled: {}, /* Styles applied to the label's Typography component. */ label: { '&$disabled': { color: theme.palette.text.disabled } } }; }; /** * Drop in replacement of the `Radio`, `Switch` and `Checkbox` component. * Use this component if you want to display an extra label. */ exports.styles = styles; var FormControlLabel = _react.default.forwardRef(function FormControlLabel(props, ref) { var checked = props.checked, classes = props.classes, classNameProp = props.className, control = props.control, disabledProp = props.disabled, inputRef = props.inputRef, label = props.label, _props$labelPlacement = props.labelPlacement, labelPlacement = _props$labelPlacement === void 0 ? 'end' : _props$labelPlacement, name = props.name, onChange = props.onChange, value = props.value, other = (0, _objectWithoutProperties2.default)(props, ["checked", "classes", "className", "control", "disabled", "inputRef", "label", "labelPlacement", "name", "onChange", "value"]); var muiFormControl = (0, _FormControl.useFormControl)(); var disabled = disabledProp; if (typeof disabled === 'undefined' && typeof control.props.disabled !== 'undefined') { disabled = control.props.disabled; } if (typeof disabled === 'undefined' && muiFormControl) { disabled = muiFormControl.disabled; } var controlProps = { disabled: disabled }; ['checked', 'name', 'onChange', 'value', 'inputRef'].forEach(function (key) { if (typeof control.props[key] === 'undefined' && typeof props[key] !== 'undefined') { controlProps[key] = props[key]; } }); return _react.default.createElement("label", (0, _extends2.default)({ className: (0, _clsx.default)(classes.root, classNameProp, labelPlacement !== 'end' && classes["labelPlacement".concat((0, _helpers.capitalize)(labelPlacement))], disabled && classes.disabled), ref: ref }, other), _react.default.cloneElement(control, controlProps), _react.default.createElement(_Typography.default, { component: "span", className: (0, _clsx.default)(classes.label, disabled && classes.disabled) }, label)); }); process.env.NODE_ENV !== "production" ? FormControlLabel.propTypes = { /** * If `true`, the component appears selected. */ checked: _propTypes.default.bool, /** * Override or extend the styles applied to the component. * See [CSS API](#css) below for more details. */ classes: _propTypes.default.object.isRequired, /** * @ignore */ className: _propTypes.default.string, /** * A control element. For instance, it can be be a `Radio`, a `Switch` or a `Checkbox`. */ control: _propTypes.default.element, /** * If `true`, the control will be disabled. */ disabled: _propTypes.default.bool, /** * This prop can be used to pass a ref callback to the `input` element. */ inputRef: _propTypes.default.oneOfType([_propTypes.default.func, _propTypes.default.object]), /** * The text to be used in an enclosing label element. */ label: _propTypes.default.node, /** * The position of the label. */ labelPlacement: _propTypes.default.oneOf(['end', 'start', 'top', 'bottom']), /* * @ignore */ name: _propTypes.default.string, /** * 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.default.func, /** * The value of the component. */ value: _propTypes.default.any } : void 0; var _default = (0, _withStyles.default)(styles, { name: 'MuiFormControlLabel' })(FormControlLabel); exports.default = _default;