@masterteam/components 0.0.291 → 0.0.292

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.
@@ -20,7 +20,7 @@ import { newFieldId } from '@masterteam/components';
20
20
  import { Skeleton } from 'primeng/skeleton';
21
21
  import { Message } from 'primeng/message';
22
22
  import { ModalService } from '@masterteam/components/modal';
23
- import { serializeEntityRawValue } from '@masterteam/components/entities';
23
+ import { readEntityValueColor, serializeEntityRawValue } from '@masterteam/components/entities';
24
24
  import * as i2$1 from 'primeng/inputtext';
25
25
  import { InputTextModule } from 'primeng/inputtext';
26
26
 
@@ -778,6 +778,16 @@ const ACCESS_ALIASES = {
778
778
  const ROW_ID_KEY = '__rowId';
779
779
  const ROW_ENTITY_ID_KEY = '__entityId';
780
780
  const ROW_VALUES_KEY = '__values';
781
+ const ROW_PREFILL_KEY = '__prefill';
782
+ /**
783
+ * View types whose form control binds the resolved object rather than the
784
+ * stored scalar, so the edit dialog has to be pre-filled from the rendered
785
+ * `value` of the cell and not its `raw`. Mirrors `resolveLoadedFieldValue`
786
+ * in `@masterteam/forms/client-form`, which makes the same exception for
787
+ * these two at the top level of a form — a user picker renders from the
788
+ * object and would otherwise display a bare id.
789
+ */
790
+ const OBJECT_VALUED_VIEW_TYPES = new Set(['User', 'MultiUser']);
781
791
  /**
782
792
  * Stable sort by the payload's `order`, falling back to array position when it
783
793
  * is absent. Used to flatten a process form into reading order: `order` on a
@@ -925,10 +935,15 @@ class EntityListField {
925
935
  // Keep the backend-rendered cells for untouched columns so resolved
926
936
  // labels (users, lookups, statuses) are not replaced by raw ids.
927
937
  const baseValues = base[ROW_VALUES_KEY] ?? {};
938
+ const basePrefill = base[ROW_PREFILL_KEY] ??
939
+ baseValues;
928
940
  const next = {
929
941
  ...base,
930
942
  [ROW_ID_KEY]: row.rowId,
931
943
  [ROW_VALUES_KEY]: { ...baseValues, ...changed },
944
+ // Re-opening the dialog must still show what the controls bind, so
945
+ // the resolved values survive a pending edit.
946
+ [ROW_PREFILL_KEY]: { ...basePrefill, ...changed },
932
947
  };
933
948
  for (const column of this.columns()) {
934
949
  const key = column.propertyKey ?? column.key;
@@ -1121,7 +1136,11 @@ class EntityListField {
1121
1136
  this.setRowDraft(rowId, values, parsed.display);
1122
1137
  this.pendingRows.set([
1123
1138
  ...this.pendingRows(),
1124
- { rowId, entityId: 0, values: this.toSubmitValues(values) },
1139
+ {
1140
+ rowId,
1141
+ entityId: 0,
1142
+ values: this.toSubmitValues(values, parsed.submit),
1143
+ },
1125
1144
  ]);
1126
1145
  this.emitValue();
1127
1146
  });
@@ -1135,6 +1154,10 @@ class EntityListField {
1135
1154
  const entityId = Number(row[ROW_ENTITY_ID_KEY]) || 0;
1136
1155
  const rowId = String(row[ROW_ID_KEY] ?? '');
1137
1156
  const initialValues = row[ROW_VALUES_KEY] ?? {};
1157
+ // The form is filled with the shapes its controls bind; the diff still
1158
+ // runs against `initialValues`, the stored scalars.
1159
+ const prefillValues = row[ROW_PREFILL_KEY] ??
1160
+ initialValues;
1138
1161
  const ref = this.modal.openModal(component, 'dialog', {
1139
1162
  header: this.transloco.translate('components.entityListField.edit'),
1140
1163
  styleClass: '!w-[48rem] max-w-[96vw]',
@@ -1145,7 +1168,7 @@ class EntityListField {
1145
1168
  levelDataId: this.levelDataId(),
1146
1169
  writeFields: this.writeFields(),
1147
1170
  lookups: this.lookups(),
1148
- initialValues,
1171
+ initialValues: prefillValues,
1149
1172
  isEdit: true,
1150
1173
  },
1151
1174
  });
@@ -1168,7 +1191,7 @@ class EntityListField {
1168
1191
  * (keys outside `readFields`) are left untouched by the backend.
1169
1192
  */
1170
1193
  applyExistingRowEdit(entityId, fallbackRowId, initialValues, parsed) {
1171
- const changed = this.diffValues(initialValues, parsed.values);
1194
+ const changed = this.diffValues(initialValues, parsed.values, parsed.submit);
1172
1195
  const current = this.pendingRows();
1173
1196
  const existing = current.find((row) => row.entityId === entityId);
1174
1197
  const rowId = existing?.rowId || (fallbackRowId ? fallbackRowId : `entity-${entityId}`);
@@ -1186,7 +1209,7 @@ class EntityListField {
1186
1209
  const next = {
1187
1210
  rowId,
1188
1211
  entityId,
1189
- values: this.toSubmitValues(merged),
1212
+ values: this.toSubmitValues(merged, parsed.submit),
1190
1213
  };
1191
1214
  this.pendingRows.set(existing
1192
1215
  ? current.map((row) => (row.entityId === entityId ? next : row))
@@ -1197,7 +1220,7 @@ class EntityListField {
1197
1220
  const values = this.dropEmptyValues(parsed.values);
1198
1221
  this.setRowDraft(rowId, values, parsed.display);
1199
1222
  this.pendingRows.set(this.pendingRows().map((row) => row.rowId === rowId
1200
- ? { ...row, values: this.toSubmitValues(values) }
1223
+ ? { ...row, values: this.toSubmitValues(values, parsed.submit) }
1201
1224
  : row));
1202
1225
  }
1203
1226
  /**
@@ -1236,9 +1259,10 @@ class EntityListField {
1236
1259
  return {
1237
1260
  values: values,
1238
1261
  display: payload['display'] ?? {},
1262
+ submit: payload['submit'] ?? {},
1239
1263
  };
1240
1264
  }
1241
- return { values: payload, display: {} };
1265
+ return { values: payload, display: {}, submit: {} };
1242
1266
  }
1243
1267
  nextRowId() {
1244
1268
  const used = new Set(this.pendingRows().map((row) => row.rowId));
@@ -1265,6 +1289,9 @@ class EntityListField {
1265
1289
  [ROW_ID_KEY]: rowId,
1266
1290
  [ROW_ENTITY_ID_KEY]: entityId,
1267
1291
  [ROW_VALUES_KEY]: values,
1292
+ // A drafted row is built from control values already, so there is
1293
+ // nothing to resolve — the two maps are the same.
1294
+ [ROW_PREFILL_KEY]: values,
1268
1295
  };
1269
1296
  for (const column of this.columns()) {
1270
1297
  // Form values are keyed by catalog property key (e.g. "task_name"),
@@ -1352,14 +1379,24 @@ class EntityListField {
1352
1379
  this.onModelChange(next);
1353
1380
  this.onTouched();
1354
1381
  }
1355
- /** `values[]` entries, restricted to `writeFields` when configured. */
1356
- toSubmitValues(values) {
1382
+ /**
1383
+ * `values[]` entries, restricted to `writeFields` when configured.
1384
+ *
1385
+ * `submitShaped` carries the dialog-normalized counterpart of `values`,
1386
+ * keyed the same way, and is preferred per key. The control value a rich
1387
+ * field holds is not the scalar the backend stores — a user picker holds
1388
+ * the whole search result, a date picker a `Date` — and only the dialog
1389
+ * knows the target module's view types, so it does that reduction. Without
1390
+ * it we would stringify the control value as-is and the row would carry a
1391
+ * JSON user object where a bare id belongs.
1392
+ */
1393
+ toSubmitValues(values, submitShaped = {}) {
1357
1394
  const allowed = new Set(this.writeFields());
1358
1395
  return Object.entries(values)
1359
1396
  .filter(([propertyKey]) => allowed.size === 0 || allowed.has(propertyKey))
1360
1397
  .map(([propertyKey, value]) => ({
1361
1398
  propertyKey,
1362
- rawValue: this.toRawValue(value),
1399
+ rawValue: this.toRawValue(propertyKey in submitShaped ? submitShaped[propertyKey] : value),
1363
1400
  }));
1364
1401
  }
1365
1402
  /** Backend `rawValue` is a serialized scalar; objects/arrays go as JSON. */
@@ -1394,14 +1431,21 @@ class EntityListField {
1394
1431
  * Keys the user actually changed. A key that was empty before and is still
1395
1432
  * empty is skipped (the dialog simply could not pre-fill it); a key that had
1396
1433
  * a value and is now empty is kept so the backend clears it.
1434
+ *
1435
+ * `initial` holds stored scalars (a `User` column pre-fills as a bare id)
1436
+ * while `next` holds control values (the whole picker result), so the two
1437
+ * are only comparable once reduced — hence comparing `submitShaped`.
1438
+ * Comparing the rich value directly marked every rich-valued column as
1439
+ * changed even when untouched, and the partial update then rewrote it.
1397
1440
  */
1398
- diffValues(initial, next) {
1441
+ diffValues(initial, next, submitShaped = {}) {
1399
1442
  const changed = {};
1400
1443
  for (const [key, value] of Object.entries(next)) {
1401
1444
  const before = initial[key];
1445
+ const comparable = key in submitShaped ? submitShaped[key] : value;
1402
1446
  if (this.isEmptyValue(value) && this.isEmptyValue(before))
1403
1447
  continue;
1404
- if (this.isSameValue(before, value))
1448
+ if (this.isSameValue(before, comparable))
1405
1449
  continue;
1406
1450
  changed[key] = value;
1407
1451
  }
@@ -1580,6 +1624,7 @@ class EntityListField {
1580
1624
  }
1581
1625
  const rows = (response.records ?? []).map((record) => {
1582
1626
  const values = {};
1627
+ const prefill = {};
1583
1628
  const row = {
1584
1629
  Id: record.id,
1585
1630
  [ROW_ID_KEY]: `entity-${record.id}`,
@@ -1591,11 +1636,17 @@ class EntityListField {
1591
1636
  const propertyKey = property?.key ?? column.key;
1592
1637
  const cell = record.values?.[propertyKey];
1593
1638
  row[column.key] = this.toEntityData(column, cell, property, record.id);
1594
- // Keep the machine-readable value so the edit dialog can pre-fill and
1595
- // partial updates can be diffed against it.
1639
+ // Keep the machine-readable value as the baseline a partial update is
1640
+ // diffed against — it is the shape the row submits.
1596
1641
  values[propertyKey] = cell?.raw ?? cell?.value ?? null;
1642
+ // What the edit dialog patches into its form, which is not always the
1643
+ // same thing: see OBJECT_VALUED_VIEW_TYPES.
1644
+ prefill[propertyKey] = OBJECT_VALUED_VIEW_TYPES.has(column.viewType ?? '')
1645
+ ? (cell?.value ?? cell?.raw ?? null)
1646
+ : values[propertyKey];
1597
1647
  }
1598
1648
  row[ROW_VALUES_KEY] = values;
1649
+ row[ROW_PREFILL_KEY] = prefill;
1599
1650
  return row;
1600
1651
  });
1601
1652
  this.rows.set(rows);
@@ -1608,6 +1659,7 @@ class EntityListField {
1608
1659
  normalizedKey: property?.normalizedKey ?? column.key,
1609
1660
  name: column.label ?? property?.label ?? column.key,
1610
1661
  rawValue: serializeEntityRawValue(cell?.raw),
1662
+ color: readEntityValueColor(cell?.color),
1611
1663
  value: (cell?.value ?? ''),
1612
1664
  viewType: (column.viewType ?? 'Text'),
1613
1665
  type: property?.source,