From ffb8afb327a1d888a5509a0ffded2c7d711a9cb8 Mon Sep 17 00:00:00 2001 From: Joshua Amaju Date: Fri, 7 Jan 2022 23:55:54 +0100 Subject: [PATCH] switch from generating handlers from schema to spawned actors --- src/index.ts | 24 +++++++++++++++--------- src/machine/index.ts | 17 ++++++++--------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/index.ts b/src/index.ts index eebd0e8..0034315 100644 --- a/src/index.ts +++ b/src/index.ts @@ -61,9 +61,16 @@ export type FormState = | 'submitted' | 'error'; -export type SubscriptionValue = Omit< +export type SubscriptionValue = Pick< Context, - '__ignore' | 'actors' | 'schema' | '__validationMarker' + | 'data' + | 'error' + | 'values' + | 'states' + | 'errors' + | 'failureCount' + | 'dataUpdatedAt' + | 'errorUpdatedAt' > & { state: FormState; isIdle: boolean; @@ -136,19 +143,18 @@ export const createForm = ({ const generate: Generate = ({ states, - schema, values, errors, + schema, + actors = {}, }: Context) => { - if (!schema || typeof schema === 'boolean') { - if (__DEV__) { - console.warn('Cannot generate handlers without schema defined'); + if (__DEV__) { + if (!schema || typeof schema === 'boolean') { + console.warn('No schema defined to generate handlers from'); } - - return; } - const entries = Object.keys(schema).map((k) => { + const entries = Object.keys(actors).map((k) => { const id = k as keyof T; const value = values[id]; const error = errors.get(id); diff --git a/src/machine/index.ts b/src/machine/index.ts index b433c21..b5127ae 100644 --- a/src/machine/index.ts +++ b/src/machine/index.ts @@ -205,10 +205,9 @@ export const machine = () => { actions: assign({ __ignore: (_, { ignore = [] }) => new Set(ignore), }), - cond: ({ schema }, { ignore = [] }) => { - if (!schema || typeof schema === 'boolean') return false; - const schemaLength = Object.values(schema).length; - return schemaLength - ignore.length > 0; + cond: ({ actors = {} }, { ignore = [] }) => { + const length = Object.values(actors).length; + return length - ignore.length > 0; }, }, { @@ -236,8 +235,8 @@ export const machine = () => { entry: [ assign({ errors: (_) => new Map() }), - pure(({ schema, values, __ignore }) => { - return Object.keys(schema as Schema) + pure(({ actors, values, __ignore }) => { + return Object.keys(actors) .filter((key) => !__ignore.has(key as keyof T)) .map((key) => { const value = values[key as keyof T]; @@ -256,16 +255,16 @@ export const machine = () => { return ( ctx.errors.size > 0 && ctx.__validationMarker.size >= - Object.keys(ctx.schema as Schema).length - ctx.__ignore.size + Object.keys(ctx.actors).length - ctx.__ignore.size ); }, }, { target: 'submitting', - cond: ({ schema, __ignore, __validationMarker }) => { + cond: ({ actors, __ignore, __validationMarker }) => { return ( __validationMarker.size >= - Object.keys(schema as Schema).length - __ignore.size + Object.keys(actors).length - __ignore.size ); }, },