1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
use crate::Icon; use yew::prelude::*; #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub enum Variant { None, Primary, Secondary, Tertiary, Warning, Danger, Link, InlineLink, Control, Plain, } impl Variant { pub fn as_classes(&self) -> Vec<&str> { match self { Variant::None => vec![], Variant::Primary => vec!["pf-m-primary"], Variant::Secondary => vec!["pf-m-secondary"], Variant::Tertiary => vec!["pf-m-tertiary"], Variant::Warning => vec!["pf-m-warning"], Variant::Danger => vec!["pf-m-danger"], Variant::Link => vec!["pf-m-link"], Variant::InlineLink => vec!["pf-m-link", "pf-m-inline"], Variant::Control => vec!["pf-m-control"], Variant::Plain => vec!["pf-m-plain"], } } } impl Default for Variant { fn default() -> Self { Variant::None } } #[derive(Clone, Copy, Eq, PartialEq, Debug)] pub enum Align { Start, End, } impl Default for Align { fn default() -> Self { Align::Start } } #[derive(Clone, PartialEq, Properties)] pub struct Props { #[prop_or_default] pub id: String, #[prop_or_default] pub label: String, #[prop_or_default] pub onclick: Callback<yew::MouseEvent>, #[prop_or_default] pub variant: Variant, #[prop_or_default] pub icon: Option<Icon>, #[prop_or_default] pub align: Align, #[prop_or_default] pub aria_label: Option<String>, #[prop_or("button".into())] pub r#type: String, #[prop_or_default] pub expanded: bool, #[prop_or_default] pub children: Children, } pub struct Button { props: Props, } impl Component for Button { type Message = (); type Properties = Props; fn create(props: Self::Properties, _link: ComponentLink<Self>) -> Self { Self { props } } fn update(&mut self, _msg: Self::Message) -> bool { true } fn change(&mut self, props: Self::Properties) -> bool { if self.props != props { self.props = props; true } else { false } } fn view(&self) -> Html { let mut classes = Classes::from("pf-c-button"); classes = classes.extend(self.props.variant.as_classes()); if self.props.expanded { classes.push("pf-m-expanded"); } return html! { <button id=&self.props.id class=classes type=self.props.r#type onclick=&self.props.onclick> { self.label() } { for self.props.children.iter() } </button> }; } } impl Button { fn icon(&self) -> Html { let mut classes = Classes::from("pf-c-button__icon"); match self.props.align { Align::Start => classes.push("pf-m-start"), Align::End => classes.push("pf-m-end"), } match self.props.icon { Some(i) => html! { <span class=classes> { i } </span> }, None => html! {}, } } fn label(&self) -> Vec<Html> { let label = self.props.label.clone().into(); match self.props.align { Align::Start => vec![self.icon(), label], Align::End => vec![label, self.icon()], } } }