Skip to content

Instantly share code, notes, and snippets.

@danielsdesk
Last active January 15, 2020 03:52
Show Gist options
  • Save danielsdesk/f1807dc2f40b3ef62bc4e8ab6cdd66a8 to your computer and use it in GitHub Desktop.
Save danielsdesk/f1807dc2f40b3ef62bc4e8ab6cdd66a8 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
const venueFieldMachine = initialValue => Machine(
{
id: 'venueField',
initial: 'init',
context: {
fieldValue: initialValue,
fieldType: 'dropdown',
fieldOptions: {
name: 'eventVenueField',
displayTitle: 'Event Venue',
placeholder: '',
shortDescription: 'Select a venue',
longDescription: '',
selections: [
{
key: 'meeting',
title: 'Meeting'
},
{
key: 'concert',
title: 'Concert'
},
{
key: 'movie',
title: 'Movie'
},
]
}
},
on: {
SELECT: [
{
target: 'empty',
cond: (_ctx, { selection }) => !selection,
actions: ['updateFieldValue']
},
{
target: 'selected',
actions: assign((_ctx, { selection }) => ({ fieldValue: selection ? String(selection) : '' }))
}
]
},
states: {
init: {
on: {
'': [
{
target: 'empty',
cond: ({ fieldValue }) => !fieldValue
},
{
target: 'selected'
}
]
}
},
empty: {
entry: 'requestFieldSet'
},
selected: {
entry: 'requestFieldSet'
}
}
}, {
actions: {
updateFieldValue: assign((_ctx, { selection }) => ({ fieldValue: selection ? String(selection) : '' })),
requestFieldSet: sendParent(({ fieldValue }) => {
if (fieldValue.toLowerCase() === 'movie') {
alert('isMovie')
return 'SHOW_MOVIE_OPTIONS'
} else {
alert('Not a movie')
return 'SHOW_REGULAR_OPTIONS'
}
})
}
}
);
venueFieldMachine('movie')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment