@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.
59 lines (58 loc) • 1.69 kB
JavaScript
'use client';
import _formatErrorMessage from "@base-ui-components/utils/formatErrorMessage";
import * as React from 'react';
import { NOOP } from "../../utils/noop.js";
import { DEFAULT_VALIDITY_STATE } from "../utils/constants.js";
import { EMPTY_OBJECT } from "../../utils/constants.js";
export const FieldRootContext = /*#__PURE__*/React.createContext({
invalid: undefined,
name: undefined,
validityData: {
state: DEFAULT_VALIDITY_STATE,
errors: [],
error: '',
value: '',
initialValue: null
},
setValidityData: NOOP,
disabled: undefined,
touched: false,
setTouched: NOOP,
dirty: false,
setDirty: NOOP,
filled: false,
setFilled: NOOP,
focused: false,
setFocused: 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 = EMPTY_OBJECT) => props,
getInputValidationProps: (props = EMPTY_OBJECT) => props,
inputRef: {
current: null
},
commit: async () => {}
}
});
if (process.env.NODE_ENV !== "production") FieldRootContext.displayName = "FieldRootContext";
export function useFieldRootContext(optional = true) {
const context = React.useContext(FieldRootContext);
if (context.setValidityData === NOOP && !optional) {
throw new Error(process.env.NODE_ENV !== "production" ? 'Base UI: FieldRootContext is missing. Field parts must be placed within <Field.Root>.' : _formatErrorMessage(28));
}
return context;
}