UNPKG

4.91 kBJavaScriptView Raw
1import _extends from "@babel/runtime/helpers/esm/extends";
2import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
3import * as React from 'react';
4import PropTypes from 'prop-types';
5import clsx from 'clsx';
6import { refType } from '@material-ui/utils';
7import { useFormControl } from '../FormControl';
8import withStyles from '../styles/withStyles';
9import Typography from '../Typography';
10import capitalize from '../utils/capitalize';
11export const styles = theme => ({
12 /* Styles applied to the root element. */
13 root: {
14 display: 'inline-flex',
15 alignItems: 'center',
16 cursor: 'pointer',
17 // For correct alignment with the text.
18 verticalAlign: 'middle',
19 WebkitTapHighlightColor: 'transparent',
20 marginLeft: -11,
21 marginRight: 16,
22 // used for row presentation of radio/checkbox
23 '&$disabled': {
24 cursor: 'default'
25 }
26 },
27
28 /* Styles applied to the root element if `labelPlacement="start"`. */
29 labelPlacementStart: {
30 flexDirection: 'row-reverse',
31 marginLeft: 16,
32 // used for row presentation of radio/checkbox
33 marginRight: -11
34 },
35
36 /* Styles applied to the root element if `labelPlacement="top"`. */
37 labelPlacementTop: {
38 flexDirection: 'column-reverse',
39 marginLeft: 16
40 },
41
42 /* Styles applied to the root element if `labelPlacement="bottom"`. */
43 labelPlacementBottom: {
44 flexDirection: 'column',
45 marginLeft: 16
46 },
47
48 /* Pseudo-class applied to the root element if `disabled={true}`. */
49 disabled: {},
50
51 /* Styles applied to the label's Typography component. */
52 label: {
53 '&$disabled': {
54 color: theme.palette.text.disabled
55 }
56 }
57});
58/**
59 * Drop in replacement of the `Radio`, `Switch` and `Checkbox` component.
60 * Use this component if you want to display an extra label.
61 */
62
63const FormControlLabel = /*#__PURE__*/React.forwardRef(function FormControlLabel(props, ref) {
64 const {
65 classes,
66 className,
67 control,
68 disabled: disabledProp,
69 label,
70 labelPlacement = 'end'
71 } = props,
72 other = _objectWithoutPropertiesLoose(props, ["checked", "classes", "className", "control", "disabled", "inputRef", "label", "labelPlacement", "name", "onChange", "value"]);
73
74 const muiFormControl = useFormControl();
75 let disabled = disabledProp;
76
77 if (typeof disabled === 'undefined' && typeof control.props.disabled !== 'undefined') {
78 disabled = control.props.disabled;
79 }
80
81 if (typeof disabled === 'undefined' && muiFormControl) {
82 disabled = muiFormControl.disabled;
83 }
84
85 const controlProps = {
86 disabled
87 };
88 ['checked', 'name', 'onChange', 'value', 'inputRef'].forEach(key => {
89 if (typeof control.props[key] === 'undefined' && typeof props[key] !== 'undefined') {
90 controlProps[key] = props[key];
91 }
92 });
93 return /*#__PURE__*/React.createElement("label", _extends({
94 className: clsx(classes.root, className, labelPlacement !== 'end' && classes[`labelPlacement${capitalize(labelPlacement)}`], disabled && classes.disabled),
95 ref: ref
96 }, other), /*#__PURE__*/React.cloneElement(control, controlProps), /*#__PURE__*/React.createElement(Typography, {
97 component: "span",
98 className: clsx(classes.label, disabled && classes.disabled)
99 }, label));
100});
101process.env.NODE_ENV !== "production" ? FormControlLabel.propTypes = {
102 // ----------------------------- Warning --------------------------------
103 // | These PropTypes are generated from the TypeScript type definitions |
104 // | To update them edit the d.ts file and run "yarn proptypes" |
105 // ----------------------------------------------------------------------
106
107 /**
108 * If `true`, the component appears selected.
109 */
110 checked: PropTypes.bool,
111
112 /**
113 * Override or extend the styles applied to the component.
114 * See [CSS API](#css) below for more details.
115 */
116 classes: PropTypes.object,
117
118 /**
119 * @ignore
120 */
121 className: PropTypes.string,
122
123 /**
124 * A control element. For instance, it can be be a `Radio`, a `Switch` or a `Checkbox`.
125 */
126 control: PropTypes.element.isRequired,
127
128 /**
129 * If `true`, the control will be disabled.
130 */
131 disabled: PropTypes.bool,
132
133 /**
134 * Pass a ref to the `input` element.
135 */
136 inputRef: refType,
137
138 /**
139 * The text to be used in an enclosing label element.
140 */
141 label: PropTypes.node,
142
143 /**
144 * The position of the label.
145 */
146 labelPlacement: PropTypes.oneOf(['bottom', 'end', 'start', 'top']),
147
148 /**
149 * @ignore
150 */
151 name: PropTypes.string,
152
153 /**
154 * Callback fired when the state is changed.
155 *
156 * @param {object} event The event source of the callback.
157 * You can pull out the new checked state by accessing `event.target.checked` (boolean).
158 */
159 onChange: PropTypes.func,
160
161 /**
162 * The value of the component.
163 */
164 value: PropTypes.any
165} : void 0;
166export default withStyles(styles, {
167 name: 'MuiFormControlLabel'
168})(FormControlLabel);
\No newline at end of file