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.

100 lines (99 loc) 3.38 kB
'use client'; import * as React from 'react'; import PropTypes from 'prop-types'; import { useComponentRenderer } from '../../utils/useComponentRenderer.js'; import { useFieldRootContext } from '../root/FieldRootContext.js'; import { useFieldError } from './useFieldError.js'; import { STYLE_HOOK_MAPPING } from '../utils/constants.js'; import { useFormContext } from '../../form/FormContext.js'; /** * An error message displayed if the field control fails validation. * Renders a `<div>` element. * * Documentation: [Base UI Field](https://base-ui.com/react/components/field) */ const FieldError = /*#__PURE__*/React.forwardRef(function FieldError(props, forwardedRef) { const { render, id, className, match, forceShow, ...otherProps } = props; const { validityData, state, name } = useFieldRootContext(false); const { errors } = useFormContext(); const formError = name ? errors[name] : null; let rendered = false; if (formError || forceShow) { rendered = true; } else if (match) { rendered = Boolean(validityData.state[match]); } else if (forceShow == null) { rendered = validityData.state.valid === false; } const { getErrorProps } = useFieldError({ id, rendered, formError }); const { renderElement } = useComponentRenderer({ propGetter: getErrorProps, render: render ?? 'div', ref: forwardedRef, className, state, extraProps: otherProps, customStyleHookMapping: STYLE_HOOK_MAPPING }); if (!rendered) { return null; } return renderElement(); }); process.env.NODE_ENV !== "production" ? FieldError.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.node, /** * CSS class applied to the element, or a function that * returns a class based on the component’s state. */ className: PropTypes.oneOfType([PropTypes.func, PropTypes.string]), /** * Whether the error message should be shown regardless of the field’s validity. */ forceShow: PropTypes.bool, /** * @ignore */ id: PropTypes.string, /** * Determines whether to show the error message according to the field’s * [ValidityState](https://developer.mozilla.org/en-US/docs/Web/API/ValidityState). */ match: PropTypes.oneOf(['badInput', 'customError', 'patternMismatch', 'rangeOverflow', 'rangeUnderflow', 'stepMismatch', 'tooLong', 'tooShort', 'typeMismatch', 'valid', 'valueMissing']), /** * 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.oneOfType([PropTypes.element, PropTypes.func]) } : void 0; export { FieldError };