Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor #50

Merged
merged 3 commits into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export type Actions<T = any, D = any> = {
cancelSubmit: () => void;
kill: (id: string) => void;
submitAsync: () => Promise<D>;
clearError: (id: string) => void;
// clearError: (id: string) => void;
set: <N extends keyof T>(name: N, value: T[N]) => void;
validate: <N extends keyof T>(name: N, value?: T[N]) => void;
spawn: (id: string, value: unknown, validator: Validator) => void;
Expand Down Expand Up @@ -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');
Expand All @@ -81,7 +81,7 @@ export const createForm = <
};

const set: Actions<T>['set'] = (id, value) => {
service.send({ id: id as string, value, type: 'change' });
service.send({ id: id as string, value, type: 'set' });
};

const validate: Actions<T>['validate'] = (id, value) => {
Expand Down Expand Up @@ -117,7 +117,7 @@ export const createForm = <
reset,
submit,
validate,
clearError,
// clearError,
submitAsync,
cancelSubmit,
__service: service,
Expand Down
45 changes: 26 additions & 19 deletions src/machine/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ export type Ctx<T extends object = any, D = any, E = any, FE = any> = {

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
Expand Down Expand Up @@ -97,23 +97,23 @@ export const config = <T extends object>(
],
},

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',
Expand Down Expand Up @@ -279,6 +279,13 @@ export const config = <T extends object>(
},
}),

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);
Expand Down
22 changes: 11 additions & 11 deletions src/machine/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ export type FlattenedSchema<T = any> = {
[K in keyof T]: Validator<T, T[K]>;
};

export type Infer<T> = T extends Schema<infer R>
? {
[K in keyof R]: T[K] extends Array<infer U>
? Infer<U>[]
: R[K] extends Validator<R, R[K]>
? ReturnType<R[K]> extends Promise<infer N>
? N
: ReturnType<R[K]>
: R[K];
}
: never;
// export type Infer<T> = T extends Schema<infer R>
// ? {
// [K in keyof R]: T[K] extends Array<infer U>
// ? Infer<U>[]
// : R[K] extends Validator<R, R[K]>
// ? ReturnType<R[K]> extends Promise<infer N>
// ? N
// : ReturnType<R[K]>
// : R[K];
// }
// : never;

export type SyncValidator<V = any, Vs = any> = (value: V, values: Vs) => V;

Expand Down