Skip to content

Instantly share code, notes, and snippets.

@iazel
Created May 29, 2019 21:03
Show Gist options
  • Save iazel/be071d74bbe9b273d282171688182367 to your computer and use it in GitHub Desktop.
Save iazel/be071d74bbe9b273d282171688182367 to your computer and use it in GitHub Desktop.
Todos Example
import { mount } from '@crui/core/dom/brower'
import { Title } from './Title'
mount(
document.getElementById('root')!
Title,
{},
)
import { h } from '@crui/core'
const input = h('input', {
props: {
className: 'add-todo-submit'
},
events: {
input: (e) => doSomething(e)
}
})
import { hp } from '@crui/core'
const input = hp('input', { className: 'add-todo-submit' })
import { hp } from '@crui/core'
import { we } from '@crui/core/combinators/events'
const input = we(
hp('input', { className: 'add-todo-submit' }),
{ input: (e) => doSomething(e) }
)
import { he } from '@crui/core'
const input = he('input', {
input: (e) => doSomething(e)
})
import { h$ } from '@crui/reactive/elems'
import { StreamBox } from '@crui/reactive/rx/box'
const $box = new StreamBox('')
const input = h$('input', {
props: { className: 'add-todo-input' },
$bind: { value: $box }
})
const submit = (store: TodoStore) =>
h('button', {
props: { className: 'add-todo-submit' },
events: {
click: (e) => {
e.preventDefault()
store.addTodo(store.input.get())
store.input.set('')
}
},
children: [
text('Add')
]
})
const submit = h('button', {
props: { className: 'add-todo-submit' },
events: {
click: (e) => {
e.preventDefault()
// what to do?
}
},
children: [
text('Add')
]
})
type AddTodo = (todo: string) => void
const submit = (todo: StreamBox<string>, addTodo: AddTodo) =>
h('button', {
props: { className: 'add-todo-submit' },
events: {
click: (e) => {
e.preventDefault()
addTodo(todo.get())
todo.set('')
}
},
children: [
text('Add')
]
})
import { ht } from '@crui/core'
export const Title = ht('h1', 'TODO App')
import { hc, text } from '@crui/core'
const Title = hc('h1', [
text('TODO App')
])
import { StreamBox } from '@crui/reactive/rx/box'
export type Todo = {
text: string,
}
export type TodoList = Todo[]
export class TodoStore {
public readonly input: StreamBox<string>
public readonly todos: TodoList
constructor() {
this.input = new StreamBox('')
this.todos = []
}
addTodo(todo: string): void {
this.todos.push({
text: todo,
})
}
dispose() {
this.input.destroy()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment