@croz/nrich-form-configuration-core
Version:
Contains core utilities related to the nrich-form-configuration module
85 lines (80 loc) • 3.3 kB
TypeScript
import { z } from 'zod';
import React from 'react';
import { F as FormConfigurationConfiguration } from '../form-configuration-types-d813b8f7.js';
interface FormZodConfiguration {
/**
* Registered form id for this form configuration.
* This matches the formId defined on the backend.
*/
formId: string;
/**
* Zod schema generated from backend validation configuration.
* Used for client-side form validation.
*/
zodSchema: z.ZodObject<any>;
}
type ZodFormConfigurationOptions = {
/**
* Array of current state form configurations.
*/
formZodConfigurations: FormZodConfiguration[];
/**
* 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: FormZodConfiguration[]) => void;
/**
* Adds form configuration to state.
* @param formConfiguration formConfiguration to add
*/
add: (formConfiguration: FormZodConfiguration) => void;
/**
* Removes form configuration to state.
* @param formConfiguration formConfiguration to add
*/
remove: (formConfiguration: FormZodConfiguration) => void;
/**
* Sets form configuration loaded to state.
* @param isLoaded loaded flag to set
*/
setFormConfigurationLoaded: (formConfigurationLoaded: boolean) => void;
};
type UseZodFormConfiguration = () => ZodFormConfigurationOptions;
/**
* A hook which simplifies the usage of the form configuration state.
* Uses the internal {@link useZodFormConfigurationStore} 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 useZodFormConfigurationState: UseZodFormConfiguration;
/**
* A hook which extracts a specific Zod configuration from the form configuration identified by the form id.
* Uses the internal {@link useZodFormConfigurationStore} hook for managing the form configuration state.
*
* @param formId Registered form id for a specific form configuration.
*
* @returns Mapped Zod configuration from the form configuration identified by the form id, or undefined if no matching form configuration is found.
*/
declare const useZodFormConfiguration: (formId: string) => z.ZodObject<any, z.core.$strip>;
type ZodProps = {
/**
* 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 FormConfigurationZodProvider: ({ children, loader, ...fetchProps }: ZodProps) => any;
export { FormConfigurationZodProvider, FormZodConfiguration, UseZodFormConfiguration, ZodFormConfigurationOptions, ZodProps, useZodFormConfiguration, useZodFormConfigurationState };