Skip to content

Instantly share code, notes, and snippets.

@mormahr
Last active October 16, 2020 13:15
Show Gist options
  • Save mormahr/a0bb4a0d04e6655261ef6ef83fb92a29 to your computer and use it in GitHub Desktop.
Save mormahr/a0bb4a0d04e6655261ef6ef83fb92a29 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 dateValid = (context, event) => {
return true
}
const dayFocused = (context, event) => {
return event.cursor >= 0 && event.cursor <=2
}
const monthFocused = (context, event) => {
return event.cursor >= 3 && event.cursor <=5
}
const yearFocused = (context, event) => {
return event.cursor >= 6 && event.cursor <=8
}
const editingDateStates = {
initial: 'date',
states: {
'date': {
on: {
UPDATE_DATE: {
target: 'date',
actions: assign({
dateString: (context, event) => event.value
})
},
UP_ARROW: [{
target: 'day',
cond: dayFocused,
actions: assign({
dateString: (context, event) => increaseDay(context)
})
}, {
target: 'month',
cond: monthFocused,
actions: assign({
dateString: (context, event) => increaseMonth(context)
})
}, {
target: 'year',
cond: yearFocused,
actions: assign({
dateString: (context, event) => increaseYear(context)
})
}],
DOWN_ARROW: [{
target: 'day',
cond: dayFocused,
actions: assign({
dateString: (context, event) => decreaseDay(context)
})
}, {
target: 'month',
cond: monthFocused,
actions: assign({
dateString: (context, event) => decreaseMonth(context)
})
}, {
target: 'year',
cond: yearFocused,
actions: assign({
dateString: (context, event) => decreaseYear(context)
})
}]
}
},
'day': {
on: {
UP_ARROW: {
target: 'day',
actions: assign({
dateString: (context, event) => increaseDay(context)
})
},
DOWN_ARROW: {
target: 'day',
actions: assign({
dateString: (context, event) => decreaseDay(context)
})
},
RIGHT_ARROW: {
target: 'month'
},
}
},
'month': {
on: {
UP_ARROW: {
target: 'month',
actions: assign({
dateString: (context, event) => increaseMonth(context)
})
},
DOWN_ARROW: {
target: 'month',
actions: assign({
dateString: (context, event) => decreaseMonth(context)
})
},
LEFT_ARROW: {
target: 'day'
},
RIGHT_ARROW: {
target: 'year'
},
}
},
'year': {
on: {
UP_ARROW: {
target: 'year',
actions: assign({
dateString: (context, event) => increaseYear(context)
})
},
DOWN_ARROW: {
target: 'year',
actions: assign({
dateString: (context, event) => decreaseYear(context)
})
},
LEFT_ARROW: {
target: 'month'
},
}
},
}
}
const fetchMachine = Machine({
id: 'uhrzeit',
initial: 'idle',
context: {
date: "",
dateString: "",
},
states: {
idle: {
on: {
FOCUS_DATE: 'editing_date'
}
},
editing_date: {
...editingDateStates,
on: {
UNFOCUS_DATE: [{
target: 'idle',
cond: dateValid,
actions: assign({
date: (context, event) => context.dateString
})
}, {
target: 'idle',
}]
},
},
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment