shelving
Version:
Toolkit for using data in JavaScript.
40 lines (39 loc) • 2.05 kB
TypeScript
import type { DataSchema } from "../../schema/DataSchema.js";
import { Schema } from "../../schema/Schema.js";
import { DataStore } from "../../store/DataStore.js";
import { DictionaryStore } from "../../store/DictionaryStore.js";
import type { Data, DataKey } from "../../util/data.js";
import type { ImmutableDictionary } from "../../util/dictionary.js";
import type { Arguments } from "../../util/function.js";
/** Store the current value of a form. */
export declare class FormStore<T extends Data> extends DataStore<Partial<T>> implements AsyncDisposable {
/** Unique ID for the form. */
readonly id: string;
/** Key used for mounting the form */
readonly key: string;
/** Schema for the current form. */
readonly schema: DataSchema<T>;
/**
* Store named error messages for individual fields.
* - Throwing a string triggers changes in this.
* - Rows prefixed with `fieldName:` are shown on those specific fields.
* - See `splitMessages()` in `shelving` for more information.
*/
readonly messages: DictionaryStore<string>;
/** Get the current valid value for this form (throws string for invalid values). */
get validated(): T;
get reason(): unknown;
set reason(reason: unknown);
constructor(schema: DataSchema<T>, partialData?: Partial<T>, messages?: ImmutableDictionary<string> | string | undefined);
/** Get a named schema for a field of this form. */
requireSchema<K extends DataKey<T>>(name: K): Schema<T[K]>;
/** Publish a value for a field of this form. */
publish<K extends DataKey<T>>(name: K, unsafeValue: T[K]): void;
/**
* Validate and submit the current values of the form.
*
* @param callback Optional callback that takes the current (validated) value of the form, processes it (possibly asynchronously) and returns any new values.
*/
submit<A extends Arguments>(callback?: ((value: T, ...args: A) => void) | undefined, ...args: A): boolean | Promise<boolean>;
[Symbol.asyncDispose](): Promise<void>;
}