Skip to content

Instantly share code, notes, and snippets.

@zettadam
Last active November 21, 2019 19:30
Show Gist options
  • Select an option

  • Save zettadam/064a901c4f10d83c9409d03299f43bfb to your computer and use it in GitHub Desktop.

Select an option

Save zettadam/064a901c4f10d83c9409d03299f43bfb 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 textInputMachine = Machine({
id: 'textInput',
context: {
validateOnEnable: true,
validateOnBlur: true,
validateOnChange: false
},
initial: 'enabled',
states: {
enabled: {
initial: 'idle',
on: {
DISABLE: 'disabled',
},
states: {
idle: {
on: {
VALIDATE: {
target: 'validating',
cond: 'shouldValidateOnEnable'
},
FOCUS: 'focused'
}
},
blurred: {
on: {
FOCUS: 'focused',
VALIDATE: {
target: 'validating',
cond: 'shouldValidateOnBlur'
}
}
},
focused: {
on: {
BLUR: 'blurred',
CHANGE: 'focused',
CLEAR: 'focused',
VALIDATE: {
target: 'validating',
cond: 'shouldValidateOnChange'
}
}
},
validating: {
on: {
VALID: 'valid',
INVALID: 'invalid'
}
},
valid: {
on: {
'': 'idle',
}
},
invalid: {
on: {
'': 'idle'
}
}
}
},
disabled: {
on: {
ENABLE: 'enabled'
}
}
},
guards: {
shouldValidateOnEnable: (context, event) => context.validateOnEnable,
shouldValidateOnBlur: (context, event) => context.validateOnBlur,
shouldValidateOnChange: (context, event) => context.validateOnChange
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment