Created
October 2, 2020 16:17
-
-
Save alavkx/a4d54ec6f96b2192165798ac65b916be to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
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
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