Skip to content

Instantly share code, notes, and snippets.

@isaacplmann
Created July 17, 2019 02:03
Show Gist options
  • Save isaacplmann/c257ff3c911b378c0a6a2a3627556387 to your computer and use it in GitHub Desktop.
Save isaacplmann/c257ff3c911b378c0a6a2a3627556387 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const selectionMachine = Machine(
{
id: 'selection',
context: {
selectedIndex: undefined,
},
initial: 'unselected',
states: {
unselected: {
on: {
SELECT: 'selected',
}
},
selected: {
onEntry: [assign((ctx, event) => ({
selectedIndex: event.selectedIndex
})),
sendParent((ctx) => ({
type: 'SELECTED',
selectedIndex: ctx.selectedIndex
}))
]
}
}
}
);
const matchingMachine = Machine(
{
id: 'matching',
context: {
choiceOneIndex: undefined,
choiceOneRef: null,
choiceTwoIndex: undefined,
choiceTwoRef: null
},
initial: 'answering',
states: {
answering: {
type: 'parallel',
states: {
choiceOne: {
initial: 'unselected',
states: {
unselected: {
on: { SELECT_ONE: 'selected' }
},
selected: {
onEntry: assign({
choiceOneIndex: (ctx, event) => event.selectedIndex
}),
type: 'final'
}
}
},
choiceTwo: {
initial: 'unselected',
states: {
unselected: {
on: { SELECT_TWO: 'selected' }
},
selected: {
onEntry: assign({
choiceTwoIndex: (ctx, event) => event.selectedIndex
}),
type: 'final'
}
}
}
},
onEntry: assign({
choiceOneIndex: () => undefined,
choiceTwoIndex: () => undefined
}),
onDone: 'submitted',
},
submitted: {
on: {
CLEAR: 'answering'
}
}
}
},
{}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment