File tree Expand file tree Collapse file tree 3 files changed +44
-25
lines changed Expand file tree Collapse file tree 3 files changed +44
-25
lines changed Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ export default function TodoApp($app) {
1212 edit : '' ,
1313 } ,
1414 {
15- idx : 2 ,
15+ idx : 1 ,
1616 content : "I'm Tami" ,
1717 state : '' ,
1818 edit : '' ,
@@ -28,26 +28,13 @@ export default function TodoApp($app) {
2828
2929 new TodoInput ( {
3030 $app,
31- onAdd : ( contents ) => {
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- } ) ;
45- } ,
31+ onAdd : ( contents ) => addTodo ( contents ) ,
4632 } ) ;
4733
4834 const todoList = new TodoList ( {
4935 $app,
5036 initialState : this . state . todoes ,
37+ onToggle : ( idx ) => toggleTodo ( idx ) ,
5138 } ) ;
5239 const todoCount = new TodoCount ( {
5340 $app,
@@ -60,4 +47,34 @@ export default function TodoApp($app) {
6047 } ) ;
6148 } ;
6249 init ( ) ;
50+
51+ const addTodo = ( contents ) => {
52+ const todos = this . state . todoes ;
53+ const nextIdx = Math . max ( 0 , ...todos . map ( ( todo ) => todo . idx ) ) + 1 ;
54+ console . log ( 'next' , nextIdx ) ;
55+ const newTodo = {
56+ idx : nextIdx ,
57+ content : contents ,
58+ state : '' ,
59+ edit : '' ,
60+ } ;
61+ this . state . todoes . push ( newTodo ) ;
62+
63+ this . setState ( {
64+ ...this . state ,
65+ } ) ;
66+ } ;
67+
68+ const toggleTodo = ( idx ) => {
69+ const todos = this . state . todoes ;
70+
71+ todos . map ( ( todo ) => {
72+ if ( todo . idx === parseInt ( idx ) ) {
73+ todo . state = todo . state === '' ? 'complete' : '' ;
74+ }
75+ } ) ;
76+ this . setState ( {
77+ ...this . state ,
78+ } ) ;
79+ } ;
6380}
Original file line number Diff line number Diff line change @@ -12,7 +12,8 @@ export default function TodoCount({ $app, initialState }) {
1212
1313 this . render = ( ) => {
1414 const todoCountTemplate = `
15- <span class="todo-count">총 <strong>${ this . state } </strong> 개</span><ul class="filters">
15+ <span class="todo-count">총 <strong>${ this . state } </strong> 개</span>
16+ <ul class="filters">
1617 <li>
1718 <a class="all selected" href="#">전체보기</a>
1819 </li>
Original file line number Diff line number Diff line change 1- export default function TodoList ( { $app, initialState } ) {
1+ export default function TodoList ( { $app, initialState, onToggle } ) {
22 //렌더링할 DOM 생성
33
44 this . state = initialState ;
@@ -12,22 +12,23 @@ export default function TodoList({ $app, initialState }) {
1212 this . state = nextState ;
1313 this . render ( ) ;
1414 } ;
15- this . $target . addEventListener ( 'click' , ( e ) => { } ) ;
1615
1716 this . $target . addEventListener ( 'click' , ( e ) => {
18- console . log ( e . target . className ) ;
19- console . log ( e ) ;
20- const $node = e ;
17+ const $node = e . target ;
2118 const { nodeId } = $node . dataset ;
22- console . log ( 'nodeid' , nodeId ) ;
19+ if ( nodeId ) {
20+ this . toggleTodoItem ( nodeId ) ;
21+ }
2322 } ) ;
24- this . toggleTodo = ( e ) => { } ;
23+ this . toggleTodoItem = ( nodeId ) => {
24+ onToggle ( nodeId ) ;
25+ } ;
2526
2627 this . render = ( ) => {
2728 const todoTemplate = `${ this . state
2829 . map (
2930 ( todo , idx ) =>
30- `<li >
31+ `<li class=" ${ todo . state === 'complete' ? 'completed' : '' } " >
3132 <div class="view">
3233 <input class="toggle" type="checkbox" data-node-id=${ todo . idx } />
3334 <label class="label">${ todo . content } </label>
You can’t perform that action at this time.
0 commit comments