Last active
September 19, 2020 22:37
-
-
Save snaquaye/b85e7f27a951d7d7d907859a4e4d545e to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
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
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