@tonycodes-ui/mortar 0.28.1 → 0.29.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +19 -2
- package/bin/mortar.mjs +16 -4
- package/catalog.json +630 -355
- package/cli/audit.mjs +24 -9
- package/cli/data.mjs +5 -0
- package/cli/packet.mjs +39 -3
- package/cli/render.mjs +315 -0
- package/cli/tools.mjs +24 -2
- package/dist/agent-data.json +1 -1
- package/dist/application.d.ts +2 -33
- package/dist/application.js +2 -56
- package/dist/data-grid.d.ts +2 -0
- package/dist/data-grid.js +4 -0
- package/dist/grid.d.ts +102 -0
- package/dist/grid.js +371 -0
- package/dist/table-core.d.ts +54 -0
- package/dist/table-core.js +59 -0
- package/docs/agents.md +17 -1
- package/docs/data-grid.md +155 -0
- package/package.json +17 -3
- package/skills/mortar/SKILL.md +1 -1
- package/src/application.tsx +15 -114
- package/src/data-grid.ts +13 -0
- package/src/data.css +64 -1
- package/src/foundation.css +3 -1
- package/src/grid.tsx +906 -0
- package/src/table-core.tsx +128 -0
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
// Table building blocks shared by DataTable and DataGrid. Internal: the public
|
|
2
|
+
// types are re-exported from application.tsx and the package entry.
|
|
3
|
+
import { useEffect, useRef, type ReactNode } from "react";
|
|
4
|
+
export type SortDirection = "ascending" | "descending";
|
|
5
|
+
export interface TableSort {
|
|
6
|
+
key: string;
|
|
7
|
+
direction: SortDirection;
|
|
8
|
+
}
|
|
9
|
+
export interface DataTableColumn<T> {
|
|
10
|
+
key: string;
|
|
11
|
+
label: string;
|
|
12
|
+
render: (row: T) => ReactNode;
|
|
13
|
+
/** `end` aligns numbers and amounts to the trailing edge. */
|
|
14
|
+
align?: "start" | "end";
|
|
15
|
+
/** Enable header sorting. Supply sortValue unless row[key] is a string, number or Date. */
|
|
16
|
+
sortable?: boolean;
|
|
17
|
+
sortValue?: (row: T) => string | number | Date | null | undefined;
|
|
18
|
+
/** CSS width, e.g. "12rem" or "30%". */
|
|
19
|
+
width?: string;
|
|
20
|
+
/** Clip long values to one line with an ellipsis. */
|
|
21
|
+
truncate?: boolean;
|
|
22
|
+
/**
|
|
23
|
+
* How long the column stays as the table narrows (its own width, not the
|
|
24
|
+
* screen's): `medium` hides below 480px, `low` below 768px. The first
|
|
25
|
+
* column always shows. Hidden columns leave the accessibility tree too, so
|
|
26
|
+
* keep what people need in visible columns or on the detail page.
|
|
27
|
+
*/
|
|
28
|
+
priority?: "high" | "medium" | "low";
|
|
29
|
+
/**
|
|
30
|
+
* `end` pins the column to the trailing edge while the table scrolls
|
|
31
|
+
* sideways, so a row-actions or status column stays reachable.
|
|
32
|
+
*/
|
|
33
|
+
pin?: "end";
|
|
34
|
+
}
|
|
35
|
+
/** Responsive attributes shared by a column's `col`, header and cells. */
|
|
36
|
+
export function columnLayout<T>(col: DataTableColumn<T>, index: number) {
|
|
37
|
+
return {
|
|
38
|
+
"data-priority":
|
|
39
|
+
index > 0 && col.priority && col.priority !== "high"
|
|
40
|
+
? col.priority
|
|
41
|
+
: undefined,
|
|
42
|
+
"data-pin": col.pin,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
const collator = new Intl.Collator(undefined, {
|
|
46
|
+
numeric: true,
|
|
47
|
+
sensitivity: "base",
|
|
48
|
+
});
|
|
49
|
+
export const isEmpty = (v: unknown) =>
|
|
50
|
+
v === null || v === undefined || v === "";
|
|
51
|
+
export function compareValues(a: unknown, b: unknown) {
|
|
52
|
+
if (isEmpty(a) || isEmpty(b))
|
|
53
|
+
return isEmpty(a) === isEmpty(b) ? 0 : isEmpty(a) ? 1 : -1;
|
|
54
|
+
if (a instanceof Date && b instanceof Date) return a.getTime() - b.getTime();
|
|
55
|
+
if (typeof a === "number" && typeof b === "number") return a - b;
|
|
56
|
+
return collator.compare(String(a), String(b));
|
|
57
|
+
}
|
|
58
|
+
/** Stable sort; empty values always sort last regardless of direction. */
|
|
59
|
+
export function sortRows<T>(
|
|
60
|
+
rows: T[],
|
|
61
|
+
column: Pick<DataTableColumn<T>, "key" | "sortValue">,
|
|
62
|
+
direction: SortDirection,
|
|
63
|
+
) {
|
|
64
|
+
const value =
|
|
65
|
+
column.sortValue ||
|
|
66
|
+
((row: T) => (row as Record<string, unknown>)[column.key] as string);
|
|
67
|
+
const sign = direction === "ascending" ? 1 : -1;
|
|
68
|
+
return rows
|
|
69
|
+
.map((row, index) => ({ row, index, v: value(row) }))
|
|
70
|
+
.sort(
|
|
71
|
+
(a, b) =>
|
|
72
|
+
// Empty values sort last in both directions, so only real values flip.
|
|
73
|
+
(isEmpty(a.v) || isEmpty(b.v) ? 1 : sign) * compareValues(a.v, b.v) ||
|
|
74
|
+
a.index - b.index,
|
|
75
|
+
)
|
|
76
|
+
.map((entry) => entry.row);
|
|
77
|
+
}
|
|
78
|
+
export function SelectAll({
|
|
79
|
+
checked,
|
|
80
|
+
partial,
|
|
81
|
+
onChange,
|
|
82
|
+
}: {
|
|
83
|
+
checked: boolean;
|
|
84
|
+
partial: boolean;
|
|
85
|
+
onChange: () => void;
|
|
86
|
+
}) {
|
|
87
|
+
const ref = useRef<HTMLInputElement>(null);
|
|
88
|
+
useEffect(() => {
|
|
89
|
+
if (ref.current) ref.current.indeterminate = partial;
|
|
90
|
+
}, [partial]);
|
|
91
|
+
return (
|
|
92
|
+
<input
|
|
93
|
+
ref={ref}
|
|
94
|
+
type="checkbox"
|
|
95
|
+
aria-label="Select all rows"
|
|
96
|
+
checked={checked}
|
|
97
|
+
onChange={onChange}
|
|
98
|
+
/>
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
/** Buckets ordered rows by group; groups keep their first-appearance order in the unsorted rows. */
|
|
102
|
+
export function groupRows<T>(
|
|
103
|
+
rows: T[],
|
|
104
|
+
ordered: T[],
|
|
105
|
+
groupBy: (row: T) => string,
|
|
106
|
+
) {
|
|
107
|
+
const groups = new Map<string, T[]>(rows.map((row) => [groupBy(row), []]));
|
|
108
|
+
ordered.forEach((row) => groups.get(groupBy(row))!.push(row));
|
|
109
|
+
return [...groups];
|
|
110
|
+
}
|
|
111
|
+
/** Full-width header row that labels the rows of its tbody. */
|
|
112
|
+
export function TableGroupRow({
|
|
113
|
+
label,
|
|
114
|
+
span,
|
|
115
|
+
}: {
|
|
116
|
+
label: string;
|
|
117
|
+
span: number;
|
|
118
|
+
}) {
|
|
119
|
+
return (
|
|
120
|
+
<tr className="m-table-group">
|
|
121
|
+
<th colSpan={span} scope="rowgroup">
|
|
122
|
+
{label}
|
|
123
|
+
</th>
|
|
124
|
+
</tr>
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
export const columnAlign = (align?: "start" | "end") =>
|
|
128
|
+
align === "end" ? "end" : undefined;
|