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