UNPKG

8.14 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4Object.defineProperty(exports, "__esModule", {
5 value: true
6});
7exports.default = void 0;
8var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
9var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));
10var React = _interopRequireWildcard(require("react"));
11var _propTypes = _interopRequireDefault(require("prop-types"));
12var _utils = require("@mui/utils");
13var _FormControlContext = _interopRequireDefault(require("./FormControlContext"));
14var _formControlClasses = require("./formControlClasses");
15var _utils2 = require("../utils");
16var _composeClasses = _interopRequireDefault(require("../composeClasses"));
17var _ClassNameConfigurator = require("../utils/ClassNameConfigurator");
18var _jsxRuntime = require("react/jsx-runtime");
19const _excluded = ["defaultValue", "children", "disabled", "error", "onChange", "required", "slotProps", "slots", "value"];
20function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
21function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
22function hasValue(value) {
23 return value != null && !(Array.isArray(value) && value.length === 0) && value !== '';
24}
25function useUtilityClasses(ownerState) {
26 const {
27 disabled,
28 error,
29 filled,
30 focused,
31 required
32 } = ownerState;
33 const slots = {
34 root: ['root', disabled && 'disabled', focused && 'focused', error && 'error', filled && 'filled', required && 'required']
35 };
36 return (0, _composeClasses.default)(slots, (0, _ClassNameConfigurator.useClassNamesOverride)(_formControlClasses.getFormControlUtilityClass));
37}
38
39/**
40 * Provides context such as filled/focused/error/required for form inputs.
41 * Relying on the context provides high flexibility and ensures that the state always stays
42 * consistent across the children of the `FormControl`.
43 * This context is used by the following components:
44 *
45 * * FormLabel
46 * * FormHelperText
47 * * Input
48 * * InputLabel
49 *
50 * You can find one composition example below and more going to [the demos](https://mui.com/material-ui/react-text-field/#components).
51 *
52 * ```jsx
53 * <FormControl>
54 * <InputLabel htmlFor="my-input">Email address</InputLabel>
55 * <Input id="my-input" aria-describedby="my-helper-text" />
56 * <FormHelperText id="my-helper-text">We'll never share your email.</FormHelperText>
57 * </FormControl>
58 * ```
59 *
60 * ⚠️ Only one `Input` can be used within a FormControl because it create visual inconsistencies.
61 * For instance, only one input can be focused at the same time, the state shouldn't be shared.
62 *
63 * Demos:
64 *
65 * - [Form Control](https://mui.com/base/react-form-control/)
66 * - [Input](https://mui.com/joy-ui/react-input/)
67 * - [Checkbox](https://mui.com/material-ui/react-checkbox/)
68 * - [Radio Group](https://mui.com/material-ui/react-radio-button/)
69 * - [Switch](https://mui.com/material-ui/react-switch/)
70 * - [Text Field](https://mui.com/material-ui/react-text-field/)
71 *
72 * API:
73 *
74 * - [FormControl API](https://mui.com/base/react-form-control/components-api/#form-control)
75 */
76const FormControl = /*#__PURE__*/React.forwardRef(function FormControl(props, forwardedRef) {
77 var _slots$root;
78 const {
79 defaultValue,
80 children,
81 disabled = false,
82 error = false,
83 onChange,
84 required = false,
85 slotProps = {},
86 slots = {},
87 value: incomingValue
88 } = props,
89 other = (0, _objectWithoutPropertiesLoose2.default)(props, _excluded);
90 const [value, setValue] = (0, _utils.unstable_useControlled)({
91 controlled: incomingValue,
92 default: defaultValue,
93 name: 'FormControl',
94 state: 'value'
95 });
96 const filled = hasValue(value);
97 const [focusedState, setFocused] = React.useState(false);
98 const focused = focusedState && !disabled;
99 React.useEffect(() => setFocused(isFocused => disabled ? false : isFocused), [disabled]);
100 const ownerState = (0, _extends2.default)({}, props, {
101 disabled,
102 error,
103 filled,
104 focused,
105 required
106 });
107 const childContext = React.useMemo(() => {
108 return {
109 disabled,
110 error,
111 filled,
112 focused,
113 onBlur: () => {
114 setFocused(false);
115 },
116 onChange: event => {
117 setValue(event.target.value);
118 onChange == null ? void 0 : onChange(event);
119 },
120 onFocus: () => {
121 setFocused(true);
122 },
123 required,
124 value: value != null ? value : ''
125 };
126 }, [disabled, error, filled, focused, onChange, required, setValue, value]);
127 const classes = useUtilityClasses(ownerState);
128 const renderChildren = () => {
129 if (typeof children === 'function') {
130 return children(childContext);
131 }
132 return children;
133 };
134 const Root = (_slots$root = slots.root) != null ? _slots$root : 'div';
135 const rootProps = (0, _utils2.useSlotProps)({
136 elementType: Root,
137 externalSlotProps: slotProps.root,
138 externalForwardedProps: other,
139 additionalProps: {
140 ref: forwardedRef,
141 children: renderChildren()
142 },
143 ownerState,
144 className: classes.root
145 });
146 return /*#__PURE__*/(0, _jsxRuntime.jsx)(_FormControlContext.default.Provider, {
147 value: childContext,
148 children: /*#__PURE__*/(0, _jsxRuntime.jsx)(Root, (0, _extends2.default)({}, rootProps))
149 });
150});
151process.env.NODE_ENV !== "production" ? FormControl.propTypes /* remove-proptypes */ = {
152 // ----------------------------- Warning --------------------------------
153 // | These PropTypes are generated from the TypeScript type definitions |
154 // | To update them edit TypeScript types and run "yarn proptypes" |
155 // ----------------------------------------------------------------------
156 /**
157 * The content of the component.
158 */
159 children: _propTypes.default /* @typescript-to-proptypes-ignore */.oneOfType([_propTypes.default.node, _propTypes.default.func]),
160 /**
161 * @ignore
162 */
163 defaultValue: _propTypes.default.any,
164 /**
165 * If `true`, the label, input and helper text should be displayed in a disabled state.
166 * @default false
167 */
168 disabled: _propTypes.default.bool,
169 /**
170 * If `true`, the label is displayed in an error state.
171 * @default false
172 */
173 error: _propTypes.default.bool,
174 /**
175 * Callback fired when the form element's value is modified.
176 */
177 onChange: _propTypes.default.func,
178 /**
179 * If `true`, the label will indicate that the `input` is required.
180 * @default false
181 */
182 required: _propTypes.default.bool,
183 /**
184 * The props used for each slot inside the FormControl.
185 * @default {}
186 */
187 slotProps: _propTypes.default.shape({
188 root: _propTypes.default.oneOfType([_propTypes.default.func, _propTypes.default.object])
189 }),
190 /**
191 * The components used for each slot inside the FormControl.
192 * Either a string to use a HTML element or a component.
193 * @default {}
194 */
195 slots: _propTypes.default.shape({
196 root: _propTypes.default.elementType
197 }),
198 /**
199 * The value of the form element.
200 */
201 value: _propTypes.default.any
202} : void 0;
203var _default = FormControl;
204exports.default = _default;
\No newline at end of file