11use yew:: prelude:: * ;
22
33pub struct Button {
4- action : ButtonAction
4+ action : Action ,
55}
66
77
88impl 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 ) ]
2331pub 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+
3544impl 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}
0 commit comments