Skip to content

Instantly share code, notes, and snippets.

@karfau
Last active October 17, 2020 19:36
Show Gist options
  • Save karfau/7711b52a5cb2e43e59d24b00e3c60b53 to your computer and use it in GitHub Desktop.
Save karfau/7711b52a5cb2e43e59d24b00e3c60b53 to your computer and use it in GitHub Desktop.
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
const CHANGE = [
{
actions: ['log', 'storeCurrent']
},
{
cond: 'isChanged',
target: 'validating'
},
{target: 'unchanged'}
]
const fieldMachine = Machine({
id: 'fetch',
initial: 'unchanged',
context: {
original: 'one',
current: 'one'
},
states: {
unchanged: {
on: {CHANGE}
},
validating: {
on: {
CHANGE,
VALID: 'submittable',
INVALID: 'invalid'
}
},
invalid: {
on: {CHANGE}
},
submittable: {
CHANGE,
SUBMIT: {
actions: 'save',
target: 'unchanged'
}
},
}
},{
actions:{
log: console.error,
storeCurrent: assign({current: (_, event) => event.value || ''
}),
save: assign({original: (c) => c.current})
},
guards:{
isChanged: c => {
console.log('isChanged', c)
return c.current !== c.original
}
}
});
@karfau
Copy link
Author

karfau commented Oct 17, 2020

This machine has several issues, the one I got stuck with (including many possible solutions):
statelyai/xstate#1565

The solution I picked and other fixes are in a second gist:
https://gist.github.com/karfau/760d85cb7ed38017a31f958fef883c7e

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment