UNPKG

7.51 kBJavaScriptView Raw
1import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
2import _extends from "@babel/runtime/helpers/esm/extends";
3import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
4import * as React from 'react';
5import PropTypes from 'prop-types';
6import clsx from 'clsx';
7import { refType } from '@mui/utils';
8import { unstable_composeClasses as composeClasses } from '@mui/base';
9import { useFormControl } from '../FormControl';
10import Typography from '../Typography';
11import capitalize from '../utils/capitalize';
12import styled from '../styles/styled';
13import useThemeProps from '../styles/useThemeProps';
14import formControlLabelClasses, { getFormControlLabelUtilityClasses } from './formControlLabelClasses';
15import formControlState from '../FormControl/formControlState';
16import { jsx as _jsx } from "react/jsx-runtime";
17import { jsxs as _jsxs } from "react/jsx-runtime";
18
19var useUtilityClasses = function useUtilityClasses(ownerState) {
20 var classes = ownerState.classes,
21 disabled = ownerState.disabled,
22 labelPlacement = ownerState.labelPlacement,
23 error = ownerState.error;
24 var slots = {
25 root: ['root', disabled && 'disabled', "labelPlacement".concat(capitalize(labelPlacement)), error && 'error'],
26 label: ['label', disabled && 'disabled']
27 };
28 return composeClasses(slots, getFormControlLabelUtilityClasses, classes);
29};
30
31export var FormControlLabelRoot = styled('label', {
32 name: 'MuiFormControlLabel',
33 slot: 'Root',
34 overridesResolver: function overridesResolver(props, styles) {
35 var ownerState = props.ownerState;
36 return [_defineProperty({}, "& .".concat(formControlLabelClasses.label), styles.label), styles.root, styles["labelPlacement".concat(capitalize(ownerState.labelPlacement))]];
37 }
38})(function (_ref2) {
39 var theme = _ref2.theme,
40 ownerState = _ref2.ownerState;
41 return _extends(_defineProperty({
42 display: 'inline-flex',
43 alignItems: 'center',
44 cursor: 'pointer',
45 // For correct alignment with the text.
46 verticalAlign: 'middle',
47 WebkitTapHighlightColor: 'transparent',
48 marginLeft: -11,
49 marginRight: 16
50 }, "&.".concat(formControlLabelClasses.disabled), {
51 cursor: 'default'
52 }), ownerState.labelPlacement === 'start' && {
53 flexDirection: 'row-reverse',
54 marginLeft: 16,
55 // used for row presentation of radio/checkbox
56 marginRight: -11
57 }, ownerState.labelPlacement === 'top' && {
58 flexDirection: 'column-reverse',
59 marginLeft: 16
60 }, ownerState.labelPlacement === 'bottom' && {
61 flexDirection: 'column',
62 marginLeft: 16
63 }, _defineProperty({}, "& .".concat(formControlLabelClasses.label), _defineProperty({}, "&.".concat(formControlLabelClasses.disabled), {
64 color: (theme.vars || theme).palette.text.disabled
65 })));
66});
67/**
68 * Drop-in replacement of the `Radio`, `Switch` and `Checkbox` component.
69 * Use this component if you want to display an extra label.
70 */
71
72var FormControlLabel = /*#__PURE__*/React.forwardRef(function FormControlLabel(inProps, ref) {
73 var props = useThemeProps({
74 props: inProps,
75 name: 'MuiFormControlLabel'
76 });
77
78 var checked = props.checked,
79 className = props.className,
80 _props$componentsProp = props.componentsProps,
81 componentsProps = _props$componentsProp === void 0 ? {} : _props$componentsProp,
82 control = props.control,
83 disabledProp = props.disabled,
84 disableTypography = props.disableTypography,
85 inputRef = props.inputRef,
86 labelProp = props.label,
87 _props$labelPlacement = props.labelPlacement,
88 labelPlacement = _props$labelPlacement === void 0 ? 'end' : _props$labelPlacement,
89 name = props.name,
90 onChange = props.onChange,
91 value = props.value,
92 other = _objectWithoutProperties(props, ["checked", "className", "componentsProps", "control", "disabled", "disableTypography", "inputRef", "label", "labelPlacement", "name", "onChange", "value"]);
93
94 var muiFormControl = useFormControl();
95 var disabled = disabledProp;
96
97 if (typeof disabled === 'undefined' && typeof control.props.disabled !== 'undefined') {
98 disabled = control.props.disabled;
99 }
100
101 if (typeof disabled === 'undefined' && muiFormControl) {
102 disabled = muiFormControl.disabled;
103 }
104
105 var controlProps = {
106 disabled: disabled
107 };
108 ['checked', 'name', 'onChange', 'value', 'inputRef'].forEach(function (key) {
109 if (typeof control.props[key] === 'undefined' && typeof props[key] !== 'undefined') {
110 controlProps[key] = props[key];
111 }
112 });
113 var fcs = formControlState({
114 props: props,
115 muiFormControl: muiFormControl,
116 states: ['error']
117 });
118
119 var ownerState = _extends({}, props, {
120 disabled: disabled,
121 labelPlacement: labelPlacement,
122 error: fcs.error
123 });
124
125 var classes = useUtilityClasses(ownerState);
126 var label = labelProp;
127
128 if (label != null && label.type !== Typography && !disableTypography) {
129 label = /*#__PURE__*/_jsx(Typography, _extends({
130 component: "span",
131 className: classes.label
132 }, componentsProps.typography, {
133 children: label
134 }));
135 }
136
137 return /*#__PURE__*/_jsxs(FormControlLabelRoot, _extends({
138 className: clsx(classes.root, className),
139 ownerState: ownerState,
140 ref: ref
141 }, other, {
142 children: [/*#__PURE__*/React.cloneElement(control, controlProps), label]
143 }));
144});
145process.env.NODE_ENV !== "production" ? FormControlLabel.propTypes
146/* remove-proptypes */
147= {
148 // ----------------------------- Warning --------------------------------
149 // | These PropTypes are generated from the TypeScript type definitions |
150 // | To update them edit the d.ts file and run "yarn proptypes" |
151 // ----------------------------------------------------------------------
152
153 /**
154 * If `true`, the component appears selected.
155 */
156 checked: PropTypes.bool,
157
158 /**
159 * Override or extend the styles applied to the component.
160 */
161 classes: PropTypes.object,
162
163 /**
164 * @ignore
165 */
166 className: PropTypes.string,
167
168 /**
169 * The props used for each slot inside.
170 * @default {}
171 */
172 componentsProps: PropTypes.shape({
173 typography: PropTypes.object
174 }),
175
176 /**
177 * A control element. For instance, it can be a `Radio`, a `Switch` or a `Checkbox`.
178 */
179 control: PropTypes.element.isRequired,
180
181 /**
182 * If `true`, the control is disabled.
183 */
184 disabled: PropTypes.bool,
185
186 /**
187 * If `true`, the label is rendered as it is passed without an additional typography node.
188 */
189 disableTypography: PropTypes.bool,
190
191 /**
192 * Pass a ref to the `input` element.
193 */
194 inputRef: refType,
195
196 /**
197 * A text or an element to be used in an enclosing label element.
198 */
199 label: PropTypes.node,
200
201 /**
202 * The position of the label.
203 * @default 'end'
204 */
205 labelPlacement: PropTypes.oneOf(['bottom', 'end', 'start', 'top']),
206
207 /**
208 * @ignore
209 */
210 name: PropTypes.string,
211
212 /**
213 * Callback fired when the state is changed.
214 *
215 * @param {React.SyntheticEvent} event The event source of the callback.
216 * You can pull out the new checked state by accessing `event.target.checked` (boolean).
217 */
218 onChange: PropTypes.func,
219
220 /**
221 * The system prop that allows defining system overrides as well as additional CSS styles.
222 */
223 sx: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])), PropTypes.func, PropTypes.object]),
224
225 /**
226 * The value of the component.
227 */
228 value: PropTypes.any
229} : void 0;
230export default FormControlLabel;
\No newline at end of file