You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
207 lines
8.0 KiB
JavaScript
207 lines
8.0 KiB
JavaScript
"use strict";
|
|
'use client';
|
|
|
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
exports.FormControl = void 0;
|
|
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
|
var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));
|
|
var React = _interopRequireWildcard(require("react"));
|
|
var _propTypes = _interopRequireDefault(require("prop-types"));
|
|
var _useControlled = _interopRequireDefault(require("@mui/utils/useControlled"));
|
|
var _FormControlContext = require("./FormControlContext");
|
|
var _formControlClasses = require("./formControlClasses");
|
|
var _utils = require("../utils");
|
|
var _composeClasses = require("../composeClasses");
|
|
var _ClassNameConfigurator = require("../utils/ClassNameConfigurator");
|
|
var _jsxRuntime = require("react/jsx-runtime");
|
|
const _excluded = ["defaultValue", "children", "disabled", "error", "onChange", "required", "slotProps", "slots", "value"];
|
|
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
function hasValue(value) {
|
|
return value != null && !(Array.isArray(value) && value.length === 0) && value !== '';
|
|
}
|
|
function useUtilityClasses(ownerState) {
|
|
const {
|
|
disabled,
|
|
error,
|
|
filled,
|
|
focused,
|
|
required
|
|
} = ownerState;
|
|
const slots = {
|
|
root: ['root', disabled && 'disabled', focused && 'focused', error && 'error', filled && 'filled', required && 'required']
|
|
};
|
|
return (0, _composeClasses.unstable_composeClasses)(slots, (0, _ClassNameConfigurator.useClassNamesOverride)(_formControlClasses.getFormControlUtilityClass));
|
|
}
|
|
|
|
/**
|
|
* Provides context such as filled/focused/error/required for form inputs.
|
|
* Relying on the context provides high flexibility and ensures that the state always stays
|
|
* consistent across the children of the `FormControl`.
|
|
* This context is used by the following components:
|
|
*
|
|
* * FormLabel
|
|
* * FormHelperText
|
|
* * Input
|
|
* * InputLabel
|
|
*
|
|
* You can find one composition example below and more going to [the demos](https://mui.com/material-ui/react-text-field/#components).
|
|
*
|
|
* ```jsx
|
|
* <FormControl>
|
|
* <InputLabel htmlFor="my-input">Email address</InputLabel>
|
|
* <Input id="my-input" aria-describedby="my-helper-text" />
|
|
* <FormHelperText id="my-helper-text">We'll never share your email.</FormHelperText>
|
|
* </FormControl>
|
|
* ```
|
|
*
|
|
* ⚠️ Only one `Input` can be used within a FormControl because it create visual inconsistencies.
|
|
* For instance, only one input can be focused at the same time, the state shouldn't be shared.
|
|
*
|
|
* Demos:
|
|
*
|
|
* - [Form Control](https://mui.com/base-ui/react-form-control/)
|
|
* - [Input](https://mui.com/joy-ui/react-input/)
|
|
* - [Checkbox](https://mui.com/material-ui/react-checkbox/)
|
|
* - [Radio Group](https://mui.com/material-ui/react-radio-button/)
|
|
* - [Switch](https://mui.com/material-ui/react-switch/)
|
|
* - [Text Field](https://mui.com/material-ui/react-text-field/)
|
|
*
|
|
* API:
|
|
*
|
|
* - [FormControl API](https://mui.com/base-ui/react-form-control/components-api/#form-control)
|
|
*/
|
|
const FormControl = exports.FormControl = /*#__PURE__*/React.forwardRef(function FormControl(props, forwardedRef) {
|
|
var _slots$root;
|
|
const {
|
|
defaultValue,
|
|
children,
|
|
disabled = false,
|
|
error = false,
|
|
onChange,
|
|
required = false,
|
|
slotProps = {},
|
|
slots = {},
|
|
value: incomingValue
|
|
} = props,
|
|
other = (0, _objectWithoutPropertiesLoose2.default)(props, _excluded);
|
|
const [value, setValue] = (0, _useControlled.default)({
|
|
controlled: incomingValue,
|
|
default: defaultValue,
|
|
name: 'FormControl',
|
|
state: 'value'
|
|
});
|
|
const filled = hasValue(value);
|
|
const [focusedState, setFocused] = React.useState(false);
|
|
const focused = focusedState && !disabled;
|
|
React.useEffect(() => setFocused(isFocused => disabled ? false : isFocused), [disabled]);
|
|
const ownerState = (0, _extends2.default)({}, props, {
|
|
disabled,
|
|
error,
|
|
filled,
|
|
focused,
|
|
required
|
|
});
|
|
const childContext = React.useMemo(() => {
|
|
return {
|
|
disabled,
|
|
error,
|
|
filled,
|
|
focused,
|
|
onBlur: () => {
|
|
setFocused(false);
|
|
},
|
|
onChange: event => {
|
|
setValue(event.target.value);
|
|
onChange == null || onChange(event);
|
|
},
|
|
onFocus: () => {
|
|
setFocused(true);
|
|
},
|
|
required,
|
|
value: value != null ? value : ''
|
|
};
|
|
}, [disabled, error, filled, focused, onChange, required, setValue, value]);
|
|
const classes = useUtilityClasses(ownerState);
|
|
const renderChildren = () => {
|
|
if (typeof children === 'function') {
|
|
return children(childContext);
|
|
}
|
|
return children;
|
|
};
|
|
const Root = (_slots$root = slots.root) != null ? _slots$root : 'div';
|
|
const rootProps = (0, _utils.useSlotProps)({
|
|
elementType: Root,
|
|
externalSlotProps: slotProps.root,
|
|
externalForwardedProps: other,
|
|
additionalProps: {
|
|
ref: forwardedRef,
|
|
children: renderChildren()
|
|
},
|
|
ownerState,
|
|
className: classes.root
|
|
});
|
|
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_FormControlContext.FormControlContext.Provider, {
|
|
value: childContext,
|
|
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(Root, (0, _extends2.default)({}, rootProps))
|
|
});
|
|
});
|
|
process.env.NODE_ENV !== "production" ? FormControl.propTypes /* remove-proptypes */ = {
|
|
// ┌────────────────────────────── Warning ──────────────────────────────┐
|
|
// │ These PropTypes are generated from the TypeScript type definitions. │
|
|
// │ To update them, edit the TypeScript types and run `pnpm proptypes`. │
|
|
// └─────────────────────────────────────────────────────────────────────┘
|
|
/**
|
|
* The content of the component.
|
|
*/
|
|
children: _propTypes.default /* @typescript-to-proptypes-ignore */.oneOfType([_propTypes.default.node, _propTypes.default.func]),
|
|
/**
|
|
* Class name applied to the root element.
|
|
*/
|
|
className: _propTypes.default.string,
|
|
/**
|
|
* @ignore
|
|
*/
|
|
defaultValue: _propTypes.default.any,
|
|
/**
|
|
* If `true`, the label, input and helper text should be displayed in a disabled state.
|
|
* @default false
|
|
*/
|
|
disabled: _propTypes.default.bool,
|
|
/**
|
|
* If `true`, the label is displayed in an error state.
|
|
* @default false
|
|
*/
|
|
error: _propTypes.default.bool,
|
|
/**
|
|
* Callback fired when the form element's value is modified.
|
|
*/
|
|
onChange: _propTypes.default.func,
|
|
/**
|
|
* If `true`, the label will indicate that the `input` is required.
|
|
* @default false
|
|
*/
|
|
required: _propTypes.default.bool,
|
|
/**
|
|
* The props used for each slot inside the FormControl.
|
|
* @default {}
|
|
*/
|
|
slotProps: _propTypes.default.shape({
|
|
root: _propTypes.default.oneOfType([_propTypes.default.func, _propTypes.default.object])
|
|
}),
|
|
/**
|
|
* The components used for each slot inside the FormControl.
|
|
* Either a string to use a HTML element or a component.
|
|
* @default {}
|
|
*/
|
|
slots: _propTypes.default.shape({
|
|
root: _propTypes.default.elementType
|
|
}),
|
|
/**
|
|
* The value of the form element.
|
|
*/
|
|
value: _propTypes.default.any
|
|
} : void 0; |