Skip to content

Instantly share code, notes, and snippets.

@snaquaye
Last active September 19, 2020 22:37
Show Gist options
  • Save snaquaye/b85e7f27a951d7d7d907859a4e4d545e to your computer and use it in GitHub Desktop.
Save snaquaye/b85e7f27a951d7d7d907859a4e4d545e to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const nextStep = assign({
currentStep: (ctx, _event) => Math.min(3, ctx.currentStep + 1)
});
const prevStep = assign({
currentStep: (ctx, _event) => Math.max(0, ctx.currentStep - 1)
});
const isLastStep = (ctx, _event) => {
return ctx.lastStep === ctx.currentStep;
};
const multistepFormMachine = Machine(
{
id: "multistepFormMachine",
initial: "notSubmitted",
context: {
maxSteps: 3,
currentStep: 0
},
states: {
notSubmitted: {
on: {
NEXT: {
target: undefined,
actions: "nextStep",
},
BACK: {
target: undefined,
actions: "prevStep",
},
SUBMIT: {
target: "submitted",
cond: "isLastStep"
}
}
},
submitted: {
}
}
},
{
actions: {
nextStep,
prevStep
},
guards: {
isLastStep
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment