@erxes/ui 4.0.0-v2.12 → 4.0.0-v2.14

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.
Files changed (70) hide show
  1. package/CATALOG.md +3 -2
  2. package/README.md +1 -1
  3. package/dist/components/color-picker.js +2 -2
  4. package/dist/components/command-bar.js +3 -0
  5. package/dist/components/input.d.ts +1 -1
  6. package/dist/components/input.js +2 -2
  7. package/dist/components/on-demand.d.ts +9 -2
  8. package/dist/components/on-demand.js +15 -1
  9. package/dist/index.d.ts +2 -2
  10. package/dist/index.js +2 -2
  11. package/dist/modules/conditions/ConditionsEditor.d.ts +5 -3
  12. package/dist/modules/conditions/ConditionsEditor.js +139 -177
  13. package/dist/modules/conditions/DateEditors.d.ts +8 -0
  14. package/dist/modules/conditions/DateEditors.js +188 -0
  15. package/dist/modules/conditions/FieldPicker.d.ts +17 -0
  16. package/dist/modules/conditions/FieldPicker.js +99 -0
  17. package/dist/modules/conditions/RuleParts.d.ts +30 -0
  18. package/dist/modules/conditions/RuleParts.js +78 -0
  19. package/dist/modules/conditions/RuleRow.d.ts +27 -0
  20. package/dist/modules/conditions/RuleRow.js +156 -0
  21. package/dist/modules/conditions/TypedInput.d.ts +13 -0
  22. package/dist/modules/conditions/TypedInput.js +36 -0
  23. package/dist/modules/conditions/ValueEditor.d.ts +18 -0
  24. package/dist/modules/conditions/ValueEditor.js +164 -0
  25. package/dist/modules/conditions/conditions.d.ts +15 -8
  26. package/dist/modules/conditions/conditions.js +1 -75
  27. package/dist/modules/conditions/fieldIcons.d.ts +6 -0
  28. package/dist/modules/conditions/fieldIcons.js +19 -0
  29. package/dist/modules/conditions/fieldOptions.d.ts +13 -0
  30. package/dist/modules/conditions/fieldOptions.js +26 -0
  31. package/dist/modules/conditions/filterRule.d.ts +45 -0
  32. package/dist/modules/conditions/filterRule.js +169 -0
  33. package/dist/modules/conditions/filterText.d.ts +16 -0
  34. package/dist/modules/conditions/filterText.js +156 -0
  35. package/dist/modules/filter/components/Filter.d.ts +1 -1
  36. package/dist/modules/filter/components/Filter.js +1 -1
  37. package/dist/modules/filter/components/FilterBar.js +3 -0
  38. package/dist/modules/list/ListFilter.d.ts +20 -7
  39. package/dist/modules/list/ListFilter.js +50 -17
  40. package/dist/modules/list/ViewBar.d.ts +2 -0
  41. package/dist/modules/list/ViewBar.js +21 -10
  42. package/dist/modules/list/filter/AddFilter.d.ts +10 -0
  43. package/dist/modules/list/filter/AddFilter.js +140 -0
  44. package/dist/modules/list/filter/AdvancedPopover.d.ts +8 -0
  45. package/dist/modules/list/filter/AdvancedPopover.js +103 -0
  46. package/dist/modules/list/filter/ListFilterBar.d.ts +3 -3
  47. package/dist/modules/list/filter/ListFilterBar.js +104 -198
  48. package/dist/modules/list/filter/RuleChips.d.ts +12 -2
  49. package/dist/modules/list/filter/RuleChips.js +132 -138
  50. package/dist/modules/list/filter/SearchChip.d.ts +6 -0
  51. package/dist/modules/list/filter/SearchChip.js +69 -0
  52. package/dist/modules/list/filter/filterRules.d.ts +15 -40
  53. package/dist/modules/list/filter/filterRules.js +27 -156
  54. package/dist/modules/list/filter/listFilterContext.d.ts +25 -18
  55. package/dist/modules/list/filter/listFilterContext.js +8 -24
  56. package/dist/modules/list/filter/listFilterQuery.d.ts +4 -0
  57. package/dist/modules/list/filter/listFilterQuery.js +10 -2
  58. package/dist/theme.css +3 -2
  59. package/exports.txt +1 -0
  60. package/package.json +1 -1
  61. package/dist/modules/conditions/ConditionValueInput.d.ts +0 -10
  62. package/dist/modules/conditions/ConditionValueInput.js +0 -166
  63. package/dist/modules/conditions/PathPicker.d.ts +0 -8
  64. package/dist/modules/conditions/PathPicker.js +0 -51
  65. package/dist/modules/list/filter/AdvancedFilterDialog.d.ts +0 -5
  66. package/dist/modules/list/filter/AdvancedFilterDialog.js +0 -52
  67. package/dist/modules/list/filter/FieldMenus.d.ts +0 -5
  68. package/dist/modules/list/filter/FieldMenus.js +0 -184
  69. package/dist/modules/list/filter/fieldIcons.d.ts +0 -7
  70. package/dist/modules/list/filter/fieldIcons.js +0 -16
@@ -1,157 +1,151 @@
1
- import { useFormat } from "../../../lib/format.js";
2
- import { useMoney } from "../../../lib/money.js";
3
- import { oneOf, textOf } from "../../../lib/text.js";
4
- import { OPERATOR_LABELS, RelativeAmount, isRule } from "../../conditions/conditions.js";
5
- import { Filter } from "../../filter/components/Filter.js";
6
- import { FIELD_ICONS } from "./fieldIcons.js";
7
- import { NUMBER_OPS, NUMBER_SYMBOLS, presetOf, valuesOf } from "./filterRules.js";
8
- import { useFieldOptions, useListFilter } from "./listFilterContext.js";
9
- import { IconAdjustmentsHorizontal } from "@tabler/icons-react";
10
- import { Fragment, jsx, jsxs } from "react/jsx-runtime";
1
+ import { Button } from "../../../components/button.js";
2
+ import { useItemKeys } from "../../../hooks/use-item-keys.js";
3
+ import { NO_VALUE } from "../../conditions/conditions.js";
4
+ import { isFilled, opOf, pathOf, ruleCount, withField, withOperator } from "../../conditions/filterRule.js";
5
+ import { FieldIcon } from "../../conditions/fieldIcons.js";
6
+ import { useRuleText } from "../../conditions/filterText.js";
7
+ import { FieldPart, OperatorPart, ValuePart } from "../../conditions/RuleParts.js";
8
+ import { useFieldAt, useListFilter } from "./listFilterContext.js";
9
+ import { IconAdjustmentsHorizontal, IconX } from "@tabler/icons-react";
10
+ import { jsx, jsxs } from "react/jsx-runtime";
11
+ import { useState } from "react";
11
12
  import { useTranslation } from "react-i18next";
12
13
  // src/modules/list/filter/RuleChips.tsx
13
- /** A filter bar chip: what it filters, the condition (opens it), remove. */
14
- const Chip = ({ name, children, onOpen, onRemove }) => {
15
- const { t } = useTranslation("views", { keyPrefix: "filter" });
16
- return /* @__PURE__ */ jsxs("div", {
17
- className: "flex h-7 items-stretch gap-px rounded bg-muted text-sm font-medium shadow-xs",
18
- children: [
19
- /* @__PURE__ */ jsx(Filter.BarName, { children: name }),
20
- /* @__PURE__ */ jsx(Filter.BarButton, {
21
- onClick: onOpen,
22
- children
23
- }),
24
- /* @__PURE__ */ jsx(Filter.BarClose, {
25
- "aria-label": t("remove", "Remove filter"),
26
- onClick: onRemove
27
- })
28
- ]
29
- });
30
- };
31
- /** Unknown paths (a removed property) read as text. */
32
- const textField = (path) => ({
33
- path,
34
- label: path,
35
- type: "text"
14
+ /** A chip: joined segments on a muted ground, the last one a remove button. */
15
+ const Chip = ({ children, removeLabel, onRemove }) => /* @__PURE__ */ jsxs("div", {
16
+ className: "flex h-7 max-w-full min-w-0 items-stretch gap-px overflow-hidden rounded bg-muted text-sm shadow-xs",
17
+ children: [children, /* @__PURE__ */ jsx(Button, {
18
+ variant: "segment",
19
+ size: "icon",
20
+ "aria-label": removeLabel,
21
+ onClick: onRemove,
22
+ children: /* @__PURE__ */ jsx(IconX, {})
23
+ })]
36
24
  });
37
- /** English for the `views` namespace's `filter.unit.<unit>`, by plural form. */
38
- const UNIT_LABELS = {
39
- days: {
40
- defaultValue_one: "{{count}} day",
41
- defaultValue_other: "{{count}} days"
42
- },
43
- weeks: {
44
- defaultValue_one: "{{count}} week",
45
- defaultValue_other: "{{count}} weeks"
46
- },
47
- months: {
48
- defaultValue_one: "{{count}} month",
49
- defaultValue_other: "{{count}} months"
50
- }
51
- };
52
- /** A rule as words: the operator and the value. */
53
- const useRuleText = (rule, field) => {
25
+ /** A segment of a chip that opens its editor. */
26
+ const Segment = ({ className, ...props }) => /* @__PURE__ */ jsx(Button, {
27
+ variant: "segment",
28
+ className: className ?? "max-w-60 min-w-0 gap-1.5 px-2",
29
+ ...props
30
+ });
31
+ /**
32
+ * A condition as a chip: its field, operator and value, each opening its editor in place, and
33
+ * remove. A change that leaves the rule unfinished (an operator that needs another value) waits
34
+ * on the chip until the value is picked; closing it unfinished keeps the rule as it was.
35
+ */
36
+ const RuleChip = ({ rule, index }) => {
54
37
  const { t } = useTranslation("views", { keyPrefix: "filter" });
55
- const { t: opT } = useTranslation("automations");
56
- const { options } = useFieldOptions(field);
57
- const money = useMoney();
58
- const fmt = useFormat();
59
- const op = opT(`op.${rule.op}`, OPERATOR_LABELS[rule.op]);
60
- const labelOf = (value) => options.find((option) => option.value === value)?.label ?? value;
61
- const preset = field.type === "date" ? presetOf(rule) : void 0;
62
- const span = RelativeAmount.safeParse(rule.value);
63
- const numberOp = field.type === "number" ? oneOf(NUMBER_OPS, rule.op) : void 0;
64
- const is = t("is", "is");
65
- if (preset) return {
66
- op: "",
67
- value: t(`date.${preset.key}`, preset.label)
68
- };
69
- if (rule.op === "exists" || rule.op === "not_exists") return {
70
- op,
71
- value: ""
38
+ const { fields, lookups, setPart } = useListFilter();
39
+ const [draft, setDraft] = useState(null);
40
+ const [open, setOpen] = useState(null);
41
+ const shown = draft ?? rule;
42
+ const field = useFieldAt(pathOf(shown));
43
+ const text = useRuleText(shown, field, lookups);
44
+ /** A finished rule applies; an unfinished one waits here, with its value open. */
45
+ const change = (next) => {
46
+ if (isFilled(next)) {
47
+ setDraft(null);
48
+ setPart(index, next);
49
+ } else {
50
+ setDraft(next);
51
+ setOpen("value");
52
+ }
72
53
  };
73
- if (span.success) {
74
- const { amount, unit } = span.data;
75
- return {
76
- op,
77
- value: t(`unit.${unit}`, {
78
- count: amount,
79
- ...UNIT_LABELS[unit]
80
- })
81
- };
82
- }
83
- if (field.type === "boolean") return {
84
- op: is,
85
- value: textOf(rule.value) === "true" ? t("yes", "Yes") : t("no", "No")
54
+ const toggle = (part) => (next) => {
55
+ setOpen(next ? part : null);
56
+ if (!next) setDraft(null);
86
57
  };
87
- if (numberOp) {
88
- const n = Number(rule.value);
89
- return {
90
- op: NUMBER_SYMBOLS[numberOp],
91
- value: field.currency ? money(n, field.currency) : fmt.number(n)
92
- };
93
- }
94
- if (rule.op === "contains" && field.type === "text") return {
95
- op,
96
- value: `“${textOf(rule.value)}”`
58
+ const pick = (next) => {
59
+ setOpen(null);
60
+ change(next);
97
61
  };
98
- const values = valuesOf(rule).map(labelOf);
99
- if (rule.op === "in" || rule.op === "eq") return {
100
- op: values.length > 1 ? t("is-any-of", "is any of") : is,
101
- value: values.join(", ")
102
- };
103
- return {
104
- op,
105
- value: values.join(", ")
106
- };
107
- };
108
- const RuleChip = ({ rule, index }) => {
109
- const { fields, edit, setPart } = useListFilter();
110
- const field = fields.find((one) => one.path === rule.path) ?? textField(rule.path);
111
- const text = useRuleText(rule, field);
112
- const Icon = field.icon ?? FIELD_ICONS[field.type];
113
62
  return /* @__PURE__ */ jsxs(Chip, {
114
- name: /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(Icon, {}), field.label] }),
115
- onOpen: () => edit(index),
63
+ removeLabel: t("remove-named", {
64
+ name: field.label,
65
+ defaultValue: "Remove {{name}} filter"
66
+ }),
116
67
  onRemove: () => setPart(index, null),
117
- children: [text.op && /* @__PURE__ */ jsx("span", {
118
- className: "font-normal text-muted-foreground",
119
- children: text.op
120
- }), /* @__PURE__ */ jsx("span", {
121
- className: "truncate",
122
- children: text.value
123
- })]
68
+ children: [
69
+ /* @__PURE__ */ jsx(FieldPart, {
70
+ fields,
71
+ field,
72
+ open: open === "field",
73
+ onOpenChange: toggle("field"),
74
+ onSelect: (next) => pick(withField(shown, field, next)),
75
+ button: /* @__PURE__ */ jsxs(Segment, {
76
+ className: "max-w-48 min-w-0 gap-1.5 rounded-l px-2 font-medium",
77
+ children: [/* @__PURE__ */ jsx(FieldIcon, {
78
+ field,
79
+ className: "text-muted-foreground"
80
+ }), /* @__PURE__ */ jsx("span", {
81
+ className: "truncate",
82
+ children: field.label
83
+ })]
84
+ })
85
+ }),
86
+ /* @__PURE__ */ jsx(OperatorPart, {
87
+ rule: shown,
88
+ field,
89
+ open: open === "op",
90
+ onOpenChange: toggle("op"),
91
+ onSelect: (op) => pick(withOperator(shown, op)),
92
+ button: /* @__PURE__ */ jsx(Segment, {
93
+ className: "shrink-0 px-2 text-muted-foreground",
94
+ children: text.op
95
+ })
96
+ }),
97
+ !NO_VALUE.has(opOf(shown)) && /* @__PURE__ */ jsx(ValuePart, {
98
+ rule: shown,
99
+ field,
100
+ lookups,
101
+ onChange: change,
102
+ open: open === "value",
103
+ onOpenChange: toggle("value"),
104
+ button: /* @__PURE__ */ jsx(Segment, {
105
+ "aria-label": t("value-of", {
106
+ name: field.label,
107
+ value: text.value,
108
+ defaultValue: "{{name}}: {{value}}"
109
+ }),
110
+ children: /* @__PURE__ */ jsx("span", {
111
+ className: "truncate font-medium",
112
+ children: text.value
113
+ })
114
+ })
115
+ })
116
+ ]
124
117
  });
125
118
  };
126
- /** A group from the advanced filter: how many conditions; it opens the advanced filter. */
127
- const GroupChip = ({ group, index }) => {
119
+ /** A filter with "or" or nested groups: one chip, which opens the advanced filter. */
120
+ const AdvancedChip = () => {
128
121
  const { t } = useTranslation("views", { keyPrefix: "filter" });
129
- const { openAdvanced, setPart } = useListFilter();
130
- const count = group.rules.length;
122
+ const { advanced, setAdvanced, setAdvancedOpen } = useListFilter();
123
+ const count = advanced ? ruleCount(advanced) : 0;
131
124
  return /* @__PURE__ */ jsx(Chip, {
132
- name: /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(IconAdjustmentsHorizontal, {}), t("advanced", "Advanced filter")] }),
133
- onOpen: openAdvanced,
134
- onRemove: () => setPart(index, null),
135
- children: group.match === "any" ? t("any-of", {
136
- count,
137
- defaultValue_one: "any of {{count}} condition",
138
- defaultValue_other: "any of {{count}} conditions"
139
- }) : t("conditions", {
140
- count,
141
- defaultValue_one: "{{count}} condition",
142
- defaultValue_other: "{{count}} conditions"
125
+ removeLabel: t("remove-advanced", "Remove the advanced filter"),
126
+ onRemove: () => setAdvanced(null),
127
+ children: /* @__PURE__ */ jsxs(Segment, {
128
+ className: "min-w-0 gap-1.5 rounded-l px-2 font-medium",
129
+ onClick: () => setAdvancedOpen(true),
130
+ children: [/* @__PURE__ */ jsx(IconAdjustmentsHorizontal, { className: "text-muted-foreground" }), t("advanced-chip", {
131
+ count,
132
+ defaultValue_one: "Advanced · {{count}} rule",
133
+ defaultValue_other: "Advanced · {{count}} rules"
134
+ })]
143
135
  })
144
136
  });
145
137
  };
146
- /** Each part of the list's filter as a chip (parts are distinct, so a part is its own key). */
138
+ /**
139
+ * The list's conditions as chips, one per rule (keyed by place, so an open editor stays open as
140
+ * its rule changes), or the advanced filter's chip.
141
+ */
147
142
  const RuleChips = () => {
148
- const { parts } = useListFilter();
149
- return parts.map((part, index) => isRule(part) ? /* @__PURE__ */ jsx(RuleChip, {
150
- rule: part,
151
- index
152
- }, JSON.stringify(part)) : /* @__PURE__ */ jsx(GroupChip, {
153
- group: part,
143
+ const { chips } = useListFilter();
144
+ const keys = useItemKeys(chips?.length ?? 0);
145
+ if (!chips) return /* @__PURE__ */ jsx(AdvancedChip, {});
146
+ return chips.map((rule, index) => /* @__PURE__ */ jsx(RuleChip, {
147
+ rule,
154
148
  index
155
- }, JSON.stringify(part)));
149
+ }, keys.keyAt(index)));
156
150
  };
157
- export { RuleChips };
151
+ export { Chip, RuleChips };
@@ -0,0 +1,6 @@
1
+ /** The search box: the list follows as typing pauses; Enter closes it. */
2
+ export declare const SearchBox: ({ onDone }: {
3
+ onDone: () => void;
4
+ }) => import("react").JSX.Element;
5
+ /** The search as a chip: what is searched, which opens the box; remove clears it. */
6
+ export declare const SearchChip: () => import("react").JSX.Element | null;
@@ -0,0 +1,69 @@
1
+ import { Button } from "../../../components/button.js";
2
+ import { Popover } from "../../../components/popover.js";
3
+ import { TypedInput } from "../../conditions/TypedInput.js";
4
+ import { useListConditions } from "./listFilterQuery.js";
5
+ import { Chip } from "./RuleChips.js";
6
+ import { IconSearch } from "@tabler/icons-react";
7
+ import { jsx, jsxs } from "react/jsx-runtime";
8
+ import { useState } from "react";
9
+ import { useTranslation } from "react-i18next";
10
+ // src/modules/list/filter/SearchChip.tsx
11
+ /** The search box: the list follows as typing pauses; Enter closes it. */
12
+ const SearchBox = ({ onDone }) => {
13
+ const { t } = useTranslation("views", { keyPrefix: "filter" });
14
+ const { search, setSearch } = useListConditions();
15
+ return /* @__PURE__ */ jsxs("div", {
16
+ className: "flex items-center gap-2 p-2",
17
+ children: [/* @__PURE__ */ jsx(IconSearch, { className: "size-4 shrink-0 text-muted-foreground" }), /* @__PURE__ */ jsx(TypedInput, {
18
+ focusOnMount: true,
19
+ className: "h-8",
20
+ "aria-label": t("search", "Search"),
21
+ placeholder: t("search-placeholder", "Search…"),
22
+ initial: search ?? "",
23
+ onCommit: (text) => setSearch(text.trim() || null),
24
+ onEnter: onDone
25
+ })]
26
+ });
27
+ };
28
+ /** The search as a chip: what is searched, which opens the box; remove clears it. */
29
+ const SearchChip = () => {
30
+ const { t } = useTranslation("views", { keyPrefix: "filter" });
31
+ const { search, setSearch } = useListConditions();
32
+ const [open, setOpen] = useState(false);
33
+ if (!search) return null;
34
+ return /* @__PURE__ */ jsx(Chip, {
35
+ removeLabel: t("remove-search", "Clear the search"),
36
+ onRemove: () => setSearch(null),
37
+ children: /* @__PURE__ */ jsxs(Popover, {
38
+ open,
39
+ onOpenChange: setOpen,
40
+ children: [/* @__PURE__ */ jsxs(Popover.Trigger, {
41
+ render: /* @__PURE__ */ jsx(Button, {
42
+ variant: "segment",
43
+ className: "max-w-60 min-w-0 gap-1.5 rounded-l px-2"
44
+ }),
45
+ children: [
46
+ /* @__PURE__ */ jsx(IconSearch, { className: "text-muted-foreground" }),
47
+ /* @__PURE__ */ jsx("span", {
48
+ className: "text-muted-foreground",
49
+ children: t("search", "Search")
50
+ }),
51
+ /* @__PURE__ */ jsxs("span", {
52
+ className: "truncate font-medium",
53
+ children: [
54
+ "“",
55
+ search,
56
+ "”"
57
+ ]
58
+ })
59
+ ]
60
+ }), /* @__PURE__ */ jsx(Popover.Content, {
61
+ variant: "flush",
62
+ align: "start",
63
+ className: "w-72",
64
+ children: /* @__PURE__ */ jsx(SearchBox, { onDone: () => setOpen(false) })
65
+ })]
66
+ })
67
+ });
68
+ };
69
+ export { SearchBox, SearchChip };
@@ -1,46 +1,21 @@
1
- import { RelativeAmount, TConditionGroup, TConditionRule } from "../../conditions/conditions.js";
2
- import { TListFilterField } from "../ListFilter.js";
3
- /** One thing the list's filter requires: a rule, or a group of rules (from the advanced filter). */
4
- export type FilterPart = TConditionRule | TConditionGroup;
5
- type Operator = TConditionRule['op'];
6
- /** The list's filter as parts that must all hold; an "any" group of several rules is one part. */
7
- export declare const partsOf: (group: TConditionGroup | null) => FilterPart[];
8
- /** Parts back to the list's filter: null when there are none. */
9
- export declare const groupOf: (parts: FilterPart[]) => TConditionGroup | null;
1
+ import { TConditionGroup, TConditionRule } from "../../conditions/conditions.js";
2
+ import { TFilterRule } from "../../conditions/filterRule.js";
10
3
  /**
11
- * Replaces the part at `index`, appends when `index` is null, removes when
12
- * `next` is null. Parts stay distinct: a part already there isn't added twice.
4
+ * The list's filter as chips: rules that must all hold. Null when it has "or" or nested groups:
5
+ * the advanced filter edits it, and the bar shows it as one chip.
13
6
  */
14
- export declare const withPart: (parts: FilterPart[], index: number | null, next: FilterPart | null) => FilterPart[];
15
- /** Comparisons of a number, as the menu writes them. */
16
- export declare const NUMBER_OPS: readonly ['eq', 'neq', 'gt', 'gte', 'lt', 'lte'];
17
- export declare const NUMBER_SYMBOLS: Record<(typeof NUMBER_OPS)[number], string>;
18
- /** Whether the menu can show and edit this rule. */
19
- export declare const inMenu: (rule: TConditionRule, field: TListFilterField | undefined) => boolean;
20
- /** Which part the menu of `field` changes: the one being edited, else its existing single rule, else a new one. */
21
- export declare const targetOf: (parts: FilterPart[], field: TListFilterField, editing: number | null) => number | null;
7
+ export declare const chipsOf: (group: TConditionGroup | null) => TFilterRule[] | null;
8
+ /** Parts back to the list's filter: null when there are none; one group stands for itself. */
9
+ export declare const groupOf: (parts: FilterPart[]) => TConditionGroup | null;
10
+ /** A top-level entry of the filter: a rule, or a group (the advanced filter's, or a "between"). */
11
+ export type FilterPart = TConditionRule | TConditionGroup;
22
12
  /**
23
- * The values a rule picks: the list of "is any of", or the one of "is". A value that isn't text or
24
- * numbers (a date range, a relative amount) picks nothing.
13
+ * The parts that must all hold: the chips, or the advanced filter as one part, so a chip added
14
+ * beside it narrows it.
25
15
  */
26
- export declare const valuesOf: (rule: TConditionRule | undefined) => string[];
27
- /** An "is any of" rule; null when nothing is picked. */
28
- export declare const anyOf: (path: string, values: string[]) => TConditionRule | null;
16
+ export declare const partsOf: (group: TConditionGroup | null) => FilterPart[];
29
17
  /**
30
- * Date filters in one pick, on the relative operators (they stay current in a saved view);
31
- * `label` is the English for the `views` namespace's `filter.date.<key>`.
18
+ * Replaces the part at `index`, appends when `index` is null, removes when `next` is null.
19
+ * Parts stay distinct: a part already there isn't added twice.
32
20
  */
33
- export declare const DATE_PRESETS: {
34
- key: string;
35
- label: string;
36
- op: Operator;
37
- value?: RelativeAmount;
38
- }[];
39
- /** The preset a rule is, if any. */
40
- export declare const presetOf: (rule: TConditionRule | undefined) => {
41
- key: string;
42
- label: string;
43
- op: Operator;
44
- value?: RelativeAmount;
45
- } | undefined;
46
- export {};
21
+ export declare const withPart: (parts: FilterPart[], index: number | null, next: FilterPart | null) => FilterPart[];
@@ -1,166 +1,37 @@
1
1
  import { isRule } from "../../conditions/conditions.js";
2
- import { z } from "zod";
2
+ import { isFilterRule } from "../../conditions/filterRule.js";
3
3
  // src/modules/list/filter/filterRules.ts
4
- /** The list's filter as parts that must all hold; an "any" group of several rules is one part. */
5
- const partsOf = (group) => !group ? [] : group.match === "all" || group.rules.length < 2 ? group.rules : [group];
6
- /** Parts back to the list's filter: null when there are none. */
7
- const groupOf = (parts) => parts.length ? {
8
- match: "all",
9
- rules: parts
10
- } : null;
11
- const same = (a, b) => JSON.stringify(a) === JSON.stringify(b);
12
4
  /**
13
- * Replaces the part at `index`, appends when `index` is null, removes when
14
- * `next` is null. Parts stay distinct: a part already there isn't added twice.
5
+ * The list's filter as chips: rules that must all hold. Null when it has "or" or nested groups:
6
+ * the advanced filter edits it, and the bar shows it as one chip.
15
7
  */
16
- const withPart = (parts, index, next) => {
17
- if (next && parts.some((part, at) => at !== index && same(part, next))) return index === null ? parts : parts.filter((_, at) => at !== index);
18
- if (index === null) return next ? [...parts, next] : parts;
19
- return next ? parts.map((part, at) => at === index ? next : part) : parts.filter((_, at) => at !== index);
20
- };
21
- /** Comparisons of a number, as the menu writes them. */
22
- const NUMBER_OPS = [
23
- "eq",
24
- "neq",
25
- "gt",
26
- "gte",
27
- "lt",
28
- "lte"
29
- ];
30
- const NUMBER_SYMBOLS = {
31
- eq: "=",
32
- neq: "≠",
33
- gt: ">",
34
- gte: "≥",
35
- lt: "<",
36
- lte: "≤"
37
- };
38
- /** The operators the filter menu shows and edits for each kind of field; other rules open the advanced filter. */
39
- const MENU_OPS = {
40
- select: ["in", "eq"],
41
- member: ["in", "eq"],
42
- tags: ["in", "eq"],
43
- boolean: ["eq"],
44
- date: [
45
- "within_last",
46
- "more_than_ago",
47
- "within_next",
48
- "exists",
49
- "not_exists"
50
- ],
51
- number: NUMBER_OPS,
52
- text: ["contains"]
8
+ const chipsOf = (group) => {
9
+ if (!group) return [];
10
+ if (group.match === "any" && group.rules.length > 1) return null;
11
+ return group.rules.every(isFilterRule) ? group.rules : null;
53
12
  };
54
- /** Whether the menu can show and edit this rule. */
55
- const inMenu = (rule, field) => !!field && MENU_OPS[field.type].includes(rule.op);
56
- /** Fields with one value at a time (a choice, a yes/no, a date range): picking again changes it rather than adding one. */
57
- const SINGLE = /* @__PURE__ */ new Set([
58
- "select",
59
- "member",
60
- "tags",
61
- "boolean",
62
- "date"
63
- ]);
64
- /** Which part the menu of `field` changes: the one being edited, else its existing single rule, else a new one. */
65
- const targetOf = (parts, field, editing) => {
66
- if (editing !== null) return editing;
67
- if (!SINGLE.has(field.type)) return null;
68
- const index = parts.findIndex((part) => isRule(part) && part.path === field.path && inMenu(part, field));
69
- return index < 0 ? null : index;
13
+ /** Parts back to the list's filter: null when there are none; one group stands for itself. */
14
+ const groupOf = (parts) => {
15
+ const [only] = parts;
16
+ if (parts.length === 1 && only && !isRule(only) && !isFilterRule(only)) return only;
17
+ return parts.length ? {
18
+ match: "all",
19
+ rules: parts
20
+ } : null;
70
21
  };
71
- const Values = z.array(z.union([z.string(), z.number()]).transform(String));
72
22
  /**
73
- * The values a rule picks: the list of "is any of", or the one of "is". A value that isn't text or
74
- * numbers (a date range, a relative amount) picks nothing.
23
+ * The parts that must all hold: the chips, or the advanced filter as one part, so a chip added
24
+ * beside it narrows it.
75
25
  */
76
- const valuesOf = (rule) => {
77
- if (!rule || rule.value === void 0 || rule.value === null) return [];
78
- return Values.safeParse(rule.op === "in" ? rule.value : [rule.value]).data ?? [];
79
- };
80
- /** An "is any of" rule; null when nothing is picked. */
81
- const anyOf = (path, values) => values.length ? {
82
- path,
83
- op: "in",
84
- value: values
85
- } : null;
86
- const days = (amount) => ({
87
- amount,
88
- unit: "days"
89
- });
90
- const months = (amount) => ({
91
- amount,
92
- unit: "months"
93
- });
26
+ const partsOf = (group) => chipsOf(group) ?? (group ? [group] : []);
27
+ const same = (a, b) => JSON.stringify(a) === JSON.stringify(b);
94
28
  /**
95
- * Date filters in one pick, on the relative operators (they stay current in a saved view);
96
- * `label` is the English for the `views` namespace's `filter.date.<key>`.
29
+ * Replaces the part at `index`, appends when `index` is null, removes when `next` is null.
30
+ * Parts stay distinct: a part already there isn't added twice.
97
31
  */
98
- const DATE_PRESETS = [
99
- {
100
- key: "past-day",
101
- label: "Past day",
102
- op: "within_last",
103
- value: days(1)
104
- },
105
- {
106
- key: "past-week",
107
- label: "Past 7 days",
108
- op: "within_last",
109
- value: days(7)
110
- },
111
- {
112
- key: "past-month",
113
- label: "Past 30 days",
114
- op: "within_last",
115
- value: days(30)
116
- },
117
- {
118
- key: "past-quarter",
119
- label: "Past 3 months",
120
- op: "within_last",
121
- value: months(3)
122
- },
123
- {
124
- key: "past-year",
125
- label: "Past year",
126
- op: "within_last",
127
- value: months(12)
128
- },
129
- {
130
- key: "older-month",
131
- label: "More than 30 days ago",
132
- op: "more_than_ago",
133
- value: days(30)
134
- },
135
- {
136
- key: "older-year",
137
- label: "More than a year ago",
138
- op: "more_than_ago",
139
- value: months(12)
140
- },
141
- {
142
- key: "next-week",
143
- label: "Next 7 days",
144
- op: "within_next",
145
- value: days(7)
146
- },
147
- {
148
- key: "next-month",
149
- label: "Next 30 days",
150
- op: "within_next",
151
- value: days(30)
152
- },
153
- {
154
- key: "set",
155
- label: "Is set",
156
- op: "exists"
157
- },
158
- {
159
- key: "empty",
160
- label: "Is empty",
161
- op: "not_exists"
162
- }
163
- ];
164
- /** The preset a rule is, if any. */
165
- const presetOf = (rule) => rule && DATE_PRESETS.find((preset) => preset.op === rule.op && JSON.stringify(preset.value) === JSON.stringify(rule.value));
166
- export { DATE_PRESETS, NUMBER_OPS, NUMBER_SYMBOLS, anyOf, groupOf, inMenu, partsOf, presetOf, targetOf, valuesOf, withPart };
32
+ const withPart = (parts, index, next) => {
33
+ if (next && parts.some((part, at) => at !== index && same(part, next))) return index === null ? parts : parts.filter((_, at) => at !== index);
34
+ if (index === null) return next ? [...parts, next] : parts;
35
+ return next ? parts.map((part, at) => at === index ? next : part) : parts.filter((_, at) => at !== index);
36
+ };
37
+ export { chipsOf, groupOf, partsOf, withPart };