use crate::{ components::form::{date_input::DateTimeInput, file_input::JpegFileInput}, infra::{schema::AttributeType, tooltip::Tooltip}, }; use web_sys::Element; use yew::{ function_component, html, use_effect_with_deps, use_node_ref, virtual_dom::AttrValue, Component, Context, Html, Properties, }; #[derive(Properties, PartialEq)] struct AttributeInputProps { name: AttrValue, attribute_type: AttributeType, #[prop_or(None)] value: Option, } #[function_component(AttributeInput)] fn attribute_input(props: &AttributeInputProps) -> Html { let input_type = match props.attribute_type { AttributeType::String => "text", AttributeType::Integer => "number", AttributeType::DateTime => { return html! { } } AttributeType::Jpeg => { return html! { } } }; html! { } } #[derive(Properties, PartialEq)] struct AttributeLabelProps { pub name: String, } #[function_component(AttributeLabel)] fn attribute_label(props: &AttributeLabelProps) -> Html { let tooltip_ref = use_node_ref(); use_effect_with_deps( move |tooltip_ref| { Tooltip::new( tooltip_ref .cast::() .expect("Tooltip element should exist"), ); || {} }, tooltip_ref.clone(), ); html! { } } #[derive(Properties, PartialEq)] pub struct SingleAttributeInputProps { pub name: String, pub attribute_type: AttributeType, #[prop_or(None)] pub value: Option, } #[function_component(SingleAttributeInput)] pub fn single_attribute_input(props: &SingleAttributeInputProps) -> Html { html! {
} } #[derive(Properties, PartialEq)] pub struct ListAttributeInputProps { pub name: String, pub attribute_type: AttributeType, #[prop_or(vec!())] pub values: Vec, } pub enum ListAttributeInputMsg { Remove(usize), Append, } pub struct ListAttributeInput { indices: Vec, next_index: usize, values: Vec, } impl Component for ListAttributeInput { type Message = ListAttributeInputMsg; type Properties = ListAttributeInputProps; fn create(ctx: &Context) -> Self { let values = ctx.props().values.clone(); Self { indices: (0..values.len()).collect(), next_index: values.len(), values, } } fn update(&mut self, _ctx: &Context, msg: Self::Message) -> bool { match msg { ListAttributeInputMsg::Remove(removed) => { self.indices.retain_mut(|x| *x != removed); } ListAttributeInputMsg::Append => { self.indices.push(self.next_index); self.next_index += 1; } }; true } fn changed(&mut self, ctx: &Context) -> bool { if ctx.props().values != self.values { self.values.clone_from(&ctx.props().values); self.indices = (0..self.values.len()).collect(); self.next_index = self.values.len(); } true } fn view(&self, ctx: &Context) -> Html { let props = &ctx.props(); let link = &ctx.link(); html! {
{self.indices.iter().map(|&i| html! {
}).collect::()}
} } }