Skip to content

Instantly share code, notes, and snippets.

@getfatday
Last active March 15, 2023 15:52
Show Gist options
  • Save getfatday/8d8114579986c6178bf6dee06d8d6f10 to your computer and use it in GitHub Desktop.
Save getfatday/8d8114579986c6178bf6dee06d8d6f10 to your computer and use it in GitHub Desktop.
Empty*
Empty*
error -> Invalid
focus -> Empty Focus
success -> Valid
Empty Focus
blur -> Empty
error -> Invalid Focus
success -> Valid Focus
Invalid
delete -> Empty
error -> Invalid
focus -> Invalid Focus
success -> Valid
Invalid Focus
blur -> Invalid
delete -> Empty Focus
error -> Invalid Focus
success -> Valid Focus
Valid
delete -> Empty
error -> Invalid
focus -> Valid Focus
success -> Valid
Valid Focus
blur -> Valid
delete -> Empty Focus
error -> Invalid Focus
success -> Valid Focus
const input = ({ error, focus = false, placeholder = "Empty", style, value = "" } = {}) => $(
"div",
{},
[
$("textarea", { placeholder, style, value }),
!!error ? $("div", { style: { color: "red" } }, error) : undefined
]
)
const inputFactory = style => (props = {}) => input(Object.assign(props, { style }))
const focus = inputFactory({
borderColor: "lightblue",
borderWidth: "2px"
})
const textarea = ({ active_states: [next] }) => {
switch(next.name) {
case "Invalid":
return input({ error: "Only greetings are allowed.", value: "Good Bye!" });
case "Invalid Focus":
return focus({ error: "Only greetings are allowed.", value: "Good Bye!" });
case "Valid":
return input({ value: "Hello!" });
case "Valid Focus":
return focus({ value: "Hello!" });
case "Empty Focus":
return focus();
case "Empty":
default:
return input()
}
}
const title = ({ active_states: [next] }) => $(
"h1",
{ style: {color: "darkBlue" } },
`The current state is: ${next.name}`
)
const render = model => $(
"div",
{},
[
title(model),
textarea(model)
]
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment