Created
September 16, 2020 19:44
-
-
Save alavkx/e4f1cdaea7e9ccbcc2df7a119d7e9857 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains 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