UNPKG

vanilajs-form-lib

Version:

Reusable vanilla JS form library that works with React, Angular, Ionic, etc.

205 lines (191 loc) 8.22 kB
// TypeScript definitions for unvired-forms-lib export interface FormioLibPath { formioPath: string; lessFilesPath: string; } export interface LoadUnviredFormsOptions { nestedFormData?: any[]; masterData?: any[]; title?: string; description?: string; mode?: "pdf" | "readOnly" | "form" | "render" | "print" | string; privateExternal?: string | boolean; permission?: "writemultiple" | "writesingle" | "read" | string; platform?: "android" | "web" | "ios" | string; language?: string; translations?: any; environmentVariable?: any[]; themeData?: any; userData?: any; appData?: any; controlData?: any; showBackButton?: boolean; showMoreButton?: boolean; formioLibPath?: string | FormioLibPath; } // Event Types export const FORM_EVENTS: { FORM_RENDER: "FORM_RENDER"; SAVE: "FORM_SAVE"; SUBMIT: "FORM_SUBMIT"; SUBMIT_ERROR: "FORM_SUBMIT_ERROR"; BACK_NAVIGATION: "FORM_BACK_NAVIGATION"; ONCHANGE: "FORM_ONCHANGE"; ACTION: "ACTION"; GET_ATTACHMENT: "GET_ATTACHMENT"; }; // Event Structures export interface SuccessEvent { type: "FORM_RENDER" | "FORM_SAVE" | "FORM_SUBMIT" | "FORM_BACK_NAVIGATION" | "FORM_ONCHANGE" | "ACTION" | "OPEN_CAMERA" | "BARCODE_SCAN" | "LOCATION_REQUEST" | "GET_ATTACHMENT"; message: string; data: { formData?: any; templateData?: any; completeFlag?: boolean; completionPercentage?: number; result?: any; controlId?: string; scannedValue?: string; location?: string; coordinates?: { lat: number; lng: number }; fileName?: string; missingFiles?: Array<{fileId: string, field: string}>; [key: string]: any; }; } export interface ErrorEvent { type: "ERROR"; errorMessage: string; data: { technicalError?: any; reason?: string; error?: any; formData?: any; completeFlag?: boolean; completionPercentage?: number; [key: string]: any; }; } export type FormEvent = SuccessEvent | ErrorEvent; // Error Codes Reference export const ERROR_CODES: { "001": "Invalid formsData JSON"; "002": "Nested form validation error"; "003": "Master data validation error"; "004": "User Data not Provided"; "005": "Form render error"; "006": "Form submit error"; "007": "Form complete error"; "008": "Location permission denied"; "009": "Location unavailable"; "010": "Location request timeout"; "011": "Location unknown error"; "012": "Camera access error"; "013": "Attachments missing from storage"; "014": "Failed to validate attachments"; "015": "File not found in IndexedDB during processing"; }; // Event Callback Function export type EventCallback = (event: FormEvent) => void; // Main Function Interface export interface LoadUnviredFormsParams { formsData: string | any; submissionData?: any; eventCallback?: EventCallback; container?: HTMLElement; options?: LoadUnviredFormsOptions; } export interface AttachmentInfo { id: string; name: string; originalName: string; size: number; type: string; url: string; } export interface FormInstance { sendAction: (action: any) => void; getAttachments?: () => Promise<AttachmentInfo[]>; clearAttachments?: () => Promise<boolean>; validateAttachments?: (formData: any) => void; deleteAttachment?: (fileId: string) => Promise<boolean>; } export function loadUnviredForms(params: LoadUnviredFormsParams): FormInstance; // Events that will be triggered: /** * SUCCESS EVENTS: * * 1. FORM_RENDER - Form rendered successfully * { type: "FORM_RENDER", message: "Success: Form rendered successfully.", data: {} } * * 2. FORM_ONCHANGE - Form data changed * { type: "FORM_ONCHANGE", message: "Success: Form onchange triggered.", data: { formData: submission, templateData: template } } * * 3. FORM_SUBMIT - Form submitted successfully * { type: "FORM_SUBMIT", message: "Success: Form submitted successfully.", data: { formData: currentData, completeFlag, completionPercentage, result } } * * 4. FORM_SAVE - Form data saved * { type: "FORM_SAVE", message: "Success: Data saved successfully.", data: { formData: updatedData, completeFlag, completionPercentage } } * * 5. FORM_BACK_NAVIGATION - Back navigation triggered * { type: "FORM_BACK_NAVIGATION", message: "Back navigation triggered.", data: {} } * * 6. OPEN_CAMERA - Photo captured successfully * { type: "OPEN_CAMERA", message: "Photo captured successfully", data: { controlId: "id", fileName: "photo.jpg" } } * * 7. BARCODE_SCAN - Barcode scanned successfully * { type: "BARCODE_SCAN", message: "Barcode scanned successfully", data: { controlId: "id", scannedValue: "value" } } * * 8. LOCATION_REQUEST - Location retrieved successfully * { type: "LOCATION_REQUEST", message: "Location retrieved successfully", data: { controlId: "id", location: "lat,lng", coordinates: { lat: 0, lng: 0 } } } * * 9. GET_ATTACHMENT - Request missing attachments from middleware * { type: "GET_ATTACHMENT", message: "Requesting missing attachments from middleware", data: { missingFiles: [{fileId: "id", field: "fieldName"}] } } * * ERROR EVENTS: * * 1. ERROR Code 001 - Invalid formsData JSON * { type: "ERROR", errorMessage: "Error Code 001 contact your admin", data: { error: e } } * * 2. ERROR Code 002 - Nested form validation error * { type: "ERROR", errorMessage: "Error Code 002 contact your admin", data: { technicalError: nestedFormError } } * * 3. ERROR Code 003 - Master data validation error * { type: "ERROR", errorMessage: "Error Code 003 contact your admin", data: { technicalError: masterdataError } } * * 4. ERROR Code 004 - User Data not Provided * { type: "ERROR", errorMessage: "Error Code 004 contact your admin", data: { reason: "User Data not Provided" } } * * 5. ERROR Code 005 - Form render error * { type: "ERROR", errorMessage: "Error Code 005 contact your admin", data: { technicalError: error } } * * 6. ERROR Code 006 - Form submit error * { type: "ERROR", errorMessage: "Error Code 006 contact your admin", data: { formData: currentData, completeFlag, completionPercentage, technicalError: error } } * * 7. ERROR Code 007 - Form complete error * { type: "ERROR", errorMessage: "Error Code 007 contact your admin", data: { formData: currentData, completeFlag, completionPercentage, technicalError: error } } * * 8. ERROR Code 008 - Location permission denied * { type: "ERROR", errorMessage: "Error Code 008 contact your admin", data: { technicalError: error, reason: "Location permission denied" } } * * 9. ERROR Code 009 - Location unavailable * { type: "ERROR", errorMessage: "Error Code 009 contact your admin", data: { technicalError: error, reason: "Location unavailable" } } * * 10. ERROR Code 010 - Location request timeout * { type: "ERROR", errorMessage: "Error Code 010 contact your admin", data: { technicalError: error, reason: "Location timeout" } } * * 11. ERROR Code 011 - Location unknown error * { type: "ERROR", errorMessage: "Error Code 011 contact your admin", data: { technicalError: error, reason: "Location unknown error" } } * * 12. ERROR Code 012 - Camera access error * { type: "ERROR", errorMessage: "Error Code 012 contact your admin", data: { technicalError: error, reason: "Camera access error" } } * * 13. ERROR Code 013 - Attachments missing from storage * { type: "ERROR", errorMessage: "ErrorCode : 013, contact your admin or tech team", data: { reason: "Some attachments are missing from storage", missingFiles: [] } } * * 14. ERROR Code 014 - Failed to validate attachments * { type: "ERROR", errorMessage: "ErrorCode : 014, contact your admin or tech team", data: { reason: "Failed to validate attachments", technicalError: error } } * * 15. ERROR Code 015 - File not found in IndexedDB during processing * { type: "ERROR", errorMessage: "ErrorCode : 015, contact your admin or tech team", data: { reason: "File not found in IndexedDB: fileId", fileId: "id", field: "fieldName" } } */