@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.
Files changed (145) hide show
  1. package/package.json +187 -21
  2. package/src/.kiva/config.json +27 -0
  3. package/src/components/avatar-group.tsx +222 -0
  4. package/src/components/kiva/bulk-update-dialog/bulk-update-dialog.tsx +240 -0
  5. package/src/components/kiva/bulk-update-dialog/index.tsx +6 -0
  6. package/src/components/kiva/bulk-update-dialog/types.ts +33 -0
  7. package/src/components/kiva/calendar/animations.ts +40 -0
  8. package/src/components/kiva/calendar/calendar-body.tsx +106 -0
  9. package/src/components/kiva/calendar/calendar-sidebar.tsx +103 -0
  10. package/src/components/kiva/calendar/calendar.tsx +243 -0
  11. package/src/components/kiva/calendar/constants.ts +12 -0
  12. package/src/components/kiva/calendar/contexts/calendar-context.tsx +696 -0
  13. package/src/components/kiva/calendar/contexts/dnd-context.tsx +173 -0
  14. package/src/components/kiva/calendar/dialogs/add-edit-event-dialog.tsx +287 -0
  15. package/src/components/kiva/calendar/dialogs/delete-event-dialog.tsx +73 -0
  16. package/src/components/kiva/calendar/dialogs/event-details-dialog.tsx +163 -0
  17. package/src/components/kiva/calendar/dialogs/events-list-dialog.tsx +122 -0
  18. package/src/components/kiva/calendar/dnd/draggable-event.tsx +66 -0
  19. package/src/components/kiva/calendar/dnd/droppable-area.tsx +53 -0
  20. package/src/components/kiva/calendar/dnd/resizable-event.tsx +190 -0
  21. package/src/components/kiva/calendar/header/calendar-header.tsx +84 -0
  22. package/src/components/kiva/calendar/header/date-navigator.tsx +188 -0
  23. package/src/components/kiva/calendar/header/filter.tsx +80 -0
  24. package/src/components/kiva/calendar/header/today-button.tsx +49 -0
  25. package/src/components/kiva/calendar/header/user-select.tsx +111 -0
  26. package/src/components/kiva/calendar/header/view-tabs.tsx +144 -0
  27. package/src/components/kiva/calendar/helpers.ts +617 -0
  28. package/src/components/kiva/calendar/hooks/use-disclosure.ts +19 -0
  29. package/src/components/kiva/calendar/hooks/use-local-storage.ts +40 -0
  30. package/src/components/kiva/calendar/hooks/use-media-query.ts +25 -0
  31. package/src/components/kiva/calendar/hooks.ts +5 -0
  32. package/src/components/kiva/calendar/index.tsx +27 -0
  33. package/src/components/kiva/calendar/interfaces.ts +35 -0
  34. package/src/components/kiva/calendar/schemas.ts +20 -0
  35. package/src/components/kiva/calendar/settings/settings.tsx +116 -0
  36. package/src/components/kiva/calendar/skeletons/calendar-header-skeleton.tsx +27 -0
  37. package/src/components/kiva/calendar/skeletons/calendar-skeleton.tsx +19 -0
  38. package/src/components/kiva/calendar/skeletons/day-view-skeleton.tsx +40 -0
  39. package/src/components/kiva/calendar/skeletons/month-view-skeleton.tsx +36 -0
  40. package/src/components/kiva/calendar/skeletons/week-view-skeleton.tsx +48 -0
  41. package/src/components/kiva/calendar/skeletons/year-view-skeleton.tsx +42 -0
  42. package/src/components/kiva/calendar/types.ts +25 -0
  43. package/src/components/kiva/calendar/views/agenda-view/agenda-events.tsx +150 -0
  44. package/src/components/kiva/calendar/views/month-view/calendar-month-view.tsx +82 -0
  45. package/src/components/kiva/calendar/views/month-view/day-cell.tsx +218 -0
  46. package/src/components/kiva/calendar/views/month-view/event-bullet.tsx +46 -0
  47. package/src/components/kiva/calendar/views/month-view/month-event-badge.tsx +154 -0
  48. package/src/components/kiva/calendar/views/timeline/calendar-timeline-view.tsx +100 -0
  49. package/src/components/kiva/calendar/views/timeline/lib/timeline-layout.ts +130 -0
  50. package/src/components/kiva/calendar/views/timeline/lib/timeline-model.ts +504 -0
  51. package/src/components/kiva/calendar/views/timeline/timeline-event-bar.tsx +68 -0
  52. package/src/components/kiva/calendar/views/timeline/timeline-grid-header.tsx +125 -0
  53. package/src/components/kiva/calendar/views/timeline/timeline-row.tsx +159 -0
  54. package/src/components/kiva/calendar/views/timeline/timeline-section.tsx +61 -0
  55. package/src/components/kiva/calendar/views/timeline/use-timeline-sections.ts +278 -0
  56. package/src/components/kiva/calendar/views/week-and-day-view/calendar-day-view.tsx +277 -0
  57. package/src/components/kiva/calendar/views/week-and-day-view/calendar-time-line.tsx +47 -0
  58. package/src/components/kiva/calendar/views/week-and-day-view/calendar-week-view.tsx +229 -0
  59. package/src/components/kiva/calendar/views/week-and-day-view/day-view-multi-day-events-row.tsx +79 -0
  60. package/src/components/kiva/calendar/views/week-and-day-view/event-block.tsx +116 -0
  61. package/src/components/kiva/calendar/views/week-and-day-view/render-grouped-events.tsx +54 -0
  62. package/src/components/kiva/calendar/views/week-and-day-view/week-view-multi-day-events-row.tsx +159 -0
  63. package/src/components/kiva/calendar/views/year-view/calendar-year-view.tsx +159 -0
  64. package/src/components/kiva/data-table-side-filters/components/side-filter-active-chips.tsx +137 -0
  65. package/src/components/kiva/data-table-side-filters/components/side-filter-clear.tsx +34 -0
  66. package/src/components/kiva/data-table-side-filters/components/side-filter-search.tsx +67 -0
  67. package/src/components/kiva/data-table-side-filters/components/side-filter-section.tsx +77 -0
  68. package/src/components/kiva/data-table-side-filters/controllers/side-filter-async-options.tsx +354 -0
  69. package/src/components/kiva/data-table-side-filters/controllers/side-filter-boolean.tsx +66 -0
  70. package/src/components/kiva/data-table-side-filters/controllers/side-filter-checkbox-list.tsx +137 -0
  71. package/src/components/kiva/data-table-side-filters/controllers/side-filter-date-range.tsx +204 -0
  72. package/src/components/kiva/data-table-side-filters/controllers/side-filter-numeric-range.tsx +162 -0
  73. package/src/components/kiva/data-table-side-filters/controllers/side-filter-options-dropdown.tsx +191 -0
  74. package/src/components/kiva/data-table-side-filters/controllers/side-filter-text.tsx +53 -0
  75. package/src/components/kiva/data-table-side-filters/core/query-translator.ts +367 -0
  76. package/src/components/kiva/data-table-side-filters/core/types.ts +81 -0
  77. package/src/components/kiva/data-table-side-filters/data-table-side-filters.tsx +483 -0
  78. package/src/components/kiva/data-table-side-filters/hooks/use-data-table-side-filters.tsx +160 -0
  79. package/src/components/kiva/data-table-side-filters/hooks/use-show-more.tsx +62 -0
  80. package/src/components/kiva/data-table-side-filters/index.tsx +44 -0
  81. package/src/components/kiva/data-table-side-filters/lib/render-mode.ts +62 -0
  82. package/src/components/kiva/date-time-picker.tsx +163 -0
  83. package/src/components/kiva/editable-record-detail.tsx +809 -0
  84. package/src/components/kiva/enum-option-editor/enum-option-editor.tsx +891 -0
  85. package/src/components/kiva/enum-option-editor/index.tsx +26 -0
  86. package/src/components/kiva/enum-option-editor/kiva-enum-option-editor.tsx +320 -0
  87. package/src/components/kiva/enum-option-editor/types.ts +90 -0
  88. package/src/components/kiva/kanban.tsx +1376 -0
  89. package/src/components/kiva/kiva-data-export-menu.tsx +543 -0
  90. package/src/components/kiva/map.tsx +1631 -0
  91. package/src/components/kiva/pivot-calendar/components/group-sidebar.tsx +95 -0
  92. package/src/components/kiva/pivot-calendar/components/pivot-calendar-toolbar.tsx +113 -0
  93. package/src/components/kiva/pivot-calendar/components/stats-cell.tsx +126 -0
  94. package/src/components/kiva/pivot-calendar/helpers.ts +374 -0
  95. package/src/components/kiva/pivot-calendar/hooks/use-pivot-calendar.ts +222 -0
  96. package/src/components/kiva/pivot-calendar/index.tsx +32 -0
  97. package/src/components/kiva/pivot-calendar/pivot-calendar.tsx +107 -0
  98. package/src/components/kiva/pivot-calendar/types.ts +154 -0
  99. package/src/components/kiva/pivot-calendar/views/days-of-month-view.tsx +37 -0
  100. package/src/components/kiva/pivot-calendar/views/days-of-week-view.tsx +37 -0
  101. package/src/components/kiva/pivot-calendar/views/month-calendar-view.tsx +154 -0
  102. package/src/components/kiva/pivot-calendar/views/months-of-year-view.tsx +40 -0
  103. package/src/components/kiva/pivot-calendar/views/pivot-grid-view.tsx +251 -0
  104. package/src/components/kiva/pivot-filter/index.tsx +9 -0
  105. package/src/components/kiva/pivot-filter/pivot-filter-item.tsx +159 -0
  106. package/src/components/kiva/pivot-filter/pivot-filter-settings.tsx +161 -0
  107. package/src/components/kiva/pivot-filter/pivot-filter.tsx +187 -0
  108. package/src/components/kiva/pivot-filter/types.ts +82 -0
  109. package/src/components/kiva/pivot-grid/hooks/use-pivot-grid.ts +683 -0
  110. package/src/components/kiva/pivot-grid/index.tsx +23 -0
  111. package/src/components/kiva/pivot-grid/lib/pivot-grid-color-rules.ts +98 -0
  112. package/src/components/kiva/pivot-grid/lib/pivot-grid-export.ts +265 -0
  113. package/src/components/kiva/pivot-grid/lib/pivot-grid-model.ts +1110 -0
  114. package/src/components/kiva/pivot-grid/pivot-grid-demo-data.ts +200 -0
  115. package/src/components/kiva/pivot-grid/pivot-grid-export-menu.tsx +66 -0
  116. package/src/components/kiva/pivot-grid/pivot-grid-toolbar.tsx +85 -0
  117. package/src/components/kiva/pivot-grid/pivot-grid.tsx +601 -0
  118. package/src/components/kiva/pivot-grid/types.ts +195 -0
  119. package/src/components/kiva/place-autocomplete.tsx +484 -0
  120. package/src/components/kiva/record-delete-confirm-dialog/index.tsx +10 -0
  121. package/src/components/kiva/record-delete-confirm-dialog/record-delete-confirm-dialog.tsx +266 -0
  122. package/src/components/kiva/record-delete-confirm-dialog/types.ts +25 -0
  123. package/src/hooks/kiva/_internal/autonumber-setting.ts +112 -0
  124. package/src/hooks/kiva/_internal/form-layout-to-view.ts +665 -0
  125. package/src/hooks/kiva/_internal/kiva-hook-utils.ts +143 -0
  126. package/src/hooks/kiva/_internal/use-computed-field-evaluator.tsx +310 -0
  127. package/src/hooks/kiva/_internal/use-field-actions-evaluator.tsx +398 -0
  128. package/src/hooks/kiva/_internal/use-form-actions-evaluator.tsx +363 -0
  129. package/src/hooks/kiva/_internal/use-form-view-engine.tsx +2200 -0
  130. package/src/hooks/kiva/_internal/use-kiva-form-view-fields.tsx +190 -0
  131. package/src/hooks/kiva/_internal/use-kiva-kanban-cards.tsx +311 -0
  132. package/src/hooks/kiva/use-data-export.tsx +265 -0
  133. package/src/hooks/kiva/use-kiva-calendar.tsx +992 -0
  134. package/src/hooks/kiva/use-kiva-data-export.tsx +173 -0
  135. package/src/hooks/kiva/use-kiva-data-grid.tsx +4750 -0
  136. package/src/hooks/kiva/use-kiva-data-view-select.tsx +1273 -0
  137. package/src/hooks/kiva/use-kiva-form-view.tsx +1581 -0
  138. package/src/hooks/kiva/use-kiva-inventory.tsx +184 -0
  139. package/src/hooks/kiva/use-kiva-kanban.tsx +1750 -0
  140. package/src/hooks/kiva/use-kiva-map-view.tsx +2255 -0
  141. package/src/hooks/kiva/use-kiva-pivot-calendar.tsx +759 -0
  142. package/src/hooks/kiva/use-kiva-pivot-filter.tsx +1523 -0
  143. package/src/hooks/kiva/use-kiva-pivot-grid.tsx +543 -0
  144. package/src/hooks/kiva/use-kiva-tenant.tsx +313 -0
  145. package/src/hooks/kiva/use-parameter-persistence.ts +191 -0
@@ -0,0 +1,665 @@
1
+ // @ts-nocheck
2
+ /* eslint-disable */
3
+ /*
4
+ * Converts a Kiva saved-form `layout` (returned by
5
+ * `GET /v1/apps/{app}/data-sources/{ds}/forms`) into the `layout` /
6
+ * `fieldLayout` / `fieldSlugs` shape that `useKivaFormView` consumes.
7
+ *
8
+ * Per-field settings the form carries (validation tokens, computed formulas,
9
+ * field actions, custom validations) are surfaced into `fieldLayout`, where
10
+ * they override the data-source field's own values for this form.
11
+ *
12
+ * Bundled as a transitive file under `use-kiva-form-view`; not a public
13
+ * entry point. The hook calls `convertKivaFormLayout` after the schema
14
+ * resolves so field bindings (stored as data-source field ids) map back to slugs.
15
+ *
16
+ * Two persisted formats are supported:
17
+ *
18
+ * 1. Build-studio (current) — a flat `fields` array, columns via `gridColumns`:
19
+ * {
20
+ * gridColumns: 3,
21
+ * fields: [
22
+ * { id, dataSourceFieldId: <slug|id>, fieldConfig: { slug, type, ... }, componentType, columnSpan? },
23
+ * { id, componentType: 'panel', fields: [ ...nested ] } // sections (if present)
24
+ * ]
25
+ * }
26
+ *
27
+ * 2. Legacy `KvFormView` — a `children` tree, columns via `options.columnCount`:
28
+ * {
29
+ * type: 'KvFormView',
30
+ * options: { columnCount, tabView },
31
+ * children: [
32
+ * { id: <fieldId>, type: <fieldType>, options: { columnSpan, validations, hidden } },
33
+ * { id: 'panel_x', type: 'panel', options: { label, columnCount, tabView }, children: [...] }
34
+ * ]
35
+ * }
36
+ * Field nodes have no `children`; container nodes (`panel`) do.
37
+ */
38
+
39
+ import { normalizeValidationTokens } from '@kivacore/ui-pro-shared/components/kiva/form-fields/lib/validation-tokens';
40
+ import {
41
+ type ComputedBooleanFormula,
42
+ type CustomValidationRule,
43
+ type FieldAction,
44
+ type FormAction,
45
+ type FormCustomValidationRule
46
+ } from '@kivacore/ui-pro-shared/components/kiva/form-fields/types';
47
+
48
+ import {
49
+ type KivaFormViewColSpan,
50
+ type KivaFormViewFieldLayout,
51
+ type KivaFormViewLayoutItem,
52
+ type KivaFormViewTabSection
53
+ } from '../use-kiva-form-view';
54
+
55
+ interface RawFieldConfig {
56
+ id?: string;
57
+ slug?: string;
58
+ type?: string;
59
+ readOnly?: boolean;
60
+ hidden?: boolean;
61
+ columnSpan?: unknown;
62
+ validations?: unknown;
63
+ fieldActions?: unknown;
64
+ computedHidden?: unknown;
65
+ computedRequired?: unknown;
66
+ computedLabel?: unknown;
67
+ computedDescription?: unknown;
68
+ computedFormula?: unknown;
69
+ computedReadonly?: unknown;
70
+ computedValueFormula?: unknown;
71
+ customValidations?: unknown;
72
+ /** Static help text authored on the form field (builder format). */
73
+ description?: unknown;
74
+ /** User / relation picker display mode. */
75
+ selectionViewMode?: unknown;
76
+ /** Per-field label placement override. */
77
+ labelAlign?: unknown;
78
+ options?: { editorOptions?: Record<string, unknown> | null } | null;
79
+ }
80
+
81
+ interface RawNode {
82
+ id?: string;
83
+ type?: string;
84
+ componentType?: string;
85
+ dataSourceFieldId?: string;
86
+ hidden?: boolean | null;
87
+ columnSpan?: unknown;
88
+ label?: unknown;
89
+ title?: unknown;
90
+ options?: Record<string, unknown> | null;
91
+ fieldConfig?: RawFieldConfig | null;
92
+ children?: Array<RawNode> | null;
93
+ fields?: Array<RawNode> | null;
94
+ items?: Array<RawNode> | null;
95
+ gridColumns?: unknown;
96
+ columnCount?: unknown;
97
+ collapsible?: unknown;
98
+ defaultCollapsed?: unknown;
99
+ formActions?: unknown;
100
+ formCustomValidations?: unknown;
101
+ labelAlign?: unknown;
102
+ labelWidth?: unknown;
103
+ fieldSize?: unknown;
104
+ fieldVariant?: unknown;
105
+ }
106
+
107
+ export interface ConvertedKivaFormLayout {
108
+ layout?: Array<KivaFormViewLayoutItem>;
109
+ fieldLayout?: Record<string, KivaFormViewFieldLayout>;
110
+ /** Slugs the form references — used as `fieldSlugs` so omitted fields aren't appended. */
111
+ fieldSlugs?: Array<string>;
112
+ columns: 1 | 2 | 3 | 4;
113
+ /** Form-level lifecycle actions embedded in a build-studio form layout. */
114
+ formActions?: Array<FormAction>;
115
+ /** Form-level submit validations embedded in a build-studio form layout. */
116
+ formCustomValidations?: Array<FormCustomValidationRule>;
117
+ /** Form-level label placement — 'left' renders a horizontal label column. */
118
+ labelAlign?: 'top' | 'left';
119
+ /** Label column width bucket for `labelAlign: 'left'`. */
120
+ labelWidth?: 'sm' | 'md' | 'lg';
121
+ /** Form-level field density. */
122
+ fieldSize?: 'sm' | 'md' | 'lg';
123
+ /** Form-level input style. */
124
+ fieldVariant?: 'outline' | 'filled';
125
+ }
126
+
127
+ interface ConvertContext {
128
+ idToSlug: Map<string, string>;
129
+ validSlugs: Set<string>;
130
+ fieldLayout: Record<string, KivaFormViewFieldLayout>;
131
+ usedSlugs: Array<string>;
132
+ }
133
+
134
+ function clampColumns(value: unknown): 1 | 2 | 3 | 4 {
135
+ if (typeof value !== 'number' || !Number.isFinite(value)) return 1;
136
+ if (value <= 1) return 1;
137
+ if (value >= 4) return 4;
138
+
139
+ return Math.round(value) as 2 | 3;
140
+ }
141
+
142
+ function toColSpan(span: unknown, columnCount: number): KivaFormViewColSpan | undefined {
143
+ const value = Array.isArray(span) ? span[0] : span;
144
+
145
+ if (typeof value !== 'number' || !Number.isFinite(value)) return undefined;
146
+ if (columnCount > 1 && value >= columnCount) return 'full';
147
+ if (value <= 1) return 1;
148
+ if (value >= 4) return 4;
149
+
150
+ return Math.round(value) as 2 | 3;
151
+ }
152
+
153
+ function resolveSlug(id: string | undefined, ctx: ConvertContext): string | null {
154
+ if (!id) return null;
155
+ if (ctx.validSlugs.has(id)) return id;
156
+
157
+ return ctx.idToSlug.get(id) ?? null;
158
+ }
159
+
160
+ function pickString(value: unknown): string | undefined {
161
+ return typeof value === 'string' && value.length > 0 ? value : undefined;
162
+ }
163
+
164
+ function nestedNodes(node: RawNode): Array<RawNode> | null {
165
+ if (Array.isArray(node.fields)) return node.fields;
166
+ if (Array.isArray(node.children)) return node.children;
167
+ if (Array.isArray(node.items)) return node.items;
168
+
169
+ return null;
170
+ }
171
+
172
+ /*
173
+ * A field's real computed value, or `undefined`. The legacy builder stores the
174
+ * actual expression under the plain key (`computedFormula`) and a mode marker
175
+ * (`"formula"` / `"template"`) under the `__`-prefixed key; we read the plain
176
+ * key and reject a stray marker string leaking through. QB JSON objects pass
177
+ * through unchanged.
178
+ */
179
+ function realComputed(value: unknown): ComputedBooleanFormula | undefined {
180
+ if (value == null) return undefined;
181
+
182
+ if (typeof value === 'string') {
183
+ const trimmed = value.trim();
184
+
185
+ return trimmed && trimmed !== 'formula' && trimmed !== 'template' ? trimmed : undefined;
186
+ }
187
+
188
+ if (typeof value === 'object') return value as ComputedBooleanFormula;
189
+
190
+ return undefined;
191
+ }
192
+
193
+ interface FieldOverrideOptions {
194
+ columnSpan?: unknown;
195
+ validations?: unknown;
196
+ fieldActions?: unknown;
197
+ computedHidden?: unknown;
198
+ computedRequired?: unknown;
199
+ computedReadonly?: unknown;
200
+ computedLabel?: unknown;
201
+ computedDescription?: unknown;
202
+ computedFormula?: unknown;
203
+ computedValueFormula?: unknown;
204
+ customValidations?: unknown;
205
+ description?: unknown;
206
+ selectionViewMode?: unknown;
207
+ labelAlign?: unknown;
208
+ }
209
+
210
+ /** A real stored boolean; `undefined` for anything else (including `"true"`). */
211
+ function pickStoredBoolean(value: unknown): boolean | undefined {
212
+ return typeof value === 'boolean' ? value : undefined;
213
+ }
214
+
215
+ /**
216
+ * Merge the `editorOptions` bag a builder field config carries into the flat
217
+ * option shape the converter reads. The builder nests a field's picker/help
218
+ * settings one level deeper than its computed formulas, so without this the
219
+ * flat lookups below silently miss every one of them.
220
+ */
221
+ function withEditorOptions(cfg: RawFieldConfig | null | undefined): Record<string, unknown> {
222
+ const editorOptions = cfg?.options?.editorOptions;
223
+
224
+ return editorOptions && typeof editorOptions === 'object' ? editorOptions : {};
225
+ }
226
+
227
+ function applyFieldOverrides(
228
+ slug: string,
229
+ options: FieldOverrideOptions,
230
+ columnCount: number,
231
+ ctx: ConvertContext
232
+ ): KivaFormViewLayoutItem {
233
+ const fieldLayout: KivaFormViewFieldLayout = ctx.fieldLayout[slug] ?? {};
234
+ /*
235
+ * `normalizeValidationTokens` also accepts the designer's TUPLE spelling
236
+ * (`['maxValue', 10]`). A plain string filter dropped those, so a form's
237
+ * numeric and length bounds were silently lost while `required` survived.
238
+ */
239
+ const validations = normalizeValidationTokens(options.validations);
240
+
241
+ if (validations.includes('required')) fieldLayout.required = true;
242
+
243
+ /*
244
+ * Carry the whole token list, not just `required` — the engine enforces
245
+ * `minLength:` / `maxLength:` / `pattern:` / `min:` / `max:` too, and a form
246
+ * may be stricter than the data-source field it renders.
247
+ */
248
+ if (validations.length > 0) fieldLayout.validations = validations;
249
+
250
+ const colSpan = toColSpan(options.columnSpan, columnCount);
251
+
252
+ if (colSpan) fieldLayout.colSpan = colSpan;
253
+
254
+ /*
255
+ * Per-field onFieldChange actions defined in the build-studio field config.
256
+ * Surfaced into `fieldLayout.fieldActions` so the hook's field-action
257
+ * evaluator runs them automatically — no consumer wiring.
258
+ */
259
+ if (Array.isArray(options.fieldActions) && options.fieldActions.length > 0) {
260
+ fieldLayout.fieldActions = options.fieldActions as Array<FieldAction>;
261
+ }
262
+
263
+ /*
264
+ * Per-field computed formulas carried on the form field (build-studio
265
+ * `fieldConfig.*` or legacy node `options.*`). Surfaced into `fieldLayout`,
266
+ * which the form-view engine reads as the PRIMARY computed mechanism — so a
267
+ * form's per-field hide/require/label/description/value rules survive the
268
+ * save-and-render round-trip without living on the data-source field.
269
+ */
270
+ const computedHidden = realComputed(options.computedHidden);
271
+
272
+ if (computedHidden !== undefined) fieldLayout.computedHidden = computedHidden;
273
+
274
+ const computedRequired = realComputed(options.computedRequired);
275
+
276
+ if (computedRequired !== undefined) fieldLayout.computedRequired = computedRequired;
277
+
278
+ const computedLabel = realComputed(options.computedLabel);
279
+
280
+ if (typeof computedLabel === 'string') fieldLayout.computedLabel = computedLabel;
281
+
282
+ const computedDescription = realComputed(options.computedDescription);
283
+
284
+ if (typeof computedDescription === 'string') fieldLayout.computedDescription = computedDescription;
285
+
286
+ const computedFormula = realComputed(options.computedFormula);
287
+
288
+ if (typeof computedFormula === 'string') fieldLayout.computedFormula = computedFormula;
289
+
290
+ const computedReadonly = realComputed(options.computedReadonly);
291
+
292
+ if (computedReadonly !== undefined) fieldLayout.computedReadonly = computedReadonly;
293
+
294
+ if (Array.isArray(options.customValidations) && options.customValidations.length > 0) {
295
+ fieldLayout.customValidations = options.customValidations as Array<CustomValidationRule>;
296
+ }
297
+
298
+ /*
299
+ * Static help text. The converter used to lift only the COMPUTED variant, so
300
+ * a field's plain description ("enter the same name as the related user")
301
+ * never reached the runtime even though the designer offered the input.
302
+ */
303
+ const description = pickString(options.description);
304
+
305
+ if (description) fieldLayout.description = description;
306
+
307
+ /*
308
+ * How a user / relation picker presents its selection. A layout-only key, so
309
+ * lifting it here is the only way it can reach the field component.
310
+ *
311
+ * Three sibling picker keys the designer also stores — `parentDataSourceField`
312
+ * (gate a relation on a parent field's value), `filterByRoles` (restrict a
313
+ * user picker to role uids) and `hideInactiveUsers` — are deliberately NOT
314
+ * carried yet: nothing reads them, and the option shapes they would filter
315
+ * carry neither a role list nor an account status, so lifting them now would
316
+ * add config that silently does nothing.
317
+ */
318
+ const selectionViewMode = pickString(options.selectionViewMode);
319
+
320
+ if (selectionViewMode) {
321
+ fieldLayout.fieldProps = { ...fieldLayout.fieldProps, selectionViewMode };
322
+ }
323
+
324
+ /*
325
+ * A field with a real computed-VALUE formula is DERIVED: force it read-only
326
+ * so the user cannot edit a total the backend recomputes, and carry the
327
+ * formula so the engine can evaluate it. The formula was previously dropped
328
+ * entirely, so a faithfully configured computed field never recomputed.
329
+ */
330
+ const computedValueFormula = realComputed(options.computedValueFormula);
331
+
332
+ if (typeof computedValueFormula === 'string') {
333
+ fieldLayout.readOnly = true;
334
+ fieldLayout.computedFormula = computedValueFormula;
335
+ }
336
+
337
+ if (Object.keys(fieldLayout).length > 0) ctx.fieldLayout[slug] = fieldLayout;
338
+
339
+ ctx.usedSlugs.push(slug);
340
+
341
+ /*
342
+ * PER-FIELD label placement. Only `top` is meaningful as an override — it
343
+ * forces this field to stack even inside a label-left section, while `left`
344
+ * equals the common default and is not worth emitting.
345
+ */
346
+ const labelAlign = options.labelAlign === 'top' ? 'top' : undefined;
347
+
348
+ return colSpan || labelAlign ? {
349
+ type: 'field', slug, colSpan, labelAlign
350
+ } : slug;
351
+ }
352
+
353
+ /* ---- Build-studio format (flat `fields` array) -------------------------- */
354
+
355
+ function resolveBuilderSlug(node: RawNode, ctx: ConvertContext): string | null {
356
+ const cfgSlug = pickString(node.fieldConfig?.slug);
357
+
358
+ if (cfgSlug && ctx.validSlugs.has(cfgSlug)) return cfgSlug;
359
+
360
+ const byDataSourceId = resolveSlug(node.dataSourceFieldId, ctx);
361
+
362
+ if (byDataSourceId) return byDataSourceId;
363
+
364
+ return cfgSlug ?? resolveSlug(node.id, ctx);
365
+ }
366
+
367
+ function convertBuilderNodes(nodes: Array<RawNode>, columnCount: number, ctx: ConvertContext): Array<KivaFormViewLayoutItem> {
368
+ const items: Array<KivaFormViewLayoutItem> = [];
369
+
370
+ for (const node of nodes) {
371
+ if (!node || typeof node !== 'object') continue;
372
+
373
+ const options = node.options ?? {};
374
+ const hidden = node.hidden ?? node.fieldConfig?.hidden ?? options.hidden;
375
+
376
+ if (hidden === true) continue;
377
+
378
+ const nested = nestedNodes(node);
379
+
380
+ if (nested && !node.dataSourceFieldId && !node.fieldConfig) {
381
+ const sectionColumns = clampColumns(node.gridColumns ?? node.columnCount ?? options.columnCount ?? columnCount);
382
+ const title = pickString(node.label) ?? pickString(node.title) ?? pickString(options.label);
383
+ const colSpan = toColSpan(node.columnSpan ?? options.columnSpan, columnCount);
384
+
385
+ /*
386
+ * The designer's fold toggle. The engine has rendered collapsible
387
+ * fieldsets for a while, but the converter never carried the flags, so a
388
+ * panel marked collapsible always rendered as a static legend.
389
+ */
390
+ const collapsible = pickStoredBoolean(node.collapsible ?? options.collapsible);
391
+ const defaultCollapsed = pickStoredBoolean(node.defaultCollapsed ?? options.defaultCollapsed ?? options.collapsed);
392
+
393
+ items.push({
394
+ id: node.id ?? `section_${items.length}`,
395
+ variant: 'fieldset',
396
+ title,
397
+ colSpan,
398
+ /*
399
+ * The panel renders in its OWN grid. Without this the fieldset falls
400
+ * back to the form-level column count and a saved "1-column panel
401
+ * inside a 2-column form" silently renders 2-up.
402
+ */
403
+ columns: sectionColumns,
404
+ ...(collapsible === undefined ? {} : { collapsible }),
405
+ ...(defaultCollapsed === undefined ? {} : { defaultCollapsed }),
406
+ items: convertBuilderNodes(nested, sectionColumns, ctx)
407
+ });
408
+ continue;
409
+ }
410
+
411
+ const slug = resolveBuilderSlug(node, ctx);
412
+
413
+ if (!slug) continue;
414
+
415
+ const cfg = node.fieldConfig ?? {};
416
+ /*
417
+ * The builder nests a field's picker / help settings under
418
+ * `fieldConfig.options.editorOptions`, one level deeper than its computed
419
+ * formulas. Read it as the last fallback so both shapes resolve.
420
+ */
421
+ const editorOptions = withEditorOptions(cfg);
422
+
423
+ items.push(applyFieldOverrides(slug, {
424
+ columnSpan: node.columnSpan ?? cfg.columnSpan ?? options.columnSpan,
425
+ validations: cfg.validations ?? options.validations,
426
+ fieldActions: cfg.fieldActions ?? options.fieldActions,
427
+ computedHidden: cfg.computedHidden ?? options.computedHidden,
428
+ computedRequired: cfg.computedRequired ?? options.computedRequired,
429
+ computedReadonly: cfg.computedReadonly ?? options.computedReadonly ?? editorOptions['computedReadonly'],
430
+ computedLabel: cfg.computedLabel ?? options.computedLabel,
431
+ computedDescription: cfg.computedDescription ?? options.computedDescription,
432
+ computedFormula: cfg.computedFormula ?? options.computedFormula,
433
+ computedValueFormula: cfg.computedValueFormula ?? options.computedValueFormula ?? editorOptions['computedValueFormula'],
434
+ customValidations: cfg.customValidations ?? options.customValidations,
435
+ description: cfg.description ?? options.description ?? editorOptions['description'],
436
+ selectionViewMode: cfg.selectionViewMode ?? options.selectionViewMode ?? editorOptions['selectionViewMode'],
437
+ labelAlign: cfg.labelAlign ?? options.labelAlign
438
+ }, columnCount, ctx));
439
+ }
440
+
441
+ return items;
442
+ }
443
+
444
+ /* ---- Legacy KvFormView format (`children` tree) ------------------------- */
445
+
446
+ function convertLegacyNodes(nodes: Array<RawNode> | null | undefined, columnCount: number, ctx: ConvertContext): Array<KivaFormViewLayoutItem> {
447
+ const items: Array<KivaFormViewLayoutItem> = [];
448
+
449
+ for (const node of nodes ?? []) {
450
+ if (!node || typeof node !== 'object') continue;
451
+
452
+ const options = node.options ?? {};
453
+
454
+ if (options.hidden === true) continue;
455
+
456
+ if (!Array.isArray(node.children)) {
457
+ const slug = resolveSlug(node.id, ctx);
458
+
459
+ if (!slug) continue;
460
+
461
+ const legacyEditorOptions = options['editorOptions'] && typeof options['editorOptions'] === 'object' ? options['editorOptions'] as Record<string, unknown> : {};
462
+
463
+ items.push(applyFieldOverrides(slug, {
464
+ columnSpan: options.columnSpan,
465
+ validations: options.validations,
466
+ /*
467
+ * Field actions were forwarded only on the builder path, so a legacy
468
+ * form's onFieldChange rules never ran.
469
+ */
470
+ fieldActions: options.fieldActions,
471
+ computedHidden: options.computedHidden,
472
+ computedRequired: options.computedRequired,
473
+ computedReadonly: options.computedReadonly ?? legacyEditorOptions['computedReadonly'],
474
+ computedLabel: options.computedLabel,
475
+ computedDescription: options.computedDescription,
476
+ computedFormula: options.computedFormula,
477
+ computedValueFormula: options.computedValueFormula ?? legacyEditorOptions['computedValueFormula'],
478
+ customValidations: options.customValidations,
479
+ description: options.description ?? legacyEditorOptions['description'],
480
+ selectionViewMode: options.selectionViewMode ?? legacyEditorOptions['selectionViewMode'],
481
+ labelAlign: options.labelAlign
482
+ }, columnCount, ctx));
483
+ continue;
484
+ }
485
+
486
+ const sectionColumns = clampColumns(options.columnCount ?? columnCount);
487
+ const title = options.hideLabel === true ? undefined : pickString(options.label);
488
+ const colSpan = toColSpan(options.columnSpan, columnCount);
489
+ const id = node.id ?? `section_${items.length}`;
490
+
491
+ if (options.tabView === true) {
492
+ const tabs: Array<KivaFormViewTabSection> = [];
493
+
494
+ for (const child of node.children) {
495
+ if (!child || typeof child !== 'object') continue;
496
+
497
+ const childOptions = child.options ?? {};
498
+ const childTitle = childOptions.hideLabel === true ? undefined : pickString(childOptions.label);
499
+
500
+ tabs.push({
501
+ id: child.id ?? `tab_${tabs.length}`,
502
+ variant: 'tab',
503
+ title: childTitle,
504
+ items: Array.isArray(child.children) ? convertLegacyNodes(child.children, clampColumns(childOptions.columnCount ?? sectionColumns), ctx) : convertLegacyNodes([child], sectionColumns, ctx)
505
+ });
506
+ }
507
+
508
+ if (tabs.length > 0) {
509
+ items.push({
510
+ id, variant: 'tabpanel', items: tabs, colSpan
511
+ });
512
+ }
513
+ continue;
514
+ }
515
+
516
+ const collapsible = pickStoredBoolean(options.collapsible);
517
+ const defaultCollapsed = pickStoredBoolean(options.defaultCollapsed ?? options.collapsed);
518
+
519
+ items.push({
520
+ id,
521
+ variant: 'fieldset',
522
+ title,
523
+ colSpan,
524
+ columns: sectionColumns,
525
+ ...(collapsible === undefined ? {} : { collapsible }),
526
+ ...(defaultCollapsed === undefined ? {} : { defaultCollapsed }),
527
+ items: convertLegacyNodes(node.children, sectionColumns, ctx)
528
+ });
529
+ }
530
+
531
+ return items;
532
+ }
533
+
534
+ /**
535
+ * Translate a saved form `layout` payload into `useKivaFormView` props.
536
+ *
537
+ * @param formLayout - The `DataForm.layout` JSON (build-studio or `KvFormView`) or `null`.
538
+ * @param fields - Data-source fields (`{ id, slug }`) used to resolve id → slug bindings.
539
+ */
540
+ export function convertKivaFormLayout(
541
+ formLayout: Record<string, unknown> | null | undefined,
542
+ fields: Array<{ id?: string; slug?: string }>
543
+ ): ConvertedKivaFormLayout {
544
+ const ctx: ConvertContext = {
545
+ idToSlug: new Map(),
546
+ validSlugs: new Set(),
547
+ fieldLayout: {},
548
+ usedSlugs: []
549
+ };
550
+
551
+ for (const field of fields) {
552
+ if (field.slug) ctx.validSlugs.add(field.slug);
553
+ if (field.id && field.slug) ctx.idToSlug.set(field.id, field.slug);
554
+ }
555
+
556
+ const root = (formLayout ?? {}) as RawNode;
557
+ const rootOptions = root.options ?? {};
558
+ const columns = clampColumns(rootOptions.columnCount ?? root.gridColumns);
559
+
560
+ /*
561
+ * Form-level lifecycle actions + submit validations live at the top of a
562
+ * build-studio layout. Surfaced so the hook auto-runs them without the
563
+ * consumer passing `formActions` / `formCustomValidations` props. Legacy
564
+ * KvFormView layouts don't carry these (they yield `undefined`).
565
+ */
566
+ const formMeta: FormMeta = {
567
+ formActions: Array.isArray(root.formActions) && root.formActions.length > 0 ? (root.formActions as Array<FormAction>) : undefined,
568
+ formCustomValidations: Array.isArray(root.formCustomValidations) && root.formCustomValidations.length > 0 ? (root.formCustomValidations as Array<FormCustomValidationRule>) : undefined,
569
+ /*
570
+ * Form-level layout styling: build-studio keys live at the layout root;
571
+ * legacy KvFormView keeps them under `options` (with `field`-prefixed names).
572
+ */
573
+ labelAlign: normLabelAlign(root.labelAlign ?? rootOptions.labelAlign ?? rootOptions.fieldLabelAlign),
574
+ labelWidth: normLabelWidth(root.labelWidth ?? rootOptions.labelWidth ?? rootOptions.fieldLabelWidth),
575
+ fieldSize: normSize(root.fieldSize ?? rootOptions.fieldSize),
576
+ fieldVariant: normVariant(root.fieldVariant ?? rootOptions.fieldVariant)
577
+ };
578
+
579
+ if (Array.isArray(root.fields)) {
580
+ const items = convertBuilderNodes(root.fields, columns, ctx);
581
+
582
+ return packResult(items, ctx, columns, formMeta);
583
+ }
584
+
585
+ const children = Array.isArray(root.children) ? root.children : null;
586
+
587
+ if (!children || children.length === 0) {
588
+ return { columns, ...formMeta };
589
+ }
590
+
591
+ const items = rootOptions.tabView === true ? convertLegacyNodes([
592
+ {
593
+ id: '__root_tabs__', type: 'panel', options: { tabView: true, columnCount: columns }, children
594
+ }
595
+ ], columns, ctx) : convertLegacyNodes(children, columns, ctx);
596
+
597
+ return packResult(items, ctx, columns, formMeta);
598
+ }
599
+
600
+ interface FormMeta {
601
+ formActions?: Array<FormAction>;
602
+ formCustomValidations?: Array<FormCustomValidationRule>;
603
+ labelAlign?: 'top' | 'left';
604
+ labelWidth?: 'sm' | 'md' | 'lg';
605
+ fieldSize?: 'sm' | 'md' | 'lg';
606
+ fieldVariant?: 'outline' | 'filled';
607
+ }
608
+
609
+ /** Normalize legacy (`fieldLabelAlign`) + build-studio (`labelAlign`) label placement. */
610
+ function normLabelAlign(value: unknown): 'top' | 'left' | undefined {
611
+ return value === 'left' ? 'left' : value === 'top' ? 'top' : undefined;
612
+ }
613
+
614
+ /** Normalize a size-ish value (legacy `xs`/`xl` fold into `sm`/`lg`). */
615
+ function normSize(value: unknown): 'sm' | 'md' | 'lg' | undefined {
616
+ if (value === 'sm' || value === 'md' || value === 'lg') return value;
617
+ if (value === 'xs') return 'sm';
618
+ if (value === 'xl') return 'lg';
619
+
620
+ return undefined;
621
+ }
622
+
623
+ /** Legacy label width was a grid-unit number; bucket it. Build-studio may already be a bucket. */
624
+ function normLabelWidth(value: unknown): 'sm' | 'md' | 'lg' | undefined {
625
+ if (value === 'sm' || value === 'md' || value === 'lg') return value;
626
+ if (typeof value === 'number' && Number.isFinite(value)) return value < 8 ? 'sm' : value <= 14 ? 'md' : 'lg';
627
+
628
+ return undefined;
629
+ }
630
+
631
+ function normVariant(value: unknown): 'outline' | 'filled' | undefined {
632
+ return value === 'filled' ? 'filled' : value === 'outline' ? 'outline' : undefined;
633
+ }
634
+
635
+ function packResult(items: Array<KivaFormViewLayoutItem>, ctx: ConvertContext, columns: 1 | 2 | 3 | 4, formMeta?: FormMeta): ConvertedKivaFormLayout {
636
+ return {
637
+ layout: items.length > 0 ? items : undefined,
638
+ fieldLayout: Object.keys(ctx.fieldLayout).length > 0 ? ctx.fieldLayout : undefined,
639
+ fieldSlugs: ctx.usedSlugs.length > 0 ? ctx.usedSlugs : undefined,
640
+ columns,
641
+ formActions: formMeta?.formActions,
642
+ formCustomValidations: formMeta?.formCustomValidations,
643
+ labelAlign: formMeta?.labelAlign,
644
+ labelWidth: formMeta?.labelWidth,
645
+ fieldSize: formMeta?.fieldSize,
646
+ fieldVariant: formMeta?.fieldVariant
647
+ };
648
+ }
649
+
650
+ /** Merge a derived field-layout map with explicit per-field overrides (overrides win per key). */
651
+ export function mergeKivaFieldLayouts(
652
+ base: Record<string, KivaFormViewFieldLayout> | undefined,
653
+ override: Record<string, KivaFormViewFieldLayout> | undefined
654
+ ): Record<string, KivaFormViewFieldLayout> | undefined {
655
+ if (!base) return override;
656
+ if (!override) return base;
657
+
658
+ const merged: Record<string, KivaFormViewFieldLayout> = { ...base };
659
+
660
+ for (const [slug, layout] of Object.entries(override)) {
661
+ merged[slug] = { ...merged[slug], ...layout };
662
+ }
663
+
664
+ return merged;
665
+ }