@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.
67 lines (66 loc) • 2.17 kB
JavaScript
;
'use client';
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.FieldRootContext = void 0;
exports.useFieldRootContext = useFieldRootContext;
var _formatErrorMessage2 = _interopRequireDefault(require("@base-ui-components/utils/formatErrorMessage"));
var React = _interopRequireWildcard(require("react"));
var _noop = require("../../utils/noop");
var _constants = require("../utils/constants");
var _constants2 = require("../../utils/constants");
const FieldRootContext = exports.FieldRootContext = /*#__PURE__*/React.createContext({
invalid: undefined,
name: undefined,
validityData: {
state: _constants.DEFAULT_VALIDITY_STATE,
errors: [],
error: '',
value: '',
initialValue: null
},
setValidityData: _noop.NOOP,
disabled: undefined,
touched: false,
setTouched: _noop.NOOP,
dirty: false,
setDirty: _noop.NOOP,
filled: false,
setFilled: _noop.NOOP,
focused: false,
setFocused: _noop.NOOP,
validate: () => null,
validationMode: 'onSubmit',
validationDebounceTime: 0,
shouldValidateOnChange: () => false,
state: {
disabled: false,
valid: null,
touched: false,
dirty: false,
filled: false,
focused: false
},
markedDirtyRef: {
current: false
},
validation: {
getValidationProps: (props = _constants2.EMPTY_OBJECT) => props,
getInputValidationProps: (props = _constants2.EMPTY_OBJECT) => props,
inputRef: {
current: null
},
commit: async () => {}
}
});
if (process.env.NODE_ENV !== "production") FieldRootContext.displayName = "FieldRootContext";
function useFieldRootContext(optional = true) {
const context = React.useContext(FieldRootContext);
if (context.setValidityData === _noop.NOOP && !optional) {
throw new Error(process.env.NODE_ENV !== "production" ? 'Base UI: FieldRootContext is missing. Field parts must be placed within <Field.Root>.' : (0, _formatErrorMessage2.default)(28));
}
return context;
}