import Bind from "@web-atoms/core/dist/core/Bind"; import { StringHelper } from "@web-atoms/core/dist/core/StringHelper"; import XNode from "@web-atoms/core/dist/core/XNode"; import "../styles/form.global.css"; const checkClick = (e: MouseEvent) => { const form = e.currentTarget as HTMLDivElement; const element = e.target as HTMLElement; // find button... let target = element; while (target.tagName !== "BUTTON") { target = target.parentElement; if (!target || target === form) { return; } } const submitEvent = target.getAttribute("data-submit-event"); if (!submitEvent) { if (!/submit/i.test(target.getAttribute("type") ?? target.getAttribute("data-type") )) { return; } } form.setAttribute("data-show-validation", "true"); setTimeout(() => { const all = Array.from(form.getElementsByClassName("field-error")); for (const iterator of all) { if (iterator.textContent) { form.setAttribute("data-valid", "false"); return; } } form.setAttribute("data-valid", "true"); const eventName = StringHelper.fromHyphenToCamel( submitEvent ?? form.getAttribute("data-submit-event")); target.dispatchEvent(new CustomEvent(eventName, { bubbles: true, cancelable: true })); }, 100); }; export interface IForm { /** * The event that will be dispatched, default is `submitForm` */ "data-submit-event"?: string; /** * Validation will be displayed if set to true, default is false. * It will be set to true if button with type=submit or data-type=submit will be clicked. */ "data-show-validation"?: "true" | "false" /** * Default error message to be displayed if any field contains error. */ "data-error-message"?: string; /** * Please change your form style to catch event-submit-form */ eventSubmit?: never; /** * Please change your form style to catch event-submit-form */ submitCommand?: never; [key: string]: any; } export default function Form({ "data-submit-event": submitEvent = "submitForm", "data-show-validation": showValidation = "false", "data-error-message": errorMessage = "Please fix all validations", ... a }: IForm, ... nodes: XNode[]) { return