Skip to content

Commit e73462b

Browse files
committed
colored buttons
1 parent 6ac51ad commit e73462b

2 files changed

Lines changed: 31 additions & 17 deletions

File tree

src/button.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,46 @@
11
use yew::prelude::*;
22

33
pub struct Button {
4-
action: ButtonAction
4+
action: Action,
55
}
66

77

88
impl Button {
99
fn content(&self) -> Html {
1010
match self.action {
11-
ButtonAction::Add(n) => html! {format!("{}", n)},
12-
ButtonAction::Reset => html!{
11+
Action::Add(n) => html! {format!("{}", n)},
12+
Action::Reset => html!{
1313
<sl-icon name="x-lg" />
1414
},
15-
ButtonAction::Delete => html!{
15+
Action::Delete => html!{
1616
<sl-icon name="chevron-left" />
1717
},
1818
}
1919
}
20+
21+
fn variant(&self) -> AttrValue {
22+
match self.action {
23+
Action::Add(_) => AttrValue::from("default"),
24+
Action::Reset => AttrValue::from("danger"),
25+
Action::Delete => AttrValue::from("warning"),
26+
}
27+
}
2028
}
2129

2230
#[derive(Properties, PartialEq)]
2331
pub struct Props {
24-
pub action: ButtonAction,
25-
pub onclick: Callback<ButtonAction>
32+
pub action: Action,
33+
pub onclick: Callback<Action>,
2634
}
2735

2836
#[derive(PartialEq, Clone)]
29-
pub enum ButtonAction {
37+
pub enum Action {
3038
Add(u8),
3139
Reset,
3240
Delete,
3341
}
3442

43+
3544
impl Component for Button {
3645
type Message = ();
3746
type Properties = Props;
@@ -47,10 +56,15 @@ impl Component for Button {
4756
fn view(&self, ctx: &Context<Self>) -> Html {
4857

4958
let action = self.action.clone();
59+
let variant = self.variant();
5060
let onclick = ctx.props().onclick.reform(move |_| action.clone());
61+
let outline = match self.action {
62+
Action::Reset | Action::Delete => Some("outline"),
63+
_ => None
64+
};
5165

5266
html! {
53-
<sl-button {onclick}>{self.content()}</sl-button>
67+
<sl-button {onclick} {variant} {outline}>{self.content()}</sl-button>
5468
}
5569
}
5670
}

src/calculator.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::ops::Range;
22
use yew::prelude::*;
3-
use crate::button::{Button, ButtonAction};
3+
use crate::button::{Button, Action};
44
const WEBSITE_URL: &str = "https://vanawy.dev";
55

66
pub struct Calculator {
@@ -11,7 +11,7 @@ pub struct Calculator {
1111
}
1212

1313
pub enum CalculatorMsg {
14-
ButtonClicked(ButtonAction),
14+
ButtonClicked(Action),
1515
}
1616

1717
impl Calculator {
@@ -39,13 +39,13 @@ impl Component for Calculator {
3939
match msg {
4040
CalculatorMsg::ButtonClicked(action) => {
4141
match action {
42-
ButtonAction::Add(n) => {
42+
Action::Add(n) => {
4343
self.scores.push(n as u64);
4444
},
45-
ButtonAction::Reset => {
45+
Action::Reset => {
4646
self.scores = Vec::new();
4747
},
48-
ButtonAction::Delete => {
48+
Action::Delete => {
4949
self.scores.pop();
5050
},
5151
};
@@ -72,10 +72,10 @@ impl Component for Calculator {
7272

7373
let mut actions = Vec::new();
7474

75-
Range::from(1..10).for_each(|i| actions.push(ButtonAction::Add(i)));
76-
actions.push(ButtonAction::Reset);
77-
actions.push(ButtonAction::Add(10));
78-
actions.push(ButtonAction::Delete);
75+
Range::from(1..10).for_each(|i| actions.push(Action::Add(i)));
76+
actions.push(Action::Reset);
77+
actions.push(Action::Add(10));
78+
actions.push(Action::Delete);
7979

8080
let buttons = actions.iter().map(|action| {
8181
html! {

0 commit comments

Comments
 (0)