Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disambiguate if expressions in rsx by requiring curlies, allow shorthand component/element initialization #1810

Merged
merged 11 commits into from
Jan 11, 2024
6 changes: 5 additions & 1 deletion examples/all_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ fn app(cx: Scope) -> Element {

"Hover, click, type or scroll to see the info down below"
}
div { events.read().iter().map(|event| rsx!( div { "{event:?}" } )) }
div {
for event in events.read().iter() {
div { "{event:?}" }
}
}
}
))
}
8 changes: 4 additions & 4 deletions examples/calculator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ fn app(cx: Scope) -> Element {
};

cx.render(rsx!(
style { include_str!("./assets/calculator.css") }
style { {include_str!("./assets/calculator.css")} }
div { id: "wrapper",
div { class: "app",
div { class: "calculator",
tabindex: "0",
onkeydown: handle_key_down_event,
div { class: "calculator-display", val.to_string() }
div { class: "calculator-display", "{val}" }
div { class: "calculator-keypad",
div { class: "input-keys",
div { class: "function-keys",
Expand Down Expand Up @@ -103,14 +103,14 @@ fn app(cx: Scope) -> Element {
div { class: "digit-keys",
button { class: "calculator-key key-0", onclick: move |_| input_digit(0), "0" }
button { class: "calculator-key key-dot", onclick: move |_| val.make_mut().push('.'), "●" }
(1..10).map(|k| rsx!{
for k in 1..10 {
button {
class: "calculator-key {k}",
name: "key-{k}",
onclick: move |_| input_digit(k),
"{k}"
}
}),
}
}
}
div { class: "operator-keys",
Expand Down
4 changes: 2 additions & 2 deletions examples/compose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ fn app(cx: Scope) -> Element {
}

ul {
emails_sent.read().iter().map(|message| cx.render(rsx! {
for message in emails_sent.read().iter() {
li {
h3 { "email" }
span {"{message}"}
}
}))
}
}
}
})
Expand Down
4 changes: 2 additions & 2 deletions examples/control_focus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ fn app(cx: Scope) -> Element {
if *running.current() {
loop {
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
if let Some(element) = elements.read().get(focused) {
element.set_focus(true);
if let Some(element) = elements.with(|f| f.get(focused).cloned()) {
_ = element.set_focus(true).await;
} else {
focused = 0;
}
Expand Down
4 changes: 2 additions & 2 deletions examples/crm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ fn ClientList(cx: Scope) -> Element {
Link { to: Route::ClientAdd {}, class: "pure-button pure-button-primary", "Add Client" }
Link { to: Route::Settings {}, class: "pure-button", "Settings" }

clients.read().iter().map(|client| rsx! {
for client in clients.read().iter() {
div {
class: "client",
style: "margin-bottom: 50px",

p { "Name: {client.first_name} {client.last_name}" }
p { "Description: {client.description}" }
}
})
}
})
}

Expand Down
4 changes: 2 additions & 2 deletions examples/drops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ fn app(cx: Scope) -> Element {
}

render! {
(0..count).map(|_| rsx!{
for _ in 0..count {
drop_child {}
})
}
}
}

Expand Down
8 changes: 3 additions & 5 deletions examples/dynamic_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ fn app(cx: Scope) -> Element {
response.respond(Response::new(include_bytes!("./assets/logo.png").to_vec()));
});

cx.render(rsx! {
render! {
div {
img {
src: "/logos/logo.png"
}
img { src: "/logos/logo.png" }
}
})
}
}
8 changes: 3 additions & 5 deletions examples/error_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ fn DemoC(cx: Scope, x: i32) -> Element {

result.throw()?;

cx.render(rsx! {
h1 {
"{x}"
}
})
render! {
h1 { "{x}" }
}
}
6 changes: 3 additions & 3 deletions examples/fermi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ fn ChildWithRef(cx: Scope) -> Element {
cx.render(rsx! {
div {
ul {
names.read().iter().map(|f| rsx!{
li { "hello: {f}" }
})
for name in names.read().iter() {
li { "hello: {name}" }
}
}
button {
onclick: move |_| {
Expand Down
31 changes: 14 additions & 17 deletions examples/file_explorer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,39 +28,36 @@ fn app(cx: Scope) -> Element {
link { href:"https://fonts.googleapis.com/icon?family=Material+Icons", rel:"stylesheet", }
header {
i { class: "material-icons icon-menu", "menu" }
h1 { "Files: ", files.read().current() }
h1 { "Files: ", {files.read().current()} }
span { }
i { class: "material-icons", onclick: move |_| files.write().go_up(), "logout" }
}
main {
files.read().path_names.iter().enumerate().map(|(dir_id, path)| {
{files.read().path_names.iter().enumerate().map(|(dir_id, path)| {
let path_end = path.split('/').last().unwrap_or(path.as_str());
let icon_type = if path_end.contains('.') {
"description"
} else {
"folder"
};
rsx! (
div {
class: "folder",
key: "{path}",
i { class: "material-icons",
onclick: move |_| files.write().enter_dir(dir_id),
"{icon_type}"
if path_end.contains('.') {
"description"
} else {
"folder"
}
p { class: "cooltip", "0 folders / 0 files" }
}
h1 { "{path_end}" }
}
)
}),
files.read().err.as_ref().map(|err| {
rsx! (
div {
code { "{err}" }
button { onclick: move |_| files.write().clear_err(), "x" }
}
)
})
})},
if let Some(err) = files.read().err.as_ref() {
div {
code { "{err}" }
button { onclick: move |_| files.write().clear_err(), "x" }
}
}
}
}
})
Expand Down
11 changes: 6 additions & 5 deletions examples/framework_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ fn app(cx: Scope) -> Element {
}
table {
tbody {
items.read().iter().enumerate().map(|(id, item)| {
let is_in_danger = if (*selected).map(|s| s == id).unwrap_or(false) {"danger"} else {""};
rsx!(tr { class: "{is_in_danger}",
for (id, item) in items.read().iter().enumerate() {
tr {
class: if (*selected).map(|s| s == id).unwrap_or(false) { "danger" },
td { class:"col-md-1" }
td { class:"col-md-1", "{item.key}" }
td { class:"col-md-1", onclick: move |_| selected.set(Some(id)),
Expand All @@ -80,8 +80,9 @@ fn app(cx: Scope) -> Element {
}
}
td { class: "col-md-6" }
})
})
}
}

}
}
span { class: "preloadicon glyphicon glyphicon-remove", aria_hidden: "true" }
Expand Down
14 changes: 6 additions & 8 deletions examples/generic_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ fn main() {
}

fn app(cx: Scope) -> Element {
cx.render(rsx! { generic_child {
data: 0i32
} })
render! {
generic_child { data: 0 }
}
}

#[derive(PartialEq, Props)]
Expand All @@ -18,9 +18,7 @@ struct GenericChildProps<T: Display + PartialEq> {
}

fn generic_child<T: Display + PartialEq>(cx: Scope<GenericChildProps<T>>) -> Element {
let data = &cx.props.data;

cx.render(rsx! { div {
"{data}"
} })
render! {
div { "{&cx.props.data}" }
}
}
4 changes: 2 additions & 2 deletions examples/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ fn main() {
}

fn app(cx: Scope) -> Element {
cx.render(rsx! (
render! {
div { "Hello, world!" }
))
}
}
6 changes: 3 additions & 3 deletions examples/inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const FIELDS: &[(&str, &str)] = &[
fn app(cx: Scope) -> Element {
cx.render(rsx! {
div { margin_left: "30px",
select_example(cx),
{select_example(cx)},
div {
// handling inputs on divs will catch all input events below
// so the value of our input event will be either huey, dewey, louie, or true/false (because of the checkboxe)
Expand Down Expand Up @@ -114,7 +114,7 @@ fn app(cx: Scope) -> Element {
}
}

FIELDS.iter().map(|(field, value)| rsx! {
for (field, value) in FIELDS.iter() {
div {
input {
id: "{field}",
Expand All @@ -131,7 +131,7 @@ fn app(cx: Scope) -> Element {
}
br {}
}
})
}
}
})
}
Expand Down
10 changes: 5 additions & 5 deletions examples/openid_connect_demo/src/views/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub fn LogOut(cx: Scope<ClientProps>) -> Element {
}
Err(error) => {
rsx! {
div { format!{"Failed to load disconnection url: {:?}", error} }
div { "Failed to load disconnection url: {error:?}" }
}
}
},
Expand Down Expand Up @@ -143,9 +143,9 @@ pub fn LoadClient(cx: Scope) -> Element {
}
}
Err(error) => {
log::info! {"Failed to load client: {:?}", error};
rsx! {
div { format!{"Failed to load client: {:?}", error} }
log::info!{"Failed to load client: {:?}", error},
div { "Failed to load client: {error:?}" }
Outlet::<Route> {}
}
}
Expand Down Expand Up @@ -184,7 +184,7 @@ pub fn AuthHeader(cx: Scope) -> Element {
Ok(email) => {
rsx! {
div {
div { email }
div { {email} }
LogOut { client_id: client_props.client_id, client: client_props.client }
Outlet::<Route> {}
}
Expand All @@ -207,7 +207,7 @@ pub fn AuthHeader(cx: Scope) -> Element {
log::info!("Other issue with token");
rsx! {
div {
div { error.to_string() }
div { "{error}" }
Outlet::<Route> {}
}
}
Expand Down
7 changes: 5 additions & 2 deletions examples/openid_connect_demo/src/views/not_found.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ use dioxus::prelude::*;

#[component]
pub fn NotFound(cx: Scope, route: Vec<String>) -> Element {
let routes = route.join("");
render! {rsx! {div{routes}}}
render! {
div{
{route.join("")}
}
}
}
12 changes: 7 additions & 5 deletions examples/pattern_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,17 @@ fn main() {
dioxus_desktop::launch_cfg(app, cfg);
}

const STYLE: &str = include_str!("./assets/calculator.css");

fn app(cx: Scope) -> Element {
let state = use_ref(cx, Calculator::new);

cx.render(rsx! {
style { include_str!("./assets/calculator.css") }
style { {STYLE} }
div { id: "wrapper",
div { class: "app",
div { class: "calculator", onkeypress: move |evt| state.write().handle_keydown(evt),
div { class: "calculator-display", state.read().formatted_display() }
div { class: "calculator-display", {state.read().formatted_display()} }
div { class: "calculator-keypad",
div { class: "input-keys",
div { class: "function-keys",
Expand Down Expand Up @@ -74,14 +76,14 @@ fn app(cx: Scope) -> Element {
onclick: move |_| state.write().input_dot(),
"●"
}
(1..10).map(move |k| rsx!{
for k in 1..10 {
CalculatorKey {
key: "{k}",
name: "key-{k}",
onclick: move |_| state.write().input_digit(k),
"{k}"
}
})
}
}
}
div { class: "operator-keys",
Expand Down Expand Up @@ -130,7 +132,7 @@ fn CalculatorKey<'a>(cx: Scope<'a, CalculatorKeyProps<'a>>) -> Element {
button {
class: "calculator-key {cx.props.name}",
onclick: move |e| cx.props.onclick.call(e),
&cx.props.children
{&cx.props.children}
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion examples/pattern_reducer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn app(cx: Scope) -> Element {
cx.render(rsx!(
div {
h1 {"Select an option"}
h3 { "The radio is... ", state.is_playing(), "!" }
h3 { "The radio is... ", {state.is_playing()}, "!" }
button { onclick: move |_| state.make_mut().reduce(PlayerAction::Pause),
"Pause"
}
Expand Down
Loading
Loading