@asteby/metacore-runtime-react 8.0.0 → 9.0.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.
- package/CHANGELOG.md +63 -0
- package/dist/__tests__/dynamic-form.test.d.ts +2 -0
- package/dist/__tests__/dynamic-form.test.d.ts.map +1 -0
- package/dist/__tests__/dynamic-form.test.js +93 -0
- package/dist/__tests__/dynamic-relation.test.d.ts +2 -0
- package/dist/__tests__/dynamic-relation.test.d.ts.map +1 -0
- package/dist/__tests__/dynamic-relation.test.js +228 -0
- package/dist/dynamic-form-schema.d.ts +7 -0
- package/dist/dynamic-form-schema.d.ts.map +1 -0
- package/dist/dynamic-form-schema.js +68 -0
- package/dist/dynamic-form.d.ts +2 -0
- package/dist/dynamic-form.d.ts.map +1 -1
- package/dist/dynamic-form.js +28 -9
- package/dist/dynamic-relation-helpers.d.ts +77 -0
- package/dist/dynamic-relation-helpers.d.ts.map +1 -0
- package/dist/dynamic-relation-helpers.js +186 -0
- package/dist/dynamic-relation.d.ts +64 -0
- package/dist/dynamic-relation.d.ts.map +1 -0
- package/dist/dynamic-relation.js +226 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/types.d.ts +9 -0
- package/dist/types.d.ts.map +1 -1
- package/docs/relations.md +290 -0
- package/package.json +8 -3
- package/src/__tests__/dynamic-form.test.ts +104 -0
- package/src/__tests__/dynamic-relation.test.ts +293 -0
- package/src/dynamic-form-schema.ts +66 -0
- package/src/dynamic-form.tsx +34 -9
- package/src/dynamic-relation-helpers.ts +226 -0
- package/src/dynamic-relation.tsx +497 -0
- package/src/index.ts +10 -0
- package/src/types.ts +24 -0
- package/vitest.config.ts +8 -0
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
// Pure helpers for DynamicRelation. Live in their own module so unit tests
|
|
2
|
+
// run in node (no DOM, no metacore-ui primitives) and consumers can reuse the
|
|
3
|
+
// URL/payload conventions outside the component.
|
|
4
|
+
import type { ActionFieldDef, ColumnDefinition, TableMetadata } from './types'
|
|
5
|
+
|
|
6
|
+
export type DynamicRelationKind = 'one_to_many' | 'many_to_many'
|
|
7
|
+
|
|
8
|
+
export interface PivotRowLike {
|
|
9
|
+
id?: string | number | null
|
|
10
|
+
[k: string]: unknown
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface TargetRowLike {
|
|
14
|
+
id?: string | number | null
|
|
15
|
+
[k: string]: unknown
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Builds the query params used by `<DynamicRelation kind="one_to_many">` to
|
|
20
|
+
* scope a child list to a single parent record. Mirrors the
|
|
21
|
+
* `f_<column>=eq:<value>` convention enforced by `query/params.go` in the
|
|
22
|
+
* kernel.
|
|
23
|
+
*/
|
|
24
|
+
export function buildRelationFilterParams(
|
|
25
|
+
foreignKey: string,
|
|
26
|
+
parentId: string | number,
|
|
27
|
+
): Record<string, string> {
|
|
28
|
+
if (!foreignKey) throw new Error('foreignKey requerido')
|
|
29
|
+
if (parentId === undefined || parentId === null || parentId === '') {
|
|
30
|
+
throw new Error('parentId requerido')
|
|
31
|
+
}
|
|
32
|
+
return { [`f_${foreignKey}`]: `eq:${String(parentId)}` }
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Builds the POST body for creating a child row. The foreign key is forced to
|
|
37
|
+
* `parentId` regardless of what the form returned — the inline form hides the
|
|
38
|
+
* FK input but a misbehaving caller (or a manual override) should not be able
|
|
39
|
+
* to redirect the row to a different parent.
|
|
40
|
+
*/
|
|
41
|
+
export function buildCreatePayload(
|
|
42
|
+
foreignKey: string,
|
|
43
|
+
parentId: string | number,
|
|
44
|
+
formValues: Record<string, any>,
|
|
45
|
+
): Record<string, any> {
|
|
46
|
+
if (!foreignKey) throw new Error('foreignKey requerido')
|
|
47
|
+
return { ...formValues, [foreignKey]: parentId }
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Maps a `TableMetadata.columns` shape into `ActionFieldDef[]` so the inline
|
|
52
|
+
* form can be rendered with `<DynamicForm>`. Excludes the foreign-key column
|
|
53
|
+
* (already fixed to parentId) and any column flagged `hidden`. Falls back to
|
|
54
|
+
* sensible widget hints derived from `ColumnDefinition.type`.
|
|
55
|
+
*/
|
|
56
|
+
export function deriveRelationFormFields(
|
|
57
|
+
metadata: Pick<TableMetadata, 'columns'> | null | undefined,
|
|
58
|
+
foreignKey: string,
|
|
59
|
+
): ActionFieldDef[] {
|
|
60
|
+
if (!metadata?.columns) return []
|
|
61
|
+
const out: ActionFieldDef[] = []
|
|
62
|
+
for (const col of metadata.columns) {
|
|
63
|
+
if (col.key === foreignKey) continue
|
|
64
|
+
if (col.hidden) continue
|
|
65
|
+
out.push({
|
|
66
|
+
key: col.key,
|
|
67
|
+
label: col.label,
|
|
68
|
+
type: columnTypeToFieldType(col),
|
|
69
|
+
required: false,
|
|
70
|
+
options: col.options?.map(o => ({ value: String(o.value), label: o.label })),
|
|
71
|
+
})
|
|
72
|
+
}
|
|
73
|
+
return out
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function columnTypeToFieldType(col: ColumnDefinition): string {
|
|
77
|
+
switch (col.type) {
|
|
78
|
+
case 'number': return 'number'
|
|
79
|
+
case 'boolean': return 'boolean'
|
|
80
|
+
case 'date': return 'date'
|
|
81
|
+
case 'select': return 'select'
|
|
82
|
+
case 'text':
|
|
83
|
+
default:
|
|
84
|
+
return 'string'
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Stable id for relation rows. Falls back to a synthetic
|
|
90
|
+
* `__rel-<foreignKey>-<index>` when the row has no id — keeps React keys
|
|
91
|
+
* stable and lets optimistic creates render before the backend assigns an id.
|
|
92
|
+
*/
|
|
93
|
+
export function relationRowKey(
|
|
94
|
+
row: { id?: string | number | null } | undefined,
|
|
95
|
+
index: number,
|
|
96
|
+
foreignKey: string,
|
|
97
|
+
): string {
|
|
98
|
+
if (row && row.id !== undefined && row.id !== null && row.id !== '') {
|
|
99
|
+
return String(row.id)
|
|
100
|
+
}
|
|
101
|
+
return `__rel-${foreignKey}-${index}`
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// ---------------------------------------------------------------------------
|
|
105
|
+
// many_to_many helpers
|
|
106
|
+
// ---------------------------------------------------------------------------
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Builds the POST body for attaching a target row to the parent through the
|
|
110
|
+
* pivot table. Both FKs are required: `foreignKey -> parentId` (pivot side)
|
|
111
|
+
* and `referencesKey -> targetId` (target side). Extra pivot fields can be
|
|
112
|
+
* passed via `extra` (e.g. `role`, `position`); never override the two FKs.
|
|
113
|
+
*/
|
|
114
|
+
export function buildPivotAttachPayload(
|
|
115
|
+
foreignKey: string,
|
|
116
|
+
parentId: string | number,
|
|
117
|
+
referencesKey: string,
|
|
118
|
+
targetId: string | number,
|
|
119
|
+
extra?: Record<string, any>,
|
|
120
|
+
): Record<string, any> {
|
|
121
|
+
if (!foreignKey) throw new Error('foreignKey requerido')
|
|
122
|
+
if (!referencesKey) throw new Error('referencesKey requerido')
|
|
123
|
+
if (parentId === undefined || parentId === null || parentId === '') {
|
|
124
|
+
throw new Error('parentId requerido')
|
|
125
|
+
}
|
|
126
|
+
if (targetId === undefined || targetId === null || targetId === '') {
|
|
127
|
+
throw new Error('targetId requerido')
|
|
128
|
+
}
|
|
129
|
+
return {
|
|
130
|
+
...(extra || {}),
|
|
131
|
+
[foreignKey]: parentId,
|
|
132
|
+
[referencesKey]: targetId,
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* From a list of pivot rows, returns the set of currently-attached target ids
|
|
138
|
+
* coerced to string (the form expected by `<MultiSelect>` `selected`).
|
|
139
|
+
* Skips rows missing the FK (defensive — the kernel should never return them).
|
|
140
|
+
*/
|
|
141
|
+
export function extractSelectedTargetIds(
|
|
142
|
+
pivotRows: ReadonlyArray<PivotRowLike> | null | undefined,
|
|
143
|
+
referencesKey: string,
|
|
144
|
+
): string[] {
|
|
145
|
+
if (!pivotRows || !referencesKey) return []
|
|
146
|
+
const out: string[] = []
|
|
147
|
+
for (const row of pivotRows) {
|
|
148
|
+
const v = row[referencesKey]
|
|
149
|
+
if (v === undefined || v === null || v === '') continue
|
|
150
|
+
out.push(String(v))
|
|
151
|
+
}
|
|
152
|
+
return out
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Builds a `targetId -> pivotRowId` lookup so detach can DELETE the pivot row
|
|
157
|
+
* by its own id without re-fetching. When several pivot rows point at the
|
|
158
|
+
* same target (shouldn't happen with a unique constraint but the kernel does
|
|
159
|
+
* not guarantee it), the *last* one wins — detach will only drop one at a
|
|
160
|
+
* time, the next render will surface the leftover.
|
|
161
|
+
*/
|
|
162
|
+
export function buildPivotRowIndex(
|
|
163
|
+
pivotRows: ReadonlyArray<PivotRowLike> | null | undefined,
|
|
164
|
+
referencesKey: string,
|
|
165
|
+
): Map<string, string | number> {
|
|
166
|
+
const map = new Map<string, string | number>()
|
|
167
|
+
if (!pivotRows || !referencesKey) return map
|
|
168
|
+
for (const row of pivotRows) {
|
|
169
|
+
const target = row[referencesKey]
|
|
170
|
+
if (target === undefined || target === null || target === '') continue
|
|
171
|
+
if (row.id === undefined || row.id === null || row.id === '') continue
|
|
172
|
+
map.set(String(target), row.id as string | number)
|
|
173
|
+
}
|
|
174
|
+
return map
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Diffs the previous selection against the next one and returns the work to
|
|
179
|
+
* apply: target ids that need a POST (toAdd) and ids that need a DELETE
|
|
180
|
+
* (toRemove). Both arrays are deduped and order-stable to make tests
|
|
181
|
+
* deterministic.
|
|
182
|
+
*/
|
|
183
|
+
export function diffSelection(
|
|
184
|
+
prev: ReadonlyArray<string>,
|
|
185
|
+
next: ReadonlyArray<string>,
|
|
186
|
+
): { toAdd: string[]; toRemove: string[] } {
|
|
187
|
+
const prevSet = new Set(prev)
|
|
188
|
+
const nextSet = new Set(next)
|
|
189
|
+
const toAdd: string[] = []
|
|
190
|
+
for (const id of next) {
|
|
191
|
+
if (!prevSet.has(id) && !toAdd.includes(id)) toAdd.push(id)
|
|
192
|
+
}
|
|
193
|
+
const toRemove: string[] = []
|
|
194
|
+
for (const id of prev) {
|
|
195
|
+
if (!nextSet.has(id) && !toRemove.includes(id)) toRemove.push(id)
|
|
196
|
+
}
|
|
197
|
+
return { toAdd, toRemove }
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Picks a human-readable label for a target row. Tries `displayKey` first,
|
|
202
|
+
* then walks the metadata columns in order looking for the first non-id /
|
|
203
|
+
* non-FK string-ish value. Falls back to the row id, then to '—'.
|
|
204
|
+
*/
|
|
205
|
+
export function pickOptionLabel(
|
|
206
|
+
row: TargetRowLike | null | undefined,
|
|
207
|
+
displayKey: string | undefined,
|
|
208
|
+
columns: ReadonlyArray<ColumnDefinition> | null | undefined,
|
|
209
|
+
): string {
|
|
210
|
+
if (!row) return '—'
|
|
211
|
+
if (displayKey) {
|
|
212
|
+
const v = row[displayKey]
|
|
213
|
+
if (v !== undefined && v !== null && v !== '') return String(v)
|
|
214
|
+
}
|
|
215
|
+
if (columns) {
|
|
216
|
+
for (const col of columns) {
|
|
217
|
+
if (col.key === 'id' || col.hidden) continue
|
|
218
|
+
const v = row[col.key]
|
|
219
|
+
if (v === undefined || v === null || v === '') continue
|
|
220
|
+
if (typeof v === 'object') continue
|
|
221
|
+
return String(v)
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
if (row.id !== undefined && row.id !== null && row.id !== '') return String(row.id)
|
|
225
|
+
return '—'
|
|
226
|
+
}
|