feat: re-unify set_state
This commit is contained in:
1 parent
3928856a9b
commit
6c3f5195f4
16 files changed
+199
-136
No files matched your search
+24
-32
@@ -18,20 +18,16 @@ fn main() {
|
||||
}
|
||||
|
||||
fn app(cx: Scope) -> Element {
|
||||
let (display_value, set_display_value) = use_state(&cx, || String::from("0"));
|
||||
let val = use_state(&cx, || String::from("0"));
|
||||
|
||||
let input_digit = move |num: u8| {
|
||||
if display_value == "0" {
|
||||
set_display_value(String::new());
|
||||
if val.get() == "0" {
|
||||
val.set(String::new());
|
||||
}
|
||||
set_display_value
|
||||
.make_mut()
|
||||
.push_str(num.to_string().as_str());
|
||||
val.make_mut().push_str(num.to_string().as_str());
|
||||
};
|
||||
|
||||
let input_operator = move |key: &str| {
|
||||
set_display_value.make_mut().push_str(key);
|
||||
};
|
||||
let input_operator = move |key: &str| val.make_mut().push_str(key);
|
||||
|
||||
cx.render(rsx!(
|
||||
style { [include_str!("./assets/calculator.css")] }
|
||||
@@ -54,34 +50,34 @@ fn app(cx: Scope) -> Element {
|
||||
KeyCode::Num8 => input_digit(8),
|
||||
KeyCode::Num9 => input_digit(9),
|
||||
KeyCode::Backspace => {
|
||||
if !display_value.len() != 0 {
|
||||
set_display_value.make_mut().pop();
|
||||
if !val.len() != 0 {
|
||||
val.make_mut().pop();
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
div { class: "calculator-display", [display_value.to_string()] }
|
||||
div { class: "calculator-display", [val.to_string()] }
|
||||
div { class: "calculator-keypad",
|
||||
div { class: "input-keys",
|
||||
div { class: "function-keys",
|
||||
button {
|
||||
class: "calculator-key key-clear",
|
||||
onclick: move |_| {
|
||||
set_display_value(String::new());
|
||||
if !display_value.is_empty(){
|
||||
set_display_value("0".into());
|
||||
val.set(String::new());
|
||||
if !val.is_empty(){
|
||||
val.set("0".into());
|
||||
}
|
||||
},
|
||||
[if display_value.is_empty() { "C" } else { "AC" }]
|
||||
[if val.is_empty() { "C" } else { "AC" }]
|
||||
}
|
||||
button {
|
||||
class: "calculator-key key-sign",
|
||||
onclick: move |_| {
|
||||
let temp = calc_val(display_value.clone());
|
||||
let temp = calc_val(val.as_str());
|
||||
if temp > 0.0 {
|
||||
set_display_value(format!("-{}", temp));
|
||||
val.set(format!("-{}", temp));
|
||||
} else {
|
||||
set_display_value(format!("{}", temp.abs()));
|
||||
val.set(format!("{}", temp.abs()));
|
||||
}
|
||||
},
|
||||
"±"
|
||||
@@ -89,8 +85,8 @@ fn app(cx: Scope) -> Element {
|
||||
button {
|
||||
class: "calculator-key key-percent",
|
||||
onclick: move |_| {
|
||||
set_display_value(
|
||||
format!("{}", calc_val(display_value.clone()) / 100.0)
|
||||
val.set(
|
||||
format!("{}", calc_val(val.as_str()) / 100.0)
|
||||
);
|
||||
},
|
||||
"%"
|
||||
@@ -100,7 +96,7 @@ fn app(cx: Scope) -> Element {
|
||||
button { class: "calculator-key key-0", onclick: move |_| input_digit(0),
|
||||
"0"
|
||||
}
|
||||
button { class: "calculator-key key-dot", onclick: move |_| set_display_value.make_mut().push('.'),
|
||||
button { class: "calculator-key key-dot", onclick: move |_| val.make_mut().push('.'),
|
||||
"●"
|
||||
}
|
||||
(1..10).map(|k| rsx!{
|
||||
@@ -114,25 +110,21 @@ fn app(cx: Scope) -> Element {
|
||||
}
|
||||
}
|
||||
div { class: "operator-keys",
|
||||
button { class: "calculator-key key-divide",
|
||||
onclick: move |_| input_operator("/"),
|
||||
button { class: "calculator-key key-divide", onclick: move |_| input_operator("/"),
|
||||
"÷"
|
||||
}
|
||||
button { class: "calculator-key key-multiply",
|
||||
onclick: move |_| input_operator("*"),
|
||||
button { class: "calculator-key key-multiply", onclick: move |_| input_operator("*"),
|
||||
"×"
|
||||
}
|
||||
button { class: "calculator-key key-subtract",
|
||||
onclick: move |_| input_operator("-"),
|
||||
button { class: "calculator-key key-subtract", onclick: move |_| input_operator("-"),
|
||||
"−"
|
||||
}
|
||||
button { class: "calculator-key key-add",
|
||||
onclick: move |_| input_operator("+"),
|
||||
button { class: "calculator-key key-add", onclick: move |_| input_operator("+"),
|
||||
"+"
|
||||
}
|
||||
button { class: "calculator-key key-equals",
|
||||
onclick: move |_| {
|
||||
set_display_value(format!("{}", calc_val(display_value.clone())));
|
||||
val.set(format!("{}", calc_val(val.as_str())));
|
||||
},
|
||||
"="
|
||||
}
|
||||
@@ -145,7 +137,7 @@ fn app(cx: Scope) -> Element {
|
||||
))
|
||||
}
|
||||
|
||||
fn calc_val(val: String) -> f64 {
|
||||
fn calc_val(val: &str) -> f64 {
|
||||
let mut temp = String::new();
|
||||
let mut operation = "+".to_string();
|
||||
|
||||
|
||||
Reference in new issue
Block a user