Skip to content

Instantly share code, notes, and snippets.

@hnordt
Created December 18, 2018 19:01
Show Gist options
  • Save hnordt/3b38da76bf8a6717bd7e380e3881bbb3 to your computer and use it in GitHub Desktop.
Save hnordt/3b38da76bf8a6717bd7e380e3881bbb3 to your computer and use it in GitHub Desktop.
// 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