dao_ui/components/table/table_row/
table_row.rs1use css_in_rust::Style;
2use yew::{
3 html, Component, ShouldRender, Html, ComponentLink, Properties, Classes, NodeRef,
4 html::{
5 ChildrenRenderer,
6 },
7 virtual_dom::{
8 VComp, VChild,
9 }
10};
11use crate::components::table::TableSize;
12use crate::components::table::table_cell::table_cell::{
13 TableCell,
14 Props as TableCellProps,
15 TableCellVariant,
16};
17
18#[derive(Clone, PartialEq)]
19pub enum Variants {
20 Cell(<TableCell as Component>::Properties),
21}
22
23impl From<TableCellProps> for Variants {
24 fn from(props: TableCellProps) -> Self {
25 Variants::Cell(props)
26 }
27}
28
29#[derive(PartialEq, Clone)]
30pub struct ChildVariant {
31 props: Variants,
32}
33
34pub struct TableRow {
35 style: Style,
36 props: Props,
37}
38
39#[derive(Properties, Clone, PartialEq, Debug)]
40pub struct Props {
41 #[prop_or_default]
42 pub class: String,
43 #[prop_or_default]
44 pub children: ChildrenRenderer<ChildVariant>,
45 #[prop_or_default]
46 pub variant: Option<TableCellVariant>,
47 #[prop_or(TableSize::Small)]
48 pub size: TableSize,
49}
50
51impl<CHILD> From<VChild<CHILD>> for ChildVariant
52where
53 CHILD: Component,
54 CHILD::Properties: Into<Variants>,
55{
56 fn from(vchild: VChild<CHILD>) -> Self {
57 Self {
58 props: vchild.props.into(),
59 }
60 }
61}
62
63impl From<ChildVariant> for Html {
64 fn from(variant: ChildVariant) -> Html {
65 match variant.props {
66 Variants::Cell(props) => VComp::new::<TableCell>(props, NodeRef::default(), None).into(),
67 }
68 }
69}
70
71impl Component for TableRow {
72 type Message = ();
73 type Properties = Props;
74
75 fn create(props: Self::Properties, _link: ComponentLink<Self>) -> Self {
76 let style = Style::create(
77 String::from("table_row"),
78 include_str!("table_row.scss"),
79 )
80 .expect("An error occured while creating the style");
81
82 Self {
83 style,
84 props,
85 }
86 }
87
88 fn update(&mut self, _msg: Self::Message) -> ShouldRender {
89 false
90 }
91
92 fn change(&mut self, props: Self::Properties) -> ShouldRender {
93 if self.props != props {
94 self.props = props;
95
96 true
97 } else {
98 false
99 }
100 }
101
102 fn view(&self) -> Html {
103 html! {
104 <tr
105 class=Classes::from(self.style.clone().to_string())
106 >
107 {
108 self.props.children.iter()
109 .filter(|c| matches!(c.props, Variants::Cell(_)))
110 .map(|mut c| {
111 let Variants::Cell(ref mut props) = c.props;
112 props.variant = self.props.variant.clone();
113 props.size = self.props.size.clone();
114 c
115 })
116 .collect::<Html>()
117 }
118 </tr>
119 }
120 }
121}