Last active
November 16, 2019 22:32
-
-
Save danielkcz/577d8ba60bae726aa4d1105c461fb80a 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 reset = assign({ | |
value: ({ initialValue }) => initialValue | |
}) | |
const updateValue = assign({ | |
value: (_, ev) => ev.value | |
}) | |
const isEnterOrTab = (_, ev) => ['Enter', 'Tab'].includes(ev.key) | |
const isEscape = (ctx, ev) => ['Escape'].includes(ev.key) | |
const isValueDirty = ({ initialValue, value }) => initialValue !== value | |
const smartTextMachine = Machine( | |
{ | |
id: 'smartText', | |
initial: 'reading', | |
context: { | |
initialValue: '', | |
value: '' | |
}, | |
states: { | |
reading: { | |
initial: 'hideIndicator', | |
states: { | |
hideIndicator: {}, | |
showIndicator: {} | |
}, | |
onEntry: 'reset', | |
on: { | |
START_EDIT: 'editing', | |
MOUSE_LEAVE: '.hideIndicator', | |
MOUSE_ENTER: '.showIndicator' | |
} | |
}, | |
editing: { | |
initial: 'unknown', | |
states: { | |
unknown: { | |
on: { | |
'': [ | |
{ target: '#smartText.editing.dirty', cond: 'isValueDirty' }, | |
{ target: '#smartText.editing.pristine' } | |
] | |
} | |
}, | |
pristine: {}, | |
dirty: {} | |
}, | |
onEntry: 'setFocus', | |
on: { | |
KEYPRESS: [ | |
{ target: '#smartText.confirm', cond: 'isEnterOrTab' }, | |
{ target: '#smartText.cancel', cond: 'isEscape' } | |
], | |
CHANGE: { | |
actions: 'updateValue', | |
target: '.unknown' | |
}, | |
CANCEL_EDIT: 'cancel' | |
} | |
}, | |
confirm: { | |
entry: 'submit', | |
on: { | |
'': 'reading' | |
} | |
}, | |
cancel: { | |
entry: 'reset', | |
on: { | |
'': 'reading' | |
} | |
} | |
} | |
}, | |
{ | |
guards: { | |
isValueDirty, | |
isEnterOrTab, | |
isEscape | |
}, | |
actions: { | |
reset, | |
updateValue, | |
setFocus: () => {}, | |
submit: () => {} | |
} | |
} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment