Created
December 18, 2018 19:01
-
-
Save hnordt/3b38da76bf8a6717bd7e380e3881bbb3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// You can preview it on https://statecharts.github.io/xstate-viz/ | |
const formMachine = Machine({ | |
id: "form", | |
context: { | |
initialValues: {}, | |
values: {}, | |
touched: {}, | |
errors: {}, | |
submitCount: 0, | |
validate: () => Promise.reject(new Error("No validate function provided")), | |
submit: () => Promise.reject(new Error("No submit function provided")) | |
}, | |
initial: "idle", | |
states: { | |
idle: { | |
on: { | |
CHANGE: { | |
actions: "setValue" | |
}, | |
BLUR: { | |
actions: "touch" | |
}, | |
SUBMIT: { | |
target: "validating", | |
actions: "incSubmitCount" | |
}, | |
RESET: { | |
actions: "reset" | |
} | |
} | |
}, | |
validating: { | |
invoke: { | |
src: "validate", | |
onDone: { | |
target: "submitting", | |
actions: "resetErrors" | |
}, | |
onError: { | |
target: "idle", | |
actions: "setErrors" | |
} | |
} | |
}, | |
submitting: { | |
invoke: { | |
src: "submit", | |
onDone: { | |
target: "idle" | |
}, | |
onError: { | |
target: "idle" | |
} | |
} | |
} | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment