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