UNPKG

6.78 kBJavaScriptView Raw
1'use client';
2
3import _extends from "@babel/runtime/helpers/esm/extends";
4import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
5const _excluded = ["defaultValue", "children", "disabled", "error", "onChange", "required", "slotProps", "slots", "value"];
6import * as React from 'react';
7import PropTypes from 'prop-types';
8import useControlled from '@mui/utils/useControlled';
9import { FormControlContext } from './FormControlContext';
10import { getFormControlUtilityClass } from './formControlClasses';
11import { useSlotProps } from '../utils';
12import { unstable_composeClasses as composeClasses } from '../composeClasses';
13import { useClassNamesOverride } from '../utils/ClassNameConfigurator';
14import { jsx as _jsx } from "react/jsx-runtime";
15function hasValue(value) {
16 return value != null && !(Array.isArray(value) && value.length === 0) && value !== '';
17}
18function 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 * Provides context such as filled/focused/error/required for form inputs.
34 * Relying on the context provides high flexibility and ensures that the state always stays
35 * consistent across the children of the `FormControl`.
36 * This context is used by the following components:
37 *
38 * * FormLabel
39 * * FormHelperText
40 * * Input
41 * * InputLabel
42 *
43 * You can find one composition example below and more going to [the demos](https://mui.com/material-ui/react-text-field/#components).
44 *
45 * ```jsx
46 * <FormControl>
47 * <InputLabel htmlFor="my-input">Email address</InputLabel>
48 * <Input id="my-input" aria-describedby="my-helper-text" />
49 * <FormHelperText id="my-helper-text">We'll never share your email.</FormHelperText>
50 * </FormControl>
51 * ```
52 *
53 * ⚠️ Only one `Input` can be used within a FormControl because it create visual inconsistencies.
54 * For instance, only one input can be focused at the same time, the state shouldn't be shared.
55 *
56 * Demos:
57 *
58 * - [Form Control](https://mui.com/base-ui/react-form-control/)
59 * - [Input](https://mui.com/joy-ui/react-input/)
60 * - [Checkbox](https://mui.com/material-ui/react-checkbox/)
61 * - [Radio Group](https://mui.com/material-ui/react-radio-button/)
62 * - [Switch](https://mui.com/material-ui/react-switch/)
63 * - [Text Field](https://mui.com/material-ui/react-text-field/)
64 *
65 * API:
66 *
67 * - [FormControl API](https://mui.com/base-ui/react-form-control/components-api/#form-control)
68 */
69const FormControl = /*#__PURE__*/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 /*#__PURE__*/_jsx(FormControlContext.Provider, {
140 value: childContext,
141 children: /*#__PURE__*/_jsx(Root, _extends({}, rootProps))
142 });
143});
144process.env.NODE_ENV !== "production" ? FormControl.propTypes /* remove-proptypes */ = {
145 // ┌────────────────────────────── Warning ──────────────────────────────┐
146 // │ These PropTypes are generated from the TypeScript type definitions. │
147 // │ To update them, edit the TypeScript types and run `pnpm proptypes`. │
148 // └─────────────────────────────────────────────────────────────────────┘
149 /**
150 * The content of the component.
151 */
152 children: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([PropTypes.node, PropTypes.func]),
153 /**
154 * Class name applied to the root element.
155 */
156 className: PropTypes.string,
157 /**
158 * @ignore
159 */
160 defaultValue: PropTypes.any,
161 /**
162 * If `true`, the label, input and helper text should be displayed in a disabled state.
163 * @default false
164 */
165 disabled: PropTypes.bool,
166 /**
167 * If `true`, the label is displayed in an error state.
168 * @default false
169 */
170 error: PropTypes.bool,
171 /**
172 * Callback fired when the form element's value is modified.
173 */
174 onChange: PropTypes.func,
175 /**
176 * If `true`, the label will indicate that the `input` is required.
177 * @default false
178 */
179 required: PropTypes.bool,
180 /**
181 * The props used for each slot inside the FormControl.
182 * @default {}
183 */
184 slotProps: PropTypes.shape({
185 root: PropTypes.oneOfType([PropTypes.func, PropTypes.object])
186 }),
187 /**
188 * The components used for each slot inside the FormControl.
189 * Either a string to use a HTML element or a component.
190 * @default {}
191 */
192 slots: PropTypes.shape({
193 root: PropTypes.elementType
194 }),
195 /**
196 * The value of the form element.
197 */
198 value: PropTypes.any
199} : void 0;
200export { FormControl };
\No newline at end of file