import type { Machine, StateMachine as S } from "@zag-js/core" import type { CommonProperties, DirectionProperty, PropTypes, RequiredBy } from "@zag-js/types" import type { JSX } from "@zag-js/types" interface FieldOptions { type?: string validate?: ValidatorFn } type ValiidatorResult = string | null | Promise type ValidatorFn = (value: any, values: Record) => ValiidatorResult type ValidateRules = Partial>> /* ----------------------------------------------------------------------------- * Field Machine context * -----------------------------------------------------------------------------*/ interface FieldPublicContext extends DirectionProperty, CommonProperties { /** * The value of the field */ value?: any /** * Validation rule for the field */ validate?: ValidatorFn } export type FieldUserDefinedContext = RequiredBy type FieldComputedContext = Readonly<{}> interface FieldPrivateContext { /** * @internal * Whether the field is focused */ focused?: boolean /** * @internal * Whether the field is touched * Field is considered to be touched when user focused it or its value was changed programmatically */ touched?: boolean /** * @internal * Whether the field is dirty * Field is considered to be dirty when its value was changed and new value is different from field value specified in initialValues (compared with fast-deep-equal) */ dirty?: boolean /** * @internal * The default value of the field */ defaultValue?: any /** * @internal * Whether the form is validating */ validating?: boolean /** * @internal * Erros in the field */ error?: string | null } export interface FieldMachineContext extends FieldPublicContext, FieldPrivateContext, FieldComputedContext {} export interface FieldMachineState { value: "idle" | "validating" | "valid" | "invalid" } type FieldChangeEvent = { type: "CHANGE"; value: any } type FieldFocusEvent = { type: "FOCUS" } type FieldBlurEvent = { type: "BLUR" } type FieldValidateEvent = { type: "VALIDATE"; validator?: ValidatorFn; values?: Record } type FieldResetErrorEvent = { type: "RESET_ERROR" } export type FieldMachineEvent = | FieldFocusEvent | FieldBlurEvent | FieldChangeEvent | FieldValidateEvent | FieldResetErrorEvent export type FieldState = S.State export type FieldSend = S.Send export type FieldService = Machine /* ----------------------------------------------------------------------------- * Form Machine context * -----------------------------------------------------------------------------*/ export type ElementIds = Partial<{ form: string field(name: K): string }> interface FormPublicContext extends DirectionProperty, CommonProperties { /** * The ids of the elements in the form. Useful for composition. */ ids?: ElementIds /** * The default values of the fields in the form */ defaultValues: Record | (() => Record | Promise>) /** * Validation rules for the form */ validate?: ValidateRules /** * When validation gets triggered * @default "all" */ validation: "change" | "submit" | "blur" | "all" /** * Whether to focus on the first field with error after submitting * @default true */ focusOnError?: boolean } export type FormUserDefinedContext = RequiredBy, "id"> type FormComputedContext = Readonly<{}> interface FormPrivateContext { /** * @internal * The child field machines (spawned by the form) */ fields: Record } export interface FormMachineContext extends FormPublicContext, FormPrivateContext, FormComputedContext {} export interface FormMachineState { value: "loading" | "initialized" | "submitting" | "submitted" } type FormInitializeEvent = { type: "INITIALIZE" } type FormInitializedEvent = { type: "INITIALIZED" } type FormFieldChangeEvent = { type: "FIELD.CHANGE"; name: K; value: any } type FormFieldFocusEvent = { type: "FIELD.FOCUS"; name: K } type FormFieldBlurEvent = { type: "FIELD.BLUR"; name: K } type FormSubmitEvent = { type: "SUBMIT"; cb: (values: Record) => void } type FormSubmittedEvent = { type: "SUBMITTED" } type FormSubmitAbortEvent = { type: "SUBMIT.ABORT" } export type FormMachineEvent = | FormInitializeEvent | FormInitializedEvent | FormFieldChangeEvent | FormFieldFocusEvent | FormFieldBlurEvent | FormSubmitEvent | FormSubmittedEvent | FormSubmitAbortEvent export type FormState = S.State, FormMachineState, FormMachineEvent> export type FormSend = S.Send> export type FormService = Machine, FormMachineState, FormMachineEvent> /* ----------------------------------------------------------------------------- * Component API * -----------------------------------------------------------------------------*/ export interface FormMachineApi { /** * Whether the form is dirty */ dirty: boolean /** * Whether the form is validating */ validating: boolean /** * Errors in the form */ errors: Record /** * Function to get Current values of the form */ getValues: () => Record /** * Function to submit the form. */ onSubmit>(cb: (values: Record) => void): (event?: E) => void getFieldProps(name: K, options?: FieldOptions): T["element"] }