Last active
October 17, 2020 19:37
-
-
Save karfau/760d85cb7ed38017a31f958fef883c7e to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz/?gist=760d85cb7ed38017a31f958fef883c7e
This file contains hidden or 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
// Available variables: | |
// - Machine | |
// - interpret | |
// - assign | |
// - send | |
// - sendParent | |
// - spawn | |
// - raise | |
// - actions | |
// - XState (all XState exports) | |
const fieldMachine = Machine({ | |
id: 'fetch', | |
initial: 'unchanged', | |
context: { | |
original: 'one', | |
current: 'one' | |
}, | |
on: { | |
CHANGE: [ | |
{ | |
cond: 'isChanged', | |
actions: 'storeCurrent', | |
target: 'validating' | |
}, | |
{ | |
actions: 'storeCurrent', | |
target: 'unchanged' | |
} | |
] | |
}, | |
states: { | |
unchanged: {}, | |
validating: { | |
on: { | |
VALID: 'submittable', | |
INVALID: 'invalid' | |
} | |
}, | |
invalid: {}, | |
submittable: { | |
on: { | |
SUBMIT: { | |
actions: 'save', | |
target: 'unchanged' | |
} | |
} | |
}, | |
} | |
},{ | |
actions:{ | |
storeCurrent: assign({current: (_, event) => event.value || '' | |
}), | |
save: assign({original: (c) => c.current}) | |
}, | |
guards:{ | |
isChanged: (c, e) => { | |
// console.log('isChanged', c, e) | |
return e.value !== c.original | |
} | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Related to statelyai/xstate#1565