Skip to content

Commit

Permalink
example: add todo input
Browse files Browse the repository at this point in the history
  • Loading branch information
sebkolind committed Nov 27, 2023
1 parent da93852 commit 2347847
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 12 deletions.
3 changes: 1 addition & 2 deletions example/app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { one, createStore, html } from '../dist/one'
import { TodoList } from './components/todo/list'
import { TodoList } from './components'

createStore(function () {
return {
Expand All @@ -12,7 +12,6 @@ one({
components: [TodoList],
template: html`
<div>
<h1>Hello world</h1>
<todo-list></todo-list>
</div>
`,
Expand Down
4 changes: 2 additions & 2 deletions example/components/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SimpleList } from './simple-list'
import { TodoList } from './todo/list'

export {
SimpleList,
TodoList,
}
29 changes: 29 additions & 0 deletions example/components/todo/input/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { html } from '../../../../lib/one'

const TodoInput = {
name: 'todo-input',
template: html`
<div>
<input type="text" />
<button id="add-btn">Add</button>
</div>
`,
state: {
text: '',
},
setup({ query, state, parent }) {
const input = query('input')
const btn = query('#add-btn')

input.on('input', function () {
state.text = input.value
})

btn.on('click', function () {
parent.events.add(state.text)
state.text = ''
})
},
}

export { TodoInput }
2 changes: 1 addition & 1 deletion example/components/todo/item/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const TodoItem = {
const toggleBtn = query('.toggleBtn')
const done = query('.done')

const {show, hide, toggle} = done.if({ initial: state.done })
const { toggle } = done.if({ initial: state.done })

removeBtn.on('click', function () {
parent.events.delete(state.id)
Expand Down
23 changes: 16 additions & 7 deletions example/components/todo/list/index.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,41 @@
import { TodoItem } from '../item'
import { html } from '../../../../lib/one'
import { TodoInput } from '../input'

const TodoList = {
name: 'todo-list',
components: [TodoItem, TodoInput],
state: {
items: [
{ id: 1, text: 'Item #1', done: false },
{ id: 2, text: 'Item #2', done: false },
{ id: 3, text: 'Item #3', done: false },
]
},
components: [TodoItem],
template: html`
<ul>
<todo-item></todo-item>
</ul>
<div>
<todo-input></todo-input>
<ul>
<todo-item></todo-item>
</ul>
</div>
`,
events: {
add(text, { state }) {
const id = Math.random().toString(36).substring(2, 7)

state.items.push({ id, text, done: false })

console.log('add todo', state.items)
},
delete(id, { state }) {
state.items = state.items.filter(item => item.id !== id)
},
},
setup({ query, state }) {
const item = query('todo-item')

item.for(['items', 'id'], function ({ el, item }) {
// console.log('item', item, el)
})
item.for(['items', 'id'])
}
}

Expand Down

0 comments on commit 2347847

Please sign in to comment.