/** * @typedef {Object} FormStateMember - The form state member. * @property {string | number | boolean | string[] | null} value - The value of the form element. * @property {ValidityState} validity - The validity state of the form element, stored when fired from the form element. * @property {boolean} required - Whether the form element is required or not. * @property {HTMLElement} element - Whether the form element is required or not. */ /** * @typedef {Object.} FormState - The form state. */ /** * The `auro-form` element provides users a way to create and manage forms in a consistent manner. * @customElement auro-form * * @slot default - The default slot for form elements. * * @event input - Fires when a child form element receives user input. * @event change - Fires when a child form element's value changes or the form is initialized. * @event reset - Fires when the form is reset. The event detail contains the previous value of the form before reset. * @event submit - Fires when the form is submitted. The event detail contains the current value of the form. */ export class AuroForm extends LitElement { static get properties(): { /** @private */ formState: { type: ObjectConstructor; attribute: boolean; }; /** @private */ _validity: { type: ObjectConstructor; attribute: boolean; }; /** @private */ _isInitialState: { type: BooleanConstructor; attribute: boolean; }; /** @private */ _elements: { type: ArrayConstructor; attribute: boolean; }; /** @private */ _submitElements: { type: ArrayConstructor; attribute: boolean; }; /** @private */ _resetElements: { type: ArrayConstructor; attribute: boolean; }; }; static get formElementTags(): string[]; static get buttonElementTags(): string[]; static get styles(): import("lit").CSSResult[]; /** * Registers the `auro-form` custom element with the browser under a given tag name. * @param {string} [name="auro-form"] - The custom element tag name to register. * * @example * AuroForm.register("custom-form") // registers as */ static register(name?: string): void; /** * @type {FormState} * @private */ private formState; /** * @type {"valid" | "invalid" | null} * @private */ private _validity; /** @private */ private _isInitialState; /** * @type {(HTMLElement & {reset: () => void})[]} * @private */ private _elements; /** * @type {HTMLButtonElement[]} * @private */ private _submitElements; /** * @type {HTMLButtonElement[]} * @private */ private _resetElements; /** * @private * @type {MutationObserver[]} */ private mutationObservers; /** * Resets all form elements to their initial state and fires a `reset` event. The event's `detail.previousValue` contains the form values captured immediately before the reset. * @returns {void} */ reset(): void; /** * Validates all form elements. If all are valid, fires a `submit` event with `detail.value` containing the current form values. If any element is invalid, its error state is surfaced and the `submit` event is not fired. * @returns {Promise} */ submit(): Promise; /** * Shared input listener for all form elements. * @param {Event} event - The event that is fired from the form element. * @private */ private sharedInputListener; /** * Shared validation listener for all form elements. * @param {Event} event - The event that is fired from the form element. * @private */ private sharedValidationListener; /** * Mutation observer for form elements. Slot change does not trigger unless * root-level elements are added/removed. This is a workaround to ensure * nested form elements are also observed. * * @returns {void} * @private */ private mutationEventListener; /** * Handle Enter key press on form elements. * @param {KeyboardEvent} event - The keyboard event. * @private */ private handleKeyDown; /** * Compare tag name with element to identify it (for API purposes). * @param {string} elementTag - The HTML tag name like `auro-datepicker`. * @param {HTMLElement} element - The actual HTML element to compare. * @returns {boolean} * @private */ private _isElementTag; /** * Shared code for determining if an element is something we care about (submit, form element, etc.). * @param {string[]} collection - The array to use for tag name search. * @param {HTMLElement} element - The element to compare against the master list. * @returns {boolean} * @private */ private _isInElementCollection; /** * Check if the tag name is a form element. * @param {HTMLElement} element - The element to check (attr or tag name). * @returns {boolean} * @private */ private isFormElement; /** * Validates if an event is from a valid form element with a name. * @param {Event} event - The event to validate. * @returns {boolean} - True if event is valid for processing. * @private */ private _eventIsValidFormEvent; /** * Check if the tag name is a button element. * @param {HTMLElement} element - The element to check. * @returns {boolean} * @private */ private isButtonElement; /** * Returns the current values of all named form elements as a key-value object, keyed by each element's `name` attribute. * @returns {Record} The current form values. */ get value(): Record; /** * Getter for internal _submitElements. * @returns {HTMLButtonElement[]} * @private */ private get submitElements(); /** * Returns a collection of elements that will reset the form. * @returns {HTMLButtonElement[]} * @private */ private get resetElements(); /** * Infer validity status based on current formState. * @private */ private _calculateValidity; /** * Returns `'valid'` if all required and interacted-with form elements are valid, `'invalid'` if any are not, or `null` if the form has not been interacted with yet. * @returns {"valid" | "invalid" | null} */ get validity(): "valid" | "invalid" | null; /** * Determines whether the form is in its initial (untouched) state and updates `_isInitialState` accordingly. * @returns {void} * @private */ private _setInitialState; /** * Returns `true` if no form element has been interacted with or had its value changed since the form was initialized or last reset. * @returns {boolean} */ get isInitialState(): boolean; /** * Enables or disables submit and reset buttons based on the current form state and validity. * @returns {void} * @private */ private setDisabledStateOnButtons; /** * Construct the query strings from elements, append them together, execute, and return the NodeList. * @returns {NodeList} * @private */ private queryAuroElements; /** * Store an element in state and on the _elements array. * @param {HTMLElement} element - The element to add to our state. * @private */ private _addElementToState; /** * Initialize (or reinitialize) the form state. * @returns {void} * @private */ private initializeState; /** * Attaches input, validation, and keydown listeners to all tracked form and button elements. * Removes existing listeners first to avoid duplicates on re-initialization. * @returns {void} * @private */ private _attachEventListeners; /** * Slot change event listener. This is the main entry point for the form element. * @param {Event} event - The slot change event. * @returns {void} * @private */ private onSlotChange; /** * @returns {import('lit').TemplateResult} */ render(): import("lit").TemplateResult; } /** * - The form state member. */ export type FormStateMember = { /** * - The value of the form element. */ value: string | number | boolean | string[] | null; /** * - The validity state of the form element, stored when fired from the form element. */ validity: ValidityState; /** * - Whether the form element is required or not. */ required: boolean; /** * - Whether the form element is required or not. */ element: HTMLElement; }; /** * - The form state. */ export type FormState = { [x: string]: FormStateMember; }; import { LitElement } from "lit";