@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.d.mts CHANGED
@@ -30,8 +30,9 @@ interface FieldViewProps {
30
30
  field: FormField;
31
31
  value: any;
32
32
  services?: FormServices;
33
+ colors?: FormColors;
33
34
  }
34
- declare const FieldView: ({ field, value, services }: FieldViewProps) => react_jsx_runtime.JSX.Element;
35
+ declare const FieldView: ({ field, value, services, colors }: FieldViewProps) => react_jsx_runtime.JSX.Element;
35
36
 
36
37
  interface SimpleSelectOption {
37
38
  value: string | number;
package/dist/index.d.ts CHANGED
@@ -30,8 +30,9 @@ interface FieldViewProps {
30
30
  field: FormField;
31
31
  value: any;
32
32
  services?: FormServices;
33
+ colors?: FormColors;
33
34
  }
34
- declare const FieldView: ({ field, value, services }: FieldViewProps) => react_jsx_runtime.JSX.Element;
35
+ declare const FieldView: ({ field, value, services, colors }: FieldViewProps) => react_jsx_runtime.JSX.Element;
35
36
 
36
37
  interface SimpleSelectOption {
37
38
  value: string | number;
package/dist/index.js CHANGED
@@ -1390,7 +1390,22 @@ var FieldRenderer = (props) => {
1390
1390
  return null;
1391
1391
  }
1392
1392
  };
1393
- var FieldView = ({ field, value, services }) => {
1393
+ var REFERENCE_OPTIONS_CACHE_TTL = 5 * 60 * 1e3;
1394
+ var referenceOptionsCache = /* @__PURE__ */ new Map();
1395
+ var getCachedOptions = (key) => {
1396
+ const cached = referenceOptionsCache.get(key);
1397
+ if (!cached) return null;
1398
+ if (Date.now() - cached.timestamp > REFERENCE_OPTIONS_CACHE_TTL) {
1399
+ referenceOptionsCache.delete(key);
1400
+ return null;
1401
+ }
1402
+ return cached.options;
1403
+ };
1404
+ var setCachedOptions = (key, options) => {
1405
+ referenceOptionsCache.set(key, { options, timestamp: Date.now() });
1406
+ };
1407
+ var FieldView = ({ field, value, services, colors }) => {
1408
+ const formColors = useFormColors(colors);
1394
1409
  const dateFormatter = services?.dateFormatter || defaultDateFormatterService;
1395
1410
  const FileDisplayComponent = services?.FileDisplayComponent;
1396
1411
  const CKEditorDisplayComponent = services?.CKEditorDisplayComponent;
@@ -1428,13 +1443,16 @@ var FieldView = ({ field, value, services }) => {
1428
1443
  borderColor: "divider"
1429
1444
  },
1430
1445
  children: [
1431
- /* @__PURE__ */ jsxRuntime.jsx(material.Typography, { variant: "body2", sx: { fontWeight: 500 }, children: field.label }),
1446
+ /* @__PURE__ */ jsxRuntime.jsx(material.Typography, { variant: "body2", sx: { fontWeight: 500, color: formColors.primary }, children: field.label }),
1432
1447
  /* @__PURE__ */ jsxRuntime.jsx(material.Typography, { variant: "body1", sx: { color: "text.secondary" }, children: value ? Array.isArray(value) ? `${value.length} file(s)` : "1 file" : "\u2014" })
1433
1448
  ]
1434
1449
  },
1435
1450
  field.name
1436
1451
  );
1437
1452
  }
1453
+ if (field.type === "formReference" || field.type === "apiReference") {
1454
+ return /* @__PURE__ */ jsxRuntime.jsx(ReferenceFieldView, { field, value, services });
1455
+ }
1438
1456
  if (field.type === "ckeditor") {
1439
1457
  if (CKEditorDisplayComponent) {
1440
1458
  return /* @__PURE__ */ jsxRuntime.jsxs(
@@ -1524,6 +1542,93 @@ var FieldView = ({ field, value, services }) => {
1524
1542
  field.name
1525
1543
  );
1526
1544
  };
1545
+ var ReferenceFieldView = ({
1546
+ field,
1547
+ value,
1548
+ services
1549
+ }) => {
1550
+ const [displayLabels, setDisplayLabels] = react.useState(null);
1551
+ react.useEffect(() => {
1552
+ if (value === null || value === void 0 || value === "") {
1553
+ setDisplayLabels([]);
1554
+ return;
1555
+ }
1556
+ let values;
1557
+ if (field.allowMultiple) {
1558
+ values = Array.isArray(value) ? value : value ? [value] : [];
1559
+ } else {
1560
+ values = Array.isArray(value) && value.length > 0 ? [value[0]] : value ? [value] : [];
1561
+ }
1562
+ if (values.length === 0) {
1563
+ setDisplayLabels([]);
1564
+ return;
1565
+ }
1566
+ const fetchLabels = async () => {
1567
+ try {
1568
+ let cacheKey = null;
1569
+ let options = null;
1570
+ if (field.type === "formReference" && field.referenceFormName && field.referenceFieldName) {
1571
+ cacheKey = `formRef:${field.referenceFormName}:${field.referenceFieldName}`;
1572
+ options = getCachedOptions(cacheKey);
1573
+ if (!options) {
1574
+ const formRefService = services?.formReference || defaultFormReferenceService;
1575
+ options = await formRefService.fetchOptions(field.referenceFormName, field.referenceFieldName);
1576
+ setCachedOptions(cacheKey, options);
1577
+ }
1578
+ } else if (field.type === "apiReference" && field.apiEndpoint && field.apiLabelField) {
1579
+ const valueField = field.apiValueField || "_id";
1580
+ cacheKey = `apiRef:${field.apiEndpoint}:${field.apiLabelField}:${valueField}`;
1581
+ options = getCachedOptions(cacheKey);
1582
+ if (!options) {
1583
+ const apiRefService = services?.apiReference || defaultApiReferenceService;
1584
+ options = await apiRefService.fetchOptions(field.apiEndpoint, field.apiLabelField, valueField);
1585
+ setCachedOptions(cacheKey, options);
1586
+ }
1587
+ }
1588
+ const labels = values.map((val) => {
1589
+ const option = (options || []).find((opt) => opt.value === val || String(opt.value) === String(val));
1590
+ return option ? option.label : String(val);
1591
+ });
1592
+ setDisplayLabels(labels);
1593
+ } catch {
1594
+ setDisplayLabels(values.map((v) => String(v)));
1595
+ }
1596
+ };
1597
+ fetchLabels();
1598
+ }, [field, value, services]);
1599
+ const displayText = displayLabels === null ? "..." : displayLabels.length === 0 ? "\u2014" : displayLabels.join(", ");
1600
+ return /* @__PURE__ */ jsxRuntime.jsxs(
1601
+ material.Box,
1602
+ {
1603
+ sx: {
1604
+ display: "flex",
1605
+ flexDirection: "column",
1606
+ gap: 0.5,
1607
+ py: 1.5,
1608
+ px: 1,
1609
+ borderColor: "divider"
1610
+ },
1611
+ children: [
1612
+ /* @__PURE__ */ jsxRuntime.jsx(material.Typography, { variant: "body2", sx: { fontWeight: 500 }, children: field.label }),
1613
+ /* @__PURE__ */ jsxRuntime.jsx(
1614
+ material.Typography,
1615
+ {
1616
+ variant: "body1",
1617
+ sx: {
1618
+ color: displayText === "\u2014" ? "text.disabled" : "text.primary",
1619
+ fontStyle: displayText === "\u2014" ? "italic" : "normal",
1620
+ fontWeight: 400,
1621
+ fontSize: "0.875rem",
1622
+ lineHeight: 1.5
1623
+ },
1624
+ children: displayText
1625
+ }
1626
+ )
1627
+ ]
1628
+ },
1629
+ field.name
1630
+ );
1631
+ };
1527
1632
  var formatFieldValue = (field, value, dateFormatter, services) => {
1528
1633
  if (value === null || value === void 0 || value === "") {
1529
1634
  return "\u2014";
@@ -1535,8 +1640,6 @@ var formatFieldValue = (field, value, dateFormatter, services) => {
1535
1640
  case "datepicker":
1536
1641
  return dateFormatter?.format(value, { datePickerMode: field.datePickerMode }) || String(value);
1537
1642
  case "select":
1538
- case "formReference":
1539
- case "apiReference":
1540
1643
  let values;
1541
1644
  if (field.allowMultiple) {
1542
1645
  values = Array.isArray(value) ? value : value ? [value] : [];
@@ -1598,7 +1701,7 @@ var FormViewMode = ({ formSchema, initialValues, hideTitle = false, services, co
1598
1701
  id: `panel-${section.id}-header`,
1599
1702
  children: /* @__PURE__ */ jsxRuntime.jsxs(material.Box, { sx: { display: "flex", alignItems: "center", gap: 1 }, children: [
1600
1703
  /* @__PURE__ */ jsxRuntime.jsx(DoubleArrowOutlinedIcon__default.default, { fontSize: "small", sx: { color: formColors.primary } }),
1601
- /* @__PURE__ */ jsxRuntime.jsx(material.Typography, { variant: "subtitle1", sx: { fontWeight: 600, color: formColors.primary }, children: section.title })
1704
+ /* @__PURE__ */ jsxRuntime.jsx(material.Typography, { variant: "subtitle1", sx: { color: formColors.primary }, children: section.title })
1602
1705
  ] })
1603
1706
  }
1604
1707
  ),
@@ -1612,7 +1715,7 @@ var FormViewMode = ({ formSchema, initialValues, hideTitle = false, services, co
1612
1715
  gridTemplateColumns: isMobile ? "repeat(1, 1fr)" : "repeat(2, 1fr)",
1613
1716
  gap: 1
1614
1717
  },
1615
- children: section.fields.map((field) => /* @__PURE__ */ jsxRuntime.jsx(FieldView, { field, value: initialValues?.[field.name], services }, field.name))
1718
+ children: section.fields.map((field) => /* @__PURE__ */ jsxRuntime.jsx(FieldView, { field, value: initialValues?.[field.name], services, colors }, field.name))
1616
1719
  }
1617
1720
  )
1618
1721
  ] })