@asteby/metacore-runtime-react 18.26.0 → 18.27.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 CHANGED
@@ -1,5 +1,11 @@
1
1
  # @asteby/metacore-runtime-react
2
2
 
3
+ ## 18.27.0
4
+
5
+ ### Minor Changes
6
+
7
+ - b71dcc0: feat(action-modal): line-items fields can prefill their rows from the acted-on record. A field whose `default` is a `PrefillSpec` (`{$prefillFromRecord, map, remaining}`) seeds one row per record array entry, copying mapped keys and computing a remaining quantity (`of - minus`), dropping fully-satisfied rows. Enables receive-goods/partial-reception modals (e.g. inventory transfers) to open pre-loaded with the pending lines instead of empty. Decoupled: the SDK only projects a record array into the field's item_fields.
8
+
3
9
  ## 18.26.0
4
10
 
5
11
  ### Minor Changes
@@ -1 +1 @@
1
- {"version":3,"file":"action-modal-dispatcher.d.ts","sourceRoot":"","sources":["../src/action-modal-dispatcher.tsx"],"names":[],"mappings":"AA+CA,OAAO,EACH,KAAK,cAAc,EACnB,KAAK,gBAAgB,EAExB,MAAM,sBAAsB,CAAA;AAE7B,YAAY,EAAE,cAAc,EAAE,gBAAgB,EAAE,CAAA;AAEhD,wBAAgB,qBAAqB,CAAC,EAClC,IAAI,EACJ,YAAY,EACZ,MAAM,EACN,KAAK,EACL,MAAM,EACN,QAAQ,EACR,SAAS,GACZ,EAAE,gBAAgB,sCAiDlB"}
1
+ {"version":3,"file":"action-modal-dispatcher.d.ts","sourceRoot":"","sources":["../src/action-modal-dispatcher.tsx"],"names":[],"mappings":"AA+CA,OAAO,EACH,KAAK,cAAc,EACnB,KAAK,gBAAgB,EAExB,MAAM,sBAAsB,CAAA;AAE7B,YAAY,EAAE,cAAc,EAAE,gBAAgB,EAAE,CAAA;AAkEhD,wBAAgB,qBAAqB,CAAC,EAClC,IAAI,EACJ,YAAY,EACZ,MAAM,EACN,KAAK,EACL,MAAM,EACN,QAAQ,EACR,SAAS,GACZ,EAAE,gBAAgB,sCAiDlB"}
@@ -21,6 +21,47 @@ import { UploadField } from './upload-field';
21
21
  import { isLineItemsField, resolveWidget, resolveDependsValue, getDependsOn } from './dynamic-form-schema';
22
22
  // Canonical registry lives in @asteby/metacore-sdk
23
23
  import { getActionComponent, } from '@asteby/metacore-sdk';
24
+ function isPrefillSpec(v) {
25
+ return (typeof v === 'object' &&
26
+ v !== null &&
27
+ typeof v.$prefillFromRecord === 'string');
28
+ }
29
+ // lineItemsDefault reads the field's declared default tolerating BOTH the
30
+ // camelCase `defaultValue` the host serves and the snake/legacy `default` the
31
+ // raw manifest carries (the kernel maps action-field `default` through without
32
+ // renaming it to `defaultValue`).
33
+ function lineItemsDefault(field) {
34
+ const f = field;
35
+ return f.defaultValue ?? f.default;
36
+ }
37
+ function toNum(v) {
38
+ const n = typeof v === 'number' ? v : parseFloat(String(v ?? ''));
39
+ return Number.isFinite(n) ? n : 0;
40
+ }
41
+ // buildPrefillRows projects record[spec.$prefillFromRecord] into modal rows.
42
+ function buildPrefillRows(spec, record) {
43
+ const src = record?.[spec.$prefillFromRecord];
44
+ if (!Array.isArray(src))
45
+ return [];
46
+ const rows = [];
47
+ for (const item of src) {
48
+ if (!item || typeof item !== 'object')
49
+ continue;
50
+ const row = {};
51
+ if (spec.map) {
52
+ for (const [target, from] of Object.entries(spec.map))
53
+ row[target] = item[from];
54
+ }
55
+ if (spec.remaining) {
56
+ const remaining = toNum(item[spec.remaining.of]) - (spec.remaining.minus ? toNum(item[spec.remaining.minus]) : 0);
57
+ if (remaining <= 0)
58
+ continue; // line already fully satisfied → omit
59
+ row[spec.remaining.target] = remaining;
60
+ }
61
+ rows.push(row);
62
+ }
63
+ return rows;
64
+ }
24
65
  export function ActionModalDispatcher({ open, onOpenChange, action, model, record, endpoint, onSuccess, }) {
25
66
  const CustomComponent = useMemo(() => getActionComponent(model, action.key), [model, action.key]);
26
67
  if (CustomComponent) {
@@ -83,14 +124,19 @@ function GenericActionModal({ open, onOpenChange, action, model, record, endpoin
83
124
  const defaults = {};
84
125
  for (const field of action.fields) {
85
126
  if (isLineItemsField(field)) {
86
- defaults[field.key] = field.defaultValue ?? [];
127
+ const dv = lineItemsDefault(field);
128
+ defaults[field.key] = isPrefillSpec(dv)
129
+ ? buildPrefillRows(dv, record)
130
+ : Array.isArray(dv)
131
+ ? dv
132
+ : [];
87
133
  continue;
88
134
  }
89
135
  defaults[field.key] = field.defaultValue ?? (field.type === 'boolean' ? false : '');
90
136
  }
91
137
  setFormData(defaults);
92
138
  }
93
- }, [open, action.fields]);
139
+ }, [open, action.fields, record]);
94
140
  const updateField = (key, value) => setFormData((prev) => ({ ...prev, [key]: value }));
95
141
  const execute = async () => {
96
142
  if (action.fields) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@asteby/metacore-runtime-react",
3
- "version": "18.26.0",
3
+ "version": "18.27.0",
4
4
  "description": "React runtime for metacore hosts — renders addon contributions dynamically",
5
5
  "repository": {
6
6
  "type": "git",
@@ -53,6 +53,70 @@ import {
53
53
 
54
54
  export type { ActionMetadata, ActionModalProps }
55
55
 
56
+ // ---- line-items prefill from the acted-on record ----------------------------
57
+ //
58
+ // A line-items action field can seed its rows from the record being acted on,
59
+ // instead of opening empty. The manifest declares this by setting the field's
60
+ // `default`/`defaultValue` to a PrefillSpec object: the modal reads the record's
61
+ // `$prefillFromRecord` array, copies the mapped keys, and (for a receive-style
62
+ // flow) computes a `remaining` quantity = of - minus, dropping fully-satisfied
63
+ // rows. Decoupled + generic: the SDK knows nothing about transfers, only how to
64
+ // project a record array into the field's item_fields. Example (receive goods):
65
+ //
66
+ // "default": {
67
+ // "$prefillFromRecord": "items",
68
+ // "map": { "product_id": "product_id" },
69
+ // "remaining": { "target": "qty_received", "of": "quantity", "minus": "received" }
70
+ // }
71
+ interface PrefillSpec {
72
+ $prefillFromRecord: string
73
+ map?: Record<string, string>
74
+ remaining?: { target: string; of: string; minus?: string }
75
+ }
76
+
77
+ function isPrefillSpec(v: unknown): v is PrefillSpec {
78
+ return (
79
+ typeof v === 'object' &&
80
+ v !== null &&
81
+ typeof (v as { $prefillFromRecord?: unknown }).$prefillFromRecord === 'string'
82
+ )
83
+ }
84
+
85
+ // lineItemsDefault reads the field's declared default tolerating BOTH the
86
+ // camelCase `defaultValue` the host serves and the snake/legacy `default` the
87
+ // raw manifest carries (the kernel maps action-field `default` through without
88
+ // renaming it to `defaultValue`).
89
+ function lineItemsDefault(field: ActionFieldDef): unknown {
90
+ const f = field as { defaultValue?: unknown; default?: unknown }
91
+ return f.defaultValue ?? f.default
92
+ }
93
+
94
+ function toNum(v: unknown): number {
95
+ const n = typeof v === 'number' ? v : parseFloat(String(v ?? ''))
96
+ return Number.isFinite(n) ? n : 0
97
+ }
98
+
99
+ // buildPrefillRows projects record[spec.$prefillFromRecord] into modal rows.
100
+ function buildPrefillRows(spec: PrefillSpec, record: any): Array<Record<string, any>> {
101
+ const src = record?.[spec.$prefillFromRecord]
102
+ if (!Array.isArray(src)) return []
103
+ const rows: Array<Record<string, any>> = []
104
+ for (const item of src) {
105
+ if (!item || typeof item !== 'object') continue
106
+ const row: Record<string, any> = {}
107
+ if (spec.map) {
108
+ for (const [target, from] of Object.entries(spec.map)) row[target] = item[from]
109
+ }
110
+ if (spec.remaining) {
111
+ const remaining = toNum(item[spec.remaining.of]) - (spec.remaining.minus ? toNum(item[spec.remaining.minus]) : 0)
112
+ if (remaining <= 0) continue // line already fully satisfied → omit
113
+ row[spec.remaining.target] = remaining
114
+ }
115
+ rows.push(row)
116
+ }
117
+ return rows
118
+ }
119
+
56
120
  export function ActionModalDispatcher({
57
121
  open,
58
122
  onOpenChange,
@@ -188,14 +252,19 @@ function GenericActionModal({ open, onOpenChange, action, model, record, endpoin
188
252
  const defaults: Record<string, any> = {}
189
253
  for (const field of action.fields) {
190
254
  if (isLineItemsField(field)) {
191
- defaults[field.key] = field.defaultValue ?? []
255
+ const dv = lineItemsDefault(field)
256
+ defaults[field.key] = isPrefillSpec(dv)
257
+ ? buildPrefillRows(dv, record)
258
+ : Array.isArray(dv)
259
+ ? dv
260
+ : []
192
261
  continue
193
262
  }
194
263
  defaults[field.key] = field.defaultValue ?? (field.type === 'boolean' ? false : '')
195
264
  }
196
265
  setFormData(defaults)
197
266
  }
198
- }, [open, action.fields])
267
+ }, [open, action.fields, record])
199
268
 
200
269
  const updateField = (key: string, value: any) => setFormData((prev: Record<string, any>) => ({ ...prev, [key]: value }))
201
270