import { ValidatableInput } from "./validatable-input"; /** * Custom element that displays inline constraint-validation feedback. * Applies `.valid` / `.invalid` classes to each input and scrolls the * first invalid input into view when the user tries to submit. * * Usage: wrap a `
` with ``. * * Each input's error text is rendered inside the element referenced by * that input's `aria-errormessage` attribute (customisable via * `message-target-attr`). * * Per-input custom messages are supported via `validation-[validityKey]` * or `data-validation-[validityKey]` attributes, for example * `validation-valueMissing="Please fill this out"`. The prefix defaults * to `"validation"` and can be changed with the `message-prefix` attribute. * * See the constraint-validation API for supported validity keys and elements: * https://developer.mozilla.org/en-US/docs/Learn_web_development/Extensions/Forms/Form_validation#the_constraint_validation_api * * Note: when changing a input's value in code, dispatch a bubbling change * event so this component picks it up: * `input.dispatchEvent(new Event('change', { bubbles: true }))` */ export declare class ValidationEnhancer extends HTMLElement { get validClass(): string; get invalidClass(): string; get messageTargetAttr(): string; get messagePrefix(): string; get messageAriaLive(): string; get scrollIntoViewOptions(): ScrollIntoViewOptions | boolean; mutationObserver: MutationObserver | null; constructor(); connectedCallback(): void; disconnectedCallback(): void; /** * On mutation, add novalidate to forms and aria-live to validation message target elements * @param mutations Array of MutationRecord from the MutationObserver */ handleMutation(mutations: MutationRecord[]): void; /** * Add novalidate to a list of forms, disabling browser validation * @param forms Array of form elements to set novalidate on */ setupForms(forms: HTMLFormElement[]): void; /** * Add aria-live attribute to an element if it's referenced by an input's message target attribute * @param element The potential message target element to add aria-live to */ ensureAriaLive(element: Element): void; /** * Add aria-live attribute to message target elements for inputs that lack it * @param elements Array of input elements to check for message targets */ setupValidationTargetElements(elements: Element[]): void; /** * Run polite validation for the targeted input whenever the user keys up * @param event The keyup event from the input */ handleKeyUp(event: Event): void; /** * Run validation for the targeted input whenever its value changes or it loses focus * @param event The change or focusout event from the input */ handleChangeAndFocusOut(event: Event): void; /** * Validate the whole form, and prevent submission if it's invalid * @param event The submit event from the form */ handleSubmit(event: SubmitEvent): void; /** * Check a single input's validity. Toggles `.valid` / `.invalid` * classes and updates the associated message element. * @param input The input element to validate * @returns boolean, whether the input has passed validation */ validateInput(input: HTMLElement): boolean; /** * Validates an input, and may apply a valid class, but never applies the invalid class. * This is for use while the user is typing - it avoids that "nagging" sensation of the * validation turning red all the time. * @param input The input element to validate * @returns boolean, whether the input has passed validation */ validateInputPolitely(input: HTMLElement): boolean; /** * Walk every input owned by the form, validate it, and scroll the * first invalid one into view. Inputs without an `aria-errormessage` * target fall back to the browser's native reporting. * @param form The form element to validate * @returns boolean, whether all inputs have passed validation */ validateAllInputs(form: HTMLFormElement): boolean; /** * Checks whether an element participates in validation * @param el The element to check * @returns boolean, whether the element has the validation API */ isValidatableInput(el: Element | null): el is ValidatableInput; /** * Set classes and attributes on one input to valid. * Hides any validation message. * @param input The input element to mark as valid */ makeInputValid(input: ValidatableInput): void; /** * Set classes and attributes on one input to invalid. * Shows validation message. * @param input The input element to mark as invalid */ makeInputInvalid(input: ValidatableInput): void; /** * Removes visual validity from an input, but leaves invalidity and aria validity * @param input The input element to remove valid class from */ removeInputValidity(input: ValidatableInput): void; /** * Write the appropriate error string into the input's companion * message element. Prefers a custom message attribute when one exists. * @param input The input element to show validation message for */ showMessage(input: ValidatableInput): void; /** * Empties the companion message element for a given input. * @param input The input element to clear validation message for */ clearMessage(input: ValidatableInput): void; /** * Resolve the element pointed to by the input's message target * attribute (defaults to `aria-errormessage`, customisable via * `message-target-attr`), or `null` if the attribute is missing * / the target doesn't exist. * @param input The input element to find the message target for * @returns The message target element, or null if not found */ getValidationMessageTargetElement(input: Element): Element | null; /** * Validity keys checked in spec order to locate a matching * override attribute on the input. For each key, looks up * `{prefix}-{key}` then `data-{prefix}-{key}`, where prefix * defaults to `"validation"` (customisable via `message-prefix`). * * https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#the-constraint-validation-api */ validityKeys: readonly ["valueMissing", "typeMismatch", "patternMismatch", "tooLong", "tooShort", "rangeUnderflow", "rangeOverflow", "stepMismatch", "badInput"]; /** * Look up a developer-supplied message for the first failing * validity key. Returns `null` when no override is present or * the input is completely valid. * @param input The input element to get custom message for * @returns The custom validation message, or null if not found */ getCustomMessage(input: ValidatableInput): string | null; /** * Gets all validatable inputs on the form * @param form The form element to get inputs from * @returns Array of validatable input elements */ getValidatableInputs(form: HTMLFormElement): ValidatableInput[]; /** * Scrolls an element into view, respecting the configured scroll options. * When `scroll-container` is set, scrolls that container instead of * using the element's own scrollIntoView. * @param element The element to scroll into view */ scrollToElement(element: Element): void; /** * Gets an input's label element * @param input The input element to find the label for * @returns The label element, or null if not found */ getLabel(input: ValidatableInput): HTMLLabelElement | null; }