aboutsummaryrefslogtreecommitdiffstats
path: root/src/server/template/table.ts
blob: 34f27719ba088db3a49ab4ff377a74eab5ecd126 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { c, node } from "./vdom.js";
import { Attribute, caption, table as Table, td, th, thead, tr } from "./html.js";

interface TableAttr extends Attribute {
	header: boolean;
	data: node[][];
	caption?: string;
}

export default function table({ header, data, caption: captio, ...attr }: TableAttr) {
	const capt = captio ? c(caption, {}, captio) : undefined;
	if (header) {
		const [head, ...tbldata] = data;
		return c(Table, attr,
			...tbldata.map(
				row => c(tr, {}, ...row.map(
					key => c(td, {}, key ?? "")
				))
			),
			c(thead, {}, c(tr, {}, ...(head ?? []).map(e => c(th, {}, e ?? "")))),
			...(capt ? [capt] : [])
		);
	} else {
		return c(Table, attr, ...data.map(
			row => c(tr, {}, ...row.map(
				key => c(td, {}, key ?? "")
			))
		),
			...(capt ? [capt] : [])
		);
	}
}