UNPKG

@base-ui-components/react

Version:

Base UI is a library of headless ('unstyled') React components and low-level hooks. You gain complete control over your app's CSS and accessibility features.

165 lines (164 loc) 6.71 kB
"use strict"; 'use client'; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.FieldRoot = void 0; var React = _interopRequireWildcard(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _useComponentRenderer = require("../../utils/useComponentRenderer"); var _FieldRootContext = require("./FieldRootContext"); var _constants = require("../utils/constants"); var _FieldsetRootContext = require("../../fieldset/root/FieldsetRootContext"); var _useEventCallback = require("../../utils/useEventCallback"); var _FormContext = require("../../form/FormContext"); var _jsxRuntime = require("react/jsx-runtime"); 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 && {}.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; } /** * Groups all parts of the field. * Renders a `<div>` element. * * Documentation: [Base UI Field](https://base-ui.com/react/components/field) */ const FieldRoot = exports.FieldRoot = /*#__PURE__*/React.forwardRef(function FieldRoot(props, forwardedRef) { const { render, className, validate: validateProp, validationDebounceTime = 0, validationMode = 'onBlur', name, disabled: disabledProp = false, invalid: invalidProp, ...otherProps } = props; const { disabled: disabledFieldset } = (0, _FieldsetRootContext.useFieldsetRootContext)(); const { errors } = (0, _FormContext.useFormContext)(); const validate = (0, _useEventCallback.useEventCallback)(validateProp || (() => null)); const disabled = disabledFieldset || disabledProp; const [controlId, setControlId] = React.useState(undefined); const [labelId, setLabelId] = React.useState(undefined); const [messageIds, setMessageIds] = React.useState([]); const [touched, setTouched] = React.useState(false); const [dirty, setDirtyUnwrapped] = React.useState(false); const markedDirtyRef = React.useRef(false); const setDirty = React.useCallback(value => { if (value) { markedDirtyRef.current = true; } setDirtyUnwrapped(value); }, []); const invalid = Boolean(invalidProp || name && {}.hasOwnProperty.call(errors, name)); const [validityData, setValidityData] = React.useState({ state: _constants.DEFAULT_VALIDITY_STATE, error: '', errors: [], value: null, initialValue: null }); const valid = !invalid && validityData.state.valid; const state = React.useMemo(() => ({ disabled, touched, dirty, valid }), [disabled, touched, dirty, valid]); const contextValue = React.useMemo(() => ({ invalid, controlId, setControlId, labelId, setLabelId, messageIds, setMessageIds, name, validityData, setValidityData, disabled, touched, setTouched, dirty, setDirty, validate, validationMode, validationDebounceTime, state, markedDirtyRef }), [invalid, controlId, labelId, messageIds, name, validityData, disabled, touched, dirty, setDirty, validate, validationMode, validationDebounceTime, state]); const { renderElement } = (0, _useComponentRenderer.useComponentRenderer)({ render: render ?? 'div', ref: forwardedRef, className, state, extraProps: otherProps, customStyleHookMapping: _constants.STYLE_HOOK_MAPPING }); return /*#__PURE__*/(0, _jsxRuntime.jsx)(_FieldRootContext.FieldRootContext.Provider, { value: contextValue, children: renderElement() }); }); process.env.NODE_ENV !== "production" ? FieldRoot.propTypes /* remove-proptypes */ = { // ┌────────────────────────────── Warning ──────────────────────────────┐ // │ These PropTypes are generated from the TypeScript type definitions. │ // │ To update them, edit the TypeScript types and run `pnpm proptypes`. │ // └─────────────────────────────────────────────────────────────────────┘ /** * @ignore */ children: _propTypes.default.node, /** * CSS class applied to the element, or a function that * returns a class based on the component’s state. */ className: _propTypes.default.oneOfType([_propTypes.default.func, _propTypes.default.string]), /** * Whether the component should ignore user interaction. * Takes precedence over the `disabled` prop on the `<Field.Control>` component. * @default false */ disabled: _propTypes.default.bool, /** * Whether the field is forcefully marked as invalid. */ invalid: _propTypes.default.bool, /** * Identifies the field when a form is submitted. * Takes precedence over the `name` prop on the `<Field.Control>` component. */ name: _propTypes.default.string, /** * Allows you to replace the component’s HTML element * with a different tag, or compose it with another component. * * Accepts a `ReactElement` or a function that returns the element to render. */ render: _propTypes.default.oneOfType([_propTypes.default.element, _propTypes.default.func]), /** * A function for custom validation. Return a string or an array of strings with * the error message(s) if the value is invalid, or `null` if the value is valid. */ validate: _propTypes.default.func, /** * How long to wait between `validate` callbacks if * `validationMode="onChange"` is used. Specified in milliseconds. * @default 0 */ validationDebounceTime: _propTypes.default.number, /** * Determines when the field should be validated. * * - **onBlur** triggers validation when the control loses focus * - **onChange** triggers validation on every change to the control value * @default 'onBlur' */ validationMode: _propTypes.default.oneOf(['onBlur', 'onChange']) } : void 0;