@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
@@ -0,0 +1,164 @@
1
+ import { Combobox } from "../../components/combobox.js";
2
+ import { Command } from "../../components/command.js";
3
+ import { amountStep, fromAmountInput, toAmountInput } from "../../lib/money.js";
4
+ import { textOf } from "../../lib/text.js";
5
+ import { EntityPicker } from "../pickers/EntityPicker.js";
6
+ import { LIST_VALUE, NO_VALUE, RELATIVE, isRule } from "./conditions.js";
7
+ import { between, valuesBetween } from "./filterRule.js";
8
+ import { DayEditor, RelativeEditor } from "./DateEditors.js";
9
+ import { useFieldOptions } from "./fieldOptions.js";
10
+ import { valuesOf } from "./filterText.js";
11
+ import { TypedInput } from "./TypedInput.js";
12
+ import { jsx, jsxs } from "react/jsx-runtime";
13
+ import { useTranslation } from "react-i18next";
14
+ // src/modules/conditions/ValueEditor.tsx
15
+ /** Options (a choice, members, tags) by their labels: "is any of" picks several, others one. */
16
+ const OptionsEditor = ({ rule, field, lookups, onChange, onDone }) => {
17
+ const picked = valuesOf(rule);
18
+ const { options, loading, error } = useFieldOptions(field, lookups, picked);
19
+ const multiple = LIST_VALUE.has(rule.op);
20
+ return /* @__PURE__ */ jsx(EntityPicker, {
21
+ multiple,
22
+ items: options,
23
+ itemKey: (option) => ({
24
+ id: option.value,
25
+ label: option.label
26
+ }),
27
+ value: picked,
28
+ onValueChange: (values) => {
29
+ onChange({
30
+ ...rule,
31
+ value: multiple ? values : values[0]
32
+ });
33
+ if (!multiple) onDone?.();
34
+ },
35
+ loading,
36
+ error,
37
+ searchPlaceholder: field?.label
38
+ });
39
+ };
40
+ const BooleanEditor = ({ rule, onChange, onDone }) => {
41
+ const { t } = useTranslation("views", { keyPrefix: "filter" });
42
+ const value = textOf(rule.value);
43
+ return /* @__PURE__ */ jsx(Command, { children: /* @__PURE__ */ jsx(Command.List, { children: ["true", "false"].map((option) => /* @__PURE__ */ jsxs(Command.Item, {
44
+ value: option,
45
+ onSelect: () => {
46
+ onChange({
47
+ ...rule,
48
+ value: option === "true"
49
+ });
50
+ onDone?.();
51
+ },
52
+ children: [option === "true" ? t("yes", "Yes") : t("no", "No"), /* @__PURE__ */ jsx(Combobox.Check, { checked: value === option })]
53
+ }, option)) }) });
54
+ };
55
+ /** Text, or a list typed as "a, b, c" (an automation's "is any of" on text). */
56
+ const TextEditor = ({ rule, onChange, onDone }) => {
57
+ const { t } = useTranslation("views", { keyPrefix: "filter" });
58
+ const list = LIST_VALUE.has(rule.op);
59
+ return /* @__PURE__ */ jsx(TypedInput, {
60
+ focusOnMount: !!onDone,
61
+ className: "h-8",
62
+ "aria-label": t("value", "Value"),
63
+ placeholder: list ? t("list-placeholder", "a, b, c") : t("value", "Value"),
64
+ initial: list ? valuesOf(rule).join(", ") : textOf(rule.value),
65
+ onCommit: (text) => onChange({
66
+ ...rule,
67
+ value: list ? text.split(",").map((item) => item.trim()).filter(Boolean) : text.trim()
68
+ }),
69
+ onEnter: onDone
70
+ });
71
+ };
72
+ /** A number as typed: money in the currency's major unit. */
73
+ const numberText = (value, currency) => {
74
+ const n = Number(value);
75
+ if (value === void 0 || value === null || value === "" || !Number.isFinite(n)) return "";
76
+ return currency ? toAmountInput(n, currency) : String(n);
77
+ };
78
+ const numberOf = (text, currency) => {
79
+ if (!text.trim() || !Number.isFinite(Number(text))) return void 0;
80
+ return currency ? fromAmountInput(text, currency) : Number(text);
81
+ };
82
+ /** One number box; an amount of money is typed in the currency's major unit. */
83
+ const NumberInput = ({ value, currency, label, onChange, onEnter, focusOnMount }) => /* @__PURE__ */ jsx(TypedInput, {
84
+ focusOnMount,
85
+ type: "number",
86
+ className: "h-8 min-w-0 flex-1",
87
+ "aria-label": label,
88
+ placeholder: currency ?? label,
89
+ step: currency ? amountStep(currency) : "any",
90
+ initial: numberText(value, currency),
91
+ onCommit: (text) => onChange(numberOf(text, currency)),
92
+ onEnter
93
+ });
94
+ /** A number, or both ends of "between". */
95
+ const NumberEditor = ({ rule, field, onChange, onDone }) => {
96
+ const { t } = useTranslation("views", { keyPrefix: "filter" });
97
+ const currency = field?.currency;
98
+ if (isRule(rule)) return /* @__PURE__ */ jsx(NumberInput, {
99
+ focusOnMount: !!onDone,
100
+ value: rule.value,
101
+ currency,
102
+ label: t("value", "Value"),
103
+ onChange: (value) => onChange({
104
+ ...rule,
105
+ value
106
+ }),
107
+ onEnter: onDone
108
+ });
109
+ const [from, to] = valuesBetween(rule);
110
+ const path = rule.rules[0].path;
111
+ return /* @__PURE__ */ jsxs("div", {
112
+ className: "flex items-center gap-1.5",
113
+ children: [
114
+ /* @__PURE__ */ jsx(NumberInput, {
115
+ focusOnMount: !!onDone,
116
+ value: from,
117
+ currency,
118
+ label: t("from", "From"),
119
+ onChange: (value) => onChange(between(path, value, to))
120
+ }),
121
+ /* @__PURE__ */ jsx("span", {
122
+ className: "text-muted-foreground",
123
+ children: "–"
124
+ }),
125
+ /* @__PURE__ */ jsx(NumberInput, {
126
+ value: to,
127
+ currency,
128
+ label: t("to", "To"),
129
+ onChange: (value) => onChange(between(path, from, value)),
130
+ onEnter: onDone
131
+ })
132
+ ]
133
+ });
134
+ };
135
+ /** Whether the value is typed into a box (it fits in a row), rather than picked from a list. */
136
+ const isTypedValue = (rule, field) => {
137
+ if (!isRule(rule)) return field?.type !== "date";
138
+ if (NO_VALUE.has(rule.op) || RELATIVE.has(rule.op)) return false;
139
+ const type = field?.type;
140
+ if (type === "date" || type === "boolean" || type === "member" || type === "tags") return false;
141
+ return !field?.options?.length;
142
+ };
143
+ /**
144
+ * The editor for a rule's value, by its operator and its field's type: labelled options with
145
+ * search, text and numbers that apply as typing pauses, relative dates and days on a calendar,
146
+ * yes or no. None when the operator needs no value.
147
+ */
148
+ const ValueEditor = (props) => {
149
+ const { rule, field } = props;
150
+ const type = field?.type;
151
+ if (!isRule(rule)) return type === "date" ? /* @__PURE__ */ jsx(DayEditor, { ...props }) : /* @__PURE__ */ jsx(NumberEditor, { ...props });
152
+ const ruleProps = {
153
+ ...props,
154
+ rule
155
+ };
156
+ if (NO_VALUE.has(rule.op)) return null;
157
+ if (RELATIVE.has(rule.op)) return /* @__PURE__ */ jsx(RelativeEditor, { ...ruleProps });
158
+ if (type === "date") return /* @__PURE__ */ jsx(DayEditor, { ...props });
159
+ if (type === "number") return /* @__PURE__ */ jsx(NumberEditor, { ...props });
160
+ if (type === "boolean") return /* @__PURE__ */ jsx(BooleanEditor, { ...ruleProps });
161
+ if (type === "member" || type === "tags" || field?.options?.length) return /* @__PURE__ */ jsx(OptionsEditor, { ...ruleProps });
162
+ return /* @__PURE__ */ jsx(TextEditor, { ...ruleProps });
163
+ };
164
+ export { ValueEditor, isTypedValue };
@@ -1,5 +1,6 @@
1
1
  import { TagsQuery } from "../pickers/LookupPicker.js";
2
2
  import { MemberQueries } from "../pickers/members.js";
3
+ import { ElementType } from "react";
3
4
  import { z } from "zod";
4
5
  export declare const CONDITION_OPERATORS: readonly ['eq', 'neq', 'gt', 'gte', 'lt', 'lte', 'contains', 'not_contains', 'starts_with', 'in', 'not_in', 'exists', 'not_exists', 'within_last', 'more_than_ago', 'within_next', 'anniversary_within'];
5
6
  export type TConditionOperator = (typeof CONDITION_OPERATORS)[number];
@@ -31,16 +32,27 @@ export declare const RelativeAmount: z.ZodObject<{
31
32
  }>>;
32
33
  }, z.core.$strip>;
33
34
  export type RelativeAmount = z.infer<typeof RelativeAmount>;
35
+ /** A choice of a `select` field: what the rule holds, and what the picker shows. */
36
+ export type TFieldOption = {
37
+ value: string;
38
+ label: string;
39
+ };
34
40
  /**
35
- * A field conditions can use: a record's, or an earlier step's output. `group`: where it comes
36
- * from, for grouping in the picker (a list's advanced filter).
41
+ * A field conditions can use: a record's, or an earlier step's output. The picker lists fields
42
+ * under their `group`'s heading: `groupLabel` when the field brings one (a plugin's name), else
43
+ * the group's own name.
37
44
  */
38
45
  export type TConditionField = {
39
46
  path: string;
40
47
  label: string;
41
48
  type: string;
42
- options?: string[];
49
+ /** A `select`'s choices. */
50
+ options?: TFieldOption[];
43
51
  group?: string;
52
+ /** The heading of the field's group, instead of the group's own name. */
53
+ groupLabel?: string;
54
+ /** Instead of the type's icon. */
55
+ icon?: ElementType;
44
56
  /** A money amount, stored in this currency's smallest unit and typed in its major unit. */
45
57
  currency?: string;
46
58
  };
@@ -53,14 +65,9 @@ export type TConditionLookups = {
53
65
  members?: MemberQueries;
54
66
  tags?: TagsQuery;
55
67
  };
56
- export declare const OPERATOR_LABELS: Record<TConditionOperator, string>;
57
- /** Operators that make sense for a field's type. */
58
- export declare const operatorsFor: (type: string | undefined) => readonly ["eq", "neq"] | readonly ["contains", "not_contains", "exists", "not_exists"] | readonly ["eq", "neq", "in", "not_in", "exists", "not_exists"] | readonly ["eq", "neq", "gt", "gte", "lt", "lte", "exists", "not_exists"] | readonly ["within_last", "more_than_ago", "within_next", "anniversary_within", "eq", "gt", "lt", "exists", "not_exists"] | readonly ["eq", "neq", "gt", "gte", "lt", "lte", "contains", "not_contains", "starts_with", "in", "not_in", "exists", "not_exists", "within_last", "more_than_ago", "within_next", "anniversary_within"];
59
68
  export declare const NO_VALUE: Set<string>;
60
69
  export declare const RELATIVE: Set<string>;
61
70
  export declare const LIST_VALUE: Set<string>;
62
71
  /** Whether a condition entry is a rule rather than a nested group. */
63
72
  export declare const isRule: (rule: TConditionRule | TConditionGroup) => rule is TConditionRule;
64
- /** A relative date takes `{ amount, unit }`; switching to or from one resets the value. */
65
- export declare const withOperator: (rule: TConditionRule, op: TConditionOperator) => TConditionRule;
66
73
  export {};
@@ -49,65 +49,6 @@ const RelativeAmount = z.object({
49
49
  amount: z.int().min(0).max(MAX_AMOUNT),
50
50
  unit: z.enum(RELATIVE_UNITS).default("days")
51
51
  });
52
- const OPERATOR_LABELS = {
53
- eq: "is",
54
- neq: "is not",
55
- gt: "is greater than",
56
- gte: "is at least",
57
- lt: "is less than",
58
- lte: "is at most",
59
- contains: "contains",
60
- not_contains: "doesn't contain",
61
- starts_with: "starts with",
62
- in: "is one of",
63
- not_in: "is none of",
64
- exists: "is set",
65
- not_exists: "is empty",
66
- within_last: "in the last",
67
- more_than_ago: "more than … ago",
68
- within_next: "in the next",
69
- anniversary_within: "anniversary in the next"
70
- };
71
- /** Operators that make sense for a field's type. */
72
- const operatorsFor = (type) => {
73
- if (type === "date") return [
74
- "within_last",
75
- "more_than_ago",
76
- "within_next",
77
- "anniversary_within",
78
- "eq",
79
- "gt",
80
- "lt",
81
- "exists",
82
- "not_exists"
83
- ];
84
- if (type === "number") return [
85
- "eq",
86
- "neq",
87
- "gt",
88
- "gte",
89
- "lt",
90
- "lte",
91
- "exists",
92
- "not_exists"
93
- ];
94
- if (type === "tags" || type === "list") return [
95
- "contains",
96
- "not_contains",
97
- "exists",
98
- "not_exists"
99
- ];
100
- if (type === "boolean") return ["eq", "neq"];
101
- if (type === "select" || type === "member") return [
102
- "eq",
103
- "neq",
104
- "in",
105
- "not_in",
106
- "exists",
107
- "not_exists"
108
- ];
109
- return CONDITION_OPERATORS;
110
- };
111
52
  const NO_VALUE = /* @__PURE__ */ new Set(["exists", "not_exists"]);
112
53
  const RELATIVE = /* @__PURE__ */ new Set([
113
54
  "within_last",
@@ -118,19 +59,4 @@ const RELATIVE = /* @__PURE__ */ new Set([
118
59
  const LIST_VALUE = /* @__PURE__ */ new Set(["in", "not_in"]);
119
60
  /** Whether a condition entry is a rule rather than a nested group. */
120
61
  const isRule = (rule) => !("match" in rule);
121
- /** A relative date takes `{ amount, unit }`; switching to or from one resets the value. */
122
- const withOperator = (rule, op) => {
123
- if (RELATIVE.has(op) === RELATIVE.has(rule.op)) return {
124
- ...rule,
125
- op
126
- };
127
- return {
128
- ...rule,
129
- op,
130
- value: RELATIVE.has(op) ? {
131
- amount: 30,
132
- unit: "days"
133
- } : void 0
134
- };
135
- };
136
- export { CONDITION_OPERATORS, ConditionGroup, LIST_VALUE, NO_VALUE, OPERATOR_LABELS, RELATIVE, RELATIVE_UNITS, RelativeAmount, isRule, operatorsFor, withOperator };
62
+ export { CONDITION_OPERATORS, ConditionGroup, LIST_VALUE, NO_VALUE, RELATIVE, RELATIVE_UNITS, RelativeAmount, isRule };
@@ -0,0 +1,6 @@
1
+ import { TConditionField } from "./conditions.js";
2
+ /** A field's icon in the pickers and on its chips: its own, else its type's. */
3
+ export declare const FieldIcon: ({ field, className }: {
4
+ field: Pick<TConditionField, 'type' | 'icon'>;
5
+ className?: string;
6
+ }) => import("react").JSX.Element;
@@ -0,0 +1,19 @@
1
+ import { IconCalendar, IconHash, IconLetterCase, IconList, IconSelector, IconTags, IconToggleLeft, IconUser } from "@tabler/icons-react";
2
+ import { jsx } from "react/jsx-runtime";
3
+ // src/modules/conditions/fieldIcons.tsx
4
+ const FIELD_ICONS = {
5
+ text: IconLetterCase,
6
+ number: IconHash,
7
+ date: IconCalendar,
8
+ boolean: IconToggleLeft,
9
+ select: IconSelector,
10
+ member: IconUser,
11
+ tags: IconTags,
12
+ list: IconList
13
+ };
14
+ /** A field's icon in the pickers and on its chips: its own, else its type's. */
15
+ const FieldIcon = ({ field, className }) => {
16
+ const Icon = field.icon ?? FIELD_ICONS[field.type] ?? IconLetterCase;
17
+ return /* @__PURE__ */ jsx(Icon, { className });
18
+ };
19
+ export { FieldIcon };
@@ -0,0 +1,13 @@
1
+ import { TConditionField, TConditionLookups } from "./conditions.js";
2
+ /**
3
+ * What a choice field picks from, with labels: its `options`, the team ("Me" first), or the
4
+ * record type's tags. `picked` are the ids a rule holds, so a member who left still has a name.
5
+ */
6
+ export declare const useFieldOptions: (field: TConditionField | undefined, lookups: TConditionLookups, picked?: readonly string[]) => {
7
+ options: {
8
+ value: string;
9
+ label: string;
10
+ }[];
11
+ loading: boolean;
12
+ error: Error | null;
13
+ };
@@ -0,0 +1,26 @@
1
+ import { useMemberOptions, useTagOptions } from "../pickers/LookupPicker.js";
2
+ import { useTranslation } from "react-i18next";
3
+ // src/modules/conditions/fieldOptions.ts
4
+ /**
5
+ * What a choice field picks from, with labels: its `options`, the team ("Me" first), or the
6
+ * record type's tags. `picked` are the ids a rule holds, so a member who left still has a name.
7
+ */
8
+ const useFieldOptions = (field, lookups, picked = []) => {
9
+ const { t } = useTranslation("views", { keyPrefix: "filter" });
10
+ const members = useMemberOptions(lookups.members, field?.type === "member", picked);
11
+ const tags = useTagOptions(lookups.tags, field?.type === "tags");
12
+ if (field?.type === "member") return {
13
+ ...members,
14
+ options: [{
15
+ value: "me",
16
+ label: t("me", "Me")
17
+ }, ...members.options]
18
+ };
19
+ if (field?.type === "tags") return tags;
20
+ return {
21
+ options: field?.options ?? [],
22
+ loading: false,
23
+ error: null
24
+ };
25
+ };
26
+ export { useFieldOptions };
@@ -0,0 +1,45 @@
1
+ import { TConditionField, TConditionGroup, TConditionOperator, TConditionRule } from "./conditions.js";
2
+ /**
3
+ * "Between" isn't an operator of core's: it is a group of "at least" and "at most" on one field,
4
+ * which every reader of conditions (core's lists, automations, a plugin's API) already takes.
5
+ * The editors show such a group as one rule.
6
+ */
7
+ export type TBetween = TConditionGroup & {
8
+ match: 'all';
9
+ rules: [TConditionRule & {
10
+ op: 'gte';
11
+ }, TConditionRule & {
12
+ op: 'lte';
13
+ }];
14
+ };
15
+ /** A rule as the editors show one: a rule, or a "between" group. */
16
+ export type TFilterRule = TConditionRule | TBetween;
17
+ /** The operators the editors offer: core's, and "between". */
18
+ export type TFilterOperator = TConditionOperator | 'between';
19
+ export declare const isFilterRule: (node: TConditionRule | TConditionGroup) => node is TFilterRule;
20
+ export declare const pathOf: (rule: TFilterRule) => string;
21
+ export declare const opOf: (rule: TFilterRule) => TFilterOperator;
22
+ export declare const between: (path: string, from: unknown, to: unknown) => TBetween;
23
+ /** The operators that fit a field's type, in the order the menus list them. */
24
+ export declare const operatorsFor: (type: string | undefined) => readonly TFilterOperator[];
25
+ /** A new rule on `field`: its type's first operator, no value yet. */
26
+ export declare const ruleFor: (field: Pick<TConditionField, 'path' | 'type'>) => TFilterRule;
27
+ /** "Between"'s two values. */
28
+ export declare const valuesBetween: (rule: TBetween) => [unknown, unknown];
29
+ /** The same rule on another field: kept when the field reads the same way, else started anew. */
30
+ export declare const withField: (rule: TFilterRule, from: TConditionField | undefined, to: TConditionField) => TFilterRule;
31
+ /**
32
+ * The rule with another operator. The value stays while it still means the same (a list for
33
+ * "is any of" and "is none of", a number for the comparisons); otherwise it starts empty.
34
+ */
35
+ export declare const withOperator: (rule: TFilterRule, op: TFilterOperator) => TFilterRule;
36
+ /** Whether an editor's rule says enough: a "between" needs both ends. */
37
+ export declare const isFilled: (rule: TFilterRule) => boolean;
38
+ /**
39
+ * The conditions without the rules still being written: a group drops its unfinished rules,
40
+ * an empty group goes, and a group left with one rule is that rule (one side of a "between").
41
+ * Null when nothing is left.
42
+ */
43
+ export declare const completeOf: (group: TConditionGroup) => TConditionGroup | null;
44
+ /** How many rules a group holds, a "between" counting as one. */
45
+ export declare const ruleCount: (group: TConditionGroup) => number;
@@ -0,0 +1,169 @@
1
+ import { LIST_VALUE, NO_VALUE, RELATIVE, RelativeAmount, isRule } from "./conditions.js";
2
+ // src/modules/conditions/filterRule.ts
3
+ const isBetween = (node) => {
4
+ if (isRule(node) || node.match !== "all" || node.rules.length !== 2) return false;
5
+ const [from, to] = node.rules;
6
+ return !!from && !!to && isRule(from) && isRule(to) && from.op === "gte" && to.op === "lte" && from.path === to.path;
7
+ };
8
+ const isFilterRule = (node) => isRule(node) || isBetween(node);
9
+ const pathOf = (rule) => isRule(rule) ? rule.path : rule.rules[0].path;
10
+ const opOf = (rule) => isRule(rule) ? rule.op : "between";
11
+ const between = (path, from, to) => ({
12
+ match: "all",
13
+ rules: [{
14
+ path,
15
+ op: "gte",
16
+ value: from
17
+ }, {
18
+ path,
19
+ op: "lte",
20
+ value: to
21
+ }]
22
+ });
23
+ /** The operators that fit a field's type, in the order the menus list them. */
24
+ const operatorsFor = (type) => {
25
+ switch (type) {
26
+ case "date": return [
27
+ "within_last",
28
+ "within_next",
29
+ "more_than_ago",
30
+ "eq",
31
+ "lt",
32
+ "gt",
33
+ "between",
34
+ "anniversary_within",
35
+ "exists",
36
+ "not_exists"
37
+ ];
38
+ case "number": return [
39
+ "eq",
40
+ "neq",
41
+ "gt",
42
+ "gte",
43
+ "lt",
44
+ "lte",
45
+ "between",
46
+ "exists",
47
+ "not_exists"
48
+ ];
49
+ case "select":
50
+ case "member":
51
+ case "tags": return [
52
+ "in",
53
+ "not_in",
54
+ "exists",
55
+ "not_exists"
56
+ ];
57
+ case "list": return [
58
+ "contains",
59
+ "not_contains",
60
+ "exists",
61
+ "not_exists"
62
+ ];
63
+ case "boolean": return ["eq"];
64
+ default: return [
65
+ "contains",
66
+ "not_contains",
67
+ "eq",
68
+ "neq",
69
+ "starts_with",
70
+ "exists",
71
+ "not_exists"
72
+ ];
73
+ }
74
+ };
75
+ /** A new rule on `field`: its type's first operator, no value yet. */
76
+ const ruleFor = (field) => {
77
+ const op = operatorsFor(field.type)[0] ?? "eq";
78
+ if (op === "between") return between(field.path, void 0, void 0);
79
+ if (op === "within_last") return {
80
+ path: field.path,
81
+ op,
82
+ value: {
83
+ amount: 30,
84
+ unit: "days"
85
+ }
86
+ };
87
+ return {
88
+ path: field.path,
89
+ op,
90
+ value: field.type === "boolean" ? true : void 0
91
+ };
92
+ };
93
+ /** "Between"'s two values. */
94
+ const valuesBetween = (rule) => [rule.rules[0].value, rule.rules[1].value];
95
+ /** The same rule on another field: kept when the field reads the same way, else started anew. */
96
+ const withField = (rule, from, to) => {
97
+ if (from?.type !== to.type) return ruleFor(to);
98
+ return isRule(rule) ? {
99
+ ...rule,
100
+ path: to.path
101
+ } : between(to.path, ...valuesBetween(rule));
102
+ };
103
+ const kindOf = (op) => NO_VALUE.has(op) ? "none" : RELATIVE.has(op) ? "relative" : LIST_VALUE.has(op) ? "list" : "one";
104
+ /**
105
+ * The rule with another operator. The value stays while it still means the same (a list for
106
+ * "is any of" and "is none of", a number for the comparisons); otherwise it starts empty.
107
+ */
108
+ const withOperator = (rule, op) => {
109
+ const path = pathOf(rule);
110
+ const current = opOf(rule);
111
+ if (op === "between") return isRule(rule) && kindOf(current) === "one" ? between(path, rule.value, void 0) : between(path, void 0, void 0);
112
+ if (!isRule(rule)) return {
113
+ path,
114
+ op,
115
+ value: NO_VALUE.has(op) ? void 0 : rule.rules[0].value
116
+ };
117
+ if (kindOf(op) === kindOf(current)) return {
118
+ ...rule,
119
+ op
120
+ };
121
+ if (kindOf(op) === "relative") return {
122
+ path,
123
+ op,
124
+ value: {
125
+ amount: 30,
126
+ unit: "days"
127
+ }
128
+ };
129
+ if (kindOf(op) === "list" && rule.value !== void 0 && kindOf(current) === "one") return {
130
+ path,
131
+ op,
132
+ value: [rule.value]
133
+ };
134
+ return {
135
+ path,
136
+ op
137
+ };
138
+ };
139
+ const filled = (value) => value !== void 0 && value !== null && value !== "" && !(typeof value === "number" && !Number.isFinite(value));
140
+ /** Whether a rule says enough to filter by: its value is there, or it needs none. */
141
+ const isComplete = (rule) => {
142
+ if (NO_VALUE.has(rule.op)) return true;
143
+ if (RELATIVE.has(rule.op)) return RelativeAmount.safeParse(rule.value).success;
144
+ if (LIST_VALUE.has(rule.op)) return Array.isArray(rule.value) && rule.value.some(filled);
145
+ return filled(rule.value);
146
+ };
147
+ /** Whether an editor's rule says enough: a "between" needs both ends. */
148
+ const isFilled = (rule) => isRule(rule) ? isComplete(rule) : rule.rules.every(isComplete);
149
+ /**
150
+ * The conditions without the rules still being written: a group drops its unfinished rules,
151
+ * an empty group goes, and a group left with one rule is that rule (one side of a "between").
152
+ * Null when nothing is left.
153
+ */
154
+ const completeOf = (group) => {
155
+ const rules = group.rules.flatMap((node) => {
156
+ if (isRule(node)) return isComplete(node) ? [node] : [];
157
+ const inner = completeOf(node);
158
+ if (!inner) return [];
159
+ const [only] = inner.rules;
160
+ return inner.rules.length === 1 && only ? [only] : [inner];
161
+ });
162
+ return rules.length ? {
163
+ ...group,
164
+ rules
165
+ } : null;
166
+ };
167
+ /** How many rules a group holds, a "between" counting as one. */
168
+ const ruleCount = (group) => group.rules.reduce((count, node) => count + (isFilterRule(node) ? 1 : ruleCount(node)), 0);
169
+ export { between, completeOf, isFilled, isFilterRule, opOf, operatorsFor, pathOf, ruleCount, ruleFor, valuesBetween, withField, withOperator };
@@ -0,0 +1,16 @@
1
+ import { RelativeAmount, TConditionField, TConditionLookups, TConditionRule } from "./conditions.js";
2
+ import { TFilterOperator, TFilterRule } from "./filterRule.js";
3
+ /** An operator in words, for a field's type ("is before" on a date). */
4
+ export declare const useOperatorLabel: () => (op: TFilterOperator, type: string | undefined) => string;
5
+ /** A relative amount in words: "30 days". */
6
+ export declare const useSpanText: () => ({ amount, unit }: RelativeAmount) => string;
7
+ /**
8
+ * The values a rule picks: the list of "is any of", or the one of "is". A value that isn't text,
9
+ * a number or yes/no (a date range, a relative amount) picks nothing.
10
+ */
11
+ export declare const valuesOf: (rule: TConditionRule | undefined) => string[];
12
+ /** A rule in words, as a chip shows it: the operator and the value (empty when it needs none). */
13
+ export declare const useRuleText: (rule: TFilterRule, field: TConditionField | undefined, lookups: TConditionLookups) => {
14
+ op: string;
15
+ value: string;
16
+ };