/** * Form component: validation, messages, summary and focus management. * * @augments IvComponent */ export class Form extends IvComponent { /** @type {Readonly} */ static defaults: Readonly; /** * Returns the instance attached to `el`, if any. * * @param {Element} el Host element. * @returns {Form|undefined} The instance or `undefined`. */ static get(el: Element): Form | undefined; /** * Returns the existing instance or creates one. * * @param {Element} el Host element. * @param {Partial} [options] Options passed in JavaScript. * @returns {Form} The instance. */ static getOrCreate(el: Element, options?: Partial): Form; /** * Instantiates every `[data-iv-component="form"]` inside `root`. * * @param {ParentNode} [root] Subtree to scan. * @returns {Form[]} Newly created instances. */ static initAll(root?: ParentNode): Form[]; /** * @param {Element} el A `
` element. * @param {Partial} [options] Options passed in JavaScript. * @throws {IvError} `invalid-element` when the element is not a ``. */ constructor(el: Element, options?: Partial); /** @type {HTMLFormElement} The enhanced form. */ _form: HTMLFormElement; /** @type {Map>} Attributes to restore. */ _saved: Map>; /** @type {Map} What was built around each control. */ _records: Map; /** @type {Map} Message currently shown per control. */ _messages: Map; /** @type {Set} Controls the user has already interacted with. */ _touched: Set; /** @type {HTMLElement|null} The summary, when the option is on. */ _summary: HTMLElement | null; /** @type {boolean} Whether the summary element was created here. */ _ownsSummary: boolean; /** @type {ChildNode[]} Children the served summary had. */ _summaryNodes: ChildNode[]; /** @type {Set>} Pending shake timers. */ _timers: Set>; /** * Resolved options. * * @returns {Readonly} The options. */ get options(): Readonly; /** * Controls currently in error, in document order. * * @returns {FieldError[]} The errors. */ get errors(): FieldError[]; /** * Controls that take part in validation, in document order. Only the first * radio of a group is listed: the group shares one validity and one message. * * @returns {FormField[]} The controls. */ get fields(): FormField[]; /** * Validates every control and applies the resulting state. * * @returns {boolean} `true` when the form has no errors. */ validate(): boolean; /** * Validates one control and applies the resulting state. * * @param {Element} control The control to validate. * @returns {boolean} `true` when the control has no error. */ validateField(control: Element): boolean; /** * Clears every state this component applied, as after a `reset` of the form. * * @returns {void} */ reset(): void; /** * Resolves the message of a control: the browser validity first, then the * author overrides, then any listener of `iv:validate`. * * @param {FormField} control The control. * @returns {string} The message, or an empty string when the control is valid. */ _resolve(control: FormField): string; /** * Message the browser and the author attributes give for a control. * * @param {FormField} control The control. * @returns {string} The message, or an empty string when the control is valid. */ _messageFor(control: FormField): string; /** * Shows the message of a control and marks its field invalid. * * @param {FormField} control The control. * @param {string} message The message. * @returns {void} */ _showError(control: FormField, message: string): void; /** * Removes the error state of a control. * * @param {FormField} control The control. * @param {boolean} markValid Whether the "valid" state should be shown. * @returns {void} */ _clearField(control: FormField, markValid: boolean): void; /** * Returns the bookkeeping record of a control, creating it on first use. * * @param {FormField} control The control. * @returns {FieldRecord} The record. */ _record(control: FormField): FieldRecord; /** * Returns the message element of a control: the one the author served inside * the field (or referenced by `aria-describedby`) if there is one, a new * `

` otherwise. * * @param {FormField} control The control. * @param {FieldRecord} record Bookkeeping record of the control. * @returns {HTMLElement} The message element. */ _errorElement(control: FormField, record: FieldRecord): HTMLElement; /** * Looks for a served message element among the ids of `aria-describedby`. * * @param {FormField} control The control. * @returns {HTMLElement|null} The element, or `null`. */ _describedError(control: FormField): HTMLElement | null; /** * Adds an id to `aria-describedby` without repeating what is already there. * * @param {FormField} control The control. * @param {string} id Id of the message element. * @returns {void} */ _describe(control: FormField, id: string): void; /** * Builds (or adopts) the summary shown at the top of the form. * * @returns {void} */ _buildSummary(): void; /** * Fills the summary with the current errors, or hides it when there are none. * * @returns {void} */ _updateSummary(): void; /** * Puts the served summary back, or removes the generated one. * * @returns {void} */ _restoreSummary(): void; /** * Moves the focus to a control from the summary. * * @param {Event} event The click. * @returns {void} */ _onSummaryClick(event: Event): void; /** * Validates the control that lost the focus. * * @param {Event} event The `focusout`. * @returns {void} */ _onFocusOut(event: Event): void; /** * Validates while typing: always with `validateOn: "input"`, and only on a * control that is already showing an error with `validateOn: "blur"`. * * @param {Event} event The `input` or `change`. * @returns {void} */ _onInput(event: Event): void; /** * Validates on submit: a failed form never reaches the server, and a valid * one can still be held back by cancelling `iv:valid`. * * @param {SubmitEvent} event The `submit`. * @returns {void} */ _onSubmit(event: SubmitEvent): void; /** * Focuses a target and, when asked to, brings it into view. * * @param {HTMLElement} target Element to focus. * @returns {void} */ _focus(target: HTMLElement): void; /** * Shakes the invalid fields for a moment, unless the user asked for less * motion. * * @returns {void} */ _shake(): void; /** * Remembers the current value of an attribute, once per element. * * @param {Element} el Element to remember. * @param {string} name Attribute name. * @returns {void} */ _remember(el: Element, name: string): void; /** * Sets a managed attribute, remembering its previous value. * * @param {Element} el Target element. * @param {string} name Attribute name. * @param {string} value Attribute value. * @returns {void} */ _set(el: Element, name: string, value: string): void; /** * Puts a managed attribute back to the value the author served. * * @param {Element} el Target element. * @param {string} name Attribute name. * @returns {void} */ _restore(el: Element, name: string): void; } export type FormOptions = { /** * When fields are validated: `blur`, `input` or `submit`. */ validateOn: string; /** * Whether the errors are also listed at the top of the form. */ summary: boolean; /** * Heading of that list. */ summaryTitle: string; /** * Whether a failed submit moves the focus. */ focusFirst: boolean; /** * Whether the focused target is scrolled into view. */ scroll: boolean; /** * Whether error messages are announced while typing. */ live: boolean; }; export type FormField = HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement; /** * One control and the message currently shown for it. */ export type FieldError = { /** * The invalid control. */ control: FormField; /** * The message shown to the user. */ message: string; }; /** * What `init` found (or built) around one control, so `destroy` can undo it. */ export type FieldRecord = { /** * The `.iv-field` container, when there is one. */ field: HTMLElement | null; /** * The message element, once it is needed. */ error: HTMLElement | null; /** * Whether that element was created here. */ owned: boolean; /** * Text the served message element had. */ text: string; /** * Value of its `hidden` attribute, if it had one. */ hidden: string | null; }; import { IvComponent } from "../core/component.js";