@@ -116,21 +116,33 @@ pub fn todo_entry<'a>(cx: Scope<'a, TodoEntryProps<'a>>) -> Element {
116116 let todo = & cx. props . todos [ & cx. props . id ] ;
117117 let is_editing = use_state ( & cx, || false ) ;
118118 let completed = if todo. checked { "completed" } else { "" } ;
119+ let editing = if * is_editing. get ( ) { "editing" } else { "" } ;
119120
120- rsx ! ( cx, li { class: "{completed}" ,
121+ rsx ! ( cx, li {
122+ class: "{completed} {editing}" ,
123+ onclick: move |_| is_editing. set( true ) ,
124+ onfocusout: move |_| is_editing. set( false ) ,
121125 div { class: "view" ,
122126 input { class: "toggle" , r#type: "checkbox" , id: "cbg-{todo.id}" , checked: "{todo.checked}" ,
123127 onchange: move |evt| {
124128 cx. props. todos. modify( ) [ & cx. props. id] . checked = evt. value. parse( ) . unwrap( ) ;
125129 }
126130 }
127131 label { r#for: "cbg-{todo.id}" , pointer_events: "none" , "{todo.contents}" }
128- is_editing. then( || rsx!{
129- input {
130- value: "{todo.contents}" ,
131- oninput: move |evt| cx. props. todos. modify( ) [ & cx. props. id] . contents = evt. value. clone( ) ,
132- }
133- } )
134132 }
133+ is_editing. then( || rsx!{
134+ input {
135+ class: "edit" ,
136+ value: "{todo.contents}" ,
137+ oninput: move |evt| cx. props. todos. modify( ) [ & cx. props. id] . contents = evt. value. clone( ) ,
138+ autofocus: "true" ,
139+ onkeydown: move |evt| {
140+ match evt. key. as_str( ) {
141+ "Enter" | "Escape" | "Tab" => is_editing. set( false ) ,
142+ _ => { }
143+ }
144+ } ,
145+ }
146+ } )
135147 } )
136148}
0 commit comments