Skip to content

Commit 06205e4

Browse files
authored
Merge pull request #112 from sassman/master
feat(example:todomvc): add editing support
2 parents 02d995e + 007d06d commit 06205e4

File tree

5 files changed

+32
-10
lines changed

5 files changed

+32
-10
lines changed

examples/assets/todomvc.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
html,
2-
body {
2+
body, pre {
33
margin: 0;
44
padding: 0;
55
}

examples/todomvc.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

packages/desktop/src/events.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ fn make_synthetic_event(name: &str, val: serde_json::Value) -> Arc<dyn Any + Sen
5252
let evt = serde_json::from_value::<KeyboardData>(val).unwrap();
5353
Arc::new(evt)
5454
}
55-
"focus" | "blur" => {
55+
"focus" | "blur" | "focusout" | "focusin" => {
5656
//
5757
Arc::new(FocusData {})
5858
}
@@ -117,6 +117,8 @@ fn event_name_from_typ(typ: &str) -> &'static str {
117117
"keypress" => "keypress",
118118
"keyup" => "keyup",
119119
"focus" => "focus",
120+
"focusout" => "focusout",
121+
"focusin" => "focusin",
120122
"blur" => "blur",
121123
"change" => "change",
122124
"input" => "input",

packages/html/src/events.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ pub mod on {
9191
/// onfocus
9292
onfocus
9393

94+
// onfocusout
95+
onfocusout
96+
97+
// onfocusin
98+
onfocusin
99+
94100
/// onblur
95101
onblur
96102
];
@@ -1092,7 +1098,7 @@ pub(crate) fn _event_meta(event: &UserEvent) -> (bool, EventPriority) {
10921098
"keydown" | "keypress" | "keyup" => (true, High),
10931099

10941100
// Focus
1095-
"focus" | "blur" => (true, Low),
1101+
"focus" | "blur" | "focusout" | "focusin" => (true, Low),
10961102

10971103
// Form
10981104
"change" | "input" | "invalid" | "reset" | "submit" => (true, Medium),

packages/web/src/cache.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ pub static BUILTIN_INTERNED_STRINGS: &[&'static str] = &[
203203
"onended",
204204
"onerror",
205205
"onfocus",
206+
"onfocusout",
207+
"onfocusin",
206208
"onhashchange",
207209
"oninput",
208210
"oninvalid",

0 commit comments

Comments
 (0)