@aatulwork/customform-renderer 1.12.0 → 1.13.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/dist/index.mjs CHANGED
@@ -1376,7 +1376,22 @@ var FieldRenderer = (props) => {
1376
1376
  return null;
1377
1377
  }
1378
1378
  };
1379
- var FieldView = ({ field, value, services }) => {
1379
+ var REFERENCE_OPTIONS_CACHE_TTL = 5 * 60 * 1e3;
1380
+ var referenceOptionsCache = /* @__PURE__ */ new Map();
1381
+ var getCachedOptions = (key) => {
1382
+ const cached = referenceOptionsCache.get(key);
1383
+ if (!cached) return null;
1384
+ if (Date.now() - cached.timestamp > REFERENCE_OPTIONS_CACHE_TTL) {
1385
+ referenceOptionsCache.delete(key);
1386
+ return null;
1387
+ }
1388
+ return cached.options;
1389
+ };
1390
+ var setCachedOptions = (key, options) => {
1391
+ referenceOptionsCache.set(key, { options, timestamp: Date.now() });
1392
+ };
1393
+ var FieldView = ({ field, value, services, colors }) => {
1394
+ const formColors = useFormColors(colors);
1380
1395
  const dateFormatter = services?.dateFormatter || defaultDateFormatterService;
1381
1396
  const FileDisplayComponent = services?.FileDisplayComponent;
1382
1397
  const CKEditorDisplayComponent = services?.CKEditorDisplayComponent;
@@ -1414,13 +1429,16 @@ var FieldView = ({ field, value, services }) => {
1414
1429
  borderColor: "divider"
1415
1430
  },
1416
1431
  children: [
1417
- /* @__PURE__ */ jsx(Typography, { variant: "body2", sx: { fontWeight: 500 }, children: field.label }),
1432
+ /* @__PURE__ */ jsx(Typography, { variant: "body2", sx: { fontWeight: 500, color: formColors.primary }, children: field.label }),
1418
1433
  /* @__PURE__ */ jsx(Typography, { variant: "body1", sx: { color: "text.secondary" }, children: value ? Array.isArray(value) ? `${value.length} file(s)` : "1 file" : "\u2014" })
1419
1434
  ]
1420
1435
  },
1421
1436
  field.name
1422
1437
  );
1423
1438
  }
1439
+ if (field.type === "formReference" || field.type === "apiReference") {
1440
+ return /* @__PURE__ */ jsx(ReferenceFieldView, { field, value, services });
1441
+ }
1424
1442
  if (field.type === "ckeditor") {
1425
1443
  if (CKEditorDisplayComponent) {
1426
1444
  return /* @__PURE__ */ jsxs(
@@ -1510,6 +1528,93 @@ var FieldView = ({ field, value, services }) => {
1510
1528
  field.name
1511
1529
  );
1512
1530
  };
1531
+ var ReferenceFieldView = ({
1532
+ field,
1533
+ value,
1534
+ services
1535
+ }) => {
1536
+ const [displayLabels, setDisplayLabels] = useState(null);
1537
+ useEffect(() => {
1538
+ if (value === null || value === void 0 || value === "") {
1539
+ setDisplayLabels([]);
1540
+ return;
1541
+ }
1542
+ let values;
1543
+ if (field.allowMultiple) {
1544
+ values = Array.isArray(value) ? value : value ? [value] : [];
1545
+ } else {
1546
+ values = Array.isArray(value) && value.length > 0 ? [value[0]] : value ? [value] : [];
1547
+ }
1548
+ if (values.length === 0) {
1549
+ setDisplayLabels([]);
1550
+ return;
1551
+ }
1552
+ const fetchLabels = async () => {
1553
+ try {
1554
+ let cacheKey = null;
1555
+ let options = null;
1556
+ if (field.type === "formReference" && field.referenceFormName && field.referenceFieldName) {
1557
+ cacheKey = `formRef:${field.referenceFormName}:${field.referenceFieldName}`;
1558
+ options = getCachedOptions(cacheKey);
1559
+ if (!options) {
1560
+ const formRefService = services?.formReference || defaultFormReferenceService;
1561
+ options = await formRefService.fetchOptions(field.referenceFormName, field.referenceFieldName);
1562
+ setCachedOptions(cacheKey, options);
1563
+ }
1564
+ } else if (field.type === "apiReference" && field.apiEndpoint && field.apiLabelField) {
1565
+ const valueField = field.apiValueField || "_id";
1566
+ cacheKey = `apiRef:${field.apiEndpoint}:${field.apiLabelField}:${valueField}`;
1567
+ options = getCachedOptions(cacheKey);
1568
+ if (!options) {
1569
+ const apiRefService = services?.apiReference || defaultApiReferenceService;
1570
+ options = await apiRefService.fetchOptions(field.apiEndpoint, field.apiLabelField, valueField);
1571
+ setCachedOptions(cacheKey, options);
1572
+ }
1573
+ }
1574
+ const labels = values.map((val) => {
1575
+ const option = (options || []).find((opt) => opt.value === val || String(opt.value) === String(val));
1576
+ return option ? option.label : String(val);
1577
+ });
1578
+ setDisplayLabels(labels);
1579
+ } catch {
1580
+ setDisplayLabels(values.map((v) => String(v)));
1581
+ }
1582
+ };
1583
+ fetchLabels();
1584
+ }, [field, value, services]);
1585
+ const displayText = displayLabels === null ? "..." : displayLabels.length === 0 ? "\u2014" : displayLabels.join(", ");
1586
+ return /* @__PURE__ */ jsxs(
1587
+ Box,
1588
+ {
1589
+ sx: {
1590
+ display: "flex",
1591
+ flexDirection: "column",
1592
+ gap: 0.5,
1593
+ py: 1.5,
1594
+ px: 1,
1595
+ borderColor: "divider"
1596
+ },
1597
+ children: [
1598
+ /* @__PURE__ */ jsx(Typography, { variant: "body2", sx: { fontWeight: 500 }, children: field.label }),
1599
+ /* @__PURE__ */ jsx(
1600
+ Typography,
1601
+ {
1602
+ variant: "body1",
1603
+ sx: {
1604
+ color: displayText === "\u2014" ? "text.disabled" : "text.primary",
1605
+ fontStyle: displayText === "\u2014" ? "italic" : "normal",
1606
+ fontWeight: 400,
1607
+ fontSize: "0.875rem",
1608
+ lineHeight: 1.5
1609
+ },
1610
+ children: displayText
1611
+ }
1612
+ )
1613
+ ]
1614
+ },
1615
+ field.name
1616
+ );
1617
+ };
1513
1618
  var formatFieldValue = (field, value, dateFormatter, services) => {
1514
1619
  if (value === null || value === void 0 || value === "") {
1515
1620
  return "\u2014";
@@ -1521,8 +1626,6 @@ var formatFieldValue = (field, value, dateFormatter, services) => {
1521
1626
  case "datepicker":
1522
1627
  return dateFormatter?.format(value, { datePickerMode: field.datePickerMode }) || String(value);
1523
1628
  case "select":
1524
- case "formReference":
1525
- case "apiReference":
1526
1629
  let values;
1527
1630
  if (field.allowMultiple) {
1528
1631
  values = Array.isArray(value) ? value : value ? [value] : [];
@@ -1584,7 +1687,7 @@ var FormViewMode = ({ formSchema, initialValues, hideTitle = false, services, co
1584
1687
  id: `panel-${section.id}-header`,
1585
1688
  children: /* @__PURE__ */ jsxs(Box, { sx: { display: "flex", alignItems: "center", gap: 1 }, children: [
1586
1689
  /* @__PURE__ */ jsx(DoubleArrowOutlinedIcon, { fontSize: "small", sx: { color: formColors.primary } }),
1587
- /* @__PURE__ */ jsx(Typography, { variant: "subtitle1", sx: { fontWeight: 600, color: formColors.primary }, children: section.title })
1690
+ /* @__PURE__ */ jsx(Typography, { variant: "subtitle1", sx: { color: formColors.primary }, children: section.title })
1588
1691
  ] })
1589
1692
  }
1590
1693
  ),
@@ -1598,7 +1701,7 @@ var FormViewMode = ({ formSchema, initialValues, hideTitle = false, services, co
1598
1701
  gridTemplateColumns: isMobile ? "repeat(1, 1fr)" : "repeat(2, 1fr)",
1599
1702
  gap: 1
1600
1703
  },
1601
- children: section.fields.map((field) => /* @__PURE__ */ jsx(FieldView, { field, value: initialValues?.[field.name], services }, field.name))
1704
+ children: section.fields.map((field) => /* @__PURE__ */ jsx(FieldView, { field, value: initialValues?.[field.name], services, colors }, field.name))
1602
1705
  }
1603
1706
  )
1604
1707
  ] })