UNPKG

@canard/schema-form

Version:

React-based component library that renders forms based on JSON Schema with plugin system support for validators and UI components

73 lines (72 loc) 3.39 kB
import { type ReactNode } from 'react'; import { type SchemaNode } from '../core'; import type { JsonSchemaError } from '../types'; /** * Manages error states of child nodes in a branch schema node. * * This hook subscribes to error updates from all child nodes and provides both * raw error data and formatted error messages. It automatically handles: * - Real-time error state synchronization with child nodes * - Dynamic updates when children are added/removed * - Error message formatting using the current form renderer context * - Cleanup of subscriptions when the component unmounts * * The hook is designed for branch nodes (containers) that need to display * validation errors from their child fields in aggregate or individually. * * @example * ```typescript * // Basic usage in a form container component * const FormTypeObjectInput = ({ node }: FormTypeInputProps) => { * const { errorMessage,showError, formattedError,showErrors, formattedErrors, errorMatrix } = useChildNodeErrors(node); * * return ( * <div> * {node.children?.map((child, index) => ( * <div key={child.key}> * <ChildComponent node={child.node} /> * {showErrors[index] && formattedErrors[index] && ( * <span className="error">{formattedErrors[index]}</span> * )} * </div> * ))} * {showError && formattedError && ( * <div className="summary-error">{formattedError}</div> * )} * {errorMessage && <div className="summary-error">{errorMessage}</div>} * </div> * ); * }; * * // Usage with disabled state * const ConditionalFormSection = ({ node, isDisabled }) => { * const { errorMessages } = useChildNodeErrors(node, isDisabled); * // When disabled=true, errorMessages will be empty array * }; * ``` * * @param node - The branch SchemaNode instance that contains child nodes. * Must be a branch node (not a leaf node). * @param disabled - Optional flag to disable error tracking. When true, * all error states are cleared and no new subscriptions are made. * * @returns Object containing comprehensive error information: * @returns {ReactNode} errorMessage - The first non-null and visible error message from formattedErrors. * Useful for displaying a single summary error. * @returns {boolean} showError - Whether any error should be shown. * @returns {boolean[]} showErrors - Array of booleans indicating if each child should show an error. * @returns {ReactNode} formattedError - The first non-null error message from formattedErrors. * Useful for displaying a single summary error. * @returns {ReactNode[]} formattedErrors - Array of formatted error messages for each child, * indexed by child position. Null entries indicate no error. * @returns {JsonSchemaError[][]} errorMatrix - 2D array of raw error objects for each child. * Useful for custom error processing or debugging. */ export declare const useChildNodeErrors: (node: SchemaNode, disabled?: boolean) => { errorMessage: ReactNode; showError: boolean; formattedError: ReactNode; showErrors: boolean[]; formattedErrors: ReactNode[]; errorMatrix: JsonSchemaError[][]; };