@makoai/app-sdk 2.26.0 → 2.28.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/ui.js CHANGED
@@ -15,6 +15,7 @@ import {
15
15
  claimPopover,
16
16
  closePopovers,
17
17
  COLUMN_WIDTHS,
18
+ columnGroupRuns,
18
19
  columnLayout,
19
20
  compareValues,
20
21
  csvCell,
@@ -29,6 +30,7 @@ import {
29
30
  filterLeavesOf,
30
31
  filterNormalize,
31
32
  filterPickedNode,
33
+ filterSearch,
32
34
  filterText,
33
35
  filterTick,
34
36
  filterToggle,
@@ -56,6 +58,7 @@ export { formatDate, formatTime, toDayKey, toInstant } from "./time.js";
56
58
  export {
57
59
  closePopovers,
58
60
  COLUMN_WIDTHS,
61
+ columnGroupRuns,
59
62
  columnLayout,
60
63
  compareValues,
61
64
  csvCell,
@@ -124,6 +127,8 @@ const IconMinus = ({ size = 11 }) =>
124
127
  svg(size, 3.2, [h("line", { key: "l", x1: 6, y1: 12, x2: 18, y2: 12 })]);
125
128
  const IconChevron = ({ className }) =>
126
129
  svg(12, 2.2, [h("polyline", { key: "p", points: "6 9 12 15 18 9" })], className);
130
+ const IconSearch = () =>
131
+ svg(16, 2, [h("circle", { key: "c", cx: 11, cy: 11, r: 8 }), h("path", { key: "p", d: "m21 21-4.3-4.3" })], "mk-ms-search-icon");
127
132
  const IconX = ({ size = 16 }) =>
128
133
  svg(size, 2, [h("path", { key: "p", d: "M18 6 6 18M6 6l12 12" })]);
129
134
  const IconRefresh = () =>
@@ -140,28 +145,26 @@ const IconRefresh = () =>
140
145
  const PHONE = "(max-width: 640px)";
141
146
 
142
147
  /**
143
- * One kit popover is open at a time: opening this one closes any other (a
144
- * module-level registry, see claimPopover). It closes on Escape, on a
145
- * pointerdown outside it, and when focus moves outside it. Pointerdown, in
146
- * the capture phase, because Radix menu triggers cancel the mouse events of
147
- * their pointerdown, so an outside mousedown/click listener never hears them.
148
- *
149
- * It also keeps the `.mk-pop` inside `wrapRef` within the viewport: it opens
150
- * under its trigger aligned to `align`, flips to the other edge when that
151
- * would overflow, and as a last resort is pinned to the viewport margin. On
152
- * phones the popover is a bottom sheet (CSS) and is left alone.
148
+ * The closing half of usePopover: one kit popover at a time, Escape, a
149
+ * pointerdown or focus outside `wrapRef`. A portalled panel passes its own
150
+ * ref as `panelRef`, so a click inside it counts as inside. `onEscape` runs
151
+ * after an Escape close (the header info returns focus to its button).
153
152
  */
154
- function usePopover(open, setOpen, wrapRef, align) {
153
+ function usePopoverDismiss(open, setOpen, wrapRef, panelRef, onEscape) {
155
154
  React.useEffect(() => {
156
155
  if (!open) return undefined;
157
156
  const close = () => setOpen(false);
158
157
  claimPopover(close);
159
158
  const outside = e => {
160
159
  const wrap = wrapRef.current;
160
+ if (panelRef?.current && e.target instanceof Node && panelRef.current.contains(e.target)) return;
161
161
  if (wrap && e.target instanceof Node && !wrap.contains(e.target)) close();
162
162
  };
163
163
  const onKey = e => {
164
- if (e.key === "Escape") close();
164
+ if (e.key === "Escape") {
165
+ close();
166
+ onEscape?.();
167
+ }
165
168
  };
166
169
  document.addEventListener("pointerdown", outside, true);
167
170
  document.addEventListener("focusin", outside, true);
@@ -172,7 +175,23 @@ function usePopover(open, setOpen, wrapRef, align) {
172
175
  document.removeEventListener("focusin", outside, true);
173
176
  document.removeEventListener("keydown", onKey);
174
177
  };
175
- }, [open, setOpen, wrapRef]);
178
+ }, [open, setOpen, wrapRef]); // eslint-disable-line react-hooks/exhaustive-deps
179
+ }
180
+
181
+ /**
182
+ * One kit popover is open at a time: opening this one closes any other (a
183
+ * module-level registry, see claimPopover). It closes on Escape, on a
184
+ * pointerdown outside it, and when focus moves outside it. Pointerdown, in
185
+ * the capture phase, because Radix menu triggers cancel the mouse events of
186
+ * their pointerdown, so an outside mousedown/click listener never hears them.
187
+ *
188
+ * It also keeps the `.mk-pop` inside `wrapRef` within the viewport: it opens
189
+ * under its trigger aligned to `align`, flips to the other edge when that
190
+ * would overflow, and as a last resort is pinned to the viewport margin. On
191
+ * phones the popover is a bottom sheet (CSS) and is left alone.
192
+ */
193
+ function usePopover(open, setOpen, wrapRef, align) {
194
+ usePopoverDismiss(open, setOpen, wrapRef);
176
195
 
177
196
  React.useLayoutEffect(() => {
178
197
  if (!open) return undefined;
@@ -344,11 +363,16 @@ export function tooltipCell(value, format, opts) {
344
363
  * minimum width, then a bold `total` row under a rule. `after` rows follow
345
364
  * the total, apart (a value on another axis, such as an average next to a
346
365
  * rate). A null or undefined cell shows a muted "–"; "" stays blank.
366
+ * `colWidth` sets the value columns' minimum width (default 7ch; a number is
367
+ * px), so the box keeps its size while the pointer moves.
347
368
  */
348
- export function TableTooltip({ title, columns = [], rows = [], total, after, className }) {
369
+ export function TableTooltip({ title, columns = [], rows = [], total, after, className, colWidth }) {
349
370
  return h(
350
371
  "div",
351
- { className: cx("mk-tip", className) },
372
+ {
373
+ className: cx("mk-tip", className),
374
+ style: colWidth == null ? undefined : { "--mk-tip-col": typeof colWidth === "number" ? `${colWidth}px` : colWidth },
375
+ },
352
376
  title != null && h("div", { className: "mk-tip-title" }, title),
353
377
  h(
354
378
  "table",
@@ -404,6 +428,98 @@ const IconSort = ({ dir }) =>
404
428
  dir ? undefined : "mk-dt-faint",
405
429
  );
406
430
 
431
+ const IconInfo = () =>
432
+ svg(12, 2, [
433
+ h("circle", { key: "c", cx: 12, cy: 12, r: 10 }),
434
+ h("path", { key: "a", d: "M12 16v-4" }),
435
+ h("path", { key: "b", d: "M12 8h.01" }),
436
+ ]);
437
+
438
+ /**
439
+ * A column's "i" (`column.info`): a button beside the header's sort button,
440
+ * never inside it, so a click on it never sorts. It opens a kit popover
441
+ * (`.mk-pop`) portalled to <body>, so the table's scroll box never clips it.
442
+ * Like a Radix Popover, focus moves into the panel on open and Escape returns
443
+ * it to the button. A click or focus outside closes it, as every kit popover.
444
+ */
445
+ function HeaderInfo({ title, label, children }) {
446
+ const [open, setOpen] = React.useState(false);
447
+ const wrapRef = React.useRef(null);
448
+ const panelRef = React.useRef(null);
449
+ const id = React.useId();
450
+ const refocus = React.useCallback(() => wrapRef.current?.querySelector("button")?.focus(), []);
451
+ usePopoverDismiss(open, setOpen, wrapRef, panelRef, refocus);
452
+
453
+ React.useLayoutEffect(() => {
454
+ if (!open) return undefined;
455
+ const place = () => {
456
+ const pop = panelRef.current;
457
+ const btn = wrapRef.current;
458
+ if (!pop || !btn) return;
459
+ pop.style.left = "";
460
+ pop.style.top = "";
461
+ if (window.matchMedia(PHONE).matches) return;
462
+ const margin = 8;
463
+ const r = btn.getBoundingClientRect();
464
+ const w = pop.offsetWidth;
465
+ const ht = pop.offsetHeight;
466
+ const left = Math.min(Math.max(margin, r.left + r.width / 2 - w / 2), window.innerWidth - w - margin);
467
+ const below = r.bottom + 6;
468
+ const top = below + ht > window.innerHeight - margin && r.top - 6 - ht >= margin ? r.top - 6 - ht : below;
469
+ pop.style.left = `${left}px`;
470
+ pop.style.top = `${top}px`;
471
+ };
472
+ place();
473
+ panelRef.current?.focus({ preventScroll: true });
474
+ window.addEventListener("resize", place);
475
+ window.addEventListener("scroll", place, true);
476
+ return () => {
477
+ window.removeEventListener("resize", place);
478
+ window.removeEventListener("scroll", place, true);
479
+ };
480
+ }, [open]);
481
+
482
+ return h(
483
+ "span",
484
+ { className: "mk-dt-info-wrap", ref: wrapRef },
485
+ h(
486
+ "button",
487
+ {
488
+ type: "button",
489
+ className: cx("mk-dt-info", open && "open"),
490
+ "aria-label": label,
491
+ "aria-haspopup": "dialog",
492
+ "aria-expanded": open,
493
+ "aria-controls": open ? id : undefined,
494
+ onClick: e => {
495
+ e.stopPropagation();
496
+ setOpen(o => !o);
497
+ },
498
+ },
499
+ h(IconInfo),
500
+ ),
501
+ open &&
502
+ typeof document !== "undefined" &&
503
+ createPortal(
504
+ h(
505
+ "div",
506
+ {
507
+ ref: panelRef,
508
+ id,
509
+ className: "mk-pop mk-dt-info-pop",
510
+ role: "dialog",
511
+ "aria-label": label,
512
+ tabIndex: -1,
513
+ onClick: e => e.stopPropagation(),
514
+ },
515
+ title != null && h("strong", { className: "mk-dt-info-title" }, title),
516
+ h("div", { className: "mk-dt-info-body" }, children),
517
+ ),
518
+ document.body,
519
+ ),
520
+ );
521
+ }
522
+
407
523
  /** A value that is controlled when `value !== undefined`, local state otherwise. */
408
524
  function useControlled(value, initial, onChange) {
409
525
  const [local, setLocal] = React.useState(initial);
@@ -442,6 +558,36 @@ export function saveCsv(filename, text) {
442
558
  }
443
559
 
444
560
  const fmtCount = n => n.toLocaleString("en-US");
561
+ const EMPTY_EXPANDED = Object.freeze({});
562
+
563
+ /**
564
+ * The English text of a DataTable's footer, sort hints and export button.
565
+ * `labels` overrides any of them. Text with numbers is a function.
566
+ */
567
+ export const DATA_TABLE_LABELS = {
568
+ range: (from, to, total, noun) => [
569
+ h("b", { key: "r" }, `${fmtCount(from)}–${fmtCount(to)}`),
570
+ " of ",
571
+ h("b", { key: "t" }, fmtCount(total)),
572
+ ` ${noun}`,
573
+ ],
574
+ none: noun => `No ${noun}`,
575
+ empty: "No rows match these filters.",
576
+ page: (page, pageCount) => `Page ${page} of ${pageCount}`,
577
+ perPage: size => `${size} per page`,
578
+ rowsPerPage: "Rows per page",
579
+ first: "First page",
580
+ previous: "Previous page",
581
+ next: "Next page",
582
+ last: "Last page",
583
+ sortHint: label => `${label} · click to sort`,
584
+ exportCsv: "Export CSV",
585
+ exportTitle: "Export every row on every page, as sorted",
586
+ exportFailed: "The browser blocked the download. Open the app in a new tab and export again.",
587
+ expand: () => "Show details",
588
+ collapse: () => "Hide details",
589
+ info: label => `About ${label}`,
590
+ };
445
591
  // A click on a link, button or field inside a row is that control's, not the row's.
446
592
  const INTERACTIVE = "a,button,input,select,textarea,label,[role=button]";
447
593
 
@@ -458,6 +604,18 @@ const INTERACTIVE = "a,button,input,select,textarea,label,[role=button]";
458
604
  * each column's `width` or a default by kind (see `columnLayout`). The rows
459
605
  * on screen never change a width, so sorting, filtering and paging never
460
606
  * shift the layout. Cells ellipsize at their width with the full value on hover.
607
+ *
608
+ * Column groups: give columns a `group` and the header gets a second row on
609
+ * top, where each group's label spans its adjacent columns (`groups` sets a
610
+ * label or class per group id). Ungrouped columns span both rows. A line
611
+ * separates the sections. Sort, the sticky header and the pinned column work
612
+ * the same.
613
+ *
614
+ * Four options, all off by default (the table then renders exactly as 2.27):
615
+ * `wrapHeaders` lets a header label take 2 lines; `renderExpanded` adds a
616
+ * chevron in each row's first cell that opens a full-width detail row under
617
+ * it; a column's `info` adds an "i" popover beside its header; `totalRow`
618
+ * pins a total row to the bottom of the scroll box.
461
619
  */
462
620
  export function DataTable({
463
621
  rows = [],
@@ -476,16 +634,30 @@ export function DataTable({
476
634
  onPageSizeChange,
477
635
  pageSizes = [25, 50, 100],
478
636
  noun = "rows",
479
- empty = "No rows match these filters.",
637
+ empty: emptyProp,
638
+ labels: labelsProp,
480
639
  fill = false,
481
640
  maxHeight,
482
641
  onRowClick,
483
642
  rowLabel,
643
+ rowClassName,
484
644
  resetKey,
485
645
  exportFilename,
646
+ groups,
647
+ wrapHeaders = false,
648
+ renderExpanded,
649
+ canExpand,
650
+ expanded: expandedProp,
651
+ defaultExpanded = EMPTY_EXPANDED,
652
+ onExpandedChange,
653
+ totalRow,
654
+ onTotalRowClick,
655
+ exportTotalRow = false,
486
656
  className,
487
657
  style,
488
658
  }) {
659
+ const L = labelsProp ? { ...DATA_TABLE_LABELS, ...labelsProp } : DATA_TABLE_LABELS;
660
+ const empty = emptyProp ?? L.empty;
489
661
  const [sort, setSort] = useControlled(sortProp, defaultSort, onSortChange);
490
662
  const [rawPage, setRawPage] = useControlled(pageProp, 1, onPageChange);
491
663
  const [pageSize, setPageSize] = useControlled(pageSizeProp, defaultPageSize, onPageSizeChange);
@@ -496,9 +668,40 @@ export function DataTable({
496
668
 
497
669
  const sorted = React.useMemo(() => sortRows(rows, columns, sort), [rows, columns, sort]);
498
670
  const layout = React.useMemo(() => columnLayout(columns), [columns]);
671
+ const runs = React.useMemo(() => columnGroupRuns(columns), [columns]);
672
+ const sections = React.useMemo(() => new Set(runs?.filter(r => r.start).map(r => r.columns[0].id)), [runs]);
499
673
  const { page, pageCount, start, end } = pageSlice(sorted.length, rawPage, pageSize);
500
674
  const visible = sorted.slice(start, end);
501
675
 
676
+ // Expandable rows. The state has TanStack Table's ExpandedState shape:
677
+ // `true` (every row) or `{ [rowId]: true }`, rowId being String(rowKey(row)).
678
+ // Detail rows are rendered under their parent only: never sorted, paged or
679
+ // exported, since they are not rows of `sorted`.
680
+ const expandable = typeof renderExpanded === "function";
681
+ const [expanded, setExpanded] = useControlled(expandedProp, defaultExpanded, onExpandedChange);
682
+ const uid = React.useId();
683
+ const keyOf = (row, i) => (rowKey ? rowKey(row) : (row?.id ?? i));
684
+ const isOpen = id => expanded === true || !!expanded?.[id];
685
+ const toggle = id => {
686
+ let next;
687
+ if (expanded === true) {
688
+ next = {};
689
+ sorted.forEach((r, i) => {
690
+ if (!canExpand || canExpand(r)) next[String(keyOf(r, i))] = true;
691
+ });
692
+ } else next = { ...expanded };
693
+ if (next[id]) delete next[id];
694
+ else next[id] = true;
695
+ setExpanded(next);
696
+ };
697
+
698
+ // The total row: an object, or a function over the rows the table got (the
699
+ // filtered rows). It never sorts or pages; CSV only with `exportTotalRow`.
700
+ const total = React.useMemo(
701
+ () => (typeof totalRow === "function" ? totalRow(rows) : (totalRow ?? null)),
702
+ [totalRow, rows],
703
+ );
704
+
502
705
  const goTo = p => {
503
706
  if (p !== rawPage) setRawPage(p);
504
707
  scrollRef.current?.scrollTo?.({ top: 0 });
@@ -533,49 +736,83 @@ export function DataTable({
533
736
  goTo(1);
534
737
  };
535
738
  const exportCsv = () => {
536
- const res = saveCsv(exportFilename, toCsv(sorted, columns));
537
- setNote(res === "failed" ? "The browser blocked the download. Open the app in a new tab and export again." : null);
739
+ const res = saveCsv(exportFilename, toCsv(exportTotalRow && total ? [...sorted, total] : sorted, columns));
740
+ setNote(res === "failed" ? L.exportFailed : null);
538
741
  };
539
742
 
540
743
  const cellClass = (col, extra) =>
541
- cx(col.num && "mk-dt-num", col.align === "right" && "mk-dt-num", col.align === "center" && "mk-dt-center", col.sticky && "mk-dt-pin", extra);
744
+ cx(
745
+ col.num && "mk-dt-num",
746
+ col.align === "right" && "mk-dt-num",
747
+ col.align === "center" && "mk-dt-center",
748
+ col.sticky && "mk-dt-pin",
749
+ sections.has(col.id) && "mk-dt-section",
750
+ extra,
751
+ );
542
752
 
543
- const head = h(
544
- "thead",
545
- null,
546
- h(
547
- "tr",
548
- null,
549
- ...columns.map(col => {
550
- const active = sort?.id === col.id;
551
- const text = typeof col.label === "string" ? col.label : col.id;
552
- const label =
553
- col.sortable === false
554
- ? h("span", { className: "mk-dt-label", title: col.hint ?? text }, h("span", { className: "mk-dt-th-text" }, col.label))
555
- : h(
556
- "button",
557
- {
558
- type: "button",
559
- className: "mk-dt-sort",
560
- onClick: () => sortBy(col),
561
- title: `${col.hint ?? text} · click to sort`,
562
- },
563
- h("span", { className: "mk-dt-th-text" }, col.label),
564
- h(IconSort, { dir: active ? sort.dir : null }),
565
- );
566
- return h(
567
- "th",
568
- {
569
- key: col.id,
570
- scope: "col",
571
- className: cellClass(col, active && "mk-dt-sorted"),
572
- "aria-sort": col.sortable === false ? undefined : active ? (sort.dir === "asc" ? "ascending" : "descending") : "none",
573
- },
574
- label,
575
- );
576
- }),
577
- ),
578
- );
753
+ // One column's header cell: a sort button (or a plain label). In a grouped
754
+ // header an ungrouped column spans both rows (`rowSpan` 2).
755
+ const headCell = (col, rowSpan) => {
756
+ const active = sort?.id === col.id;
757
+ const text = typeof col.label === "string" ? col.label : col.id;
758
+ const label =
759
+ col.sortable === false
760
+ ? h("span", { className: "mk-dt-label", title: col.hint ?? text }, h("span", { className: "mk-dt-th-text" }, col.label))
761
+ : h(
762
+ "button",
763
+ {
764
+ type: "button",
765
+ className: "mk-dt-sort",
766
+ onClick: () => sortBy(col),
767
+ title: L.sortHint(col.hint ?? text),
768
+ },
769
+ h("span", { className: "mk-dt-th-text" }, col.label),
770
+ h(IconSort, { dir: active ? sort.dir : null }),
771
+ );
772
+ return h(
773
+ "th",
774
+ {
775
+ key: col.id,
776
+ scope: "col",
777
+ rowSpan,
778
+ className: cellClass(col, active && "mk-dt-sorted"),
779
+ "aria-sort": col.sortable === false ? undefined : active ? (sort.dir === "asc" ? "ascending" : "descending") : "none",
780
+ },
781
+ col.info == null
782
+ ? label
783
+ : h(
784
+ "div",
785
+ { className: "mk-dt-th-row" },
786
+ label,
787
+ h(HeaderInfo, { title: col.label, label: L.info(text) }, col.info),
788
+ ),
789
+ );
790
+ };
791
+
792
+ // Column groups: a top row of group labels, each spanning its columns.
793
+ const groupCell = run => {
794
+ const info = groups?.find(g => g.id === run.group);
795
+ const label = info?.label ?? run.group;
796
+ return h(
797
+ "th",
798
+ {
799
+ key: `__group_${run.columns[0].id}`,
800
+ scope: "colgroup",
801
+ colSpan: run.columns.length,
802
+ className: cx("mk-dt-group", run.start && "mk-dt-section", info?.className),
803
+ title: typeof label === "string" ? label : undefined,
804
+ },
805
+ h("span", { className: "mk-dt-th-text" }, label),
806
+ );
807
+ };
808
+ const head = runs
809
+ ? h(
810
+ "thead",
811
+ null,
812
+ h("tr", null, ...runs.map(run => (run.group == null ? headCell(run.columns[0], 2) : groupCell(run)))),
813
+ h("tr", null, ...runs.flatMap(run => (run.group == null ? [] : run.columns.map(col => headCell(col))))),
814
+ )
815
+ : h("thead", null, h("tr", null, ...columns.map(col => headCell(col))));
579
816
 
580
817
  // Every cell ellipsizes at its column's width, with the raw value (text or
581
818
  // a number) as the hover title. `truncate: false` wraps instead.
@@ -586,6 +823,41 @@ export function DataTable({
586
823
  const title = typeof v === "string" || typeof v === "number" || typeof v === "bigint" ? String(v) : undefined;
587
824
  return h("div", { className: "mk-dt-cell", title: title || undefined }, content);
588
825
  };
826
+ // The first cell of an expandable table: the chevron (or a spacer of its
827
+ // width, so every first cell lines up) before the cell's content.
828
+ const firstCell = (col, row, toggleEl) =>
829
+ h("div", { className: "mk-dt-xcell" }, toggleEl ?? h("span", { className: "mk-dt-expander-none", "aria-hidden": "true" }), cell(col, row));
830
+ const expander = (row, id, open, detailId) =>
831
+ h(
832
+ "button",
833
+ {
834
+ type: "button",
835
+ className: "mk-dt-expander",
836
+ "aria-expanded": open,
837
+ "aria-controls": open ? detailId : undefined,
838
+ "aria-label": open ? L.collapse(row) : L.expand(row),
839
+ onClick: () => toggle(id),
840
+ },
841
+ h(IconChevron),
842
+ );
843
+ // A row click (and Enter or Space on the focused row), for body rows and the total row.
844
+ const clickProps = (handler, row, label) =>
845
+ handler
846
+ ? {
847
+ tabIndex: 0,
848
+ className: "mk-dt-click",
849
+ "aria-label": label,
850
+ onClick: e => {
851
+ if (!e.target.closest?.(INTERACTIVE)) handler(row, e);
852
+ },
853
+ onKeyDown: e => {
854
+ if ((e.key === "Enter" || e.key === " ") && e.target === e.currentTarget) {
855
+ e.preventDefault();
856
+ handler(row, e);
857
+ }
858
+ },
859
+ }
860
+ : null;
589
861
 
590
862
  const body = h(
591
863
  "tbody",
@@ -593,31 +865,67 @@ export function DataTable({
593
865
  visible.length === 0
594
866
  ? h("tr", { key: "__empty" }, h("td", { colSpan: columns.length, className: "mk-dt-empty" }, empty))
595
867
  : visible.map((row, i) => {
596
- const key = rowKey ? rowKey(row) : (row?.id ?? start + i);
597
- const click = onRowClick
598
- ? {
599
- tabIndex: 0,
600
- className: "mk-dt-click",
601
- "aria-label": rowLabel ? rowLabel(row) : undefined,
602
- onClick: e => {
603
- if (!e.target.closest?.(INTERACTIVE)) onRowClick(row, e);
604
- },
605
- onKeyDown: e => {
606
- if ((e.key === "Enter" || e.key === " ") && e.target === e.currentTarget) {
607
- e.preventDefault();
608
- onRowClick(row, e);
609
- }
610
- },
611
- }
612
- : null;
613
- return h(
868
+ const key = keyOf(row, start + i);
869
+ const click = clickProps(onRowClick, row, rowLabel ? rowLabel(row) : undefined);
870
+ const rowClass = cx(click?.className, rowClassName?.(row)) || undefined;
871
+ if (!expandable) {
872
+ return h(
873
+ "tr",
874
+ { key, ...click, className: rowClass },
875
+ ...columns.map(col => h("td", { key: col.id, className: cellClass(col) || undefined }, cell(col, row))),
876
+ );
877
+ }
878
+ const id = String(key);
879
+ const can = !canExpand || canExpand(row);
880
+ const open = can && isOpen(id);
881
+ const detailId = `${uid}-x-${id.replace(/\s/g, "_")}`;
882
+ // Always a Fragment, open or not: a tr swapped for a Fragment would
883
+ // remount the row and drop the chevron's focus.
884
+ const tr = h(
614
885
  "tr",
615
- { key, ...click },
616
- ...columns.map(col => h("td", { key: col.id, className: cellClass(col) || undefined }, cell(col, row))),
886
+ { key: "row", ...click, className: cx(rowClass, open && "mk-dt-open") || undefined },
887
+ ...columns.map((col, c) =>
888
+ h(
889
+ "td",
890
+ { key: col.id, className: cellClass(col) || undefined },
891
+ c === 0 ? firstCell(col, row, can ? expander(row, id, open, detailId) : null) : cell(col, row),
892
+ ),
893
+ ),
894
+ );
895
+ return h(
896
+ React.Fragment,
897
+ { key },
898
+ tr,
899
+ open &&
900
+ h(
901
+ "tr",
902
+ { key: "detail", className: "mk-dt-detail", id: detailId },
903
+ h("td", { colSpan: columns.length }, h("div", { className: "mk-dt-detail-body" }, renderExpanded(row))),
904
+ ),
617
905
  );
618
906
  }),
619
907
  );
620
908
 
909
+ // The pinned total row (a <tfoot> that sticks to the bottom), shown with rows only.
910
+ const foot =
911
+ total != null && sorted.length > 0
912
+ ? h(
913
+ "tfoot",
914
+ null,
915
+ h(
916
+ "tr",
917
+ { ...clickProps(onTotalRowClick, total, rowLabel ? rowLabel(total) : undefined), className: cx("mk-dt-total", onTotalRowClick && "mk-dt-click") },
918
+ ...columns.map((col, c) =>
919
+ h(
920
+ "td",
921
+ { key: col.id, className: cellClass(col) || undefined },
922
+ c === 0 && expandable ? firstCell(col, total, null) : cell(col, total),
923
+ ),
924
+ ),
925
+ ),
926
+ )
927
+ : null;
928
+
621
929
  const footer = h(
622
930
  "footer",
623
931
  { className: "mk-dt-foot" },
@@ -625,8 +933,8 @@ export function DataTable({
625
933
  "span",
626
934
  { "aria-live": "polite" },
627
935
  sorted.length
628
- ? [h("b", { key: "r" }, `${fmtCount(start + 1)}–${fmtCount(end)}`), " of ", h("b", { key: "t" }, fmtCount(sorted.length)), ` ${noun}`]
629
- : `No ${noun}`,
936
+ ? L.range(start + 1, end, sorted.length, noun)
937
+ : L.none(noun),
630
938
  ),
631
939
  h(
632
940
  "div",
@@ -636,20 +944,20 @@ export function DataTable({
636
944
  "select",
637
945
  {
638
946
  className: "mk-dt-size",
639
- "aria-label": "Rows per page",
947
+ "aria-label": L.rowsPerPage,
640
948
  value: pageSize,
641
949
  onChange: e => {
642
950
  setPageSize(Number(e.target.value));
643
951
  goTo(1);
644
952
  },
645
953
  },
646
- ...sizeOptions.map(n => h("option", { key: n, value: n }, `${n} per page`)),
954
+ ...sizeOptions.map(n => h("option", { key: n, value: n }, L.perPage(n))),
647
955
  ),
648
- h(Button, { size: "sm", "aria-label": "First page", disabled: page <= 1, onClick: () => goTo(1) }, "«"),
649
- h(Button, { size: "sm", "aria-label": "Previous page", disabled: page <= 1, onClick: () => goTo(page - 1) }, "‹"),
650
- h("span", { className: "mk-dt-page" }, `Page ${page} of ${pageCount}`),
651
- h(Button, { size: "sm", "aria-label": "Next page", disabled: page >= pageCount, onClick: () => goTo(page + 1) }, "›"),
652
- h(Button, { size: "sm", "aria-label": "Last page", disabled: page >= pageCount, onClick: () => goTo(pageCount) }, "»"),
956
+ h(Button, { size: "sm", "aria-label": L.first, disabled: page <= 1, onClick: () => goTo(1) }, "«"),
957
+ h(Button, { size: "sm", "aria-label": L.previous, disabled: page <= 1, onClick: () => goTo(page - 1) }, "‹"),
958
+ h("span", { className: "mk-dt-page" }, L.page(page, pageCount)),
959
+ h(Button, { size: "sm", "aria-label": L.next, disabled: page >= pageCount, onClick: () => goTo(page + 1) }, "›"),
960
+ h(Button, { size: "sm", "aria-label": L.last, disabled: page >= pageCount, onClick: () => goTo(pageCount) }, "»"),
653
961
  ),
654
962
  );
655
963
 
@@ -657,8 +965,8 @@ export function DataTable({
657
965
  exportFilename != null &&
658
966
  h(
659
967
  Button,
660
- { key: "__export", disabled: !sorted.length, onClick: exportCsv, title: "Export every row on every page, as sorted" },
661
- "Export CSV",
968
+ { key: "__export", disabled: !sorted.length, onClick: exportCsv, title: L.exportTitle },
969
+ L.exportCsv,
662
970
  );
663
971
 
664
972
  return h(
@@ -667,7 +975,7 @@ export function DataTable({
667
975
  title,
668
976
  description,
669
977
  actions: actions != null || exportButton ? h(React.Fragment, null, actions, exportButton) : undefined,
670
- className: cx("mk-dt", fill && "mk-dt--fill", className),
978
+ className: cx("mk-dt", fill && "mk-dt--fill", wrapHeaders && "mk-dt--wrap-heads", className),
671
979
  style,
672
980
  },
673
981
  note && h("p", { className: "mk-dt-note", role: "status" }, note),
@@ -676,7 +984,7 @@ export function DataTable({
676
984
  { className: "mk-dt-scroll", ref: scrollRef, style: !fill && maxHeight != null ? { maxHeight } : undefined },
677
985
  h(
678
986
  "table",
679
- { className: "mk-dt-table", style: { minWidth: layout.minWidth } },
987
+ { className: cx("mk-dt-table", runs && "mk-dt-table--grouped"), style: { minWidth: layout.minWidth } },
680
988
  h(
681
989
  "colgroup",
682
990
  null,
@@ -684,6 +992,7 @@ export function DataTable({
684
992
  ),
685
993
  head,
686
994
  body,
995
+ foot,
687
996
  ),
688
997
  ),
689
998
  footer,
@@ -717,6 +1026,9 @@ function toNode(o) {
717
1026
  return n.children ? { ...n, children: n.children.map(toNode) } : n;
718
1027
  }
719
1028
 
1029
+ /** The All row's key in a search's highlight order (never an option value). */
1030
+ const ALL_KEY = "\u0000all";
1031
+
720
1032
  const asChecked = t => (t === "on" ? true : t === "mixed" ? "mixed" : false);
721
1033
 
722
1034
  /**
@@ -726,11 +1038,14 @@ const asChecked = t => (t === "on" ? true : t === "mixed" ? "mixed" : false);
726
1038
  * (several filters folded into one dropdown on a phone), so every filter
727
1039
  * ticks and toggles alike.
728
1040
  *
729
- * Options are strings, `{ value, label, color }`, or groups with `children`
1041
+ * Options are strings, `{ value, label, color, count }`, or groups with `children`
730
1042
  * (a flat list is a one-level tree). A group row ticks its whole group and
731
1043
  * shows a dash when part of it is picked; its children follow it, indented.
732
1044
  * A tree that needs hover submenus is a FilterMenu (`@makoai/app-sdk/menu`).
733
1045
  *
1046
+ * `count` (2.28) is a number shown muted at the row's right edge, before
1047
+ * "Only". Search matches the label only, and the trigger never shows it.
1048
+ *
734
1049
  * The All rule (2.20): with nothing filtered (`value` empty or holding every
735
1050
  * option) every row is ticked. Unticking one leaves All in the partial (–)
736
1051
  * state. A click on a full All unticks every row; a click on a partial All
@@ -738,6 +1053,13 @@ const asChecked = t => (t === "on" ? true : t === "mixed" ? "mixed" : false);
738
1053
  * keeps its selection until a row is ticked, and the first tick picks that
739
1054
  * row alone. Pass `none` / `onNoneChange` to show the draft elsewhere (the
740
1055
  * trigger); otherwise FilterItems keeps it.
1056
+ *
1057
+ * `searchable` (2.27) puts a search input above the rows, as shadcn's
1058
+ * Command does: it takes focus, filters the rows by label as you type, and
1059
+ * hides All and its separator while a query is typed. Its keys are cmdk's:
1060
+ * ArrowDown/ArrowUp (Ctrl+N/J, Ctrl+P/K) move the highlighted row, Home/End
1061
+ * and Cmd+Arrow jump to the first or last, Enter ticks the highlighted row.
1062
+ * Off (the default), FilterItems renders exactly as before.
741
1063
  */
742
1064
  export function FilterItems({
743
1065
  options,
@@ -749,6 +1071,9 @@ export function FilterItems({
749
1071
  showOnly = false,
750
1072
  none: noneProp,
751
1073
  onNoneChange,
1074
+ searchable = false,
1075
+ searchPlaceholder = "Search",
1076
+ searchEmpty = "No match",
752
1077
  className,
753
1078
  "aria-label": ariaLabel,
754
1079
  }) {
@@ -775,7 +1100,71 @@ export function FilterItems({
775
1100
  const dot = o => (o.color ? h("span", { className: "mk-ms-swatch", style: { background: o.color } }) : null);
776
1101
  const allTick = filterAllTick(selected, none);
777
1102
 
1103
+ // Search (2.27): cmdk's model. Focus stays in the input; one row is
1104
+ // highlighted (the first after every keystroke) and Enter ticks it.
1105
+ const [query, setQuery] = React.useState("");
1106
+ const [activeKey, setActiveKey] = React.useState(null);
1107
+ const uid = React.useId();
1108
+ const shown = searchable ? filterSearch(tree, query) : null;
1109
+ const keys = [];
1110
+ const runs = new Map();
1111
+ if (searchable) {
1112
+ if (!shown) {
1113
+ keys.push(ALL_KEY);
1114
+ runs.set(ALL_KEY, () => apply(filterToggleAll(selected, none)));
1115
+ }
1116
+ const walk = node => {
1117
+ if (shown && !shown.has(node.value)) return;
1118
+ keys.push(node.value);
1119
+ runs.set(node.value, () => flip(node));
1120
+ node.children?.forEach(walk);
1121
+ };
1122
+ tree.forEach(walk);
1123
+ }
1124
+ const active = keys.includes(activeKey) ? activeKey : keys[0];
1125
+ const rowId = key => `${uid}-o${keys.indexOf(key)}`;
1126
+ const activeId = active === undefined ? undefined : rowId(active);
1127
+ React.useEffect(() => {
1128
+ if (!searchable || !activeId) return;
1129
+ const el = document.getElementById(activeId);
1130
+ if (el && typeof el.scrollIntoView === "function") el.scrollIntoView({ block: "nearest" });
1131
+ }, [searchable, activeId]);
1132
+ const searchProps = key =>
1133
+ searchable
1134
+ ? { id: rowId(key), "data-selected": String(key === active), onPointerMove: () => setActiveKey(key) }
1135
+ : null;
1136
+ const onSearchKey = e => {
1137
+ if (e.defaultPrevented || e.nativeEvent.isComposing || e.keyCode === 229) return;
1138
+ const i = keys.indexOf(active);
1139
+ const go = j => {
1140
+ e.preventDefault();
1141
+ if (j >= 0 && j < keys.length) setActiveKey(keys[j]);
1142
+ };
1143
+ switch (e.key) {
1144
+ case "ArrowDown":
1145
+ return go(e.metaKey ? keys.length - 1 : i + 1);
1146
+ case "ArrowUp":
1147
+ return go(e.metaKey ? 0 : i - 1);
1148
+ case "n":
1149
+ case "j":
1150
+ return e.ctrlKey ? go(i + 1) : undefined;
1151
+ case "p":
1152
+ case "k":
1153
+ return e.ctrlKey ? go(i - 1) : undefined;
1154
+ case "Home":
1155
+ return go(0);
1156
+ case "End":
1157
+ return go(keys.length - 1);
1158
+ case "Enter":
1159
+ e.preventDefault();
1160
+ return runs.get(active)?.();
1161
+ default:
1162
+ return undefined;
1163
+ }
1164
+ };
1165
+
778
1166
  const row = (node, depth) => {
1167
+ if (shown && !shown.has(node.value)) return [];
779
1168
  const tick = filterTick(node, selected, none);
780
1169
  const group = Boolean(node.children);
781
1170
  const el = h(
@@ -789,6 +1178,7 @@ export function FilterItems({
789
1178
  title: node.title,
790
1179
  className: cx("mk-ms-opt", tick !== "off" && "active", group && "mk-ms-group"),
791
1180
  style: depth ? { paddingLeft: 8 + depth * 20 } : undefined,
1181
+ ...searchProps(node.value),
792
1182
  onClick: () => flip(node),
793
1183
  onKeyDown: e => {
794
1184
  if (e.key === "Enter" || e.key === " ") {
@@ -801,15 +1191,22 @@ export function FilterItems({
801
1191
  h(Checkbox, { checked: asChecked(tick) }),
802
1192
  dot(node),
803
1193
  h("span", { className: "mk-ms-label" }, node.label),
1194
+ node.count != null && h("span", { className: "mk-ms-count" }, typeof node.count === "number" ? fmtCount(node.count) : node.count),
804
1195
  showOnly && filterLeavesOf(node).length > 0 && h("button", { type: "button", className: "mk-ms-only", tabIndex: -1, onClick: only(node) }, "Only"),
805
1196
  );
806
1197
  return group ? [el, ...node.children.flatMap(c => row(c, depth + 1))] : [el];
807
1198
  };
808
1199
 
809
- return h(
1200
+ const list = h(
810
1201
  "div",
811
- { className: cx("mk-ms-items", className), role: "listbox", "aria-multiselectable": true, "aria-label": ariaLabel },
812
- h(
1202
+ {
1203
+ className: cx("mk-ms-items", className),
1204
+ role: "listbox",
1205
+ "aria-multiselectable": true,
1206
+ "aria-label": ariaLabel,
1207
+ ...(searchable ? { id: `${uid}-list` } : null),
1208
+ },
1209
+ !shown && h(
813
1210
  "div",
814
1211
  {
815
1212
  role: "option",
@@ -817,6 +1214,7 @@ export function FilterItems({
817
1214
  "aria-checked": asChecked(allTick),
818
1215
  tabIndex: 0,
819
1216
  className: cx("mk-ms-opt", "mk-ms-all", allTick !== "off" && "active"),
1217
+ ...searchProps(ALL_KEY),
820
1218
  onClick: () => apply(filterToggleAll(selected, none)),
821
1219
  onKeyDown: e => {
822
1220
  if (e.key === "Enter" || e.key === " ") {
@@ -829,8 +1227,41 @@ export function FilterItems({
829
1227
  h(Checkbox, { checked: asChecked(allTick) }),
830
1228
  h("span", { className: "mk-ms-label" }, allLabel),
831
1229
  ),
832
- h("div", { className: "mk-ms-sep" }),
1230
+ !shown && h("div", { className: "mk-ms-sep" }),
833
1231
  ...tree.flatMap(o => row(o, 0)),
1232
+ shown && keys.length === 0 && h("div", { className: "mk-ms-empty", role: "presentation" }, searchEmpty),
1233
+ );
1234
+ if (!searchable) return list;
1235
+ return h(
1236
+ React.Fragment,
1237
+ null,
1238
+ h(
1239
+ "div",
1240
+ { className: "mk-ms-search" },
1241
+ h(IconSearch),
1242
+ h("input", {
1243
+ type: "text",
1244
+ className: "mk-ms-search-input",
1245
+ autoFocus: true,
1246
+ value: query,
1247
+ placeholder: searchPlaceholder,
1248
+ "aria-label": searchPlaceholder,
1249
+ autoComplete: "off",
1250
+ autoCorrect: "off",
1251
+ spellCheck: false,
1252
+ role: "combobox",
1253
+ "aria-autocomplete": "list",
1254
+ "aria-expanded": true,
1255
+ "aria-controls": `${uid}-list`,
1256
+ "aria-activedescendant": activeId,
1257
+ onChange: e => {
1258
+ setQuery(e.target.value);
1259
+ setActiveKey(null);
1260
+ },
1261
+ onKeyDown: onSearchKey,
1262
+ }),
1263
+ ),
1264
+ list,
834
1265
  );
835
1266
  }
836
1267
 
@@ -850,6 +1281,9 @@ export function FilterItems({
850
1281
  *
851
1282
  * Drop-in for the apps' NestedFilterMenu: `selected` = `value`, `label` =
852
1283
  * `aria-label`, `triggerClassName`, `summary`, `probes`, `sizeTo`.
1284
+ *
1285
+ * `searchable` (2.27) adds FilterItems' search input at the top of the menu:
1286
+ * search, All, a separator, then the values.
853
1287
  */
854
1288
  export function MultiSelect({
855
1289
  options,
@@ -865,6 +1299,9 @@ export function MultiSelect({
865
1299
  sizeTo,
866
1300
  probes = [],
867
1301
  align = "start",
1302
+ searchable = false,
1303
+ searchPlaceholder,
1304
+ searchEmpty,
868
1305
  className,
869
1306
  triggerClassName,
870
1307
  label,
@@ -931,6 +1368,7 @@ export function MultiSelect({
931
1368
  none,
932
1369
  onNoneChange: setNone,
933
1370
  "aria-label": ariaLabel,
1371
+ ...(searchable ? { searchable, searchPlaceholder, searchEmpty } : null),
934
1372
  }),
935
1373
  ),
936
1374
  );