@aatulwork/customform-renderer 1.12.0 → 1.14.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,49 @@ 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
+ }
1456
+ if (field.type === "color") {
1457
+ const colorValue = value && typeof value === "string" ? value : "#000000";
1458
+ return /* @__PURE__ */ jsxRuntime.jsxs(
1459
+ material.Box,
1460
+ {
1461
+ sx: {
1462
+ display: "flex",
1463
+ flexDirection: "column",
1464
+ gap: 0.5,
1465
+ py: 1.5,
1466
+ px: 1,
1467
+ borderColor: "divider"
1468
+ },
1469
+ children: [
1470
+ /* @__PURE__ */ jsxRuntime.jsx(material.Typography, { variant: "body2", sx: { fontWeight: 500 }, children: field.label }),
1471
+ /* @__PURE__ */ jsxRuntime.jsx(material.Box, { sx: { display: "flex", alignItems: "center", gap: 1 }, children: value ? /* @__PURE__ */ jsxRuntime.jsx(
1472
+ material.Box,
1473
+ {
1474
+ sx: {
1475
+ width: 30,
1476
+ height: 30,
1477
+ backgroundColor: colorValue,
1478
+ border: "1px solid",
1479
+ borderColor: "divider",
1480
+ borderRadius: 1
1481
+ }
1482
+ }
1483
+ ) : /* @__PURE__ */ jsxRuntime.jsx(material.Typography, { variant: "body1", sx: { color: "text.disabled", fontStyle: "italic" }, children: "\u2014" }) })
1484
+ ]
1485
+ },
1486
+ field.name
1487
+ );
1488
+ }
1438
1489
  if (field.type === "ckeditor") {
1439
1490
  if (CKEditorDisplayComponent) {
1440
1491
  return /* @__PURE__ */ jsxRuntime.jsxs(
@@ -1524,6 +1575,93 @@ var FieldView = ({ field, value, services }) => {
1524
1575
  field.name
1525
1576
  );
1526
1577
  };
1578
+ var ReferenceFieldView = ({
1579
+ field,
1580
+ value,
1581
+ services
1582
+ }) => {
1583
+ const [displayLabels, setDisplayLabels] = react.useState(null);
1584
+ react.useEffect(() => {
1585
+ if (value === null || value === void 0 || value === "") {
1586
+ setDisplayLabels([]);
1587
+ return;
1588
+ }
1589
+ let values;
1590
+ if (field.allowMultiple) {
1591
+ values = Array.isArray(value) ? value : value ? [value] : [];
1592
+ } else {
1593
+ values = Array.isArray(value) && value.length > 0 ? [value[0]] : value ? [value] : [];
1594
+ }
1595
+ if (values.length === 0) {
1596
+ setDisplayLabels([]);
1597
+ return;
1598
+ }
1599
+ const fetchLabels = async () => {
1600
+ try {
1601
+ let cacheKey = null;
1602
+ let options = null;
1603
+ if (field.type === "formReference" && field.referenceFormName && field.referenceFieldName) {
1604
+ cacheKey = `formRef:${field.referenceFormName}:${field.referenceFieldName}`;
1605
+ options = getCachedOptions(cacheKey);
1606
+ if (!options) {
1607
+ const formRefService = services?.formReference || defaultFormReferenceService;
1608
+ options = await formRefService.fetchOptions(field.referenceFormName, field.referenceFieldName);
1609
+ setCachedOptions(cacheKey, options);
1610
+ }
1611
+ } else if (field.type === "apiReference" && field.apiEndpoint && field.apiLabelField) {
1612
+ const valueField = field.apiValueField || "_id";
1613
+ cacheKey = `apiRef:${field.apiEndpoint}:${field.apiLabelField}:${valueField}`;
1614
+ options = getCachedOptions(cacheKey);
1615
+ if (!options) {
1616
+ const apiRefService = services?.apiReference || defaultApiReferenceService;
1617
+ options = await apiRefService.fetchOptions(field.apiEndpoint, field.apiLabelField, valueField);
1618
+ setCachedOptions(cacheKey, options);
1619
+ }
1620
+ }
1621
+ const labels = values.map((val) => {
1622
+ const option = (options || []).find((opt) => opt.value === val || String(opt.value) === String(val));
1623
+ return option ? option.label : String(val);
1624
+ });
1625
+ setDisplayLabels(labels);
1626
+ } catch {
1627
+ setDisplayLabels(values.map((v) => String(v)));
1628
+ }
1629
+ };
1630
+ fetchLabels();
1631
+ }, [field, value, services]);
1632
+ const displayText = displayLabels === null ? "..." : displayLabels.length === 0 ? "\u2014" : displayLabels.join(", ");
1633
+ return /* @__PURE__ */ jsxRuntime.jsxs(
1634
+ material.Box,
1635
+ {
1636
+ sx: {
1637
+ display: "flex",
1638
+ flexDirection: "column",
1639
+ gap: 0.5,
1640
+ py: 1.5,
1641
+ px: 1,
1642
+ borderColor: "divider"
1643
+ },
1644
+ children: [
1645
+ /* @__PURE__ */ jsxRuntime.jsx(material.Typography, { variant: "body2", sx: { fontWeight: 500 }, children: field.label }),
1646
+ /* @__PURE__ */ jsxRuntime.jsx(
1647
+ material.Typography,
1648
+ {
1649
+ variant: "body1",
1650
+ sx: {
1651
+ color: displayText === "\u2014" ? "text.disabled" : "text.primary",
1652
+ fontStyle: displayText === "\u2014" ? "italic" : "normal",
1653
+ fontWeight: 400,
1654
+ fontSize: "0.875rem",
1655
+ lineHeight: 1.5
1656
+ },
1657
+ children: displayText
1658
+ }
1659
+ )
1660
+ ]
1661
+ },
1662
+ field.name
1663
+ );
1664
+ };
1527
1665
  var formatFieldValue = (field, value, dateFormatter, services) => {
1528
1666
  if (value === null || value === void 0 || value === "") {
1529
1667
  return "\u2014";
@@ -1535,8 +1673,6 @@ var formatFieldValue = (field, value, dateFormatter, services) => {
1535
1673
  case "datepicker":
1536
1674
  return dateFormatter?.format(value, { datePickerMode: field.datePickerMode }) || String(value);
1537
1675
  case "select":
1538
- case "formReference":
1539
- case "apiReference":
1540
1676
  let values;
1541
1677
  if (field.allowMultiple) {
1542
1678
  values = Array.isArray(value) ? value : value ? [value] : [];
@@ -1567,6 +1703,8 @@ var formatFieldValue = (field, value, dateFormatter, services) => {
1567
1703
  return labels.join(", ");
1568
1704
  }
1569
1705
  return getLabelForValue(values[0]);
1706
+ case "color":
1707
+ return value ? String(value) : "\u2014";
1570
1708
  case "radio":
1571
1709
  if (field.options) {
1572
1710
  const normalizedOptions = normalizeOptions(field.options);
@@ -1598,7 +1736,7 @@ var FormViewMode = ({ formSchema, initialValues, hideTitle = false, services, co
1598
1736
  id: `panel-${section.id}-header`,
1599
1737
  children: /* @__PURE__ */ jsxRuntime.jsxs(material.Box, { sx: { display: "flex", alignItems: "center", gap: 1 }, children: [
1600
1738
  /* @__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 })
1739
+ /* @__PURE__ */ jsxRuntime.jsx(material.Typography, { variant: "subtitle1", sx: { color: formColors.primary }, children: section.title })
1602
1740
  ] })
1603
1741
  }
1604
1742
  ),
@@ -1612,7 +1750,7 @@ var FormViewMode = ({ formSchema, initialValues, hideTitle = false, services, co
1612
1750
  gridTemplateColumns: isMobile ? "repeat(1, 1fr)" : "repeat(2, 1fr)",
1613
1751
  gap: 1
1614
1752
  },
1615
- children: section.fields.map((field) => /* @__PURE__ */ jsxRuntime.jsx(FieldView, { field, value: initialValues?.[field.name], services }, field.name))
1753
+ children: section.fields.map((field) => /* @__PURE__ */ jsxRuntime.jsx(FieldView, { field, value: initialValues?.[field.name], services, colors }, field.name))
1616
1754
  }
1617
1755
  )
1618
1756
  ] })