Skip to content

Commit

Permalink
Support for removing table rows.
Browse files Browse the repository at this point in the history
  • Loading branch information
ejoepes committed Oct 7, 2024
1 parent e75defc commit f96eb79
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 35 deletions.
57 changes: 22 additions & 35 deletions src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 7,14 @@ use futures_signals::{
};
use web_sys::HtmlElement;

use crate::{data::Aeat720Record, utils::usize_to_date};
use crate::{
data::Aeat720Record,
utils::{icons::render_svg_delete_square_icon, usize_to_date},
};

pub struct Table {
headers: Vec<&'static str>,
data: MutableVec<Aeat720Record>,
selected_row: Mutable<Option<usize>>,
}

impl Table {
Expand All @@ -28,7 30,6 @@ impl Table {
"Porcentaje",
],
data: aeat720_records,
selected_row: Mutable::new(None),
})
}

Expand All @@ -51,70 52,56 @@ impl Table {
html!("thead", {
.child(
html!("tr", {
.children(Self::render_header_cells(this))
.child(
html!("th", {
.attr("scope", "col")
.style("vertical-align", "bottom")
.style("font-weight", "bold")
.style("background-color", "#ddd")
.text(" ")
.text("Eliminar")
})
)
.children(Self::render_header_cells(this))
})
)
})
}

fn render_row(this: &Arc<Self>, index: usize, data: &Aeat720Record) -> Dom {
let date = usize_to_date(data.first_tx_date)
fn render_row(this: &Arc<Self>, index: usize, record: &Aeat720Record) -> Dom {

Check warning on line 70 in src/table.rs

View workflow job for this annotation

GitHub Actions / Unit Tests on 1.80.1

unused variable: `index`

Check warning on line 70 in src/table.rs

View workflow job for this annotation

GitHub Actions / Unit Tests on 1.80.1

unused variable: `index`

Check warning on line 70 in src/table.rs

View workflow job for this annotation

GitHub Actions / Format & Clippy (1.80.1)

unused variable: `index`
let date = usize_to_date(record.first_tx_date)
.map_or("".to_string(), |d| d.format("%d/%m/%Y").to_string());

html!("tr", {
.style_signal("background-color", this.selected_row.signal().map(
move |row| if row == Some(index) {
"#ddd"
} else {
"#fff"
}
))
.children(&mut [
html!("td" => HtmlElement, {
.style("font-weight", "bold")
.style("background-color", "#ddd")
.text(" ")
.with_node!(_element => {
.event(clone!(this => move |_: events::Click| {
if this.selected_row.get() == Some(index) {
this.selected_row.set(None)
} else {
this.selected_row.set(Some(index))
}
}))
})
html!("td", {
.text(&record.company.name)
}),
html!("td", {
.text(&data.company.name)
.text(&record.company.isin)
}),
html!("td", {
.text(&data.company.isin)
}),
html!("td", {
.text(&data.broker.country_code)
.text(&record.broker.country_code)
}),
html!("td", {
.text(&date)
}),
html!("td", {
.text(&data.value_in_euro.to_string())
.text(&record.value_in_euro.to_string())
}),
html!("td", {
.text(&data.quantity.to_string())
.text(&record.quantity.to_string())
}),
html!("td", {
.text("100%")
}),

html!("td" => HtmlElement, {
.child(render_svg_delete_square_icon("red", "24"))
.with_node!(_element => {
.event(clone!(this, record => move |_: events::Click| {
this.data.lock_mut().retain(|x| x != &record);
}))
})
}),
])
})
}
Expand Down
39 changes: 39 additions & 0 deletions src/utils/icons.rs
Original file line number Diff line number Diff line change
@@ -0,0 1,39 @@
use dominator::{svg, Dom};

pub fn render_svg_delete_square_icon(color: &str, size: &str) -> Dom {
// <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-x-square"><rect x="3" y="3" width="18" height="18" rx="2" ry="2"></rect><line x1="9" y1="9" x2="15" y2="15"></line><line x1="15" y1="9" x2="9" y2="15"></line></svg>
svg!("svg", {
.attr("alt", "facebook icon")
.attr("width", size)
.attr("height", size)
.attr("viewBox", "0 0 24 24")
.attr("fill", "none")
.attr("stroke", color)
.attr("stroke-width", "2")
.attr("stroke-linecap", "round")
.attr("stroke-linejoin", "round")
.children(&mut[
svg!("rect", {
.attr("x", "3")
.attr("y", "3")
.attr("width", "18")
.attr("height", "18")
.attr("rx", "2")
.attr("ry", "2")
}),
svg!("line", {
.attr("x1", "9")
.attr("y1", "9")
.attr("x2", "15")
.attr("y2", "15")
}),
svg!("line", {
.attr("x1", "15")
.attr("y1", "9")
.attr("x2", "9")
.attr("y2", "15")
}),
])

})
}
1 change: 1 addition & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 15,7 @@ use crate::{
};

pub mod decimal;
pub mod icons;
pub mod web;
pub mod zip;

Expand Down

1 comment on commit f96eb79

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.