use gpui::{div, prelude::*, AnyElement, SharedString, StyleRefinement};
use crate::sx::{Sx, SxStyled};
use crate::{component, styles, styles::ApplyStyleOverrides};
styles! {
TABLE = {
table: { display: flex, direction: column, width: full, text: sm },
header: { display: flex, direction: column, border_bottom: 1, border_color: border },
body: { display: flex, direction: column },
footer: {
display: flex,
direction: column,
border_top: 1,
border_color: border,
background: muted/50,
font: medium,
},
row: {
display: flex,
align: center,
width: full,
border_bottom: 1,
border_color: border,
},
selected: { background: muted },
head: {
min_width: 0,
height: 10,
padding_x: 2,
display: flex,
align: center,
font: medium,
color: foreground,
whitespace: nowrap,
},
cell: { min_width: 0, padding: 2, whitespace: nowrap, overflow: hidden },
caption: { margin_top: 4, text_align: center, text: sm, color: muted_foreground },
}
}
#[component]
pub fn Table(
#[children] children: Vec<AnyElement>,
#[style] style_overrides: StyleRefinement,
#[sx] sx: Sx,
) -> impl IntoElement {
div()
.sx((&TABLE.table, &sx))
.children(children)
.apply_style_overrides(&style_overrides)
}
#[component]
pub fn TableHeader(#[children] children: Vec<AnyElement>, #[sx] sx: Sx) -> impl IntoElement {
div().sx((&TABLE.header, &sx)).children(children)
}
#[component]
pub fn TableBody(#[children] children: Vec<AnyElement>, #[sx] sx: Sx) -> impl IntoElement {
div().sx((&TABLE.body, &sx)).children(children)
}
#[component]
pub fn TableFooter(#[children] children: Vec<AnyElement>, #[sx] sx: Sx) -> impl IntoElement {
div().sx((&TABLE.footer, &sx)).children(children)
}
#[component]
pub fn TableRow(
#[prop(optional)] selected: bool,
#[children] children: Vec<AnyElement>,
#[style] style_overrides: StyleRefinement,
#[sx] sx: Sx,
) -> impl IntoElement {
div()
.sx((&TABLE.row, selected.then_some(&TABLE.selected), &sx))
.children(children)
.apply_style_overrides(&style_overrides)
}
#[component]
pub fn TableHead(
text: SharedString,
#[style] style_overrides: StyleRefinement,
#[sx] sx: Sx,
) -> impl IntoElement {
div()
.map(|head| column_sizing(head, &style_overrides))
.sx((&TABLE.head, &sx))
.child(crate::components::bidi_text::text(text))
.apply_style_overrides(&style_overrides)
}
#[component]
pub fn TableCell(
#[children] children: Vec<AnyElement>,
#[style] style_overrides: StyleRefinement,
#[sx] sx: Sx,
) -> impl IntoElement {
div()
.map(|cell| column_sizing(cell, &style_overrides))
.sx((&TABLE.cell, &sx))
.children(children)
.apply_style_overrides(&style_overrides)
}
#[component]
pub fn TableCaption(text: SharedString, #[sx] sx: Sx) -> impl IntoElement {
div()
.sx((&TABLE.caption, &sx))
.child(crate::components::bidi_text::text(text))
}
fn column_sizing(cell: gpui::Div, style_overrides: &StyleRefinement) -> gpui::Div {
if style_overrides.size.width.is_some() {
cell.flex_none()
} else {
cell.flex_1()
}
}