@uniform-ts/core 0.0.7 → 0.0.8

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.js CHANGED
@@ -657,6 +657,24 @@ function SelectField({
657
657
  }
658
658
  );
659
659
  }
660
+ function useConditionalFields(fields, control, scopeName) {
661
+ const allValues = reactHookForm.useWatch({ control });
662
+ const scopedValues = reactHookForm.useWatch({ control, name: scopeName });
663
+ const values = scopeName ? scopedValues : allValues;
664
+ return React3.useMemo(() => {
665
+ return fields.filter((field) => {
666
+ if (field.meta.hidden) return false;
667
+ if (typeof field.meta.condition === "function") {
668
+ return field.meta.condition(values);
669
+ }
670
+ return true;
671
+ }).sort((a, b) => {
672
+ const orderA = typeof a.meta.order === "number" ? a.meta.order : Infinity;
673
+ const orderB = typeof b.meta.order === "number" ? b.meta.order : Infinity;
674
+ return orderA - orderB;
675
+ });
676
+ }, [fields, values]);
677
+ }
660
678
  function ObjectField({
661
679
  field,
662
680
  control,
@@ -665,7 +683,7 @@ function ObjectField({
665
683
  shouldUnregister
666
684
  }) {
667
685
  const { classNames, layout } = useAutoFormContext();
668
- const children = field.children;
686
+ const children = useConditionalFields(field.children, control, namePrefix);
669
687
  const content = children.map((child, idx) => /* @__PURE__ */ jsxRuntime.jsx(
670
688
  FieldRenderer,
671
689
  {
@@ -781,7 +799,7 @@ function ArrayField({ field, control, effectiveName }) {
781
799
  const RowLayout = layout.arrayRowLayout;
782
800
  const renderedRows = rows.map((row, index) => {
783
801
  const isCollapsed = showCollapse && collapsed.has(index);
784
- const collapseButton = showCollapse ? /* @__PURE__ */ jsxRuntime.jsxs(
802
+ const collapseButton = showCollapse && CollapseBtn ? /* @__PURE__ */ jsxRuntime.jsxs(
785
803
  CollapseBtn,
786
804
  {
787
805
  type: "button",
@@ -805,7 +823,7 @@ function ArrayField({ field, control, effectiveName }) {
805
823
  ]
806
824
  }
807
825
  ) : null;
808
- const moveUpButton = showMove && rows.length > 1 ? /* @__PURE__ */ jsxRuntime.jsx(
826
+ const moveUpButton = showMove && rows.length > 1 && MoveUpBtn ? /* @__PURE__ */ jsxRuntime.jsx(
809
827
  MoveUpBtn,
810
828
  {
811
829
  type: "button",
@@ -816,7 +834,7 @@ function ArrayField({ field, control, effectiveName }) {
816
834
  children: labels.arrayMoveUp ?? "\u2191"
817
835
  }
818
836
  ) : null;
819
- const moveDownButton = showMove && rows.length > 1 ? /* @__PURE__ */ jsxRuntime.jsx(
837
+ const moveDownButton = showMove && rows.length > 1 && MoveDownBtn ? /* @__PURE__ */ jsxRuntime.jsx(
820
838
  MoveDownBtn,
821
839
  {
822
840
  type: "button",
@@ -827,7 +845,7 @@ function ArrayField({ field, control, effectiveName }) {
827
845
  children: labels.arrayMoveDown ?? "\u2193"
828
846
  }
829
847
  ) : null;
830
- const duplicateButton = showDuplicate && !atMax ? /* @__PURE__ */ jsxRuntime.jsx(
848
+ const duplicateButton = showDuplicate && !atMax && DuplicateBtn ? /* @__PURE__ */ jsxRuntime.jsx(
831
849
  DuplicateBtn,
832
850
  {
833
851
  type: "button",
@@ -842,7 +860,7 @@ function ArrayField({ field, control, effectiveName }) {
842
860
  children: labels.arrayDuplicate ?? "Duplicate"
843
861
  }
844
862
  ) : null;
845
- const removeButton = /* @__PURE__ */ jsxRuntime.jsx(
863
+ const removeButton = RemoveBtn ? /* @__PURE__ */ jsxRuntime.jsx(
846
864
  RemoveBtn,
847
865
  {
848
866
  type: "button",
@@ -852,7 +870,7 @@ function ArrayField({ field, control, effectiveName }) {
852
870
  "aria-label": labels.arrayAriaRemove?.(index) ?? `Remove item ${index + 1}`,
853
871
  children: labels.arrayRemove ?? "Remove"
854
872
  }
855
- );
873
+ ) : null;
856
874
  const fieldContent = !isCollapsed ? /* @__PURE__ */ jsxRuntime.jsx(
857
875
  FieldRenderer,
858
876
  {
@@ -878,7 +896,7 @@ function ArrayField({ field, control, effectiveName }) {
878
896
  row.id
879
897
  );
880
898
  });
881
- const addButton = /* @__PURE__ */ jsxRuntime.jsx(
899
+ const addButton = AddBtn ? /* @__PURE__ */ jsxRuntime.jsx(
882
900
  AddBtn,
883
901
  {
884
902
  type: "button",
@@ -887,7 +905,7 @@ function ArrayField({ field, control, effectiveName }) {
887
905
  onClick: () => append(getDefaultValue(itemConfig)),
888
906
  children: labels.arrayAdd ?? "Add"
889
907
  }
890
- );
908
+ ) : null;
891
909
  const content = /* @__PURE__ */ jsxRuntime.jsx(
892
910
  ArrayFieldLayout,
893
911
  {
@@ -1049,22 +1067,6 @@ function FieldRenderer({
1049
1067
  }
1050
1068
  );
1051
1069
  }
1052
- function useConditionalFields(fields, control) {
1053
- const values = reactHookForm.useWatch({ control });
1054
- return React3.useMemo(() => {
1055
- return fields.filter((field) => {
1056
- if (field.meta.hidden) return false;
1057
- if (typeof field.meta.condition === "function") {
1058
- return field.meta.condition(values);
1059
- }
1060
- return true;
1061
- }).sort((a, b) => {
1062
- const orderA = typeof a.meta.order === "number" ? a.meta.order : Infinity;
1063
- const orderB = typeof b.meta.order === "number" ? b.meta.order : Infinity;
1064
- return orderA - orderB;
1065
- });
1066
- }, [fields, values]);
1067
- }
1068
1070
  function useSectionGrouping(fields) {
1069
1071
  return React3.useMemo(() => {
1070
1072
  const ungrouped = [];
@@ -1303,6 +1305,12 @@ function buildDefaults(fields) {
1303
1305
  }
1304
1306
  return result;
1305
1307
  }
1308
+
1309
+ // src/utils/resolveNullableSlot.ts
1310
+ function resolveNullableSlot(slot, fallback) {
1311
+ if (slot === null) return null;
1312
+ return slot ?? fallback;
1313
+ }
1306
1314
  function AutoForm(props) {
1307
1315
  const {
1308
1316
  form: uniForm,
@@ -1527,24 +1535,33 @@ function AutoForm(props) {
1527
1535
  const visibleFields = useConditionalFields(fieldsWithDynamic, control);
1528
1536
  const sections = useSectionGrouping(visibleFields);
1529
1537
  const resolvedLayout = React3__namespace.useMemo(() => {
1530
- const base = layout?.arrayButtons?.base ?? DefaultArrayButton;
1538
+ const base = resolveNullableSlot(
1539
+ layout?.arrayButtons?.base,
1540
+ DefaultArrayButton
1541
+ );
1531
1542
  const slots = layout?.arrayButtons;
1532
1543
  return {
1533
1544
  formWrapper: layout?.formWrapper ?? DefaultFormWrapper,
1534
1545
  sectionWrapper: layout?.sectionWrapper ?? DefaultSectionWrapper,
1535
- submitButton: layout?.submitButton ?? DefaultSubmitButton,
1546
+ submitButton: resolveNullableSlot(
1547
+ layout?.submitButton,
1548
+ DefaultSubmitButton
1549
+ ),
1536
1550
  arrayRowLayout: layout?.arrayRowLayout ?? DefaultArrayRowLayout,
1537
1551
  arrayFieldLayout: layout?.arrayFieldLayout ?? DefaultArrayFieldLayout,
1538
1552
  objectWrapper: layout?.objectWrapper ?? DefaultObjectWrapper,
1539
1553
  arrayWrapper: layout?.arrayWrapper ?? DefaultArrayWrapper,
1540
1554
  arrayButtons: {
1541
1555
  base,
1542
- add: slots?.add ?? base,
1543
- remove: slots?.remove ?? base,
1544
- moveUp: slots?.moveUp ?? base,
1545
- moveDown: slots?.moveDown ?? base,
1546
- duplicate: slots?.duplicate ?? base,
1547
- collapse: slots?.collapse ?? DefaultArrayCollapseButton
1556
+ add: resolveNullableSlot(slots?.add, base),
1557
+ remove: resolveNullableSlot(slots?.remove, base),
1558
+ moveUp: resolveNullableSlot(slots?.moveUp, base),
1559
+ moveDown: resolveNullableSlot(slots?.moveDown, base),
1560
+ duplicate: resolveNullableSlot(slots?.duplicate, base),
1561
+ collapse: resolveNullableSlot(
1562
+ slots?.collapse,
1563
+ DefaultArrayCollapseButton
1564
+ )
1548
1565
  },
1549
1566
  loadingFallback: layout?.loadingFallback ?? /* @__PURE__ */ jsxRuntime.jsx("p", { children: "Loading\u2026" })
1550
1567
  };
@@ -1566,6 +1583,7 @@ function AutoForm(props) {
1566
1583
  const contextValue = React3__namespace.useMemo(
1567
1584
  () => ({
1568
1585
  registry,
1586
+ fieldConfigs: mergedFields,
1569
1587
  fieldOverrides: fieldOverridesProp,
1570
1588
  fieldWrapper: resolvedFieldWrapper,
1571
1589
  layout: resolvedLayout,
@@ -1574,10 +1592,12 @@ function AutoForm(props) {
1574
1592
  coercions,
1575
1593
  messages,
1576
1594
  labels,
1577
- formMethods
1595
+ formMethods,
1596
+ control
1578
1597
  }),
1579
1598
  [
1580
1599
  registry,
1600
+ mergedFields,
1581
1601
  fieldOverridesProp,
1582
1602
  resolvedFieldWrapper,
1583
1603
  resolvedLayout,
@@ -1586,7 +1606,8 @@ function AutoForm(props) {
1586
1606
  coercions,
1587
1607
  messages,
1588
1608
  labels,
1589
- formMethods
1609
+ formMethods,
1610
+ control
1590
1611
  ]
1591
1612
  );
1592
1613
  if (isLoadingDefaults) {
@@ -1630,13 +1651,13 @@ function AutoForm(props) {
1630
1651
  section.title
1631
1652
  );
1632
1653
  }),
1633
- /* @__PURE__ */ jsxRuntime.jsx(
1654
+ SubmitButton ? /* @__PURE__ */ jsxRuntime.jsx(
1634
1655
  SubmitButton,
1635
1656
  {
1636
1657
  isSubmitting: formState.isSubmitting,
1637
1658
  label: labels.submit ?? "Submit"
1638
1659
  }
1639
- )
1660
+ ) : null
1640
1661
  ] })
1641
1662
  }
1642
1663
  ) });
@@ -1735,6 +1756,34 @@ var UniForm = class {
1735
1756
  function createForm(schema) {
1736
1757
  return new UniForm(schema);
1737
1758
  }
1759
+ function findArrayConfig(fields, name) {
1760
+ for (const field of fields) {
1761
+ if (field.name === name) {
1762
+ return field.type === "array" ? field : void 0;
1763
+ }
1764
+ if (field.type === "object") {
1765
+ const found = findArrayConfig(field.children, name);
1766
+ if (found) return found;
1767
+ }
1768
+ }
1769
+ return void 0;
1770
+ }
1771
+ function useArrayField(fieldName) {
1772
+ const { control, fieldConfigs } = useAutoFormContext();
1773
+ const result = reactHookForm.useFieldArray({ control, name: fieldName });
1774
+ const rowCount = result.fields.length;
1775
+ const config = findArrayConfig(fieldConfigs, fieldName);
1776
+ const minItems = config?.minItems;
1777
+ const maxItems = config?.maxItems;
1778
+ const canAdd = maxItems == null || rowCount < maxItems;
1779
+ const atMin = minItems != null && rowCount <= minItems;
1780
+ return {
1781
+ ...result,
1782
+ rowCount,
1783
+ canAdd,
1784
+ atMin
1785
+ };
1786
+ }
1738
1787
 
1739
1788
  exports.AutoForm = AutoForm;
1740
1789
  exports.DefaultArrayButton = DefaultArrayButton;
@@ -1758,6 +1807,7 @@ exports.defaultRegistry = defaultRegistry;
1758
1807
  exports.introspectObjectSchema = introspectObjectSchema;
1759
1808
  exports.introspectSchema = introspectSchema;
1760
1809
  exports.mergeRegistries = mergeRegistries;
1810
+ exports.useArrayField = useArrayField;
1761
1811
  exports.useAutoFormContext = useAutoFormContext;
1762
1812
  exports.useConditionalFields = useConditionalFields;
1763
1813
  exports.useFormPersistence = useFormPersistence;