diff --git a/README.md b/README.md index b6a32bb..e18e183 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,7 @@ An object which provides - `reset` - a function to reset the form state to its initial state - `cancelSubmit` (() => void) - function to cancel the current form submission - `submitAsync` - async version of submit and resolves with the submission result +- `set` - ((name, value) => void) - a function to set the value for any given field - `subscribe` ((stateListener) => () => void) - a state listener with the current state of the form (see below for [stateListener](#state-listener)) - `__service` - the base service (xstate interpreter), made available for library authors to creating wrappers for frameworks - `validate` ((field, value?) => void) - function to validate given field diff --git a/src/index.ts b/src/index.ts index 98e0b54..86708cd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -28,7 +28,7 @@ export type Actions = { cancelSubmit: () => void; kill: (id: string) => void; submitAsync: () => Promise; - clearError: (id: string) => void; + // clearError: (id: string) => void; set: (name: N, value: T[N]) => void; validate: (name: N, value?: T[N]) => void; spawn: (id: string, value: unknown, validator: Validator) => void; @@ -64,9 +64,9 @@ export const createForm = < service.send('submit'); }; - const clearError: Actions['clearError'] = (id) => { - service.send({ id, type: 'clear_error' }); - }; + // const clearError: Actions['clearError'] = (id) => { + // service.send({ id, type: 'clear_error' }); + // }; const cancelSubmit: Actions['cancelSubmit'] = () => { service.send('cancel'); @@ -81,7 +81,7 @@ export const createForm = < }; const set: Actions['set'] = (id, value) => { - service.send({ id: id as string, value, type: 'change' }); + service.send({ id: id as string, value, type: 'set' }); }; const validate: Actions['validate'] = (id, value) => { @@ -117,7 +117,7 @@ export const createForm = < reset, submit, validate, - clearError, + // clearError, submitAsync, cancelSubmit, __service: service, diff --git a/src/machine/index.ts b/src/machine/index.ts index 93226ab..b27bf08 100644 --- a/src/machine/index.ts +++ b/src/machine/index.ts @@ -19,11 +19,11 @@ export type Ctx = { export type Events = | { type: 'reset' } - | { type: 'change'; id: string; value: unknown } + | { type: 'set'; id: string; value: unknown } | { type: 'spawn'; id: string; value: unknown; validator: Validator } | { type: 'kill'; id: string } | { type: 'validate'; id: string; value?: any } - | { type: 'clear_error'; id: string } + // | { type: 'clear_error'; id: string } | { type: 'submit' | 'cancel' } // actor events @@ -97,23 +97,23 @@ export const config = ( ], }, - clear_error: [ - { - cond: (_, { id }) => !!id, - actions: [ - 'removeActorError', - send((_) => ({ type: 'set', name: 'error', value: null }), { - to: (_, { id }) => id, - }), - ], - }, - { - target: 'idle', - actions: 'clearError', - }, - ], - - change: { + // clear_error: [ + // { + // cond: (_, { id }) => !!id, + // actions: [ + // 'removeActorError', + // send((_) => ({ type: 'set', name: 'error', value: null }), { + // to: (_, { id }) => id, + // }), + // ], + // }, + // { + // target: 'idle', + // actions: 'clearError', + // }, + // ], + + set: { actions: [ 'setValue', 'setInitialState', @@ -279,6 +279,13 @@ export const config = ( }, }), + maybeSetValue: assign({ + values: ({ values }, { id, value }: any) => { + set(values, id, value === undefined ? get(values, id) : value); + return values; + }, + }), + setActorError: assign({ errors: ({ errors }, { id, error }: any) => { set(errors, id, error); diff --git a/src/machine/types.ts b/src/machine/types.ts index 922da15..3162791 100644 --- a/src/machine/types.ts +++ b/src/machine/types.ts @@ -15,17 +15,17 @@ export type FlattenedSchema = { [K in keyof T]: Validator; }; -export type Infer = T extends Schema - ? { - [K in keyof R]: T[K] extends Array - ? Infer[] - : R[K] extends Validator - ? ReturnType extends Promise - ? N - : ReturnType - : R[K]; - } - : never; +// export type Infer = T extends Schema +// ? { +// [K in keyof R]: T[K] extends Array +// ? Infer[] +// : R[K] extends Validator +// ? ReturnType extends Promise +// ? N +// : ReturnType +// : R[K]; +// } +// : never; export type SyncValidator = (value: V, values: Vs) => V;