@kissflow/create-app 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (123) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +9 -0
  3. package/bin/index.js +80 -0
  4. package/package.json +47 -0
  5. package/scaffolders/app/index.js +49 -0
  6. package/scaffolders/app/template/_CLAUDE.md +104 -0
  7. package/scaffolders/app/template/_README.md +54 -0
  8. package/scaffolders/app/template/_env.example +10 -0
  9. package/scaffolders/app/template/_gitignore +6 -0
  10. package/scaffolders/app/template/_package.json +55 -0
  11. package/scaffolders/app/template/_prettierrc +6 -0
  12. package/scaffolders/app/template/agents/design-guidelines.md +172 -0
  13. package/scaffolders/app/template/agents/kissflow-sdk.md +257 -0
  14. package/scaffolders/app/template/agents/theming.md +61 -0
  15. package/scaffolders/app/template/cert/localhost.crt +18 -0
  16. package/scaffolders/app/template/cert/localhost.key +28 -0
  17. package/scaffolders/app/template/components.json +21 -0
  18. package/scaffolders/app/template/index.html +12 -0
  19. package/scaffolders/app/template/jsconfig.json +8 -0
  20. package/scaffolders/app/template/lib/kf-schema.json +7 -0
  21. package/scaffolders/app/template/scripts/zip.js +54 -0
  22. package/scaffolders/app/template/src/components/app-shell.jsx +208 -0
  23. package/scaffolders/app/template/src/components/fields/AggregationField.jsx +42 -0
  24. package/scaffolders/app/template/src/components/fields/AttachmentField.jsx +175 -0
  25. package/scaffolders/app/template/src/components/fields/BooleanField.jsx +47 -0
  26. package/scaffolders/app/template/src/components/fields/CheckboxField.jsx +79 -0
  27. package/scaffolders/app/template/src/components/fields/ChecklistField.jsx +190 -0
  28. package/scaffolders/app/template/src/components/fields/CurrencyField.jsx +183 -0
  29. package/scaffolders/app/template/src/components/fields/DateField.jsx +65 -0
  30. package/scaffolders/app/template/src/components/fields/DateTimeField.jsx +186 -0
  31. package/scaffolders/app/template/src/components/fields/EmailField.jsx +30 -0
  32. package/scaffolders/app/template/src/components/fields/FieldError.jsx +11 -0
  33. package/scaffolders/app/template/src/components/fields/FieldLabel.jsx +14 -0
  34. package/scaffolders/app/template/src/components/fields/GeolocationField.jsx +99 -0
  35. package/scaffolders/app/template/src/components/fields/ImageField.jsx +149 -0
  36. package/scaffolders/app/template/src/components/fields/LookupField.jsx +256 -0
  37. package/scaffolders/app/template/src/components/fields/MultiSelectField.jsx +152 -0
  38. package/scaffolders/app/template/src/components/fields/MultiUserSelectField.jsx +153 -0
  39. package/scaffolders/app/template/src/components/fields/NumberField.jsx +30 -0
  40. package/scaffolders/app/template/src/components/fields/RadioField.jsx +78 -0
  41. package/scaffolders/app/template/src/components/fields/RatingField.jsx +64 -0
  42. package/scaffolders/app/template/src/components/fields/ScannerField.jsx +62 -0
  43. package/scaffolders/app/template/src/components/fields/SelectField.jsx +74 -0
  44. package/scaffolders/app/template/src/components/fields/SequenceNumberField.jsx +22 -0
  45. package/scaffolders/app/template/src/components/fields/SignatureField.jsx +344 -0
  46. package/scaffolders/app/template/src/components/fields/SliderField.jsx +39 -0
  47. package/scaffolders/app/template/src/components/fields/SmartAttachmentField.jsx +188 -0
  48. package/scaffolders/app/template/src/components/fields/TextField.jsx +30 -0
  49. package/scaffolders/app/template/src/components/fields/TextareaField.jsx +30 -0
  50. package/scaffolders/app/template/src/components/fields/UserSelectField.jsx +76 -0
  51. package/scaffolders/app/template/src/components/fields/index.js +87 -0
  52. package/scaffolders/app/template/src/components/fields/useGeolocationPicker.js +42 -0
  53. package/scaffolders/app/template/src/components/fields/useScanner.js +43 -0
  54. package/scaffolders/app/template/src/components/form/FormAction.jsx +86 -0
  55. package/scaffolders/app/template/src/components/form/FormBody.jsx +163 -0
  56. package/scaffolders/app/template/src/components/form/FormHeader.jsx +65 -0
  57. package/scaffolders/app/template/src/components/form/ProcessActions.jsx +270 -0
  58. package/scaffolders/app/template/src/components/form/index.jsx +119 -0
  59. package/scaffolders/app/template/src/components/form/shared.jsx +213 -0
  60. package/scaffolders/app/template/src/components/tablefields/TableAggregationField.jsx +14 -0
  61. package/scaffolders/app/template/src/components/tablefields/TableAttachmentField.jsx +53 -0
  62. package/scaffolders/app/template/src/components/tablefields/TableBooleanField.jsx +26 -0
  63. package/scaffolders/app/template/src/components/tablefields/TableCheckboxField.jsx +6 -0
  64. package/scaffolders/app/template/src/components/tablefields/TableChecklistField.jsx +48 -0
  65. package/scaffolders/app/template/src/components/tablefields/TableCurrencyField.jsx +140 -0
  66. package/scaffolders/app/template/src/components/tablefields/TableDateField.jsx +49 -0
  67. package/scaffolders/app/template/src/components/tablefields/TableDateTimeField.jsx +170 -0
  68. package/scaffolders/app/template/src/components/tablefields/TableGeolocationField.jsx +69 -0
  69. package/scaffolders/app/template/src/components/tablefields/TableImageField.jsx +78 -0
  70. package/scaffolders/app/template/src/components/tablefields/TableLookupField.jsx +165 -0
  71. package/scaffolders/app/template/src/components/tablefields/TableMultiSelectField.jsx +111 -0
  72. package/scaffolders/app/template/src/components/tablefields/TableMultiUserSelectField.jsx +109 -0
  73. package/scaffolders/app/template/src/components/tablefields/TableNumberField.jsx +33 -0
  74. package/scaffolders/app/template/src/components/tablefields/TableRatingField.jsx +45 -0
  75. package/scaffolders/app/template/src/components/tablefields/TableScannerField.jsx +67 -0
  76. package/scaffolders/app/template/src/components/tablefields/TableSelectField.jsx +47 -0
  77. package/scaffolders/app/template/src/components/tablefields/TableSequenceNumberField.jsx +7 -0
  78. package/scaffolders/app/template/src/components/tablefields/TableSignatureField.jsx +59 -0
  79. package/scaffolders/app/template/src/components/tablefields/TableSliderField.jsx +26 -0
  80. package/scaffolders/app/template/src/components/tablefields/TableTextField.jsx +28 -0
  81. package/scaffolders/app/template/src/components/tablefields/TableUserSelectField.jsx +51 -0
  82. package/scaffolders/app/template/src/components/tablefields/index.js +71 -0
  83. package/scaffolders/app/template/src/components/tablefields/useTableFieldOptions.js +33 -0
  84. package/scaffolders/app/template/src/components/ui/accordion.jsx +51 -0
  85. package/scaffolders/app/template/src/components/ui/avatar.jsx +92 -0
  86. package/scaffolders/app/template/src/components/ui/badge.jsx +40 -0
  87. package/scaffolders/app/template/src/components/ui/button.jsx +61 -0
  88. package/scaffolders/app/template/src/components/ui/calendar.jsx +207 -0
  89. package/scaffolders/app/template/src/components/ui/card.jsx +92 -0
  90. package/scaffolders/app/template/src/components/ui/chart.jsx +323 -0
  91. package/scaffolders/app/template/src/components/ui/checkbox.jsx +27 -0
  92. package/scaffolders/app/template/src/components/ui/command.jsx +154 -0
  93. package/scaffolders/app/template/src/components/ui/dialog.jsx +122 -0
  94. package/scaffolders/app/template/src/components/ui/dropdown-menu.jsx +200 -0
  95. package/scaffolders/app/template/src/components/ui/input.jsx +21 -0
  96. package/scaffolders/app/template/src/components/ui/label.jsx +19 -0
  97. package/scaffolders/app/template/src/components/ui/popover.jsx +40 -0
  98. package/scaffolders/app/template/src/components/ui/progress.jsx +25 -0
  99. package/scaffolders/app/template/src/components/ui/radio-group.jsx +37 -0
  100. package/scaffolders/app/template/src/components/ui/scroll-area.jsx +48 -0
  101. package/scaffolders/app/template/src/components/ui/select.jsx +156 -0
  102. package/scaffolders/app/template/src/components/ui/separator.jsx +26 -0
  103. package/scaffolders/app/template/src/components/ui/sheet.jsx +123 -0
  104. package/scaffolders/app/template/src/components/ui/skeleton.jsx +13 -0
  105. package/scaffolders/app/template/src/components/ui/slider.jsx +61 -0
  106. package/scaffolders/app/template/src/components/ui/sonner.jsx +60 -0
  107. package/scaffolders/app/template/src/components/ui/switch.jsx +26 -0
  108. package/scaffolders/app/template/src/components/ui/table.jsx +111 -0
  109. package/scaffolders/app/template/src/components/ui/tabs.jsx +74 -0
  110. package/scaffolders/app/template/src/components/ui/textarea.jsx +18 -0
  111. package/scaffolders/app/template/src/components/ui/tooltip.jsx +43 -0
  112. package/scaffolders/app/template/src/hooks/useForm.js +841 -0
  113. package/scaffolders/app/template/src/index.css +143 -0
  114. package/scaffolders/app/template/src/lib/utils.js +6 -0
  115. package/scaffolders/app/template/src/main.jsx +18 -0
  116. package/scaffolders/app/template/src/pages/index.jsx +244 -0
  117. package/scaffolders/app/template/src/themes.css +153 -0
  118. package/scaffolders/app/template/src/themes.js +32 -0
  119. package/scaffolders/app/template/vite-kf-live.js +117 -0
  120. package/scaffolders/app/template/vite.config.js +56 -0
  121. package/scaffolders/constants.js +25 -0
  122. package/scaffolders/helpers.js +12 -0
  123. package/scaffolders/utils.js +109 -0
@@ -0,0 +1,270 @@
1
+ import { useEffect, useState } from "react";
2
+ import { Loader2Icon } from "lucide-react";
3
+ import {
4
+ Dialog,
5
+ DialogContent,
6
+ DialogDescription,
7
+ DialogFooter,
8
+ DialogHeader,
9
+ DialogTitle
10
+ } from "@/components/ui/dialog";
11
+ import {
12
+ Select,
13
+ SelectContent,
14
+ SelectItem,
15
+ SelectTrigger,
16
+ SelectValue
17
+ } from "@/components/ui/select";
18
+ import { Checkbox } from "@/components/ui/checkbox";
19
+
20
+ // Styling per action role, all via theme tokens so it tracks the shadcn theme.
21
+ function actionClass(action) {
22
+ if (action.primary)
23
+ return "bg-primary text-primary-foreground shadow-sm hover:bg-primary/90";
24
+ if (action.destructive)
25
+ return "bg-destructive/10 text-destructive hover:bg-destructive/20";
26
+ return "bg-muted text-foreground hover:bg-muted/70";
27
+ }
28
+
29
+ const userLabel = (u) =>
30
+ u?.Name || u?.name || u?._name || u?.label || u?._id || u?.Id;
31
+ const userId = (u) => u?._id || u?.Id || u?.id;
32
+
33
+ /**
34
+ * Renders the workflow actions available at the current process step
35
+ * (resolved by resolveProcessActions in useForm). Actions open a modal first
36
+ * when they need a comment (reject / send back / withdraw), a reassignee
37
+ * (reassign), or a confirm (discard); the rest fire immediately.
38
+ *
39
+ * @param {Array} actions descriptors from useForm().formActions
40
+ * @param {Array} reassignFromCandidates users currently assigned to this step
41
+ * (useForm().reassignFromCandidates) — shown read-only when there's one, or as a
42
+ * multi-select ("who are you reassigning away from?") when there are several.
43
+ * @param {Function} onAction (key, payload) => Promise<boolean>
44
+ * @param {Function} getReassignees (query?) => Promise<user[]>
45
+ * @param {boolean} loading disables buttons while an action is in flight
46
+ * @param {boolean} disabled hard-disable (e.g. read-only form)
47
+ */
48
+ export function ProcessActions({
49
+ actions,
50
+ reassignFromCandidates = [],
51
+ onAction,
52
+ getReassignees,
53
+ loading,
54
+ disabled
55
+ }) {
56
+ const [pending, setPending] = useState(null); // action awaiting modal input
57
+ const [comment, setComment] = useState("");
58
+ const [reassignees, setReassignees] = useState([]);
59
+ const [reassignTo, setReassignTo] = useState("");
60
+ const [loadingReassignees, setLoadingReassignees] = useState(false);
61
+ const [reassigneesError, setReassigneesError] = useState(null);
62
+ const [selectedReassignFromIds, setSelectedReassignFromIds] = useState([]);
63
+
64
+ // Load candidate assignees when a reassign modal opens.
65
+ useEffect(() => {
66
+ if (!pending?.requiresReassignee || !getReassignees) return;
67
+ let active = true;
68
+ setLoadingReassignees(true);
69
+ setReassigneesError(null);
70
+ Promise.resolve(getReassignees(""))
71
+ .then((list) => active && setReassignees(list || []))
72
+ .catch((err) => {
73
+ if (!active) return;
74
+ console.error("Failed to load reassignees:", err);
75
+ setReassignees([]);
76
+ setReassigneesError(
77
+ err?.message || "Failed to load users to reassign to."
78
+ );
79
+ })
80
+ .finally(() => active && setLoadingReassignees(false));
81
+ return () => {
82
+ active = false;
83
+ };
84
+ }, [pending, getReassignees]);
85
+
86
+ if (!actions || actions.length === 0) return null;
87
+
88
+ const closeModal = () => {
89
+ setPending(null);
90
+ setComment("");
91
+ setReassignTo("");
92
+ setReassignees([]);
93
+ setReassigneesError(null);
94
+ setSelectedReassignFromIds([]);
95
+ };
96
+
97
+ const handleClick = (action) => {
98
+ if (action.requiresComment || action.confirm || action.requiresReassignee) {
99
+ setComment("");
100
+ setReassignTo("");
101
+ setSelectedReassignFromIds([]);
102
+ setPending(action);
103
+ } else {
104
+ onAction(action.key);
105
+ }
106
+ };
107
+
108
+ const toggleReassignFrom = (id) => {
109
+ setSelectedReassignFromIds((prev) =>
110
+ prev.includes(id) ? prev.filter((x) => x !== id) : [...prev, id]
111
+ );
112
+ };
113
+
114
+ const handleConfirm = async () => {
115
+ const action = pending;
116
+ const payload = {};
117
+ if (action.requiresComment) payload.comment = comment;
118
+ if (action.requiresReassignee) {
119
+ payload.reassignTo = reassignees.find((u) => userId(u) === reassignTo);
120
+ // Mirrors the native reassign hook: an explicit selection wins,
121
+ // otherwise reassign away from every current assignee on the step.
122
+ payload.reassignedFrom = selectedReassignFromIds.length
123
+ ? reassignFromCandidates.filter((u) =>
124
+ selectedReassignFromIds.includes(userId(u))
125
+ )
126
+ : reassignFromCandidates;
127
+ }
128
+ const ok = await onAction(action.key, payload);
129
+ if (ok !== false) closeModal();
130
+ };
131
+
132
+ const needsComment = pending?.requiresComment;
133
+ const needsReassignee = pending?.requiresReassignee;
134
+ const confirmDisabled =
135
+ loading ||
136
+ (needsComment && !comment.trim()) ||
137
+ (needsReassignee && !reassignTo);
138
+
139
+ return (
140
+ <>
141
+ <div className="flex items-center gap-2">
142
+ {actions.map((action) => (
143
+ <button
144
+ key={action.key}
145
+ type="button"
146
+ onClick={() => handleClick(action)}
147
+ disabled={loading || disabled}
148
+ className={`inline-flex items-center gap-1.5 px-4 py-1.5 text-sm font-semibold rounded-lg transition-all disabled:opacity-40 disabled:cursor-not-allowed ${actionClass(action)}`}
149
+ >
150
+ {loading && action.primary && (
151
+ <Loader2Icon className="w-3.5 h-3.5 animate-spin" />
152
+ )}
153
+ {action.label}
154
+ </button>
155
+ ))}
156
+ </div>
157
+
158
+ <Dialog open={!!pending} onOpenChange={(o) => !o && closeModal()}>
159
+ <DialogContent>
160
+ <DialogHeader>
161
+ <DialogTitle>{pending?.label}</DialogTitle>
162
+ <DialogDescription>
163
+ {needsReassignee
164
+ ? "Choose who to reassign this task to and add a comment."
165
+ : needsComment
166
+ ? "Add a comment for this action."
167
+ : `Are you sure you want to ${pending?.label.toLowerCase()} this item? This cannot be undone.`}
168
+ </DialogDescription>
169
+ </DialogHeader>
170
+
171
+ {needsReassignee && reassignFromCandidates.length > 0 && (
172
+ <div className="space-y-1.5">
173
+ <p className="text-sm font-medium text-foreground">
174
+ Currently assigned to
175
+ </p>
176
+ {reassignFromCandidates.length === 1 ? (
177
+ <p className="text-sm text-muted-foreground">
178
+ {userLabel(reassignFromCandidates[0])}
179
+ </p>
180
+ ) : (
181
+ <div className="space-y-2">
182
+ {reassignFromCandidates.map((u) => (
183
+ <label
184
+ key={userId(u)}
185
+ className="flex items-center gap-2 text-sm cursor-pointer"
186
+ >
187
+ <Checkbox
188
+ checked={selectedReassignFromIds.includes(userId(u))}
189
+ onCheckedChange={() => toggleReassignFrom(userId(u))}
190
+ />
191
+ {userLabel(u)}
192
+ </label>
193
+ ))}
194
+ </div>
195
+ )}
196
+ </div>
197
+ )}
198
+
199
+ {needsReassignee && (
200
+ <Select
201
+ value={reassignTo}
202
+ onValueChange={setReassignTo}
203
+ disabled={loadingReassignees}
204
+ >
205
+ <SelectTrigger autoFocus className="w-full">
206
+ <SelectValue
207
+ placeholder={
208
+ loadingReassignees ? "Loading…" : "Select a user…"
209
+ }
210
+ />
211
+ </SelectTrigger>
212
+ <SelectContent>
213
+ {reassignees.map((u) => (
214
+ <SelectItem key={userId(u)} value={userId(u)}>
215
+ {userLabel(u)}
216
+ </SelectItem>
217
+ ))}
218
+ </SelectContent>
219
+ </Select>
220
+ )}
221
+
222
+ {needsReassignee && reassigneesError && (
223
+ <p className="text-sm text-destructive">{reassigneesError}</p>
224
+ )}
225
+
226
+ {needsReassignee &&
227
+ !loadingReassignees &&
228
+ !reassigneesError &&
229
+ reassignees.length === 0 && (
230
+ <p className="text-sm rounded-md border border-amber-300 bg-amber-50 px-3 py-2 text-amber-800">
231
+ Reassignment is unavailable for this step as it has fixed
232
+ assignees.
233
+ </p>
234
+ )}
235
+
236
+ {needsComment && (
237
+ <textarea
238
+ autoFocus={!needsReassignee}
239
+ value={comment}
240
+ onChange={(e) => setComment(e.target.value)}
241
+ rows={3}
242
+ placeholder="Enter a comment…"
243
+ className="w-full text-sm border border-input rounded-md bg-background px-3 py-2 placeholder-muted-foreground focus:outline-none focus:border-ring focus:ring-1 focus:ring-ring transition-colors resize-none"
244
+ />
245
+ )}
246
+
247
+ <DialogFooter>
248
+ <button
249
+ type="button"
250
+ onClick={closeModal}
251
+ disabled={loading}
252
+ className="px-3 py-1.5 text-sm font-medium text-muted-foreground rounded-lg hover:bg-muted disabled:opacity-40 transition-colors"
253
+ >
254
+ Cancel
255
+ </button>
256
+ <button
257
+ type="button"
258
+ onClick={handleConfirm}
259
+ disabled={confirmDisabled}
260
+ className={`inline-flex items-center gap-1.5 px-4 py-1.5 text-sm font-semibold rounded-lg transition-all disabled:opacity-40 disabled:cursor-not-allowed ${actionClass(pending || {})}`}
261
+ >
262
+ {loading && <Loader2Icon className="w-3.5 h-3.5 animate-spin" />}
263
+ Confirm
264
+ </button>
265
+ </DialogFooter>
266
+ </DialogContent>
267
+ </Dialog>
268
+ </>
269
+ );
270
+ }
@@ -0,0 +1,119 @@
1
+ import { useCallback, useEffect, useState } from "react";
2
+ import { useForm } from "../../hooks/useForm.js";
3
+ import { FormHeader } from "./FormHeader.jsx";
4
+ import { FormBody } from "./FormBody.jsx";
5
+ import { FormAction } from "./FormAction.jsx";
6
+
7
+ export function Form({
8
+ flowType,
9
+ flowId,
10
+ viewId,
11
+ instanceId,
12
+ activityInstanceId,
13
+ title
14
+ }) {
15
+ const {
16
+ formData,
17
+ config,
18
+ errors,
19
+ loading,
20
+ error,
21
+ isSaving,
22
+ isNewRecord,
23
+ formActions,
24
+ reassignFromCandidates,
25
+ updateField,
26
+ runAction,
27
+ getFieldOptions,
28
+ parseAttachment,
29
+ getTable,
30
+ getReassignees
31
+ } = useForm({
32
+ flowType,
33
+ flowId,
34
+ viewId,
35
+ instanceId,
36
+ activityInstanceId
37
+ });
38
+
39
+ const [localState, setLocalState] = useState(formData);
40
+
41
+ useEffect(() => {
42
+ setLocalState(formData);
43
+ }, [formData]);
44
+
45
+ const handleLocalChange = useCallback((fieldId, value) => {
46
+ setLocalState((prev) => ({ ...prev, [fieldId]: value }));
47
+ }, []);
48
+
49
+ const handleFieldBlur = useCallback(
50
+ async (fieldId, value) => {
51
+ try {
52
+ await updateField(fieldId, value);
53
+ } catch (err) {
54
+ console.error("Field update failed:", err);
55
+ }
56
+ },
57
+ [updateField]
58
+ );
59
+
60
+ // Run any lifecycle action (Submit/Discard for dataform/board, or the
61
+ // full workflow-action set for process) through the hook's single
62
+ // dispatcher. The embedding page is responsible for navigating away from
63
+ // a completed/withdrawn item.
64
+ const handleAction = useCallback(
65
+ async (action, extra) => {
66
+ try {
67
+ return await runAction(action, extra);
68
+ } catch (err) {
69
+ console.error(`Action "${action}" failed:`, err);
70
+ return false;
71
+ }
72
+ },
73
+ [runAction]
74
+ );
75
+
76
+ // formPermission is the outer VBAC gate (whole flow/view access) — it
77
+ // overrides every field's own Permission when ReadOnly.
78
+ const isFormReadOnly = config.formPermission === "View";
79
+
80
+ const visibleSections = Array.isArray(config.sections)
81
+ ? config.sections.filter((s) => !s.IsHidden)
82
+ : [];
83
+
84
+ return (
85
+ <div className="min-h-screen bg-(--color-background) font-sans">
86
+ <FormHeader
87
+ title={title}
88
+ isNewRecord={isNewRecord}
89
+ isSaving={isSaving}
90
+ isFormReadOnly={isFormReadOnly}
91
+ />
92
+
93
+ <FormBody
94
+ sections={visibleSections}
95
+ localState={localState}
96
+ errors={errors}
97
+ loading={loading}
98
+ error={error}
99
+ isFormReadOnly={isFormReadOnly}
100
+ onFieldChange={handleLocalChange}
101
+ onFieldBlur={handleFieldBlur}
102
+ getFieldOptions={getFieldOptions}
103
+ parseAttachment={parseAttachment}
104
+ getTable={getTable}
105
+ />
106
+
107
+ <FormAction
108
+ flowType={flowType}
109
+ formActions={formActions}
110
+ reassignFromCandidates={reassignFromCandidates}
111
+ onAction={handleAction}
112
+ getReassignees={getReassignees}
113
+ loading={loading}
114
+ isNewRecord={isNewRecord}
115
+ isFormReadOnly={isFormReadOnly}
116
+ />
117
+ </div>
118
+ );
119
+ }
@@ -0,0 +1,213 @@
1
+ import { memo, useCallback, useMemo, useEffect, useState } from "react";
2
+ import { Trash2Icon } from "lucide-react";
3
+ import { getFieldComponent } from "../fields/index.js";
4
+ import { getTableFieldComponent } from "../tablefields/index.js";
5
+
6
+ function resolveFieldComponent(field) {
7
+ return getFieldComponent(field.Type, field.Widget);
8
+ }
9
+
10
+ export function withReadOnly(field, isFormReadOnly) {
11
+ return {
12
+ ...field,
13
+ ReadOnly:
14
+ isFormReadOnly || field.IsReadOnly || field.Permission === "ReadOnly"
15
+ };
16
+ }
17
+
18
+ // Memoised so individual field components only re-render when their own
19
+ // state (IsHidden, Required, Permission, IsReadOnly) actually changes — not
20
+ // on every keystroke in a sibling field.
21
+ export const FormField = memo(function FormFieldInner({
22
+ field,
23
+ isFormReadOnly,
24
+ value,
25
+ error,
26
+ disabled,
27
+ onChange,
28
+ onBlur,
29
+ getFieldOptions,
30
+ parseAttachment
31
+ }) {
32
+ const resolvedField = useMemo(
33
+ () => withReadOnly(field, isFormReadOnly),
34
+ [field, isFormReadOnly]
35
+ );
36
+ const FieldComponent = resolveFieldComponent(resolvedField);
37
+ const handleChange = useCallback(
38
+ (val) => onChange(field.Id, val),
39
+ [field.Id, onChange]
40
+ );
41
+ const handleBlur = useCallback(
42
+ (val) => onBlur(field.Id, val),
43
+ [field.Id, onBlur]
44
+ );
45
+ return (
46
+ <FieldComponent
47
+ field={resolvedField}
48
+ value={value}
49
+ onChange={handleChange}
50
+ onBlur={handleBlur}
51
+ error={error}
52
+ disabled={disabled}
53
+ getFieldOptions={getFieldOptions}
54
+ parseAttachment={parseAttachment}
55
+ />
56
+ );
57
+ });
58
+
59
+ // Memoised so individual table field components only re-render when their own
60
+ // state (IsHidden, Required, Permission, IsReadOnly) actually changes — not
61
+ // on every keystroke in a sibling field.
62
+ export const TableFieldCell = memo(function TableFieldCellInner({
63
+ field,
64
+ rowId,
65
+ tableId,
66
+ value,
67
+ table,
68
+ loading,
69
+ isTableReadOnly,
70
+ getFieldOptions,
71
+ error
72
+ }) {
73
+ const [localValue, setLocalValue] = useState(value ?? null);
74
+
75
+ useEffect(
76
+ function syncValue() {
77
+ setLocalValue(value ?? null);
78
+ },
79
+ [value]
80
+ );
81
+
82
+ const resolvedField = useMemo(
83
+ () => withReadOnly(field, isTableReadOnly),
84
+ [field, isTableReadOnly]
85
+ );
86
+
87
+ const handleChange = (val) => setLocalValue(val);
88
+ const handleBlur = (val) => table.updateRow(rowId, field.Id, val);
89
+
90
+ const boundGetFieldOptions = useCallback(
91
+ (fieldId) => getFieldOptions(fieldId, tableId, rowId),
92
+ [getFieldOptions, tableId, rowId]
93
+ );
94
+
95
+ const TableComponent = getTableFieldComponent(
96
+ resolvedField.Type,
97
+ resolvedField.Widget
98
+ );
99
+ const hasError = Array.isArray(error) ? error.length > 0 : Boolean(error);
100
+
101
+ return (
102
+ <div>
103
+ <TableComponent
104
+ field={resolvedField}
105
+ value={localValue}
106
+ onChange={handleChange}
107
+ onBlur={handleBlur}
108
+ disabled={loading}
109
+ getFieldOptions={boundGetFieldOptions}
110
+ />
111
+ {hasError && (
112
+ <p className="mt-0.5 text-[10px] text-destructive leading-tight">
113
+ {Array.isArray(error) ? error[0] : error}
114
+ </p>
115
+ )}
116
+ </div>
117
+ );
118
+ });
119
+
120
+ const DEFAULT_TABLE_THEME = {
121
+ theadClass: "bg-muted border-b border-(--color-border)",
122
+ thClass:
123
+ "px-4 py-3 text-left text-xs font-semibold text-muted-foreground uppercase tracking-wider whitespace-nowrap min-w-[140px]",
124
+ rowBorder: "border-b border-(--color-border) last:border-0",
125
+ rowBase: "bg-background",
126
+ rowAlt: "bg-muted/30",
127
+ rowHover: "hover:bg-muted/60",
128
+ deleteBtn:
129
+ "p-1 rounded text-muted-foreground hover:text-destructive hover:bg-destructive/10 disabled:opacity-40 disabled:cursor-not-allowed transition-colors",
130
+ wrapperClass: "overflow-x-auto"
131
+ };
132
+
133
+ // Shared child-table renderer used by every layout. Visual identity per
134
+ // layout comes from the `theme` prop (see DEFAULT_TABLE_THEME shape) rather
135
+ // than duplicating the <table> markup and TableFieldCell wiring five times.
136
+ export function SectionTable({
137
+ section,
138
+ columns,
139
+ rows,
140
+ table,
141
+ loading,
142
+ isTableReadOnly,
143
+ getFieldOptions,
144
+ tableErrors,
145
+ theme
146
+ }) {
147
+ const t = { ...DEFAULT_TABLE_THEME, ...theme };
148
+ return (
149
+ <div className={t.wrapperClass}>
150
+ <table className="min-w-full text-sm">
151
+ <thead className={t.theadClass}>
152
+ <tr>
153
+ {columns.map((col) => (
154
+ <th key={col.Id} className={t.thClass}>
155
+ {col.Name}
156
+ {col.Required && (
157
+ <span className="text-destructive ml-1">*</span>
158
+ )}
159
+ </th>
160
+ ))}
161
+ <th className="w-10" />
162
+ </tr>
163
+ </thead>
164
+ <tbody>
165
+ {rows.length === 0 ? (
166
+ <tr>
167
+ <td
168
+ colSpan={columns.length + 1}
169
+ className="px-4 py-10 text-center text-muted-foreground text-sm"
170
+ >
171
+ No rows yet
172
+ </td>
173
+ </tr>
174
+ ) : (
175
+ rows.map((row, rowIdx) => (
176
+ <tr
177
+ key={row._id}
178
+ className={`${t.rowBorder} ${rowIdx % 2 === 1 ? t.rowAlt : t.rowBase} ${t.rowHover} transition-colors`}
179
+ >
180
+ {columns.map((col) => (
181
+ <td key={col.Id} className="px-2 py-1.5">
182
+ <TableFieldCell
183
+ field={col}
184
+ isTableReadOnly={isTableReadOnly}
185
+ rowId={row._id}
186
+ tableId={section.Id}
187
+ value={row[col.Id]}
188
+ table={table}
189
+ loading={loading}
190
+ getFieldOptions={getFieldOptions}
191
+ error={tableErrors?.[row._id]?.[col.Id]}
192
+ />
193
+ </td>
194
+ ))}
195
+ <td className="px-3 py-1.5 text-center">
196
+ <button
197
+ type="button"
198
+ onClick={() => table.deleteRow(row._id)}
199
+ disabled={loading || isTableReadOnly}
200
+ className={t.deleteBtn}
201
+ title="Delete row"
202
+ >
203
+ <Trash2Icon className="w-3.5 h-3.5" />
204
+ </button>
205
+ </td>
206
+ </tr>
207
+ ))
208
+ )}
209
+ </tbody>
210
+ </table>
211
+ </div>
212
+ );
213
+ }
@@ -0,0 +1,14 @@
1
+ const formatValue = (type, value) => {
2
+ if (value === null || value === undefined || value === "") return "—";
3
+ if (type === "Date") return String(value).split("T")[0];
4
+ if (type === "DateTime") return String(value).slice(0, 16);
5
+ return String(value);
6
+ };
7
+
8
+ export function TableAggregationField({ field, value }) {
9
+ return (
10
+ <span className="flex items-center h-8 px-2 text-sm text-muted-foreground select-none">
11
+ {formatValue(field.Type, value)}
12
+ </span>
13
+ );
14
+ }
@@ -0,0 +1,53 @@
1
+ import { useState } from "react";
2
+ import { Paperclip } from "lucide-react";
3
+ import {
4
+ Dialog,
5
+ DialogContent,
6
+ DialogHeader,
7
+ DialogTitle
8
+ } from "@/components/ui/dialog";
9
+ import { AttachmentField } from "../fields/AttachmentField.jsx";
10
+
11
+ export function TableAttachmentField({
12
+ field,
13
+ value,
14
+ onChange,
15
+ onBlur,
16
+ disabled
17
+ }) {
18
+ const [open, setOpen] = useState(false);
19
+
20
+ const files = Array.isArray(value) ? value : [];
21
+ const count = files.length;
22
+
23
+ return (
24
+ <>
25
+ <button
26
+ type="button"
27
+ onClick={() => setOpen(true)}
28
+ disabled={disabled && field.ReadOnly}
29
+ className="flex items-center gap-1.5 h-8 px-2 text-sm text-muted-foreground hover:text-foreground disabled:opacity-50 disabled:cursor-not-allowed w-full"
30
+ >
31
+ <Paperclip className="w-3.5 h-3.5 text-muted-foreground shrink-0" />
32
+ <span className="text-xs">
33
+ {count === 0 ? "No files" : `${count} file${count !== 1 ? "s" : ""}`}
34
+ </span>
35
+ </button>
36
+
37
+ <Dialog open={open} onOpenChange={setOpen}>
38
+ <DialogContent className="sm:max-w-md">
39
+ <DialogHeader>
40
+ <DialogTitle className="text-base">{field.Name}</DialogTitle>
41
+ </DialogHeader>
42
+ <AttachmentField
43
+ field={field}
44
+ value={value}
45
+ onChange={onChange}
46
+ onBlur={onBlur}
47
+ disabled={disabled}
48
+ />
49
+ </DialogContent>
50
+ </Dialog>
51
+ </>
52
+ );
53
+ }
@@ -0,0 +1,26 @@
1
+ import { Switch } from "@/components/ui/switch";
2
+
3
+ export function TableBooleanField({
4
+ field,
5
+ value,
6
+ onChange,
7
+ onBlur,
8
+ disabled
9
+ }) {
10
+ const checked = Boolean(value);
11
+
12
+ const handleChange = (next) => {
13
+ onChange(next);
14
+ onBlur(next);
15
+ };
16
+
17
+ return (
18
+ <div className="flex items-center justify-center h-8">
19
+ <Switch
20
+ checked={checked}
21
+ onCheckedChange={handleChange}
22
+ disabled={disabled || field.ReadOnly}
23
+ />
24
+ </div>
25
+ );
26
+ }
@@ -0,0 +1,6 @@
1
+ import { TableMultiSelectField } from "./TableMultiSelectField.jsx";
2
+
3
+ // Checkbox fields share the same multi-select pattern: value is an array of selected option ids.
4
+ export function TableCheckboxField(props) {
5
+ return <TableMultiSelectField {...props} />;
6
+ }