UNPKG

@ariakit/react-core

Version:

Ariakit React core

81 lines (79 loc) 1.95 kB
"use client"; import { useFormContext } from "../__chunks/AOHIIJHJ.js"; import { useCollectionItem } from "../__chunks/4WQSNMEM.js"; import { useStoreState } from "../__chunks/SOQQIDO4.js"; import "../__chunks/CTVD4XJH.js"; import { createElement, createHook, forwardRef, memo } from "../__chunks/L4OUMOCQ.js"; import { useId, useMergeRefs } from "../__chunks/W2TDKEPX.js"; import "../__chunks/AZVQSWGA.js"; // src/form/form-error.tsx import { invariant } from "@ariakit/core/utils/misc"; import { useCallback, useRef } from "react"; var TagName = "div"; var useFormError = createHook( function useFormError2({ store, name: nameProp, getItem: getItemProp, ...props }) { const context = useFormContext(); store = store || context; invariant( store, process.env.NODE_ENV !== "production" && "FormError must be wrapped in a Form component." ); const id = useId(props.id); const ref = useRef(null); const name = String(nameProp); const getItem = useCallback( (item) => { const nextItem = { ...item, id: id || item.id, name, type: "error" }; if (getItemProp) { return getItemProp(nextItem); } return nextItem; }, [id, name, getItemProp] ); const children = useStoreState(store, () => { const error = store == null ? void 0 : store.getError(name); if (error == null) return; if (!(store == null ? void 0 : store.getFieldTouched(name))) return; return error; }); props = { role: "alert", children, ...props, id, ref: useMergeRefs(ref, props.ref) }; props = useCollectionItem({ store, ...props, getItem }); return props; } ); var FormError = memo( forwardRef(function FormError2(props) { const htmlProps = useFormError(props); return createElement(TagName, htmlProps); }) ); export { FormError, useFormError };