1+ <!DOCTYPE html>
2+ < html >
3+
4+ < head >
5+ < meta charset ="utf-8 " />
6+ < meta http-equiv ="X-UA-Compatible " content ="IE=edge ">
7+ < title > Counter example with debugger</ title >
8+ < meta name ="viewport " content ="width=device-width, initial-scale=1 ">
9+
10+ < script src ="https://unpkg.com/hyperapp "> </ script >
11+ < script src ="hyperapp-devtools.js "> </ script >
12+
13+ < style >
14+ main {
15+ width : 40% ;
16+ margin-left : 3rem ;
17+ font-size : 1rem ;
18+ }
19+
20+ .input {
21+ width : 100% ;
22+ }
23+
24+ button {
25+ border-radius : 0px ;
26+ border : 1px solid black;
27+ font-size : 1rem ;
28+ }
29+
30+ .item {
31+ display : flex;
32+ justify-content : space-between;
33+ margin : 0.4rem 0rem ;
34+ }
35+
36+ .item span {
37+ flex-grow : 1 ;
38+ cursor : pointer;
39+ }
40+
41+ .done {
42+ text-decoration : line-through;
43+ }
44+ </ style >
45+ </ head >
46+
47+ < body >
48+ < div id ="app " />
49+ < br />
50+ < br />
51+ < a href ="index.html "> Back to index</ a >
52+
53+ < script >
54+ const h = hyperapp . h
55+
56+ const state = {
57+ todos : [ ] ,
58+ input : ""
59+ }
60+
61+ const actions = {
62+ add : name => state => ( { todos : state . todos . concat ( { name } ) , input : "" } ) ,
63+ toggle : id => state => ( {
64+ todos : state . todos . map (
65+ ( t , i ) => ( i === id ? { name : t . name , done : ! t . done } : t )
66+ )
67+ } ) ,
68+ remove : id => state => ( { todos : state . todos . filter ( ( t , i ) => i !== id ) } ) ,
69+ input : input => ( ) => ( { input } )
70+ }
71+
72+ const Todo = ( { item, id, actions } ) => (
73+ h ( "div" , { class : item . done ? "item done" : "item" } ,
74+ h ( "span" , { onclick : ( ) => actions . toggle ( id ) } , item . name ) ,
75+ h ( "button" , { onclick : ( ) => actions . remove ( id ) } , "x" )
76+ )
77+ )
78+
79+ const view = ( state , actions ) => (
80+ h ( "main" , { } ,
81+ h ( "h1" , { } , "Todo list" ) ,
82+ h ( "input" , {
83+ type : "text" ,
84+ class : "input" ,
85+ value : state . input ,
86+ onkeyup : e => ( e . keyCode === 13 ? actions . add ( state . input ) : "" ) ,
87+ oninput : e => actions . input ( e . target . value ) ,
88+ placeholder : "Enter item..."
89+ } ) ,
90+ state . todos . map ( ( item , id ) => Todo ( { item, id, actions } ) )
91+ )
92+ )
93+
94+ devtools ( hyperapp . app ) ( state , actions , view , document . getElementById ( "app" ) )
95+
96+ </ script >
97+
98+ </ body >
99+
100+ </ html >
0 commit comments