Skip to content

Instantly share code, notes, and snippets.

@VinSpee
Last active October 29, 2019 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VinSpee/654e2d1e446495e1741e34703379c12f to your computer and use it in GitHub Desktop.
Save VinSpee/654e2d1e446495e1741e34703379c12f to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const inputActions = {
CHANGE: "input/change",
FOCUS: "input/focus",
BLUR: "input/blur"
};
const valid = "editing.dirty.valid";
const inputMachine = Machine(
{
id: "input",
initial: "pristine",
context: { value: "", errors: [] },
states: {
pristine: {
on: {
"": {
cond: "validate",
target: "editing.dirty.valid"
},
[inputActions.FOCUS]: {
target: "editing",
actions: "focusInput"
}
}
},
editing: {
initial: "focused",
on: {
[inputActions.CHANGE]: [
{
cond: "validate",
target: "editing.dirty.valid",
actions: "updateValue"
},
{
target: "editing.dirty.invalid",
actions: "updateValue"
}
],
[inputActions.BLUR]: {
actions: "blurInput",
target: ".blurred"
}
},
states: {
focused: {},
blurred: {
type: "final"
},
dirty: {
initial: "invalid",
states: {
valid: {},
invalid: {}
}
}
}
}
}
},
{
actions: {
focusInput: () => {},
updateValue: assign({
value: (ctx, evt) => evt.data || ""
})
},
guards: {
validate: (ctx, evt) => evt.data === "valid"
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment