use super::*;
#[derive(Clone, CustomDebug, Data, Default, New)]
pub struct EuvSwitchProps {
#[get(type(copy))]
pub id: &'static str,
#[get(type(copy))]
pub name: &'static str,
#[get(type(copy))]
pub checked: Signal<bool>,
#[get(type(copy))]
pub label: &'static str,
#[get(type(copy))]
pub disabled: Signal<bool>,
}
#[derive(Clone, Copy, CustomDebug, Data, Default)]
pub struct EuvSwitchState {
#[get(type(copy))]
pub checked: Signal<bool>,
}
impl EuvSwitchState {
pub fn new(checked: Signal<bool>) -> Self {
Self { checked }
}
pub fn toggle(&self) {
let current: bool = self.get_checked().get();
self.get_checked().set(!current);
}
pub fn on_toggle(&self, disabled: Signal<bool>) -> Option<Rc<dyn Fn(Event)>> {
let state: EuvSwitchState = *self;
Some(Rc::new(move |_: Event| {
if disabled.get() {
return;
}
state.toggle();
}))
}
}
pub fn use_euv_switch_state(checked: Signal<bool>) -> EuvSwitchState {
HookContext::use_hook(move || EuvSwitchState::new(checked))
}