mcai_workflow/components/
toggle_button.rs

1use css_in_rust_next::Style;
2use web_sys::MouseEvent;
3use yew::{classes, html, Callback, Component, Context, Html, Properties};
4
5#[derive(PartialEq, Properties)]
6pub struct ToggleButtonProperties {
7  pub label: String,
8  pub icon: Option<Html>,
9  #[prop_or_default]
10  pub checked: bool,
11  #[prop_or_default]
12  pub disabled: bool,
13  pub onclick: Callback<MouseEvent>,
14  #[prop_or_default]
15  pub class: Option<String>,
16}
17
18pub struct ToggleButton {
19  style: Style,
20}
21
22impl Component for ToggleButton {
23  type Message = ();
24  type Properties = ToggleButtonProperties;
25
26  fn create(_ctx: &Context<Self>) -> Self {
27    let style = Style::create("Component", include_str!("toggle_button.css")).unwrap();
28    Self { style }
29  }
30
31  fn view(&self, ctx: &Context<Self>) -> Html {
32    let class_names = classes!(
33      self.style.to_string(),
34      ctx.props().class.clone().unwrap_or_default()
35    );
36
37    html!(
38      <div class={class_names} onclick={ctx.props().onclick.clone()}>
39        <label class="switch">
40          <input type="checkbox" checked={ctx.props().checked}/>
41          <span class="slider"></span>
42        </label>
43        <span class="label">{&ctx.props().label}</span>
44      </div>
45    )
46  }
47}