Skip to content

Commit 14f7b78

Browse files
jkelleyrtperichulburd
authored andcommitted
Feat: use components instead of rebuilding the list each run
1 parent b953b1b commit 14f7b78

File tree

1 file changed

+46
-34
lines changed

1 file changed

+46
-34
lines changed

frameworks/keyed/dioxus/src/main.rs

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,11 @@ use rand::prelude::*;
55

66
fn main() {
77
// for performance reasons, we want to cache these strings on the edge of js/rust boundary
8-
for &name in ADJECTIVES
9-
.iter()
10-
.chain(NOUNS.iter())
11-
.chain(COLOURS.iter())
12-
{
8+
for &name in ADJECTIVES.iter().chain(NOUNS.iter()).chain(COLOURS.iter()) {
139
wasm_bindgen::intern(name);
14-
}
15-
16-
dioxus::web::launch(app);
17-
}
10+
}
1811

19-
fn app1(cx: Scope) -> Element {
20-
cx.render(rsx!{
21-
div { "hello, wasm!" }
22-
})
12+
dioxus::web::launch(app);
2313
}
2414

2515
#[derive(Clone, PartialEq)]
@@ -62,7 +52,8 @@ impl LabelsContainer {
6252
}
6353

6454
fn append(&mut self, num: usize) {
65-
self.labels.extend(Label::new_list(1_000, self.last_key + 1));
55+
self.labels
56+
.extend(Label::new_list(1_000, self.last_key + 1));
6657
self.last_key += num;
6758
}
6859

@@ -77,16 +68,14 @@ impl LabelsContainer {
7768
}
7869
}
7970

80-
#[feature(int_roundings)]
8171
fn remove(&mut self, index: usize) {
8272
self.labels.remove(index as usize);
8373
}
84-
8574
}
8675

8776
fn app(cx: Scope) -> Element {
8877
let labels_container = use_ref(&cx, || LabelsContainer::new(0, 0));
89-
let selected: &UseState<Option<usize>> = use_state(&cx, || None);
78+
let selected = use_state(&cx, || None as Option<usize>);
9079

9180
cx.render(rsx! {
9281
div { class: "container",
@@ -117,31 +106,55 @@ fn app(cx: Scope) -> Element {
117106
}
118107
}
119108
}
109+
120110
table { class: "table table-hover table-striped test-data",
121111
tbody { id: "tbody",
122-
labels_container.read().labels.iter().enumerate().map(|(index, item)| {
123-
let key = item.key;
124-
let is_in_danger = if (*selected).map(|s| s == item.key).unwrap_or(false) {"danger"} else {""};
125-
rsx!(tr { key: "{key}", class: "{is_in_danger}",
126-
td { class:"col-md-1", "{key}" }
127-
td { class:"col-md-4", onclick: move |_| selected.set(Some(key)),
128-
a { class: "lbl", [item.labels.join(" ").as_str()] }
129-
}
130-
td { class: "col-md-1",
131-
a { class: "remove", onclick: move |_| { labels_container.write().remove(index); },
132-
span { class: "glyphicon glyphicon-remove remove", aria_hidden: "true" }
133-
}
134-
}
135-
td { class: "col-md-6" }
136-
})
112+
labels_container.read().labels.iter().enumerate().map(|(idx, _)| rsx! {
113+
Row {
114+
labels: labels_container.clone(),
115+
selected: selected.clone(),
116+
key: "{idx}",
117+
index: idx
118+
}
137119
})
138120
}
139-
}
121+
}
122+
140123
span { class: "preloadicon glyphicon glyphicon-remove", aria_hidden: "true" }
141124
}
142125
})
143126
}
144127

128+
#[inline_props]
129+
fn Row(
130+
cx: Scope,
131+
labels: UseRef<LabelsContainer>,
132+
selected: UseState<Option<usize>>,
133+
index: usize,
134+
) -> Element {
135+
let item = &labels.read().labels[*index];
136+
let is_in_danger = if **selected == Some(*index) {
137+
"danger"
138+
} else {
139+
""
140+
};
141+
142+
cx.render(rsx! {
143+
tr { class: "{is_in_danger}",
144+
td { class:"col-md-1", "{index}" }
145+
td { class:"col-md-4", onclick: move |_| selected.set(Some(*index)),
146+
a { class: "lbl", [item.labels.join(" ").as_str()] }
147+
}
148+
td { class: "col-md-1",
149+
a { class: "remove", onclick: move |_| { labels.write().remove(*index); },
150+
span { class: "glyphicon glyphicon-remove remove", aria_hidden: "true" }
151+
}
152+
}
153+
td { class: "col-md-6" }
154+
}
155+
})
156+
}
157+
145158
#[derive(Props)]
146159
struct ActionButtonProps<'a> {
147160
name: &'a str,
@@ -158,7 +171,6 @@ fn ActionButton<'a>(cx: Scope<'a, ActionButtonProps<'a>>) -> Element {
158171
r#type: "button",
159172
id: "{cx.props.id}",
160173
onclick: move |_| cx.props.onclick.call(()),
161-
162174
"{cx.props.name}"
163175
}
164176
}

0 commit comments

Comments
 (0)