svelte-hook-form
Version:
A better version of form validation.
51 lines (50 loc) • 1.3 kB
TypeScript
import { type Readable } from "svelte/store";
import type { FieldState, FieldStore, ValidationRule } from "./types";
export declare class FormField {
private readonly store;
private readonly rules;
private readonly bail;
constructor(store: FieldStore, rules: ValidationRule[], bail: boolean);
/**
* Returns a readonly field state.
*
* @returns {Readable<FieldState>} state of the field.
*/
get state(): Readonly<{
defaultValue: any;
value: any;
pending: boolean;
dirty: boolean;
touched: boolean;
valid: boolean;
errors: string[];
}>;
/**
* Observe the field state.
*
* @returns {Readable<FieldState>} state of the field.
*
* @example
* ```svelte
* <script lang="ts">
* const state$ = watch("text");
* state$.subscribe((v) => {
* console.log("State =>", v);
* });
* </script>
* ```
*/
get watch(): Readable<FieldState>;
/**
* Update the current field state.
*
* @param {Partial<FieldState>} state
*
* @example
* ```ts
* field.setState({ valid: true });
* ```
*/
setState(state: Partial<FieldState>): void;
validate: (value?: any) => Promise<FieldState>;
}