@anushase/json-form-builder
Version:
A dynamic JSON form builder with multi-language support, validation, and responsive design
123 lines (122 loc) • 7.46 kB
TypeScript
import { FormField, FormState, Label } from "../types";
type LabelObject = Record<string, string>;
/**
* Helps to get the label text for a form field, including a required indicator if the field is marked as required.
* @param {FormState} state form state containing current language and default language
* @param {FormField | null} field form field object containing label and required properties
* @param {LabelObject} additionalLabel Optional additional label object to use instead of the field's label.
* @returns {string} The label text for the field, including a required indicator if applicable.
*/
declare const getLabelText: (state: FormState, field: FormField | null, additionalLabel?: LabelObject) => string;
/**
* Gets the label text for a form field, including a required indicator if the field is marked as required.
* @param {FormState} state Current form state containing schema, container, and other properties.
* @param {LabelObject | undefined} labels Labels object containing multilingual labels for the field.
* @param {boolean} strictOnly Boolean flag to determine if strict fallback is required.
* @param {string} currLang Current language code to use for fetching the label.
* @param {string} defaultLang Default language code to use if no label is found in the current language.
* @returns {string} The label text for the field, including a required indicator if applicable.
*/
declare const getMultiLangText: (state: FormState, labels: LabelObject | undefined, strictOnly?: boolean, currLang?: string, defaultLang?: string) => string;
/**
* Appends an error message to the specified container.
* It creates an error icon and a text node, and appends them to the container.
* @param {HTMLDivElement} container Container element where the error message will be appended.
* @param {Label|string} message Message to display in the error container, can be a string or a multilingual label object.
* @param {FormState} state Current form state containing schema, container, and other properties.
*/
declare const appendError: (container: HTMLDivElement, message: string | Label, // Label = { [langCode: string]: string }
state?: FormState) => void;
/**
* Handles the required validation for a form field.
* @param {FormState} state Current form state containing schema, container, and other properties.
* @param {string} normalizedLang Current language code normalized to a 3-letter code.
* @param {string} normalizedDefault Default language code normalized to a 3-letter code.
* @param {HTMLDivElement} errorContainer Error container element where error messages will be appended.
* @returns { lastError: 'required'; isValid: false }
*/
declare const handleRequiredValidation: (state: FormState, errorContainer: HTMLDivElement, normalizedLang?: string, normalizedDefault?: string) => {
lastError: "required";
isValid: false;
};
/**
* Handles regex validation for a form field.
* @param {FormState} state Current form state containing schema, container, and other properties.
* @param {HTMLDivElement} errorContainer Error container element where error messages will be appended.
* @param {any[]} validators Validators array containing regex or validator functions.
* @param {string} value Value to validate against the regex.
* @param {boolean} useLangCode Language code usage flag to filter validators based on language.
* @param {string} currentLang Current language code normalized to a 3-letter code.
* @param {string} defaultLang Default language code normalized to a 3-letter code.
* @returns { lastError: number | null; isValid: boolean }
*/
declare const handleRegexValidation: (state: FormState, errorContainer: HTMLDivElement, validators: any[], value: string, useLangCode: boolean, currentLang?: string, defaultLang?: string) => {
lastError: number;
isValid: boolean;
} | {
lastError: null;
isValid: boolean;
};
/**
* Creates an SVG element representing an info icon.
* @param {number | string} size size of the info icon in px
* @returns {SVGSVGElement} returns an SVG element with the info icon.
*/
declare const createInfoIconSvg: (size?: number | string) => SVGSVGElement;
/**
* Create Info icon for a form field
* @param {string} infoMessage The message to display in the info icon tooltip.
* @returns {HTMLSpanElement} Returns a span element containing the info icon and tooltip.
*/
declare const createInfoIcon: (infoMessage: string) => HTMLSpanElement;
/**
* Get caps lock span
* @param {FormState} state Current form state containing schema, container, and other properties.
* @param {FormField} field form field object containing label and required properties
* @returns {HTMLSpanElement} returns a span element for caps lock info
*/
declare const getCapsLockSpan: (state: FormState, field: FormField) => HTMLSpanElement;
/**
* Toggle caps lock info on click of caps lock button
* @param {KeyboardEvent | MouseEvent} event event from click or keyup
* @param {HTMLSpanElement} capsLockSpan span element of the caps lock span
*/
declare const checkCapsLock: (event: KeyboardEvent | MouseEvent, capsLockSpan: HTMLSpanElement) => void;
/**
* Enables caps lock check for a form field.
* @param {FormField} field Form field object containing type, id, label, required, and other properties.
* @param {HTMLDivElement} wrapper Wrapper HTMLDivElement that contains the form field.
* @param {HTMLInputElement} input Input HTMLInputElement that will have the caps lock check enabled.
*/
declare const enableCapsLockCheck: (field: FormField, wrapper: HTMLDivElement, input: HTMLInputElement) => void;
/**
* Prevents the default action of an event.
* @param {Event} e Event to prevent default action for.
*/
declare const preventDefaultFn: (e: Event) => void;
/**
* Disables a form field by preventing user input and interaction.
* @param {HTMLInputElement | HTMLSelectElement} field HTMLInputElement or HTMLSelectElement to disable.
*/
declare const disableField: (field: HTMLInputElement | HTMLSelectElement) => void;
/**
* Creates a new div element to be used as an error container.
* @returns {HTMLDivElement} A new div element to be used as an error container.
*/
declare const createErrorContainer: () => HTMLDivElement;
/**
* Converts a one-way language map into a two-way map.
* This allows for bidirectional lookup where both keys and values are language codes.
* @param {Record<string, string>}oneWayMap A map where keys are language codes and values are their corresponding labels.
* @returns Two-way map where both keys and values are language codes, allowing for bidirectional lookup.
*/
declare function buildBidirectionalLanguageMap(oneWayMap: Record<string, string>): Record<string, string>;
declare const dataUrlToBlob: (dataUrl: string) => Blob;
/**
* Validate Form
* @param {FormState} state Current form state containing schema, container, and other properties.
* @returns {boolean} Returns true if the form is valid, false otherwise.
*/
declare const validateForm: (state: FormState) => boolean;
declare const emptyInvalidFn: (input: HTMLInputElement | HTMLSelectElement) => (() => void);
export { getLabelText, getMultiLangText, appendError, handleRequiredValidation, handleRegexValidation, createInfoIconSvg, createInfoIcon, getCapsLockSpan, checkCapsLock, disableField, preventDefaultFn, createErrorContainer, buildBidirectionalLanguageMap, enableCapsLockCheck, dataUrlToBlob, validateForm, emptyInvalidFn, };