1 | "use strict";
|
2 |
|
3 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4 | Object.defineProperty(exports, "__esModule", {
|
5 | value: true
|
6 | });
|
7 | exports.default = void 0;
|
8 | var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
9 | var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));
|
10 | var React = _interopRequireWildcard(require("react"));
|
11 | var _propTypes = _interopRequireDefault(require("prop-types"));
|
12 | var _utils = require("@mui/utils");
|
13 | var _FormControlContext = _interopRequireDefault(require("./FormControlContext"));
|
14 | var _formControlClasses = require("./formControlClasses");
|
15 | var _utils2 = require("../utils");
|
16 | var _composeClasses = _interopRequireDefault(require("../composeClasses"));
|
17 | var _ClassNameConfigurator = require("../utils/ClassNameConfigurator");
|
18 | var _jsxRuntime = require("react/jsx-runtime");
|
19 | const _excluded = ["defaultValue", "children", "disabled", "error", "onChange", "required", "slotProps", "slots", "value"];
|
20 | function _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); }
|
21 | function _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; }
|
22 | function hasValue(value) {
|
23 | return value != null && !(Array.isArray(value) && value.length === 0) && value !== '';
|
24 | }
|
25 | function 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 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | const FormControl = 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 (0, _jsxRuntime.jsx)(_FormControlContext.default.Provider, {
|
147 | value: childContext,
|
148 | children: (0, _jsxRuntime.jsx)(Root, (0, _extends2.default)({}, rootProps))
|
149 | });
|
150 | });
|
151 | process.env.NODE_ENV !== "production" ? FormControl.propTypes = {
|
152 |
|
153 |
|
154 |
|
155 |
|
156 | |
157 |
|
158 |
|
159 | children: _propTypes.default .oneOfType([_propTypes.default.node, _propTypes.default.func]),
|
160 | |
161 |
|
162 |
|
163 | defaultValue: _propTypes.default.any,
|
164 | |
165 |
|
166 |
|
167 |
|
168 | disabled: _propTypes.default.bool,
|
169 | |
170 |
|
171 |
|
172 |
|
173 | error: _propTypes.default.bool,
|
174 | |
175 |
|
176 |
|
177 | onChange: _propTypes.default.func,
|
178 | |
179 |
|
180 |
|
181 |
|
182 | required: _propTypes.default.bool,
|
183 | |
184 |
|
185 |
|
186 |
|
187 | slotProps: _propTypes.default.shape({
|
188 | root: _propTypes.default.oneOfType([_propTypes.default.func, _propTypes.default.object])
|
189 | }),
|
190 | |
191 |
|
192 |
|
193 |
|
194 |
|
195 | slots: _propTypes.default.shape({
|
196 | root: _propTypes.default.elementType
|
197 | }),
|
198 | |
199 |
|
200 |
|
201 | value: _propTypes.default.any
|
202 | } : void 0;
|
203 | var _default = FormControl;
|
204 | exports.default = _default; |
\ | No newline at end of file |