UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

93 lines (92 loc) 5.42 kB
import React from 'react'; import type { ValidateFunction } from 'ajv/dist/2020.js'; import { FormError } from '../utils'; import type { FieldPropsGeneric, SubmitState, FieldStatus, ErrorProp, MessageProp, MessageTypes, MessagePropParams, Identifier, DefaultErrorMessages } from '../types'; import type { GetValueByPath } from './useDataValue'; import type { FormsTranslation } from './useTranslation'; import type { FieldBlockContextProps } from '../FieldBlock/FieldBlockContext'; export declare function checkForError(potentialErrors: Array<unknown>): boolean; export type SubmitStateWithValidating = SubmitState | 'validating'; export type PersistErrorStateMethod = /** * Add or remove the error regardless */ 'weak' /** * Check if there is an existing error, and if so, keep it */ | 'gracefully' /** * Remove the error, if any */ | 'wipe'; export type ErrorInitiator = 'required' | 'schema' | 'errorProp' | 'onChangeValidator' | 'onBlurValidator' | 'dataContextError'; export type UseFieldErrorParams<Value> = { initialErrorProp: FieldPropsGeneric<Value>['error'] | 'initial'; warningProp: FieldStatus['warning'] | MessageProp<Value, FieldStatus['warning']>; infoProp: FieldStatus['info'] | MessageProp<Value, FieldStatus['info']>; errorMessages: DefaultErrorMessages; validateInitially: boolean; validateContinuously: boolean; disabled: boolean; identifier: Identifier; locale: string; handleFieldAsVisible: boolean | undefined; inFieldBlock: boolean; prerenderFieldProps: boolean; updateContextDataInSync: boolean; hasZodSchema: boolean; setFieldStateDataContext: (identifier: Identifier, state: SubmitState) => void; setFieldStateFieldBlock: FieldBlockContextProps['setFieldState']; setFieldErrorDataContext: (identifier: Identifier, error: Error | FormError | undefined) => void; setFieldErrorBoundary: (identifier: Identifier, error: Error | FormError | undefined) => void; setFieldErrorWizard: (wizardIndex: number, identifier: Identifier, hasError: boolean | undefined) => void; setBlockRecord: FieldBlockContextProps['setBlockRecord']; showFieldErrorFieldBlock: FieldBlockContextProps['showFieldError']; revealErrorDataContext: (identifier: Identifier, hasError: boolean) => void; revealErrorBoundary: (identifier: Identifier, hasError: boolean) => void; wizardIndex: number; dataContextErrors: Record<string, Error | FormError>; contextErrorMessages: Record<string, unknown>; valueRef: React.RefObject<Value>; hasFocusRef: React.RefObject<boolean>; isInternalRerenderRef: React.RefObject<unknown>; schemaValidatorRef: React.RefObject<ValidateFunction | ((value: unknown) => unknown)>; translationRef: React.RefObject<FormsTranslation>; formatMessage: (key: string, values?: Record<string, string>) => string; getFieldByPath: MessagePropParams<Value, unknown>['getFieldByPath']; getValueByPath: GetValueByPath<Value>; forceUpdate: () => void; }; export default function useFieldError<Value>({ initialErrorProp, warningProp, infoProp, errorMessages, validateInitially, validateContinuously, disabled, identifier, locale, handleFieldAsVisible, inFieldBlock, prerenderFieldProps, updateContextDataInSync, hasZodSchema, setFieldStateDataContext, setFieldStateFieldBlock, setFieldErrorDataContext, setFieldErrorBoundary, setFieldErrorWizard, setBlockRecord, showFieldErrorFieldBlock, revealErrorDataContext, revealErrorBoundary, wizardIndex, dataContextErrors, contextErrorMessages, valueRef, hasFocusRef, isInternalRerenderRef, schemaValidatorRef, translationRef, formatMessage, getFieldByPath, getValueByPath, forceUpdate, }: UseFieldErrorParams<Value>): { revealErrorRef: React.RefObject<boolean>; localErrorRef: React.RefObject<Error | FormError>; localErrorInitiatorRef: React.RefObject<ErrorInitiator>; contextErrorRef: React.RefObject<Error | FormError>; errorMethodRef: React.RefObject<Partial<Record<PersistErrorStateMethod, Error | FormError>>>; fieldStateRef: React.RefObject<SubmitStateWithValidating>; warningRef: React.RefObject<import("../types").StatusMessage>; infoRef: React.RefObject<import("../types").StatusMessage>; bufferedErrorRef: React.RefObject<import("../types").StatusError>; error: ErrorProp<unknown>; warning: import("../types").StatusMessage; info: import("../types").StatusMessage; errorProp: ErrorProp<Value>; combinedErrorMessages: any; bufferedError: Error; errorIsVisible: boolean; ensureErrorMessageObject: <T>(error: T) => T; executeMessage: <ReturnValue extends MessageTypes<Value>>(message: MessageProp<Value, ReturnValue>, forceReturnErrorMessageObject?: boolean) => ReturnValue; prepareError: (error: FieldPropsGeneric<Value>["error"]) => FormError | undefined; persistErrorState: (method: PersistErrorStateMethod, initiator: ErrorInitiator, errorArg?: Error | FormError | Array<Error | FormError> | undefined) => any; clearErrorState: () => void; revealError: () => any; hideError: () => void; setErrorState: (hasError: boolean) => void; setFieldState: (state: SubmitStateWithValidating) => void; hasError: () => boolean; handleError: () => void; getBufferedError: () => Error; }; declare function resolveValidatingState(state: SubmitStateWithValidating): SubmitState; export { resolveValidatingState };