dao_ui/components/table/table_cell/
table_cell.rs1use css_in_rust::Style;
2use yew::{
3 html, Component, ShouldRender, Html, ComponentLink,
4 Properties, Children, Classes,
5};
6use crate::components::table::TableSize;
7use crate::theme::{Theme};
8
9#[derive(Clone, PartialEq, Debug)]
10pub enum TableCellVariant {
11 Head,
12 Body,
13}
14
15#[derive(Clone, PartialEq, Debug)]
16#[allow(dead_code)]
17pub enum TableCellAlign {
18 Right,
19 Left,
20 Center,
21}
22
23pub struct TableCell {
24 style: Style,
25 props: Props,
26}
27
28#[derive(Properties, Clone, PartialEq, Debug)]
29pub struct Props {
30 #[prop_or_default]
31 pub class: String,
32 #[prop_or_default]
33 pub children: Children,
34 #[prop_or(TableCellAlign::Left)]
35 pub align: TableCellAlign,
36 #[prop_or_default]
37 pub variant: Option<TableCellVariant>,
38 #[prop_or(TableSize::Medium)]
39 pub size: TableSize,
40
41}
42
43fn get_align_class(size: &TableCellAlign) -> String {
44 let s = match size {
45 TableCellAlign::Left => { "left" }
46 TableCellAlign::Right => { "right" }
47 TableCellAlign::Center => { "center" }
48 };
49
50 format!("align-{}", s)
51}
52
53fn get_size_class(size: &TableSize) -> String {
54 let s = match size {
55 TableSize::Small => { "small" }
56 TableSize::Medium => { "medium" }
57 };
58
59 format!("size-{}", s)
60}
61
62impl Component for TableCell {
63 type Message = ();
64 type Properties = Props;
65
66 fn create(props: Self::Properties, _link: ComponentLink<Self>) -> Self {
67 let theme = Theme::default();
68 let style = Style::create(
69 String::from("table_cell"),
70 include_str!("table_cell.scss")
71 .replace("$palette_divider", &theme.palette.divider),
72 )
73 .expect("An error occured while creating the style");
74
75 Self {
76 style,
77 props,
78 }
79 }
80
81 fn update(&mut self, _msg: Self::Message) -> ShouldRender {
82 false
83 }
84
85 fn change(&mut self, props: Self::Properties) -> ShouldRender {
86 if self.props != props {
87 self.props = props;
88
89 true
90 } else {
91 false
92 }
93 }
94
95 fn view(&self) -> Html {
96 let class = self.format_classes();
97 let component = if let Some(v) = &self.props.variant {
98 match v {
99 TableCellVariant::Body => { "td" }
100 TableCellVariant::Head => { "th" }
101 }
102 } else {
103 "td"
104 };
105
106 html! {
107 <@{component}
108 class=class
109 >
110 {self.props.children.clone()}
111 </@>
112 }
113 }
114}
115
116impl TableCell {
117 fn format_classes(&self) -> Classes {
118 let mut classes = Classes::from(self.style.clone().to_string());
119
120 classes.push(self.props.class.clone());
121 classes.push(get_size_class(&self.props.size));
122 classes.push(get_align_class(&self.props.align));
123
124 classes
125 }
126}