Skip to content

Commit dc0b8aa

Browse files
authored
Merge pull request #4 from ink-0/week1/3/checkBox
Week1/3/check box
2 parents 0386adf + 3544be9 commit dc0b8aa

File tree

5 files changed

+69
-24
lines changed

5 files changed

+69
-24
lines changed

src/css/style.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ main {
171171
border: none;
172172
-webkit-appearance: none;
173173
appearance: none;
174+
cursor: pointer;
174175
}
175176

176177
.todo-list li .toggle {

src/js/TodoApp.js

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,47 @@ import TodoList from './TodoList.js';
44

55
export default function TodoApp($app) {
66
this.state = {
7-
todoes: ["hi I'm Tami"],
8-
count: 0,
7+
todoes: [
8+
{
9+
idx: 0,
10+
content: 'hi EveryOne',
11+
state: '',
12+
edit: '',
13+
},
14+
{
15+
idx: 2,
16+
content: "I'm Tami",
17+
state: '',
18+
edit: '',
19+
},
20+
],
921
};
1022

1123
this.setState = (nextState) => {
1224
this.state = nextState;
1325
todoList.setState(this.state.todoes);
14-
todoCount.setState(this.state.count);
26+
todoCount.setState(this.state.todoes);
1527
};
1628

1729
new TodoInput({
1830
$app,
1931
onAdd: (contents) => {
20-
this.state.todoes.push(contents);
21-
this.setState(this.state);
32+
const prevIdx = this.state.todoes[this.state.todoes.length - 1].idx;
33+
34+
const newTodo = {
35+
idx: prevIdx + 1,
36+
content: contents,
37+
state: '',
38+
edit: '',
39+
};
40+
this.state.todoes.push(newTodo);
41+
42+
this.setState({
43+
...this.state,
44+
});
2245
},
2346
});
47+
2448
const todoList = new TodoList({
2549
$app,
2650
initialState: this.state.todoes,

src/js/TodoInput.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ export default function TodoInput({ $app, onAdd }) {
66
this.$target.autofocus;
77
$app.appendChild(this.$target);
88

9+
this.setState = (nextState) => {
10+
this.setState = nextState;
11+
this.render();
12+
};
13+
914
this.$target.addEventListener('keydown', (e) => {
1015
this.addTodoItem(e);
1116
});
@@ -18,10 +23,6 @@ export default function TodoInput({ $app, onAdd }) {
1823
$newTodoTarget.value = '';
1924
}
2025
};
21-
this.setState = (nextState) => {
22-
this.setState = nextState;
23-
this.render();
24-
};
2526

2627
this.render = () => {};
2728
}

src/js/TodoItem.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1-
// export default TodoItem = ({ $app, content }) => {
2-
// this.state = content
3-
// this.$target = document.createElement('li')
1+
export default TodoItem = ({ $app, content }) => {
2+
this.state = content;
3+
this.$target = document.createElement('li');
4+
$app.appendChild(this.$target);
5+
this.setState = (nextState) => {
6+
this.state = nextState;
7+
this.render();
8+
};
9+
this.render = () => {
10+
const todoItemTemplate = `
11+
<div class="view">
12+
<input class="toggle" type="checkbox"/>
13+
<label class="label">${this.state}</label>
14+
<button class="destroy"></button>
15+
</div>
16+
<input class="edit" value=${this.state}/>
17+
`;
418

5-
// this.setState = (nextState) => {
6-
// this.state = nextState;
7-
// this.render()
8-
// }
9-
// this.render = () => {
10-
// const todoItemTemplate = `<li>${this.state</li>`
11-
// }
12-
// }
19+
this.$target.innerHTML = todoItemTemplate;
20+
};
21+
};

src/js/TodoList.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,28 @@ export default function TodoList({ $app, initialState }) {
1212
this.state = nextState;
1313
this.render();
1414
};
15+
this.$target.addEventListener('click', (e) => {});
16+
17+
this.$target.addEventListener('click', (e) => {
18+
console.log(e.target.className);
19+
console.log(e);
20+
const $node = e;
21+
const { nodeId } = $node.dataset;
22+
console.log('nodeid', nodeId);
23+
});
24+
this.toggleTodo = (e) => {};
1525

1626
this.render = () => {
1727
const todoTemplate = `${this.state
1828
.map(
1929
(todo, idx) =>
20-
`<li>
30+
`<li >
2131
<div class="view">
22-
<input class="toggle" type="checkbox"/>
23-
<label class="label">${todo}</label>
32+
<input class="toggle" type="checkbox" data-node-id=${todo.idx} />
33+
<label class="label">${todo.content}</label>
2434
<button class="destroy"></button>
2535
</div>
26-
<input class="edit" value=${todo}/>
36+
<input class="edit " value=${todo.content}/>
2737
</li>`
2838
)
2939
.join('')}`;

0 commit comments

Comments
 (0)