use std::collections::HashMap; use leptos::prelude::*; use crate::components::ValueType; #[component] pub fn Checkbox( #[prop()] checked: Signal, #[prop(into)] on_change: Callback<(), ()>, #[prop(into)] value: String, #[prop(default = RwSignal::new(false))] disabled: RwSignal, ) -> impl IntoView { view! { } } #[component] pub fn CheckboxList( #[prop(optional)] selected: RwSignal>, #[prop(into)] data_member: String, #[prop()] data_map: RwSignal>, #[prop()] items: Vec, #[prop(default = RwSignal::new(false))] disabled: RwSignal, #[prop(optional, into)] label: String, ) -> impl IntoView { view! {
{label} {items .into_iter() .map(|item| { let checked_signal = Signal::derive({ let item_name = item.clone(); let data_member = data_member.clone(); move || { data_map.get() // HashMap .get(&data_member) // Option .unwrap_or(&ValueType::List(None)) // ValueType .as_list() // Result>, ValueType> .unwrap_or(Some(vec!())) // Option> .unwrap_or(vec!()) // Vec .contains(&ValueType::String(Some(item_name.clone()))) // bool } }); let on_change = { let item_name = item.clone(); let data_member = data_member.clone(); move || { let result = ValueType::String(Some(item_name.clone())); data_map.update(|map| { // Attempt to get the value type from the hash map. match map.get_mut(&data_member) { Some(value_type) => { // Attempt to get the value as a list. match value_type.as_list().unwrap_or(None) { Some(mut list) => { // Attempt to find the selected value in the selected list. // If it's present, remove it, or vice versa. match list.iter().position(|val| *val == result) { Some(index) => { list.remove(index); }, None => { list.push(result); } }; // Update the existing value_type entry in the hash map. *value_type = ValueType::List(Some(list)); }, // Insert a new value_type entry. None => { *value_type = ValueType::List(Some(vec![result])); } }; }, // Insert a new value_type entry. None => { map.insert(data_member.clone(), ValueType::List(Some(vec![result]))); } }; }); } }; view! { } }) .collect::>()}
} }