UNPKG

@croz/nrich-form-configuration-core

Version:

Contains core utilities related to the nrich-form-configuration module

87 lines (82 loc) 3.58 kB
import { F as FormConfigurationConfiguration } from './form-configuration-types-d813b8f7.js'; export { b as ConstrainedPropertyClientValidatorConfiguration, C as ConstrainedPropertyConfiguration, a as FormConfiguration, V as ValidatorConverter } from './form-configuration-types-d813b8f7.js'; import * as yup from 'yup'; import { ObjectSchema } from 'yup'; import React from 'react'; interface FormYupConfiguration<T = any> { /** * Registered form id for this form configuration. * This matches the formId defined on the backend. */ formId: string; /** * Yup schema generated from backend validation configuration. * Used for client-side form validation. */ yupSchema: yup.ObjectSchema<T>; } type FormConfigurationOptions = { /** * Array of current state form configurations. */ formYupConfigurations: FormYupConfiguration[]; /** * Flag that indicates whether form configuration is fetched from API. */ formConfigurationLoaded: boolean; /** * Sets form configurations to state. * Use on initial call to find-all endpoint. * @param formConfigurations formConfigurations to set */ set: (formConfigurations: FormYupConfiguration[]) => void; /** * Adds form configuration to state. * @param formConfiguration formConfiguration to add */ add: (formConfiguration: FormYupConfiguration) => void; /** * Removes form configuration to state. * @param formConfiguration formConfiguration to add */ remove: (formConfiguration: FormYupConfiguration) => void; /** * Sets form configuration loaded to state. * @param isLoaded loaded flag to set */ setFormConfigurationLoaded: (formConfigurationLoaded: boolean) => void; }; type UseFormConfiguration = () => FormConfigurationOptions; /** * A hook which simplifies the usage of the form configuration state. * Uses the internal {@link useYupFormConfigurationStore} hook for managing the form configuration state. * * @returns An array of options to access and set the form configuration state and remove or add a single form configuration. */ declare const useFormConfiguration: UseFormConfiguration; /** * A hook which extracts a specific Yup configuration from the form configuration identified by the form id. * Uses the internal {@link useYupFormConfigurationStore} hook for managing the form configuration state. * * @param formId Registered form id for a specific form configuration. * * @returns Mapped Yup configuration from the form configuration identified by the form id, or undefined if no matching form configuration is found. */ declare const useYupFormConfiguration: <T extends Record<string, any> = any>(formId: string) => ObjectSchema<T, yup.AnyObject, any, "">; type Props = { /** * Content to show conditionally */ children?: React.ReactNode; /** * Custom loader to show until content loads */ loader?: React.ReactNode; } & FormConfigurationConfiguration; /** * Should be used to wrap the whole app that includes forms, so it doesn't render them without loading form configuration from API first. * @param children content to show conditionally * @param loader custom loader to show until content loads */ declare const FormConfigurationProvider: ({ children, loader, ...fetchProps }: Props) => any; export { FormConfigurationConfiguration, FormConfigurationOptions, FormConfigurationProvider, FormYupConfiguration, Props, UseFormConfiguration, useFormConfiguration, useYupFormConfiguration };