Last active
October 29, 2019 20:26
-
-
Save VinSpee/654e2d1e446495e1741e34703379c12f 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 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