1 | 'use client';
|
2 |
|
3 | import _extends from "@babel/runtime/helpers/esm/extends";
|
4 | import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
|
5 | const _excluded = ["defaultValue", "children", "disabled", "error", "onChange", "required", "slotProps", "slots", "value"];
|
6 | import * as React from 'react';
|
7 | import PropTypes from 'prop-types';
|
8 | import useControlled from '@mui/utils/useControlled';
|
9 | import { FormControlContext } from './FormControlContext';
|
10 | import { getFormControlUtilityClass } from './formControlClasses';
|
11 | import { useSlotProps } from '../utils';
|
12 | import { unstable_composeClasses as composeClasses } from '../composeClasses';
|
13 | import { useClassNamesOverride } from '../utils/ClassNameConfigurator';
|
14 | import { jsx as _jsx } from "react/jsx-runtime";
|
15 | function hasValue(value) {
|
16 | return value != null && !(Array.isArray(value) && value.length === 0) && value !== '';
|
17 | }
|
18 | function useUtilityClasses(ownerState) {
|
19 | const {
|
20 | disabled,
|
21 | error,
|
22 | filled,
|
23 | focused,
|
24 | required
|
25 | } = ownerState;
|
26 | const slots = {
|
27 | root: ['root', disabled && 'disabled', focused && 'focused', error && 'error', filled && 'filled', required && 'required']
|
28 | };
|
29 | return composeClasses(slots, useClassNamesOverride(getFormControlUtilityClass));
|
30 | }
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
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 | const FormControl = React.forwardRef(function FormControl(props, forwardedRef) {
|
70 | const {
|
71 | defaultValue,
|
72 | children,
|
73 | disabled = false,
|
74 | error = false,
|
75 | onChange,
|
76 | required = false,
|
77 | slotProps = {},
|
78 | slots = {},
|
79 | value: incomingValue
|
80 | } = props,
|
81 | other = _objectWithoutPropertiesLoose(props, _excluded);
|
82 | const [value, setValue] = useControlled({
|
83 | controlled: incomingValue,
|
84 | default: defaultValue,
|
85 | name: 'FormControl',
|
86 | state: 'value'
|
87 | });
|
88 | const filled = hasValue(value);
|
89 | const [focusedState, setFocused] = React.useState(false);
|
90 | const focused = focusedState && !disabled;
|
91 | React.useEffect(() => setFocused(isFocused => disabled ? false : isFocused), [disabled]);
|
92 | const ownerState = _extends({}, props, {
|
93 | disabled,
|
94 | error,
|
95 | filled,
|
96 | focused,
|
97 | required
|
98 | });
|
99 | const childContext = React.useMemo(() => {
|
100 | return {
|
101 | disabled,
|
102 | error,
|
103 | filled,
|
104 | focused,
|
105 | onBlur: () => {
|
106 | setFocused(false);
|
107 | },
|
108 | onChange: event => {
|
109 | setValue(event.target.value);
|
110 | onChange?.(event);
|
111 | },
|
112 | onFocus: () => {
|
113 | setFocused(true);
|
114 | },
|
115 | required,
|
116 | value: value ?? ''
|
117 | };
|
118 | }, [disabled, error, filled, focused, onChange, required, setValue, value]);
|
119 | const classes = useUtilityClasses(ownerState);
|
120 | const renderChildren = () => {
|
121 | if (typeof children === 'function') {
|
122 | return children(childContext);
|
123 | }
|
124 | return children;
|
125 | };
|
126 | const Root = slots.root ?? 'div';
|
127 | const rootProps = useSlotProps({
|
128 | elementType: Root,
|
129 | externalSlotProps: slotProps.root,
|
130 | externalForwardedProps: other,
|
131 | additionalProps: {
|
132 | ref: forwardedRef,
|
133 | children: renderChildren()
|
134 | },
|
135 | ownerState,
|
136 | className: classes.root
|
137 | });
|
138 | return _jsx(FormControlContext.Provider, {
|
139 | value: childContext,
|
140 | children: _jsx(Root, _extends({}, rootProps))
|
141 | });
|
142 | });
|
143 | process.env.NODE_ENV !== "production" ? FormControl.propTypes = {
|
144 |
|
145 |
|
146 |
|
147 |
|
148 | |
149 |
|
150 |
|
151 | children: PropTypes .oneOfType([PropTypes.node, PropTypes.func]),
|
152 | |
153 |
|
154 |
|
155 | className: PropTypes.string,
|
156 | |
157 |
|
158 |
|
159 | defaultValue: PropTypes.any,
|
160 | |
161 |
|
162 |
|
163 |
|
164 | disabled: PropTypes.bool,
|
165 | |
166 |
|
167 |
|
168 |
|
169 | error: PropTypes.bool,
|
170 | |
171 |
|
172 |
|
173 | onChange: PropTypes.func,
|
174 | |
175 |
|
176 |
|
177 |
|
178 | required: PropTypes.bool,
|
179 | |
180 |
|
181 |
|
182 |
|
183 | slotProps: PropTypes.shape({
|
184 | root: PropTypes.oneOfType([PropTypes.func, PropTypes.object])
|
185 | }),
|
186 | |
187 |
|
188 |
|
189 |
|
190 |
|
191 | slots: PropTypes.shape({
|
192 | root: PropTypes.elementType
|
193 | }),
|
194 | |
195 |
|
196 |
|
197 | value: PropTypes.any
|
198 | } : void 0;
|
199 | export { FormControl }; |
\ | No newline at end of file |