Skip to content

Instantly share code, notes, and snippets.

@jsmanifest
Created June 9, 2020 14:57
Show Gist options
  • Save jsmanifest/4570d7ad89f8079c4e90eab3c2898e48 to your computer and use it in GitHub Desktop.
Save jsmanifest/4570d7ad89f8079c4e90eab3c2898e48 to your computer and use it in GitHub Desktop.
function resolveStyles(component) {
// Restrict it from displaying in a smaller size
if (component.style.height < 300) {
component.style['height'] = 300
}
if (component.type === 'button') {
// Give all button components a dashed teal border
component.style['border'] = '1px dashed teal'
}
if (component.type === 'input') {
if (component.inputType === 'email') {
// Uppercase every letter for email inputs
component.style.textTransform = 'uppercase'
}
}
return component
}
function resolveChildren(component) {
if (Array.isArray(component.children)) {
component.children = component.children.map((child) => {
// resolveStyles's return value is a component, so we can use the return value from resolveStyles to be the the result for child components
return resolveStyles(child)
})
}
return component
}
function start(component, resolvers = []) {
return resolvers.reduce((acc, resolve) => {
return resolve(acc)
}, component)
}
const component = {
type: 'div',
style: {
height: 250,
fontSize: 14,
fontWeight: 'bold',
textAlign: 'center',
},
children: [
{
type: 'input',
inputType: 'email',
placeholder: 'Enter your email',
style: {
border: '1px solid magenta',
},
},
],
}
const result = start(component, [resolveStyles, resolveChildren])
console.log(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment