@makoai/app-sdk 2.19.0 → 2.21.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
@@ -22,6 +22,18 @@ import {
22
22
  csvToTsv,
23
23
  defaultColumnWidth,
24
24
  formatSeconds as fmtSecs,
25
+ filterAllTick,
26
+ filterFind,
27
+ filterFromParam,
28
+ filterLeaves,
29
+ filterLeavesOf,
30
+ filterNormalize,
31
+ filterPickedNode,
32
+ filterText,
33
+ filterTick,
34
+ filterToggle,
35
+ filterToggleAll,
36
+ filterToParam,
25
37
  multiSelectAll,
26
38
  multiSelectState,
27
39
  multiSelectText,
@@ -40,6 +52,7 @@ import {
40
52
  tooltipValueState,
41
53
  } from "./ui-core.js";
42
54
 
55
+ export { formatDate, formatTime, toDayKey, toInstant } from "./time.js";
43
56
  export {
44
57
  closePopovers,
45
58
  COLUMN_WIDTHS,
@@ -49,6 +62,18 @@ export {
49
62
  csvDate,
50
63
  csvToTsv,
51
64
  defaultColumnWidth,
65
+ filterAllTick,
66
+ filterFind,
67
+ filterFromParam,
68
+ filterLeaves,
69
+ filterLeavesOf,
70
+ filterNormalize,
71
+ filterPickedNode,
72
+ filterText,
73
+ filterTick,
74
+ filterToggle,
75
+ filterToggleAll,
76
+ filterToParam,
52
77
  multiSelectState,
53
78
  multiSelectText,
54
79
  multiSelectToggle,
@@ -687,44 +712,180 @@ export function Checkbox({ checked = false, className }) {
687
712
  );
688
713
  }
689
714
 
715
+ function toNode(o) {
716
+ const n = toOption(o);
717
+ return n.children ? { ...n, children: n.children.map(toNode) } : n;
718
+ }
719
+
720
+ const asChecked = t => (t === "on" ? true : t === "mixed" ? "mixed" : false);
721
+
722
+ /**
723
+ * The rows of a multi-select filter, without trigger or popover: the All row,
724
+ * a separator, then every option with a tri-state checkbox. MultiSelect
725
+ * renders them in its popover; an app renders them in its own composite menu
726
+ * (several filters folded into one dropdown on a phone), so every filter
727
+ * ticks and toggles alike.
728
+ *
729
+ * Options are strings, `{ value, label, color }`, or groups with `children`
730
+ * (a flat list is a one-level tree). A group row ticks its whole group and
731
+ * shows a dash when part of it is picked; its children follow it, indented.
732
+ * A tree that needs hover submenus is a FilterMenu (`@makoai/app-sdk/menu`).
733
+ *
734
+ * The All rule (2.20): with nothing filtered (`value` empty or holding every
735
+ * option) every row is ticked. Unticking one leaves All in the partial (–)
736
+ * state. A click on a full All unticks every row; a click on a partial All
737
+ * ticks every row. "Nothing ticked" is a draft: it emits nothing, the data
738
+ * keeps its selection until a row is ticked, and the first tick picks that
739
+ * row alone. Pass `none` / `onNoneChange` to show the draft elsewhere (the
740
+ * trigger); otherwise FilterItems keeps it.
741
+ */
742
+ export function FilterItems({
743
+ options,
744
+ value: valueProp,
745
+ selected: selectedProp,
746
+ onChange,
747
+ allLabel = "All",
748
+ emptyMeansAll = true,
749
+ showOnly = false,
750
+ none: noneProp,
751
+ onNoneChange,
752
+ className,
753
+ "aria-label": ariaLabel,
754
+ }) {
755
+ const value = valueProp ?? selectedProp;
756
+ const tree = options.map(toNode);
757
+ const leaves = filterLeaves(tree);
758
+ const selected = filterNormalize(tree, value ?? []);
759
+ const [ownNone, setOwnNone] = React.useState(false);
760
+ const none = noneProp ?? ownNone;
761
+ const setNone = onNoneChange ?? setOwnNone;
762
+ const emit = next => {
763
+ setNone(false);
764
+ onChange(next.length ? next : multiSelectAll(leaves, emptyMeansAll));
765
+ };
766
+ const apply = r => {
767
+ if (r.none) setNone(true);
768
+ else emit(r.selected);
769
+ };
770
+ const flip = node => apply(filterToggle(tree, selected, node, none));
771
+ const only = node => e => {
772
+ e.stopPropagation();
773
+ emit(filterNormalize(tree, filterLeavesOf(node)));
774
+ };
775
+ const dot = o => (o.color ? h("span", { className: "mk-ms-swatch", style: { background: o.color } }) : null);
776
+ const allTick = filterAllTick(selected, none);
777
+
778
+ const row = (node, depth) => {
779
+ const tick = filterTick(node, selected, none);
780
+ const group = Boolean(node.children);
781
+ const el = h(
782
+ "div",
783
+ {
784
+ key: node.value,
785
+ role: "option",
786
+ "aria-selected": tick === "on",
787
+ "aria-checked": asChecked(tick),
788
+ tabIndex: 0,
789
+ title: node.title,
790
+ className: cx("mk-ms-opt", tick !== "off" && "active", group && "mk-ms-group"),
791
+ style: depth ? { paddingLeft: 8 + depth * 20 } : undefined,
792
+ onClick: () => flip(node),
793
+ onKeyDown: e => {
794
+ if (e.key === "Enter" || e.key === " ") {
795
+ e.preventDefault();
796
+ e.stopPropagation();
797
+ flip(node);
798
+ }
799
+ },
800
+ },
801
+ h(Checkbox, { checked: asChecked(tick) }),
802
+ dot(node),
803
+ h("span", { className: "mk-ms-label" }, node.label),
804
+ showOnly && filterLeavesOf(node).length > 0 && h("button", { type: "button", className: "mk-ms-only", tabIndex: -1, onClick: only(node) }, "Only"),
805
+ );
806
+ return group ? [el, ...node.children.flatMap(c => row(c, depth + 1))] : [el];
807
+ };
808
+
809
+ return h(
810
+ "div",
811
+ { className: cx("mk-ms-items", className), role: "listbox", "aria-multiselectable": true, "aria-label": ariaLabel },
812
+ h(
813
+ "div",
814
+ {
815
+ role: "option",
816
+ "aria-selected": allTick === "on",
817
+ "aria-checked": asChecked(allTick),
818
+ tabIndex: 0,
819
+ className: cx("mk-ms-opt", "mk-ms-all", allTick !== "off" && "active"),
820
+ onClick: () => apply(filterToggleAll(selected, none)),
821
+ onKeyDown: e => {
822
+ if (e.key === "Enter" || e.key === " ") {
823
+ e.preventDefault();
824
+ e.stopPropagation();
825
+ apply(filterToggleAll(selected, none));
826
+ }
827
+ },
828
+ },
829
+ h(Checkbox, { checked: asChecked(allTick) }),
830
+ h("span", { className: "mk-ms-label" }, allLabel),
831
+ ),
832
+ h("div", { className: "mk-ms-sep" }),
833
+ ...tree.flatMap(o => row(o, 0)),
834
+ );
835
+ }
836
+
690
837
  /**
691
- * Multi-select filter (shadcn combobox look). Controlled: `value` is the
692
- * selected values, `onChange` gets the next array.
838
+ * Multi-select filter (shadcn combobox look) around FilterItems. Controlled:
839
+ * `value` is the selected leaf values, `onChange` gets the next array. See
840
+ * FilterItems for the All rule. `emptyMeansAll` (default true) only picks
841
+ * what All emits: [] by default, every leaf's value when false.
693
842
  *
694
- * The All rule: nothing is filtered when `value` is empty or holds every
695
- * option. The All row is then ticked and every option row unticked. A click
696
- * on an option under All selects only that option. Unticking the last
697
- * option, or ticking the last missing one, returns to All. The trigger reads
698
- * the All label, the one selected label, or "N selected".
843
+ * The trigger reads the All label, the one picked label (a leaf or a whole
844
+ * group), "N selected" for a flat list (`formatMany`), the picked parts for a
845
+ * tree ("Pages + Assets: Images"), or `noneLabel` while nothing is ticked;
846
+ * `summary` replaces that text. Its width is fixed to the longest of
847
+ * `sizeTo` (default: the All label, the top-level labels, "N selected" and
848
+ * the none label) plus `probes`, so a pick never moves the layout; a longer
849
+ * text ends in an ellipsis and stays whole in the title.
699
850
  *
700
- * `emptyMeansAll` (default true) only picks what All emits: [] by default,
701
- * every option's value when false.
851
+ * Drop-in for the apps' NestedFilterMenu: `selected` = `value`, `label` =
852
+ * `aria-label`, `triggerClassName`, `summary`, `probes`, `sizeTo`.
702
853
  */
703
854
  export function MultiSelect({
704
855
  options,
705
- value,
856
+ value: valueProp,
857
+ selected: selectedProp,
706
858
  onChange,
707
859
  allLabel = "All",
860
+ noneLabel = "None",
708
861
  emptyMeansAll = true,
709
862
  showOnly = false,
710
863
  formatMany,
864
+ summary,
865
+ sizeTo,
866
+ probes = [],
711
867
  align = "start",
712
868
  className,
713
- "aria-label": ariaLabel,
869
+ triggerClassName,
870
+ label,
871
+ "aria-label": ariaLabelProp,
714
872
  }) {
873
+ const value = valueProp ?? selectedProp;
874
+ const ariaLabel = ariaLabelProp ?? label;
715
875
  const [open, setOpen] = React.useState(false);
876
+ const [none, setNone] = React.useState(false);
716
877
  const ref = React.useRef(null);
717
878
  usePopover(open, setOpen, ref, align);
718
- const opts = options.map(toOption);
719
- const values = opts.map(o => o.value);
720
- const { all: allOn, selected } = multiSelectState(value, values);
721
- const text = multiSelectText(value, opts, allLabel, formatMany);
722
- const single = selected.length === 1 ? opts.find(o => o.value === selected[0]) : null;
723
-
724
- const toggle = v => onChange(multiSelectToggle(value, values, v, emptyMeansAll));
725
- const all = () => onChange(multiSelectAll(values, emptyMeansAll));
726
- const dot = o =>
727
- o?.color ? h("span", { className: "mk-ms-swatch", style: { background: o.color } }) : null;
879
+ React.useEffect(() => {
880
+ if (!open) setNone(false);
881
+ }, [open]);
882
+ const tree = options.map(toNode);
883
+ const selected = filterNormalize(tree, value ?? []);
884
+ const many = formatMany ?? (n => `${n} selected`);
885
+ const text = none ? noneLabel : (summary ?? filterText(tree, selected, allLabel, many));
886
+ const sizes = [...(sizeTo ?? [allLabel, ...tree.map(o => o.label), many(filterLeaves(tree).length), noneLabel]), ...probes];
887
+ const single = selected.length ? filterPickedNode(tree, selected) : null;
888
+ const dot = o => (o?.color ? h("span", { className: "mk-ms-swatch", style: { background: o.color } }) : null);
728
889
 
729
890
  return h(
730
891
  "div",
@@ -733,69 +894,43 @@ export function MultiSelect({
733
894
  "button",
734
895
  {
735
896
  type: "button",
736
- className: cx("mk-ms-trigger", open && "open"),
897
+ className: cx("mk-ms-trigger", open && "open", triggerClassName),
737
898
  "aria-haspopup": "listbox",
738
899
  "aria-expanded": open,
739
900
  "aria-label": ariaLabel,
740
901
  title: selected.length > 1
741
- ? selected.map(v => opts.find(o => o.value === v)?.label ?? v).join(", ")
742
- : undefined,
902
+ ? selected.map(v => filterFind(tree, v)?.label ?? v).join(", ")
903
+ : text,
743
904
  onClick: () => setOpen(o => !o),
744
905
  },
745
- h("span", { className: "mk-ms-value" }, dot(single), h("span", { className: "mk-ms-text" }, text)),
906
+ h(
907
+ "span",
908
+ { className: "mk-ms-value" },
909
+ none ? null : dot(single),
910
+ h(
911
+ "span",
912
+ { className: "mk-ms-fit" },
913
+ // The text first: a grid's baseline is its first item's, and the probes are 0 px tall.
914
+ h("span", { key: "text", className: "mk-ms-text" }, text),
915
+ ...sizes.map((t, i) => h("span", { key: i, className: "mk-ms-probe", "aria-hidden": "true" }, t)),
916
+ ),
917
+ ),
746
918
  h(IconChevron, { className: "mk-ms-caret" }),
747
919
  ),
748
920
  open &&
749
921
  h(
750
922
  "div",
751
- {
752
- className: "mk-pop mk-ms-menu",
753
- role: "listbox",
754
- "aria-multiselectable": true,
755
- onClick: cancelLabelActivation,
756
- },
757
- h(
758
- "button",
759
- { type: "button", className: cx("mk-ms-opt", allOn && "active"), "aria-pressed": allOn, onClick: all },
760
- h(Checkbox, { checked: allOn }),
761
- h("span", { className: "mk-ms-label" }, allLabel),
762
- ),
763
- h("div", { className: "mk-ms-sep" }),
764
- ...opts.map(o => {
765
- const on = selected.includes(o.value);
766
- return h(
767
- "div",
768
- {
769
- key: o.value,
770
- role: "option",
771
- "aria-selected": on,
772
- tabIndex: 0,
773
- className: cx("mk-ms-opt", on && "active"),
774
- onClick: () => toggle(o.value),
775
- onKeyDown: e => {
776
- if (e.key === "Enter" || e.key === " ") {
777
- e.preventDefault();
778
- toggle(o.value);
779
- }
780
- },
781
- },
782
- h(Checkbox, { checked: on }),
783
- dot(o),
784
- h("span", { className: "mk-ms-label" }, o.label),
785
- showOnly &&
786
- h(
787
- "button",
788
- {
789
- type: "button",
790
- className: "mk-ms-only",
791
- onClick: e => {
792
- e.stopPropagation();
793
- onChange(multiSelectToggle([], values, o.value, emptyMeansAll));
794
- },
795
- },
796
- "Only",
797
- ),
798
- );
923
+ { className: "mk-pop mk-ms-menu", onClick: cancelLabelActivation },
924
+ h(FilterItems, {
925
+ options: tree,
926
+ value,
927
+ onChange,
928
+ allLabel,
929
+ emptyMeansAll,
930
+ showOnly,
931
+ none,
932
+ onNoneChange: setNone,
933
+ "aria-label": ariaLabel,
799
934
  }),
800
935
  ),
801
936
  );