/** * Shared form participation for every `formAssociated` SekiUI control. * * One `FormControl` per component instance owns the four things that kept * drifting apart between components: * * 1. the submitted value -> `internals.setFormValue(...)` * 2. constraint validation -> `internals.setValidity(...)` * 3. the validity API on the host -> `validity`, `validationMessage`, * `willValidate`, `checkValidity()`, * `reportValidity()` * 4. lifecycle callbacks -> reset (back to the value captured at * load), disabled, state restore * * Usage inside a component: * * ```ts * private form!: FormControl; * * componentWillLoad() { * this.form = new FormControl(this.el, this.internals, { ...options }); * this.form.captureInitial(this.value); * this.form.sync(); * } * formResetCallback() { this.form?.reset(); } * formDisabledCallback(d: boolean) { this.form?.setFieldsetDisabled(d); } * formStateRestoreCallback(s: FormControlState) { this.form?.restoreState(s); } * ``` * * ## Why plain host members and not `@Method()` * * Stencil `@Method()`s are always async: `el.checkValidity()` would return a * `Promise`, which is truthy, so the idiomatic `if (!el.checkValidity())` would * silently never fire. Native parity (`validity`, `willValidate` and * `validationMessage` are synchronous reads) matters more than Stencil's method * proxy, so the helper defines the members directly on the host element. That * works in every build target (lazy loader, CDN and custom-elements) because the * host is a real `HTMLElement` instance. The one limitation is that, like any * Stencil API, the members exist once the component has loaded, not before the * lazy loader has hydrated the element; `form.checkValidity()` and form * submission go through `ElementInternals` and are always correct. */ export type FormControlState = string | File | FormData | null; /** * The members every form control gains on its host element. Stencil's generated * `HTMLSeki*Element` types cannot be augmented with them (the generated * `ariaLabel` typing clashes under interface merging), so TypeScript consumers * that want typed access cast: `(el as HTMLSekiInputElement & ValidatableElement)`. */ export interface ValidatableElement { readonly validity: ValidityState; readonly validationMessage: string; readonly willValidate: boolean; checkValidity(): boolean; reportValidity(): boolean; } type NativeControl = HTMLInputElement | HTMLTextAreaElement; export interface FormControlOptions { /** The value to submit. `null` means "contribute nothing". Not called while disabled. */ getFormValue: () => string | FormData | null; /** True when the control currently holds no value (drives `valueMissing`). */ isEmpty: () => boolean; /** Put the control back to a captured initial value. Must update its own state. */ restore: (initial: T) => void; /** Apply a value handed back by the browser (history navigation / autofill). */ restoreState?: (state: FormControlState) => void; isRequired?: () => boolean; isDisabled?: () => boolean; /** Read-only controls still submit but are barred from constraint validation. */ isReadonly?: () => boolean; /** Message for `valueMissing`. Browsers do not expose their localized ones. */ requiredMessage?: string; /** The focusable element the browser should focus / anchor its bubble to. */ getAnchor?: () => HTMLElement | null | undefined; /** * Constraints beyond `valueMissing` that the component models itself * (e.g. a date outside min/max). Evaluated only when the value is present. */ getConstraints?: () => { flags: ValidityStateFlags; message: string; } | null | undefined; /** * An inner native ``/`