@kivacore/ui-pro-shared 0.0.9 → 0.0.10
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/package.json +187 -21
- package/src/.kiva/config.json +27 -0
- package/src/components/avatar-group.tsx +222 -0
- package/src/components/kiva/bulk-update-dialog/bulk-update-dialog.tsx +240 -0
- package/src/components/kiva/bulk-update-dialog/index.tsx +6 -0
- package/src/components/kiva/bulk-update-dialog/types.ts +33 -0
- package/src/components/kiva/calendar/animations.ts +40 -0
- package/src/components/kiva/calendar/calendar-body.tsx +106 -0
- package/src/components/kiva/calendar/calendar-sidebar.tsx +103 -0
- package/src/components/kiva/calendar/calendar.tsx +243 -0
- package/src/components/kiva/calendar/constants.ts +12 -0
- package/src/components/kiva/calendar/contexts/calendar-context.tsx +696 -0
- package/src/components/kiva/calendar/contexts/dnd-context.tsx +173 -0
- package/src/components/kiva/calendar/dialogs/add-edit-event-dialog.tsx +287 -0
- package/src/components/kiva/calendar/dialogs/delete-event-dialog.tsx +73 -0
- package/src/components/kiva/calendar/dialogs/event-details-dialog.tsx +163 -0
- package/src/components/kiva/calendar/dialogs/events-list-dialog.tsx +122 -0
- package/src/components/kiva/calendar/dnd/draggable-event.tsx +66 -0
- package/src/components/kiva/calendar/dnd/droppable-area.tsx +53 -0
- package/src/components/kiva/calendar/dnd/resizable-event.tsx +190 -0
- package/src/components/kiva/calendar/header/calendar-header.tsx +84 -0
- package/src/components/kiva/calendar/header/date-navigator.tsx +188 -0
- package/src/components/kiva/calendar/header/filter.tsx +80 -0
- package/src/components/kiva/calendar/header/today-button.tsx +49 -0
- package/src/components/kiva/calendar/header/user-select.tsx +111 -0
- package/src/components/kiva/calendar/header/view-tabs.tsx +144 -0
- package/src/components/kiva/calendar/helpers.ts +617 -0
- package/src/components/kiva/calendar/hooks/use-disclosure.ts +19 -0
- package/src/components/kiva/calendar/hooks/use-local-storage.ts +40 -0
- package/src/components/kiva/calendar/hooks/use-media-query.ts +25 -0
- package/src/components/kiva/calendar/hooks.ts +5 -0
- package/src/components/kiva/calendar/index.tsx +27 -0
- package/src/components/kiva/calendar/interfaces.ts +35 -0
- package/src/components/kiva/calendar/schemas.ts +20 -0
- package/src/components/kiva/calendar/settings/settings.tsx +116 -0
- package/src/components/kiva/calendar/skeletons/calendar-header-skeleton.tsx +27 -0
- package/src/components/kiva/calendar/skeletons/calendar-skeleton.tsx +19 -0
- package/src/components/kiva/calendar/skeletons/day-view-skeleton.tsx +40 -0
- package/src/components/kiva/calendar/skeletons/month-view-skeleton.tsx +36 -0
- package/src/components/kiva/calendar/skeletons/week-view-skeleton.tsx +48 -0
- package/src/components/kiva/calendar/skeletons/year-view-skeleton.tsx +42 -0
- package/src/components/kiva/calendar/types.ts +25 -0
- package/src/components/kiva/calendar/views/agenda-view/agenda-events.tsx +150 -0
- package/src/components/kiva/calendar/views/month-view/calendar-month-view.tsx +82 -0
- package/src/components/kiva/calendar/views/month-view/day-cell.tsx +218 -0
- package/src/components/kiva/calendar/views/month-view/event-bullet.tsx +46 -0
- package/src/components/kiva/calendar/views/month-view/month-event-badge.tsx +154 -0
- package/src/components/kiva/calendar/views/timeline/calendar-timeline-view.tsx +100 -0
- package/src/components/kiva/calendar/views/timeline/lib/timeline-layout.ts +130 -0
- package/src/components/kiva/calendar/views/timeline/lib/timeline-model.ts +504 -0
- package/src/components/kiva/calendar/views/timeline/timeline-event-bar.tsx +68 -0
- package/src/components/kiva/calendar/views/timeline/timeline-grid-header.tsx +125 -0
- package/src/components/kiva/calendar/views/timeline/timeline-row.tsx +159 -0
- package/src/components/kiva/calendar/views/timeline/timeline-section.tsx +61 -0
- package/src/components/kiva/calendar/views/timeline/use-timeline-sections.ts +278 -0
- package/src/components/kiva/calendar/views/week-and-day-view/calendar-day-view.tsx +277 -0
- package/src/components/kiva/calendar/views/week-and-day-view/calendar-time-line.tsx +47 -0
- package/src/components/kiva/calendar/views/week-and-day-view/calendar-week-view.tsx +229 -0
- package/src/components/kiva/calendar/views/week-and-day-view/day-view-multi-day-events-row.tsx +79 -0
- package/src/components/kiva/calendar/views/week-and-day-view/event-block.tsx +116 -0
- package/src/components/kiva/calendar/views/week-and-day-view/render-grouped-events.tsx +54 -0
- package/src/components/kiva/calendar/views/week-and-day-view/week-view-multi-day-events-row.tsx +159 -0
- package/src/components/kiva/calendar/views/year-view/calendar-year-view.tsx +159 -0
- package/src/components/kiva/data-table-side-filters/components/side-filter-active-chips.tsx +137 -0
- package/src/components/kiva/data-table-side-filters/components/side-filter-clear.tsx +34 -0
- package/src/components/kiva/data-table-side-filters/components/side-filter-search.tsx +67 -0
- package/src/components/kiva/data-table-side-filters/components/side-filter-section.tsx +77 -0
- package/src/components/kiva/data-table-side-filters/controllers/side-filter-async-options.tsx +354 -0
- package/src/components/kiva/data-table-side-filters/controllers/side-filter-boolean.tsx +66 -0
- package/src/components/kiva/data-table-side-filters/controllers/side-filter-checkbox-list.tsx +137 -0
- package/src/components/kiva/data-table-side-filters/controllers/side-filter-date-range.tsx +204 -0
- package/src/components/kiva/data-table-side-filters/controllers/side-filter-numeric-range.tsx +162 -0
- package/src/components/kiva/data-table-side-filters/controllers/side-filter-options-dropdown.tsx +191 -0
- package/src/components/kiva/data-table-side-filters/controllers/side-filter-text.tsx +53 -0
- package/src/components/kiva/data-table-side-filters/core/query-translator.ts +367 -0
- package/src/components/kiva/data-table-side-filters/core/types.ts +81 -0
- package/src/components/kiva/data-table-side-filters/data-table-side-filters.tsx +483 -0
- package/src/components/kiva/data-table-side-filters/hooks/use-data-table-side-filters.tsx +160 -0
- package/src/components/kiva/data-table-side-filters/hooks/use-show-more.tsx +62 -0
- package/src/components/kiva/data-table-side-filters/index.tsx +44 -0
- package/src/components/kiva/data-table-side-filters/lib/render-mode.ts +62 -0
- package/src/components/kiva/date-time-picker.tsx +163 -0
- package/src/components/kiva/editable-record-detail.tsx +809 -0
- package/src/components/kiva/enum-option-editor/enum-option-editor.tsx +891 -0
- package/src/components/kiva/enum-option-editor/index.tsx +26 -0
- package/src/components/kiva/enum-option-editor/kiva-enum-option-editor.tsx +320 -0
- package/src/components/kiva/enum-option-editor/types.ts +90 -0
- package/src/components/kiva/kanban.tsx +1376 -0
- package/src/components/kiva/kiva-data-export-menu.tsx +543 -0
- package/src/components/kiva/map.tsx +1631 -0
- package/src/components/kiva/pivot-calendar/components/group-sidebar.tsx +95 -0
- package/src/components/kiva/pivot-calendar/components/pivot-calendar-toolbar.tsx +113 -0
- package/src/components/kiva/pivot-calendar/components/stats-cell.tsx +126 -0
- package/src/components/kiva/pivot-calendar/helpers.ts +374 -0
- package/src/components/kiva/pivot-calendar/hooks/use-pivot-calendar.ts +222 -0
- package/src/components/kiva/pivot-calendar/index.tsx +32 -0
- package/src/components/kiva/pivot-calendar/pivot-calendar.tsx +107 -0
- package/src/components/kiva/pivot-calendar/types.ts +154 -0
- package/src/components/kiva/pivot-calendar/views/days-of-month-view.tsx +37 -0
- package/src/components/kiva/pivot-calendar/views/days-of-week-view.tsx +37 -0
- package/src/components/kiva/pivot-calendar/views/month-calendar-view.tsx +154 -0
- package/src/components/kiva/pivot-calendar/views/months-of-year-view.tsx +40 -0
- package/src/components/kiva/pivot-calendar/views/pivot-grid-view.tsx +251 -0
- package/src/components/kiva/pivot-filter/index.tsx +9 -0
- package/src/components/kiva/pivot-filter/pivot-filter-item.tsx +159 -0
- package/src/components/kiva/pivot-filter/pivot-filter-settings.tsx +161 -0
- package/src/components/kiva/pivot-filter/pivot-filter.tsx +187 -0
- package/src/components/kiva/pivot-filter/types.ts +82 -0
- package/src/components/kiva/pivot-grid/hooks/use-pivot-grid.ts +683 -0
- package/src/components/kiva/pivot-grid/index.tsx +23 -0
- package/src/components/kiva/pivot-grid/lib/pivot-grid-color-rules.ts +98 -0
- package/src/components/kiva/pivot-grid/lib/pivot-grid-export.ts +265 -0
- package/src/components/kiva/pivot-grid/lib/pivot-grid-model.ts +1110 -0
- package/src/components/kiva/pivot-grid/pivot-grid-demo-data.ts +200 -0
- package/src/components/kiva/pivot-grid/pivot-grid-export-menu.tsx +66 -0
- package/src/components/kiva/pivot-grid/pivot-grid-toolbar.tsx +85 -0
- package/src/components/kiva/pivot-grid/pivot-grid.tsx +601 -0
- package/src/components/kiva/pivot-grid/types.ts +195 -0
- package/src/components/kiva/place-autocomplete.tsx +484 -0
- package/src/components/kiva/record-delete-confirm-dialog/index.tsx +10 -0
- package/src/components/kiva/record-delete-confirm-dialog/record-delete-confirm-dialog.tsx +266 -0
- package/src/components/kiva/record-delete-confirm-dialog/types.ts +25 -0
- package/src/hooks/kiva/_internal/autonumber-setting.ts +112 -0
- package/src/hooks/kiva/_internal/form-layout-to-view.ts +665 -0
- package/src/hooks/kiva/_internal/kiva-hook-utils.ts +143 -0
- package/src/hooks/kiva/_internal/use-computed-field-evaluator.tsx +310 -0
- package/src/hooks/kiva/_internal/use-field-actions-evaluator.tsx +398 -0
- package/src/hooks/kiva/_internal/use-form-actions-evaluator.tsx +363 -0
- package/src/hooks/kiva/_internal/use-form-view-engine.tsx +2200 -0
- package/src/hooks/kiva/_internal/use-kiva-form-view-fields.tsx +190 -0
- package/src/hooks/kiva/_internal/use-kiva-kanban-cards.tsx +311 -0
- package/src/hooks/kiva/use-data-export.tsx +265 -0
- package/src/hooks/kiva/use-kiva-calendar.tsx +992 -0
- package/src/hooks/kiva/use-kiva-data-export.tsx +173 -0
- package/src/hooks/kiva/use-kiva-data-grid.tsx +4750 -0
- package/src/hooks/kiva/use-kiva-data-view-select.tsx +1273 -0
- package/src/hooks/kiva/use-kiva-form-view.tsx +1581 -0
- package/src/hooks/kiva/use-kiva-inventory.tsx +184 -0
- package/src/hooks/kiva/use-kiva-kanban.tsx +1750 -0
- package/src/hooks/kiva/use-kiva-map-view.tsx +2255 -0
- package/src/hooks/kiva/use-kiva-pivot-calendar.tsx +759 -0
- package/src/hooks/kiva/use-kiva-pivot-filter.tsx +1523 -0
- package/src/hooks/kiva/use-kiva-pivot-grid.tsx +543 -0
- package/src/hooks/kiva/use-kiva-tenant.tsx +313 -0
- package/src/hooks/kiva/use-parameter-persistence.ts +191 -0
|
@@ -0,0 +1,398 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
// @ts-nocheck
|
|
4
|
+
/* eslint-disable */
|
|
5
|
+
/*
|
|
6
|
+
* Lives in `_internal/` so the registry scanner does not surface it as a
|
|
7
|
+
* standalone hook. Bundled as a transitive file under use-kiva-form-view.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import {
|
|
11
|
+
useCallback, useEffect, useRef, useState
|
|
12
|
+
} from 'react';
|
|
13
|
+
|
|
14
|
+
import { type RuleGroupTypeAny } from 'react-querybuilder';
|
|
15
|
+
|
|
16
|
+
import { evaluateJsonata } from '@kivacore/ui-pro-shared/components/kiva/jsonata-editor/lib/evaluate';
|
|
17
|
+
import {
|
|
18
|
+
computeDateBindings,
|
|
19
|
+
convertQueryToJsonata,
|
|
20
|
+
createHelperBindings
|
|
21
|
+
} from '@kivacore/ui-pro-shared/components/kiva/query-builder/lib/query-to-jsonata';
|
|
22
|
+
import {
|
|
23
|
+
type FieldAction,
|
|
24
|
+
type FieldActionBlockItem,
|
|
25
|
+
type FieldActionCondition,
|
|
26
|
+
type FieldActionPropertyOverrides,
|
|
27
|
+
type FieldActionStep,
|
|
28
|
+
type IField
|
|
29
|
+
} from '@kivacore/ui-pro-shared/components/kiva/form-fields/types';
|
|
30
|
+
|
|
31
|
+
/*
|
|
32
|
+
* ---------------------------------------------------------------------------
|
|
33
|
+
* Public interface
|
|
34
|
+
* ---------------------------------------------------------------------------
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
export interface FieldEntry {
|
|
38
|
+
slug: string;
|
|
39
|
+
field: IField;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface UseFieldActionsOptions {
|
|
43
|
+
/** All fields in the form — used to build the slug → actions lookup */
|
|
44
|
+
fields: FieldEntry[];
|
|
45
|
+
/**
|
|
46
|
+
* Per-field action overrides from `fieldLayout` — take priority over
|
|
47
|
+
* `IField.fieldActions` when both are present.
|
|
48
|
+
*/
|
|
49
|
+
fieldLayoutActions?: Partial<Record<string, FieldAction[]>>;
|
|
50
|
+
/**
|
|
51
|
+
* Called when an action step writes a new value to a field.
|
|
52
|
+
* Should be wired to `setValueExternal` in use-kiva-form-view.
|
|
53
|
+
*/
|
|
54
|
+
onSetValue: (slug: string, value: unknown) => void;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface UseFieldActionsResult {
|
|
58
|
+
/**
|
|
59
|
+
* Call this from `onFieldChange` whenever a field's value changes.
|
|
60
|
+
* `formValues` must be the current snapshot of all form values so
|
|
61
|
+
* conditions are evaluated against live data.
|
|
62
|
+
*/
|
|
63
|
+
triggerFieldChange: (
|
|
64
|
+
changedSlug: string,
|
|
65
|
+
newValue: unknown,
|
|
66
|
+
formValues: Record<string, unknown>
|
|
67
|
+
) => void;
|
|
68
|
+
/**
|
|
69
|
+
* Accumulated property overrides produced by past action runs.
|
|
70
|
+
* Stateful — overrides persist until explicitly changed by another action.
|
|
71
|
+
* Merge into `resolvedFields` at lower priority than computed formulas.
|
|
72
|
+
*/
|
|
73
|
+
propertyOverrides: Map<string, FieldActionPropertyOverrides>;
|
|
74
|
+
/** Clear all accumulated property overrides (hidden/readOnly/disabled/required). */
|
|
75
|
+
resetPropertyOverrides: () => void;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/*
|
|
79
|
+
* ---------------------------------------------------------------------------
|
|
80
|
+
* Hook
|
|
81
|
+
* ---------------------------------------------------------------------------
|
|
82
|
+
*/
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Evaluates field-level actions in response to field change events.
|
|
86
|
+
*
|
|
87
|
+
* Unlike `useComputedFieldEvaluator` (which re-evaluates reactively on every
|
|
88
|
+
* value change), this hook is **imperative + stateful**:
|
|
89
|
+
* - Actions only run when `triggerFieldChange` is called.
|
|
90
|
+
* - Property overrides accumulate and persist until another action changes them.
|
|
91
|
+
* - Blocks execute serially in `sortOrder` order.
|
|
92
|
+
* - Conditions use the same JSONata / QB-JSON evaluation pipeline as computed fields.
|
|
93
|
+
*
|
|
94
|
+
* Circular action protection: if triggerFieldChange is called recursively
|
|
95
|
+
* (e.g. action A sets field B which triggers action B which sets field A),
|
|
96
|
+
* execution stops after MAX_ACTION_DEPTH nested calls.
|
|
97
|
+
*/
|
|
98
|
+
|
|
99
|
+
const MAX_ACTION_DEPTH = 5;
|
|
100
|
+
|
|
101
|
+
export function useFieldActionsEvaluator({
|
|
102
|
+
fields,
|
|
103
|
+
fieldLayoutActions,
|
|
104
|
+
onSetValue
|
|
105
|
+
}: UseFieldActionsOptions): UseFieldActionsResult {
|
|
106
|
+
const actionMapRef = useRef<Map<string, FieldAction[]>>(new Map());
|
|
107
|
+
|
|
108
|
+
const helperBindingsRef = useRef<Record<string, unknown>>(
|
|
109
|
+
createHelperBindings() as Record<string, unknown>
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
const propertyOverridesRef = useRef<Map<string, FieldActionPropertyOverrides>>(new Map());
|
|
113
|
+
const [propertyOverrides, setPropertyOverrides] = useState<Map<string, FieldActionPropertyOverrides>>(
|
|
114
|
+
() => new Map()
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
const depthRef = useRef(0);
|
|
118
|
+
|
|
119
|
+
const onSetValueRef = useRef(onSetValue);
|
|
120
|
+
|
|
121
|
+
useEffect(() => {
|
|
122
|
+
onSetValueRef.current = onSetValue;
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
useEffect(() => {
|
|
126
|
+
const map = new Map<string, FieldAction[]>();
|
|
127
|
+
|
|
128
|
+
for (const { slug, field } of fields) {
|
|
129
|
+
const actions = fieldLayoutActions?.[slug] ?? field.fieldActions;
|
|
130
|
+
|
|
131
|
+
if (actions && actions.length > 0) {
|
|
132
|
+
map.set(slug, actions);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
actionMapRef.current = map;
|
|
137
|
+
}, [fields, fieldLayoutActions]);
|
|
138
|
+
|
|
139
|
+
const triggerFieldChange = useCallback(
|
|
140
|
+
(changedSlug: string, newValue: unknown, formValues: Record<string, unknown>) => {
|
|
141
|
+
const actions = actionMapRef.current.get(changedSlug);
|
|
142
|
+
|
|
143
|
+
if (!actions || actions.length === 0) return;
|
|
144
|
+
|
|
145
|
+
if (depthRef.current >= MAX_ACTION_DEPTH) {
|
|
146
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
147
|
+
console.warn(
|
|
148
|
+
`[useFieldActionsEvaluator] Max action depth (${MAX_ACTION_DEPTH}) reached while processing field "${changedSlug}". Possible circular action chain.`
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
depthRef.current += 1;
|
|
156
|
+
|
|
157
|
+
const context: Record<string, unknown> = { ...formValues, [changedSlug]: newValue };
|
|
158
|
+
|
|
159
|
+
void runActions(actions, context).then(({ valueChanges, nextOverrides }) => {
|
|
160
|
+
depthRef.current = Math.max(0, depthRef.current - 1);
|
|
161
|
+
|
|
162
|
+
for (const { slug, value } of valueChanges) {
|
|
163
|
+
onSetValueRef.current(slug, value);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (!mapsShallowEqual(propertyOverridesRef.current, nextOverrides)) {
|
|
167
|
+
propertyOverridesRef.current = nextOverrides;
|
|
168
|
+
setPropertyOverrides(new Map(nextOverrides));
|
|
169
|
+
}
|
|
170
|
+
}).catch(() => {
|
|
171
|
+
depthRef.current = Math.max(0, depthRef.current - 1);
|
|
172
|
+
});
|
|
173
|
+
},
|
|
174
|
+
[]
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
/*
|
|
178
|
+
* ---------------------------------------------------------------------------
|
|
179
|
+
* Internal: run all FieldAction[] for a trigger, return mutations
|
|
180
|
+
* ---------------------------------------------------------------------------
|
|
181
|
+
*/
|
|
182
|
+
|
|
183
|
+
async function runActions(
|
|
184
|
+
actions: FieldAction[],
|
|
185
|
+
context: Record<string, unknown>
|
|
186
|
+
): Promise<{
|
|
187
|
+
valueChanges: Array<{ slug: string; value: unknown }>;
|
|
188
|
+
nextOverrides: Map<string, FieldActionPropertyOverrides>;
|
|
189
|
+
}> {
|
|
190
|
+
const valueChanges: Array<{ slug: string; value: unknown }> = [];
|
|
191
|
+
const nextOverrides = new Map(propertyOverridesRef.current);
|
|
192
|
+
const nowMs = Date.now();
|
|
193
|
+
const helperBindings = helperBindingsRef.current;
|
|
194
|
+
|
|
195
|
+
for (const action of actions) {
|
|
196
|
+
if (action.triggerType !== 'onFieldChange') continue;
|
|
197
|
+
|
|
198
|
+
const sortedBlocks = [...action.blocks].sort((a, b) => a.sortOrder - b.sortOrder);
|
|
199
|
+
|
|
200
|
+
for (const block of sortedBlocks) {
|
|
201
|
+
let matchedSteps: FieldActionStep[] | null = null;
|
|
202
|
+
|
|
203
|
+
for (const item of block.conditionalItems) {
|
|
204
|
+
const condMet = await evaluateCondition(item.condition, context, nowMs, helperBindings);
|
|
205
|
+
|
|
206
|
+
if (condMet) {
|
|
207
|
+
matchedSteps = item.actions;
|
|
208
|
+
break;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
if (matchedSteps === null) {
|
|
213
|
+
matchedSteps = block.elseActions;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
for (const step of matchedSteps) {
|
|
217
|
+
await applyStep(step, nextOverrides, valueChanges, context, helperBindings);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
for (const step of block.unconditionalActions) {
|
|
221
|
+
await applyStep(step, nextOverrides, valueChanges, context, helperBindings);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
return { valueChanges, nextOverrides };
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const resetPropertyOverrides = useCallback(() => {
|
|
230
|
+
propertyOverridesRef.current = new Map();
|
|
231
|
+
setPropertyOverrides(new Map());
|
|
232
|
+
}, []);
|
|
233
|
+
|
|
234
|
+
return { triggerFieldChange, propertyOverrides, resetPropertyOverrides };
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/*
|
|
238
|
+
* ---------------------------------------------------------------------------
|
|
239
|
+
* Condition evaluation
|
|
240
|
+
* ---------------------------------------------------------------------------
|
|
241
|
+
*/
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Evaluates a FieldActionCondition against the current form context.
|
|
245
|
+
* - null / undefined / empty → always true (unconditional)
|
|
246
|
+
* - string → direct JSONata expression
|
|
247
|
+
* - QB JSON object → converted to JSONata first
|
|
248
|
+
*/
|
|
249
|
+
async function evaluateCondition(
|
|
250
|
+
condition: FieldActionCondition,
|
|
251
|
+
context: Record<string, unknown>,
|
|
252
|
+
nowMs: number,
|
|
253
|
+
helperBindings: Record<string, unknown>
|
|
254
|
+
): Promise<boolean> {
|
|
255
|
+
if (!condition) return true;
|
|
256
|
+
|
|
257
|
+
let expression: string;
|
|
258
|
+
let dateBindings: ReturnType<typeof computeDateBindings> = {};
|
|
259
|
+
|
|
260
|
+
if (typeof condition === 'string') {
|
|
261
|
+
const trimmed = condition.trim();
|
|
262
|
+
|
|
263
|
+
if (!trimmed) return true;
|
|
264
|
+
expression = trimmed;
|
|
265
|
+
} else if (
|
|
266
|
+
typeof condition === 'object'
|
|
267
|
+
&& 'combinator' in condition
|
|
268
|
+
&& 'rules' in condition
|
|
269
|
+
&& Array.isArray(condition.rules)
|
|
270
|
+
&& condition.rules.length > 0
|
|
271
|
+
) {
|
|
272
|
+
const converted = convertQueryToJsonata(condition as RuleGroupTypeAny);
|
|
273
|
+
|
|
274
|
+
({ expression } = converted);
|
|
275
|
+
dateBindings = computeDateBindings(converted.dateBindings, nowMs);
|
|
276
|
+
} else {
|
|
277
|
+
return true;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
const result = await evaluateJsonata(expression, context, {
|
|
281
|
+
bindings: { ...helperBindings, ...dateBindings }
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
return result.status === 'success' ? Boolean(result.result) : false;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/*
|
|
288
|
+
* ---------------------------------------------------------------------------
|
|
289
|
+
* Step dispatcher
|
|
290
|
+
* ---------------------------------------------------------------------------
|
|
291
|
+
*/
|
|
292
|
+
|
|
293
|
+
async function applyStep(
|
|
294
|
+
step: FieldActionStep,
|
|
295
|
+
overrides: Map<string, FieldActionPropertyOverrides>,
|
|
296
|
+
valueChanges: Array<{ slug: string; value: unknown }>,
|
|
297
|
+
context: Record<string, unknown>,
|
|
298
|
+
helperBindings: Record<string, unknown>
|
|
299
|
+
): Promise<void> {
|
|
300
|
+
switch (step.method) {
|
|
301
|
+
case 'setFieldValue':
|
|
302
|
+
valueChanges.push({ slug: step.fieldSlug, value: step.value });
|
|
303
|
+
break;
|
|
304
|
+
|
|
305
|
+
case 'setFieldValueFromFormula': {
|
|
306
|
+
const expression = step.formula?.trim();
|
|
307
|
+
|
|
308
|
+
if (!expression) break;
|
|
309
|
+
|
|
310
|
+
const result = await evaluateJsonata(expression, context, { bindings: helperBindings });
|
|
311
|
+
|
|
312
|
+
/*
|
|
313
|
+
* A failed expression writes NOTHING. Writing `undefined` would clear a
|
|
314
|
+
* value the user may have typed, which is worse than the rule not firing.
|
|
315
|
+
*/
|
|
316
|
+
if (result.status === 'success') {
|
|
317
|
+
valueChanges.push({ slug: step.fieldSlug, value: result.result });
|
|
318
|
+
}
|
|
319
|
+
break;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
case 'setFieldValues':
|
|
323
|
+
for (const { fieldSlug, value } of step.fields) {
|
|
324
|
+
valueChanges.push({ slug: fieldSlug, value });
|
|
325
|
+
}
|
|
326
|
+
break;
|
|
327
|
+
|
|
328
|
+
case 'clearFieldValue':
|
|
329
|
+
valueChanges.push({ slug: step.fieldSlug, value: null });
|
|
330
|
+
break;
|
|
331
|
+
|
|
332
|
+
case 'showField':
|
|
333
|
+
mergeOverride(overrides, step.fieldSlug, { hidden: false });
|
|
334
|
+
break;
|
|
335
|
+
|
|
336
|
+
case 'hideField':
|
|
337
|
+
mergeOverride(overrides, step.fieldSlug, { hidden: true });
|
|
338
|
+
break;
|
|
339
|
+
|
|
340
|
+
case 'setFieldRequired':
|
|
341
|
+
mergeOverride(overrides, step.fieldSlug, { required: step.required });
|
|
342
|
+
break;
|
|
343
|
+
|
|
344
|
+
case 'setFieldDisabled':
|
|
345
|
+
mergeOverride(overrides, step.fieldSlug, { disabled: step.disabled });
|
|
346
|
+
break;
|
|
347
|
+
|
|
348
|
+
case 'setFieldReadOnly':
|
|
349
|
+
mergeOverride(overrides, step.fieldSlug, { readOnly: step.readOnly });
|
|
350
|
+
break;
|
|
351
|
+
|
|
352
|
+
default:
|
|
353
|
+
break;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/*
|
|
358
|
+
* ---------------------------------------------------------------------------
|
|
359
|
+
* Helpers
|
|
360
|
+
* ---------------------------------------------------------------------------
|
|
361
|
+
*/
|
|
362
|
+
|
|
363
|
+
function mergeOverride(
|
|
364
|
+
map: Map<string, FieldActionPropertyOverrides>,
|
|
365
|
+
slug: string,
|
|
366
|
+
patch: Partial<FieldActionPropertyOverrides>
|
|
367
|
+
): void {
|
|
368
|
+
const existing = map.get(slug) ?? {};
|
|
369
|
+
|
|
370
|
+
map.set(slug, { ...existing, ...patch });
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/** Shallow-equal check on Map<string, FieldActionPropertyOverrides> to avoid unnecessary re-renders */
|
|
374
|
+
function mapsShallowEqual(
|
|
375
|
+
a: Map<string, FieldActionPropertyOverrides>,
|
|
376
|
+
b: Map<string, FieldActionPropertyOverrides>
|
|
377
|
+
): boolean {
|
|
378
|
+
if (a.size !== b.size) return false;
|
|
379
|
+
|
|
380
|
+
for (const [slug, aVal] of a.entries()) {
|
|
381
|
+
const bVal = b.get(slug);
|
|
382
|
+
|
|
383
|
+
if (!bVal) return false;
|
|
384
|
+
|
|
385
|
+
if (
|
|
386
|
+
aVal.hidden !== bVal.hidden
|
|
387
|
+
|| aVal.required !== bVal.required
|
|
388
|
+
|| aVal.disabled !== bVal.disabled
|
|
389
|
+
|| aVal.readOnly !== bVal.readOnly
|
|
390
|
+
) {
|
|
391
|
+
return false;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
return true;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
export type { FieldActionBlockItem };
|