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 | var _slots$root;
|
71 | const {
|
72 | defaultValue,
|
73 | children,
|
74 | disabled = false,
|
75 | error = false,
|
76 | onChange,
|
77 | required = false,
|
78 | slotProps = {},
|
79 | slots = {},
|
80 | value: incomingValue
|
81 | } = props,
|
82 | other = _objectWithoutPropertiesLoose(props, _excluded);
|
83 | const [value, setValue] = useControlled({
|
84 | controlled: incomingValue,
|
85 | default: defaultValue,
|
86 | name: 'FormControl',
|
87 | state: 'value'
|
88 | });
|
89 | const filled = hasValue(value);
|
90 | const [focusedState, setFocused] = React.useState(false);
|
91 | const focused = focusedState && !disabled;
|
92 | React.useEffect(() => setFocused(isFocused => disabled ? false : isFocused), [disabled]);
|
93 | const ownerState = _extends({}, props, {
|
94 | disabled,
|
95 | error,
|
96 | filled,
|
97 | focused,
|
98 | required
|
99 | });
|
100 | const childContext = React.useMemo(() => {
|
101 | return {
|
102 | disabled,
|
103 | error,
|
104 | filled,
|
105 | focused,
|
106 | onBlur: () => {
|
107 | setFocused(false);
|
108 | },
|
109 | onChange: event => {
|
110 | setValue(event.target.value);
|
111 | onChange == null || onChange(event);
|
112 | },
|
113 | onFocus: () => {
|
114 | setFocused(true);
|
115 | },
|
116 | required,
|
117 | value: value != null ? value : ''
|
118 | };
|
119 | }, [disabled, error, filled, focused, onChange, required, setValue, value]);
|
120 | const classes = useUtilityClasses(ownerState);
|
121 | const renderChildren = () => {
|
122 | if (typeof children === 'function') {
|
123 | return children(childContext);
|
124 | }
|
125 | return children;
|
126 | };
|
127 | const Root = (_slots$root = slots.root) != null ? _slots$root : 'div';
|
128 | const rootProps = useSlotProps({
|
129 | elementType: Root,
|
130 | externalSlotProps: slotProps.root,
|
131 | externalForwardedProps: other,
|
132 | additionalProps: {
|
133 | ref: forwardedRef,
|
134 | children: renderChildren()
|
135 | },
|
136 | ownerState,
|
137 | className: classes.root
|
138 | });
|
139 | return _jsx(FormControlContext.Provider, {
|
140 | value: childContext,
|
141 | children: _jsx(Root, _extends({}, rootProps))
|
142 | });
|
143 | });
|
144 | process.env.NODE_ENV !== "production" ? FormControl.propTypes = {
|
145 |
|
146 |
|
147 |
|
148 |
|
149 | |
150 |
|
151 |
|
152 | children: PropTypes .oneOfType([PropTypes.node, PropTypes.func]),
|
153 | |
154 |
|
155 |
|
156 | className: PropTypes.string,
|
157 | |
158 |
|
159 |
|
160 | defaultValue: PropTypes.any,
|
161 | |
162 |
|
163 |
|
164 |
|
165 | disabled: PropTypes.bool,
|
166 | |
167 |
|
168 |
|
169 |
|
170 | error: PropTypes.bool,
|
171 | |
172 |
|
173 |
|
174 | onChange: PropTypes.func,
|
175 | |
176 |
|
177 |
|
178 |
|
179 | required: PropTypes.bool,
|
180 | |
181 |
|
182 |
|
183 |
|
184 | slotProps: PropTypes.shape({
|
185 | root: PropTypes.oneOfType([PropTypes.func, PropTypes.object])
|
186 | }),
|
187 | |
188 |
|
189 |
|
190 |
|
191 |
|
192 | slots: PropTypes.shape({
|
193 | root: PropTypes.elementType
|
194 | }),
|
195 | |
196 |
|
197 |
|
198 | value: PropTypes.any
|
199 | } : void 0;
|
200 | export { FormControl }; |
\ | No newline at end of file |