@material-ui/core
Version:
React components that implement Google's Material Design.
187 lines (153 loc) • 5.7 kB
JavaScript
"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 _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _classnames = _interopRequireDefault(require("classnames"));
var _withStyles = _interopRequireDefault(require("../styles/withStyles"));
var _Typography = _interopRequireDefault(require("../Typography"));
/* eslint-disable jsx-a11y/label-has-for */
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: -14,
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: -14
},
/* Styles 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;
function FormControlLabel(props, context) {
var _classNames;
var checked = props.checked,
classes = props.classes,
classNameProp = props.className,
control = props.control,
disabledProp = props.disabled,
inputRef = props.inputRef,
label = props.label,
labelPlacement = 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 = context.muiFormControl;
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, _classnames.default)(classes.root, (_classNames = {}, (0, _defineProperty2.default)(_classNames, classes.labelPlacementStart, labelPlacement === 'start'), (0, _defineProperty2.default)(_classNames, classes.disabled, disabled), _classNames), classNameProp)
}, other), _react.default.cloneElement(control, controlProps), _react.default.createElement(_Typography.default, {
component: "span",
className: (0, _classnames.default)(classes.label, (0, _defineProperty2.default)({}, classes.disabled, disabled))
}, label));
}
FormControlLabel.propTypes = process.env.NODE_ENV !== "production" ? {
/**
* If `true`, the component appears selected.
*/
checked: _propTypes.default.oneOfType([_propTypes.default.bool, _propTypes.default.string]),
/**
* Override or extend the styles applied to the component.
* See [CSS API](#css-api) 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,
/**
* Use that property to pass a ref callback to the native input component.
*/
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']),
/*
* @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.string
} : {};
FormControlLabel.defaultProps = {
labelPlacement: 'end'
};
FormControlLabel.contextTypes = {
muiFormControl: _propTypes.default.object
};
var _default = (0, _withStyles.default)(styles, {
name: 'MuiFormControlLabel'
})(FormControlLabel);
exports.default = _default;