Last active
March 10, 2020 13:08
-
-
Save avanslaars/beea5242b1f393a84f3bc47cd23ebd90 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 inputMachine = Machine({ | |
id: 'input', | |
type: 'parallel', | |
context: { | |
value: '', | |
hasChanges: false, | |
}, | |
states: { | |
focusStatus: { | |
initial: 'idle', | |
states: { | |
idle: { | |
on: { | |
FOCUS: 'focused', | |
}, | |
}, | |
focused: { | |
on: { | |
BLUR: 'idle', | |
CHANGE: { actions: 'updateValue' }, | |
}, | |
}, | |
}, | |
}, | |
dirtyStatus: { | |
initial: 'clean', | |
states: { | |
clean: { | |
on: { | |
FOCUS: 'pendingDirty', | |
FORCE_DIRTY: 'dirty' // enable all fields to be made dirty on form submission | |
}, | |
}, | |
// This allows us to track FOCUS + CHANGE + BLUR before showing an error if this is dirty and invalid | |
pendingDirty: { | |
on: { | |
BLUR: [ | |
{ target: 'dirty', cond: 'isChanged' }, | |
{ target: 'clean' }, | |
], | |
CHANGE: { actions: 'markChanged' }, | |
}, | |
}, | |
dirty: {}, | |
}, | |
}, | |
validStatus: { | |
initial: 'unknown', | |
states: { | |
unknown: { | |
on: { | |
'': [{ target: 'valid', cond: 'isValid' }, { target: 'invalid' }], | |
}, | |
}, | |
valid: { | |
on: { | |
CHANGE: 'unknown', | |
}, | |
}, | |
invalid: { | |
on: { | |
CHANGE: 'unknown', | |
}, | |
}, | |
}, | |
}, | |
}, | |
}, | |
{ | |
actions: { | |
updateValue: assign({ | |
value: (_, event) => event.payload, | |
}), | |
markChanged: assign({ hasChanges: () => true }), | |
}, | |
guards: { | |
isValid: context => context.value && context.value.length > 2, | |
isChanged: context => context.hasChanges, | |
}, | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment