import type { LitElement } from "lit"; import type { MixinBase, MixinReturn } from "./types.ts"; import { type WithElementInternals } from "./with-element-internals.ts"; export declare const getFormValue: unique symbol; export declare const getFormState: unique symbol; export type FormValue = File | string | FormData; export type FormRestoreState = File | string | Array<[string, FormDataEntryValue]>; export type FormRestoreReason = "restore" | "autocomplete"; export interface FormAssociatedConstructor { /** * Indicates that an element is participating in form association. */ readonly formAssociated: true; } export interface FormAssociated { /** * The associated form element with which this element's value will submit. */ readonly form: HTMLFormElement | null; /** * The labels this element is associated with. */ readonly labels: NodeList; /** * The HTML name to use in form submission. */ name: string; /** * Whether or not the element is disabled. */ disabled: boolean; /** * Gets the current form value of a component. * * @return The current form value. */ [getFormValue](): FormValue | null; /** * Gets the current form state of a component. Defaults to the component's * `[formValue]`. * * Use this when the state of an element is different from its value, such as * checkboxes (internal boolean state and a user string value). * * @return The current form state, defaults to the form value. */ [getFormState](): FormValue | null; /** * A callback for when a form component should be disabled or enabled. This * can be called in a variety of situations, such as disabled `
`s. * * @param disabled Whether or not the form control should be disabled. */ formDisabledCallback(disabled: boolean): void; /** * A callback for when the form requests to reset its value. Typically, the * default value that is reset is represented in the attribute of an element. * * This means the attribute used for the value should not update as the value * changes. For example, a checkbox should not change its default `checked` * attribute when selected. Ensure form values do not reflect. */ formResetCallback(): void; /** * A callback for when the form restores the state of a component. For * example, when a page is reloaded or forms are autofilled. * * @param state The state to restore, or null to reset the form control's * value. * @param reason The reason state was restored, either `'restore'` or * `'autocomplete'`. */ formStateRestoreCallback(state: FormRestoreState | null, reason: FormRestoreReason): void; /** * An optional callback for when the associated form changes. * * @param form The new associated form, or `null` if there is none. */ formAssociatedCallback?(form: HTMLFormElement | null): void; } /** * Mixes in form-associated behavior for a class. This allows an element to add * values to `
` elements. * * Implementing classes should provide a `[formValue]` to return the current * value of the element, as well as reset and restore callbacks. * * @example * ```ts * const base = withFormAssociated(withElementInternals(LitElement)); * * class MyControl extends base { * \@property() * value = ''; * * override [getFormValue]() { * return this.value; * } * * override formResetCallback() { * const defaultValue = this.getAttribute('value'); * this.value = defaultValue; * } * * override formStateRestoreCallback(state: string) { * this.value = state; * } * } * ``` * * Elements may optionally provide a `[formState]` if their values do not * represent the state of the component. * * @example * ```ts * const base = withFormAssociated(withElementInternals(LitElement)); * * class MyCheckbox extends base { * \@property() * value = 'on'; * * \@property({type: Boolean}) * checked = false; * * override [getFormValue]() { * return this.checked ? this.value : null; * } * * override [getFormState]() { * return String(this.checked); * } * * override formResetCallback() { * const defaultValue = this.hasAttribute('checked'); * this.checked = defaultValue; * } * * override formStateRestoreCallback(state: string) { * this.checked = Boolean(state); * } * } * ``` * * @param base The class to mix functionality into. The base class must use * `withElementInternals()`. */ declare const withFormAssociated: >(BaseClass: T) => MixinReturn; export default withFormAssociated;