materialuiupgraded
Version:
Material-UI's workspace package
163 lines (152 loc) • 4.35 kB
JavaScript
/* eslint-disable jsx-a11y/label-has-for */
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import withStyles from '../styles/withStyles';
import Typography from '../Typography';
export const styles = theme => ({
/* 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.
*/
function FormControlLabel(props, context) {
const {
checked,
classes,
className: classNameProp,
control,
disabled: disabledProp,
inputRef,
label,
labelPlacement,
name,
onChange,
value,
...other
} = props;
const { muiFormControl } = context;
let disabled = disabledProp;
if (typeof disabled === 'undefined' && typeof control.props.disabled !== 'undefined') {
disabled = control.props.disabled;
}
if (typeof disabled === 'undefined' && muiFormControl) {
disabled = muiFormControl.disabled;
}
const controlProps = {
disabled,
};
['checked', 'name', 'onChange', 'value', 'inputRef'].forEach(key => {
if (typeof control.props[key] === 'undefined' && typeof props[key] !== 'undefined') {
controlProps[key] = props[key];
}
});
return (
<label
className={classNames(
classes.root,
{
[]: labelPlacement === 'start',
[]: disabled,
},
classNameProp,
)}
{...other}
>
{React.cloneElement(control, controlProps)}
<Typography
component="span"
className={classNames(classes.label, { [classes.disabled]: disabled })}
>
{label}
</Typography>
</label>
);
}
FormControlLabel.propTypes = {
/**
* If `true`, the component appears selected.
*/
checked: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),
/**
* Override or extend the styles applied to the component.
* See [CSS API](#css-api) below for more details.
*/
classes: PropTypes.object.isRequired,
/**
* @ignore
*/
className: PropTypes.string,
/**
* A control element. For instance, it can be be a `Radio`, a `Switch` or a `Checkbox`.
*/
control: PropTypes.element,
/**
* If `true`, the control will be disabled.
*/
disabled: PropTypes.bool,
/**
* Use that property to pass a ref callback to the native input component.
*/
inputRef: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
/**
* The text to be used in an enclosing label element.
*/
label: PropTypes.node,
/**
* The position of the label.
*/
labelPlacement: PropTypes.oneOf(['end', 'start']),
/*
* @ignore
*/
name: PropTypes.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.func,
/**
* The value of the component.
*/
value: PropTypes.string,
};
FormControlLabel.defaultProps = {
labelPlacement: 'end',
};
FormControlLabel.contextTypes = {
muiFormControl: PropTypes.object,
};
export default withStyles(styles, { name: 'MuiFormControlLabel' })(FormControlLabel);