Skip to content

Instantly share code, notes, and snippets.

@karfau
Last active October 17, 2020 19:37
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 karfau/760d85cb7ed38017a31f958fef883c7e to your computer and use it in GitHub Desktop.
Save karfau/760d85cb7ed38017a31f958fef883c7e to your computer and use it in GitHub Desktop.
// 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
}
}
});
@karfau
Copy link
Author

karfau commented Oct 17, 2020

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