Skip to content

Instantly share code, notes, and snippets.

@alavkx
Created September 16, 2020 19:44
Show Gist options
  • Save alavkx/e4f1cdaea7e9ccbcc2df7a119d7e9857 to your computer and use it in GitHub Desktop.
Save alavkx/e4f1cdaea7e9ccbcc2df7a119d7e9857 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const optimisticInputMachine = initial => Machine({
id: 'optimisticInput',
initial: 'idle',
context: {
value: initial,
savedValue: initial,
},
states: {
idle: {
on: {
FOCUS: 'focused'
}
},
focused: {
initial: 'pristine',
states: {
pristine: {
on: {
SUBMIT: '#optimisticInput.idle'
}
},
dirty: {
on: {
SUBMIT: '#optimisticInput.submitting'
}
},
},
on: {
CHANGE: {
target: '.dirty',
actions: 'update'
},
CLEAR: {
target: '.dirty',
actions: 'clear'
},
BLUR: {
target: '#optimisticInput.idle',
actions: 'reset'
}
}
},
submitting: {
on: {
RESOLVE: {
target: 'idle',
actions: 'save',
},
REJECT: {
target: 'idle',
actions: 'reset',
}
}
}
}
}, {
actions: {
update: assign({ value: (ctx, e) => e.payload}),
clear: assign({ value: null }),
reset: assign({ value: (ctx, e) => ctx.savedValue}),
save: assign({ savedValue: (ctx, e) => e.payload}),
}
});
optimisticInputMachine("Some ID")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment