1 | 'use client';
|
2 |
|
3 | import * as React from 'react';
|
4 | import PropTypes from 'prop-types';
|
5 | import clsx from 'clsx';
|
6 | import composeClasses from '@mui/utils/composeClasses';
|
7 | import { styled } from "../zero-styled/index.js";
|
8 | import { useDefaultProps } from "../DefaultPropsProvider/index.js";
|
9 | import { isFilled, isAdornedStart } from "../InputBase/utils.js";
|
10 | import capitalize from "../utils/capitalize.js";
|
11 | import isMuiElement from "../utils/isMuiElement.js";
|
12 | import FormControlContext from "./FormControlContext.js";
|
13 | import { getFormControlUtilityClasses } from "./formControlClasses.js";
|
14 | import { jsx as _jsx } from "react/jsx-runtime";
|
15 | const useUtilityClasses = ownerState => {
|
16 | const {
|
17 | classes,
|
18 | margin,
|
19 | fullWidth
|
20 | } = ownerState;
|
21 | const slots = {
|
22 | root: ['root', margin !== 'none' && `margin${capitalize(margin)}`, fullWidth && 'fullWidth']
|
23 | };
|
24 | return composeClasses(slots, getFormControlUtilityClasses, classes);
|
25 | };
|
26 | const FormControlRoot = styled('div', {
|
27 | name: 'MuiFormControl',
|
28 | slot: 'Root',
|
29 | overridesResolver: (props, styles) => {
|
30 | const {
|
31 | ownerState
|
32 | } = props;
|
33 | return [styles.root, styles[`margin${capitalize(ownerState.margin)}`], ownerState.fullWidth && styles.fullWidth];
|
34 | }
|
35 | })({
|
36 | display: 'inline-flex',
|
37 | flexDirection: 'column',
|
38 | position: 'relative',
|
39 |
|
40 | minWidth: 0,
|
41 | padding: 0,
|
42 | margin: 0,
|
43 | border: 0,
|
44 | verticalAlign: 'top',
|
45 |
|
46 | variants: [{
|
47 | props: {
|
48 | margin: 'normal'
|
49 | },
|
50 | style: {
|
51 | marginTop: 16,
|
52 | marginBottom: 8
|
53 | }
|
54 | }, {
|
55 | props: {
|
56 | margin: 'dense'
|
57 | },
|
58 | style: {
|
59 | marginTop: 8,
|
60 | marginBottom: 4
|
61 | }
|
62 | }, {
|
63 | props: {
|
64 | fullWidth: true
|
65 | },
|
66 | style: {
|
67 | width: '100%'
|
68 | }
|
69 | }]
|
70 | });
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 | const FormControl = React.forwardRef(function FormControl(inProps, ref) {
|
97 | const props = useDefaultProps({
|
98 | props: inProps,
|
99 | name: 'MuiFormControl'
|
100 | });
|
101 | const {
|
102 | children,
|
103 | className,
|
104 | color = 'primary',
|
105 | component = 'div',
|
106 | disabled = false,
|
107 | error = false,
|
108 | focused: visuallyFocused,
|
109 | fullWidth = false,
|
110 | hiddenLabel = false,
|
111 | margin = 'none',
|
112 | required = false,
|
113 | size = 'medium',
|
114 | variant = 'outlined',
|
115 | ...other
|
116 | } = props;
|
117 | const ownerState = {
|
118 | ...props,
|
119 | color,
|
120 | component,
|
121 | disabled,
|
122 | error,
|
123 | fullWidth,
|
124 | hiddenLabel,
|
125 | margin,
|
126 | required,
|
127 | size,
|
128 | variant
|
129 | };
|
130 | const classes = useUtilityClasses(ownerState);
|
131 | const [adornedStart, setAdornedStart] = React.useState(() => {
|
132 |
|
133 |
|
134 | let initialAdornedStart = false;
|
135 | if (children) {
|
136 | React.Children.forEach(children, child => {
|
137 | if (!isMuiElement(child, ['Input', 'Select'])) {
|
138 | return;
|
139 | }
|
140 | const input = isMuiElement(child, ['Select']) ? child.props.input : child;
|
141 | if (input && isAdornedStart(input.props)) {
|
142 | initialAdornedStart = true;
|
143 | }
|
144 | });
|
145 | }
|
146 | return initialAdornedStart;
|
147 | });
|
148 | const [filled, setFilled] = React.useState(() => {
|
149 |
|
150 |
|
151 | let initialFilled = false;
|
152 | if (children) {
|
153 | React.Children.forEach(children, child => {
|
154 | if (!isMuiElement(child, ['Input', 'Select'])) {
|
155 | return;
|
156 | }
|
157 | if (isFilled(child.props, true) || isFilled(child.props.inputProps, true)) {
|
158 | initialFilled = true;
|
159 | }
|
160 | });
|
161 | }
|
162 | return initialFilled;
|
163 | });
|
164 | const [focusedState, setFocused] = React.useState(false);
|
165 | if (disabled && focusedState) {
|
166 | setFocused(false);
|
167 | }
|
168 | const focused = visuallyFocused !== undefined && !disabled ? visuallyFocused : focusedState;
|
169 | let registerEffect;
|
170 | const registeredInput = React.useRef(false);
|
171 | if (process.env.NODE_ENV !== 'production') {
|
172 | registerEffect = () => {
|
173 | if (registeredInput.current) {
|
174 | console.error(['MUI: There are multiple `InputBase` components inside a FormControl.', 'This creates visual inconsistencies, only use one `InputBase`.'].join('\n'));
|
175 | }
|
176 | registeredInput.current = true;
|
177 | return () => {
|
178 | registeredInput.current = false;
|
179 | };
|
180 | };
|
181 | }
|
182 | const onFilled = React.useCallback(() => {
|
183 | setFilled(true);
|
184 | }, []);
|
185 | const onEmpty = React.useCallback(() => {
|
186 | setFilled(false);
|
187 | }, []);
|
188 | const childContext = React.useMemo(() => {
|
189 | return {
|
190 | adornedStart,
|
191 | setAdornedStart,
|
192 | color,
|
193 | disabled,
|
194 | error,
|
195 | filled,
|
196 | focused,
|
197 | fullWidth,
|
198 | hiddenLabel,
|
199 | size,
|
200 | onBlur: () => {
|
201 | setFocused(false);
|
202 | },
|
203 | onFocus: () => {
|
204 | setFocused(true);
|
205 | },
|
206 | onEmpty,
|
207 | onFilled,
|
208 | registerEffect,
|
209 | required,
|
210 | variant
|
211 | };
|
212 | }, [adornedStart, color, disabled, error, filled, focused, fullWidth, hiddenLabel, registerEffect, onEmpty, onFilled, required, size, variant]);
|
213 | return _jsx(FormControlContext.Provider, {
|
214 | value: childContext,
|
215 | children: _jsx(FormControlRoot, {
|
216 | as: component,
|
217 | ownerState: ownerState,
|
218 | className: clsx(classes.root, className),
|
219 | ref: ref,
|
220 | ...other,
|
221 | children: children
|
222 | })
|
223 | });
|
224 | });
|
225 | process.env.NODE_ENV !== "production" ? FormControl.propTypes = {
|
226 |
|
227 |
|
228 |
|
229 |
|
230 | |
231 |
|
232 |
|
233 | children: PropTypes.node,
|
234 | |
235 |
|
236 |
|
237 | classes: PropTypes.object,
|
238 | |
239 |
|
240 |
|
241 | className: PropTypes.string,
|
242 | |
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 | color: PropTypes .oneOfType([PropTypes.oneOf(['primary', 'secondary', 'error', 'info', 'success', 'warning']), PropTypes.string]),
|
249 | |
250 |
|
251 |
|
252 |
|
253 | component: PropTypes.elementType,
|
254 | |
255 |
|
256 |
|
257 |
|
258 | disabled: PropTypes.bool,
|
259 | |
260 |
|
261 |
|
262 |
|
263 | error: PropTypes.bool,
|
264 | |
265 |
|
266 |
|
267 | focused: PropTypes.bool,
|
268 | |
269 |
|
270 |
|
271 |
|
272 | fullWidth: PropTypes.bool,
|
273 | |
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 | hiddenLabel: PropTypes.bool,
|
280 | |
281 |
|
282 |
|
283 |
|
284 | margin: PropTypes.oneOf(['dense', 'none', 'normal']),
|
285 | |
286 |
|
287 |
|
288 |
|
289 | required: PropTypes.bool,
|
290 | |
291 |
|
292 |
|
293 |
|
294 | size: PropTypes .oneOfType([PropTypes.oneOf(['medium', 'small']), PropTypes.string]),
|
295 | |
296 |
|
297 |
|
298 | sx: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])), PropTypes.func, PropTypes.object]),
|
299 | |
300 |
|
301 |
|
302 |
|
303 | variant: PropTypes.oneOf(['filled', 'outlined', 'standard'])
|
304 | } : void 0;
|
305 | export default FormControl; |
\ | No newline at end of file |