fix formatted strings in components

This commit is contained in:
Evan Almloff committed 2024-01-16 12:47:23 -06:00
1 parent 407a82588a
commit 90d0164b9e
8 files changed
+41 -59

No files matched your search

+12 -44
View File
@@ -44,38 +44,22 @@ fn app() -> Element {
style { {STYLE} } style { {STYLE} }
div { id: "wrapper", div { id: "wrapper",
div { class: "app", div { class: "app",
div { class: "calculator", onkeypress: move |evt| state.write().handle_keydown(evt), 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: "calculator-keypad",
div { class: "input-keys", div { class: "input-keys",
div { class: "function-keys", div { class: "function-keys",
CalculatorKey { CalculatorKey { name: "key-clear", onclick: move |_| state.write().clear_display(),
name: "key-clear",
onclick: move |_| state.write().clear_display(),
if state.read().display_value == "0" { "C" } else { "AC" } if state.read().display_value == "0" { "C" } else { "AC" }
} }
CalculatorKey { CalculatorKey { name: "key-sign", onclick: move |_| state.write().toggle_sign(), "±" }
name: "key-sign", CalculatorKey { name: "key-percent", onclick: move |_| state.write().toggle_percent(), "%" }
onclick: move |_| state.write().toggle_sign(),
"±"
}
CalculatorKey {
name: "key-percent",
onclick: move |_| state.write().toggle_percent(),
"%"
}
} }
div { class: "digit-keys", div { class: "digit-keys",
CalculatorKey { CalculatorKey { name: "key-0", onclick: move |_| state.write().input_digit(0), "0" }
name: "key-0", CalculatorKey { name: "key-dot", onclick: move |_| state.write().input_dot(), "●" }
onclick: move |_| state.write().input_digit(0),
"0"
}
CalculatorKey {
name: "key-dot",
onclick: move |_| state.write().input_dot(),
"●"
}
for k in 1..10 { for k in 1..10 {
CalculatorKey { CalculatorKey {
key: "{k}", key: "{k}",
@@ -102,16 +86,8 @@ fn app() -> Element {
onclick: move |_| state.write().set_operator(Operator::Sub), onclick: move |_| state.write().set_operator(Operator::Sub),
"−" "−"
} }
CalculatorKey { CalculatorKey { name: "key-add", onclick: move |_| state.write().set_operator(Operator::Add), "+" }
name: "key-add", CalculatorKey { name: "key-equals", onclick: move |_| state.write().perform_operation(), "=" }
onclick: move |_| state.write().set_operator(Operator::Add),
"+"
}
CalculatorKey {
name: "key-equals",
onclick: move |_| state.write().perform_operation(),
"="
}
} }
} }
} }
@@ -121,17 +97,9 @@ fn app() -> Element {
} }
#[component] #[component]
fn CalculatorKey( fn CalculatorKey(name: String, onclick: EventHandler<MouseEvent>, children: Element) -> Element {
#[props(into)] name: String,
onclick: EventHandler<MouseEvent>,
children: Element,
) -> Element {
render! { render! {
button { button { class: "calculator-key {name}", onclick: move |e| onclick.call(e), {&children} }
class: "calculator-key {name}",
onclick: move |e| onclick.call(e),
{&children}
}
} }
} }
+1 -4
View File
@@ -22,10 +22,7 @@ fn app() -> Element {
render! { render! {
div { div {
h1 { "Current count: {count}" } h1 { "Current count: {count}" }
button { button { onclick: move |_| count.set(0), "Reset the count" }
onclick: move |_| count.set(0),
"Reset the count"
}
} }
} }
} }
+1 -1
View File
@@ -120,7 +120,7 @@ pub fn TodoEntry(mut todos: Signal<HashMap<u32, TodoItem>>, id: u32) -> Element
let contents = use_selector(move || todos.read().get(&id).unwrap().contents.clone()); let contents = use_selector(move || todos.read().get(&id).unwrap().contents.clone());
render! { render! {
li { class: if checked() { "completed" }, class: if is_editing() { "editing"}, li { class: if checked() { "completed" }, class: if is_editing() { "editing" },
div { class: "view", div { class: "view",
input { input {
class: "toggle", class: "toggle",
@@ -261,11 +261,8 @@ fn get_function(component_body: &ComponentBody) -> ItemFn {
let mut pat = pat.clone(); let mut pat = pat.clone();
// rip off mutability, but still write it out eventually // rip off mutability, but still write it out eventually
match pat.as_mut() { if let Pat::Ident(ref mut pat_ident) = pat.as_mut() {
Pat::Ident(ref mut pat_ident) => { pat_ident.mutability = None;
pat_ident.mutability = None;
}
_ => {}
} }
Some(quote!(mut #pat)) Some(quote!(mut #pat))
+21 -1
View File
@@ -173,7 +173,7 @@ mod field_info {
use quote::quote; use quote::quote;
use syn::spanned::Spanned; use syn::spanned::Spanned;
use syn::{parse::Error, punctuated::Punctuated}; use syn::{parse::Error, punctuated::Punctuated};
use syn::{Expr, Path}; use syn::{parse_quote, Expr, Path};
use super::util::{ use super::util::{
expr_to_single_string, ident_to_type, path_to_single_string, strip_raw_ident_prefix, expr_to_single_string, ident_to_type, path_to_single_string, strip_raw_ident_prefix,
@@ -204,6 +204,15 @@ mod field_info {
); );
} }
// String fields automatically use impl Display
if field.ty == parse_quote!(::std::string::String)
|| field.ty == parse_quote!(std::string::String)
|| field.ty == parse_quote!(string::String)
|| field.ty == parse_quote!(String)
{
builder_attr.from_displayable = true;
}
// extended field is automatically empty // extended field is automatically empty
if !builder_attr.extends.is_empty() { if !builder_attr.extends.is_empty() {
builder_attr.default = Some( builder_attr.default = Some(
@@ -263,6 +272,7 @@ mod field_info {
pub doc: Option<syn::Expr>, pub doc: Option<syn::Expr>,
pub skip: bool, pub skip: bool,
pub auto_into: bool, pub auto_into: bool,
pub from_displayable: bool,
pub strip_option: bool, pub strip_option: bool,
pub ignore_option: bool, pub ignore_option: bool,
pub extends: Vec<Path>, pub extends: Vec<Path>,
@@ -412,6 +422,7 @@ mod field_info {
handle_fields!( handle_fields!(
"skip", skip, "skipped"; "skip", skip, "skipped";
"into", auto_into, "calling into() on the argument"; "into", auto_into, "calling into() on the argument";
"displayable", from_displayable, "calling to_string() on the argument";
"strip_option", strip_option, "putting the argument in Some(...)"; "strip_option", strip_option, "putting the argument in Some(...)";
) )
} }
@@ -443,6 +454,10 @@ mod field_info {
self.auto_into = false; self.auto_into = false;
Ok(()) Ok(())
} }
"displayable" => {
self.from_displayable = false;
Ok(())
}
"optional" => { "optional" => {
self.strip_option = false; self.strip_option = false;
self.ignore_option = true; self.ignore_option = true;
@@ -954,6 +969,11 @@ Finally, call `.build()` to create the instance of `{name}`.
quote!(impl ::core::convert::Into<#arg_type>), quote!(impl ::core::convert::Into<#arg_type>),
quote!(#field_name.into()), quote!(#field_name.into()),
) )
} else if field.builder_attr.from_displayable {
(
quote!(impl ::core::fmt::Display),
quote!(#field_name.to_string()),
)
} else { } else {
(quote!(#arg_type), quote!(#field_name)) (quote!(#arg_type), quote!(#field_name))
}; };
+2 -2
View File
@@ -211,7 +211,7 @@ impl ToTokens for ElementAttrValue {
fn to_tokens(&self, tokens: &mut TokenStream2) { fn to_tokens(&self, tokens: &mut TokenStream2) {
match self { match self {
ElementAttrValue::Shorthand(i) => tokens.append_all(quote! { #i }), ElementAttrValue::Shorthand(i) => tokens.append_all(quote! { #i }),
ElementAttrValue::AttrLiteral(lit) => tokens.append_all(quote! { #lit }), ElementAttrValue::AttrLiteral(lit) => tokens.append_all(quote! { #lit.to_string() }),
ElementAttrValue::AttrOptionalExpr { condition, value } => { ElementAttrValue::AttrOptionalExpr { condition, value } => {
tokens.append_all(quote! { if #condition { Some(#value) } else { None } }) tokens.append_all(quote! { if #condition { Some(#value) } else { None } })
} }
@@ -224,7 +224,7 @@ impl ToTokens for ElementAttrValue {
impl ElementAttrValue { impl ElementAttrValue {
fn to_str_expr(&self) -> Option<TokenStream2> { fn to_str_expr(&self) -> Option<TokenStream2> {
match self { match self {
ElementAttrValue::AttrLiteral(lit) => Some(quote!(#lit)), ElementAttrValue::AttrLiteral(lit) => Some(quote!(#lit.to_string())),
ElementAttrValue::AttrOptionalExpr { value, .. } => value.to_str_expr(), ElementAttrValue::AttrOptionalExpr { value, .. } => value.to_str_expr(),
ElementAttrValue::AttrExpr(expr) => Some(quote!(#expr.to_string())), ElementAttrValue::AttrExpr(expr) => Some(quote!(#expr.to_string())),
_ => None, _ => None,
+1 -1
View File
@@ -242,7 +242,7 @@ impl ToTokens for IfmtInput {
.map(|ident| quote!(#ident = #ident)); .map(|ident| quote!(#ident = #ident));
quote! { quote! {
format!( ::std::format_args!(
#format_literal #format_literal
#(, #positional_args)* #(, #positional_args)*
#(, #named_args)* #(, #named_args)*
+1 -1
View File
@@ -144,7 +144,7 @@ impl ToTokens for BodyNode {
} }
BodyNode::Component(comp) => comp.to_tokens(tokens), BodyNode::Component(comp) => comp.to_tokens(tokens),
BodyNode::Text(txt) => tokens.append_all(quote! { BodyNode::Text(txt) => tokens.append_all(quote! {
dioxus_core::DynamicNode::Text(dioxus_core::VText::new(#txt)) dioxus_core::DynamicNode::Text(dioxus_core::VText::new(#txt.to_string()))
}), }),
BodyNode::RawExpr(exp) => tokens.append_all(quote! { BodyNode::RawExpr(exp) => tokens.append_all(quote! {
{ {