use derive_where::derive_where;
use toasty::stmt::List;
use topcoat::{context::Cx, view::*};
use super::{
Column, ColumnBase, ColumnWidth,
cell::{TextCell, badges_cell},
};
use crate::{Lens, values::FormScalar};
#[derive_where(Clone, Debug)]
pub struct ListColumn<M, T> {
lens: Lens<M, Vec<T>, List<T>>,
base: ColumnBase,
}
impl<M, T> ListColumn<M, T>
where
M: toasty::schema::Model,
T: FormScalar,
{
pub fn new(lens: Lens<M, Vec<T>, List<T>>) -> Self {
Self {
base: ColumnBase::new(lens.path(), ColumnWidth::Grow),
lens,
}
}
base_builders!();
}
impl<M, T> Column<M> for ListColumn<M, T>
where
M: toasty::schema::Model + Send + Sync + 'static,
T: FormScalar + Send + Sync + 'static,
{
base_column_methods!();
fn text(&self, cx: &Cx, row: &M) -> String {
self.lens
.read(row)
.iter()
.map(|value| value.to_label().localize(cx))
.collect::<Vec<_>>()
.join(", ")
}
fn cell<'a>(&self, cx: &'a Cx, row: &M) -> BoxView<'a> {
let badges = self
.lens
.read(row)
.iter()
.map(|value| {
let color = value.color();
TextCell {
text: value.to_label().localize(cx),
color,
badge: true,
icon: value.icon().map(|icon| (icon, color)),
..TextCell::default()
}
})
.collect();
badges_cell(cx, badges)
}
}
impl<M, T> crate::column::ColumnInternals<M> for ListColumn<M, T>
where
M: toasty::schema::Model + Send + Sync + 'static,
T: FormScalar + Send + Sync + 'static,
{
base_internals!(binding);
}