@uniform-ts/core 0.0.3 → 0.0.5

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
@@ -399,6 +399,46 @@ function DefaultArrayRowLayout({
399
399
  ] })
400
400
  ] });
401
401
  }
402
+ function DefaultArrayFieldLayout({
403
+ rows,
404
+ addButton
405
+ }) {
406
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
407
+ rows,
408
+ addButton
409
+ ] });
410
+ }
411
+ function DefaultObjectWrapper({
412
+ children,
413
+ label,
414
+ className,
415
+ labelClassName
416
+ }) {
417
+ return /* @__PURE__ */ jsxs("fieldset", { className, children: [
418
+ label && /* @__PURE__ */ jsx("legend", { className: labelClassName, children: label }),
419
+ children
420
+ ] });
421
+ }
422
+ function DefaultArrayWrapper({
423
+ children,
424
+ label,
425
+ className,
426
+ labelClassName
427
+ }) {
428
+ return /* @__PURE__ */ jsxs("fieldset", { className, children: [
429
+ label && /* @__PURE__ */ jsx("legend", { className: labelClassName, children: label }),
430
+ children
431
+ ] });
432
+ }
433
+ function DefaultArrayButton(props) {
434
+ return /* @__PURE__ */ jsx("button", { ...props });
435
+ }
436
+ function DefaultArrayCollapseButton({
437
+ isCollapsed: _isCollapsed,
438
+ ...props
439
+ }) {
440
+ return /* @__PURE__ */ jsx("button", { ...props });
441
+ }
402
442
 
403
443
  // src/utils/resolveErrorMessage.ts
404
444
  function resolveErrorMessage(fieldName, error, messages) {
@@ -602,6 +642,7 @@ function ObjectField({
602
642
  depth = 0,
603
643
  shouldUnregister
604
644
  }) {
645
+ const { classNames, layout } = useAutoFormContext();
605
646
  const children = field.children;
606
647
  const content = children.map((child, idx) => /* @__PURE__ */ jsx(
607
648
  FieldRenderer,
@@ -618,10 +659,16 @@ function ObjectField({
618
659
  if (field.meta.section) {
619
660
  return /* @__PURE__ */ jsx(Fragment, { children: content });
620
661
  }
621
- return /* @__PURE__ */ jsxs("fieldset", { children: [
622
- field.label && /* @__PURE__ */ jsx("legend", { children: field.label }),
623
- content
624
- ] });
662
+ const ObjectWrapper = field.meta.wrapper ?? layout.objectWrapper;
663
+ return /* @__PURE__ */ jsx(
664
+ ObjectWrapper,
665
+ {
666
+ label: field.label,
667
+ className: classNames.objectFieldset,
668
+ labelClassName: classNames.objectLegend,
669
+ children: content
670
+ }
671
+ );
625
672
  }
626
673
 
627
674
  // src/components/fields/getDefaultValue.ts
@@ -700,124 +747,147 @@ function ArrayField({ field, control, effectiveName }) {
700
747
  ...itemConfig,
701
748
  meta: { ...itemConfig.meta, section: field.meta.section }
702
749
  } : itemConfig;
703
- const content = /* @__PURE__ */ jsxs(Fragment, { children: [
704
- rows.map((row, index) => {
705
- const isCollapsed = showCollapse && collapsed.has(index);
706
- const collapseButton = showCollapse ? /* @__PURE__ */ jsxs(
707
- "button",
708
- {
709
- type: "button",
710
- className: classNames.arrayCollapse,
711
- onClick: () => toggleCollapse(index),
712
- "aria-label": isCollapsed ? `Expand item ${index + 1}` : `Collapse item ${index + 1}`,
713
- children: [
714
- isCollapsed ? labels.arrayExpand ?? "\u25BC" : labels.arrayCollapse ?? "\u25B6",
715
- " ",
716
- /* @__PURE__ */ jsx(
717
- CollapseSummary,
718
- {
719
- control,
720
- effectiveName,
721
- index,
722
- itemConfig,
723
- isCollapsed
724
- }
725
- )
726
- ]
727
- }
728
- ) : null;
729
- const moveUpButton = showMove && rows.length > 1 ? /* @__PURE__ */ jsx(
730
- "button",
731
- {
732
- type: "button",
733
- className: classNames.arrayMove,
734
- onClick: () => move(index, index - 1),
735
- disabled: index === 0,
736
- "aria-label": `Move item ${index + 1} up`,
737
- children: labels.arrayMoveUp ?? "\u2191"
738
- }
739
- ) : null;
740
- const moveDownButton = showMove && rows.length > 1 ? /* @__PURE__ */ jsx(
741
- "button",
742
- {
743
- type: "button",
744
- className: classNames.arrayMove,
745
- onClick: () => move(index, index + 1),
746
- disabled: index === rows.length - 1,
747
- "aria-label": `Move item ${index + 1} down`,
748
- children: labels.arrayMoveDown ?? "\u2193"
749
- }
750
- ) : null;
751
- const duplicateButton = showDuplicate && !atMax ? /* @__PURE__ */ jsx(
752
- "button",
753
- {
754
- type: "button",
755
- className: classNames.arrayDuplicate,
756
- onClick: () => {
757
- const values = Object.fromEntries(
758
- Object.entries(row).filter(([k]) => k !== "id")
759
- );
760
- insert(index + 1, values);
761
- },
762
- "aria-label": `Duplicate item ${index + 1}`,
763
- children: labels.arrayDuplicate ?? "Duplicate"
764
- }
765
- ) : null;
766
- const removeButton = /* @__PURE__ */ jsx(
767
- "button",
768
- {
769
- type: "button",
770
- className: classNames.arrayRemove,
771
- onClick: () => remove(index),
772
- disabled: atMin,
773
- "aria-label": `Remove item ${index + 1}`,
774
- children: labels.arrayRemove ?? "Remove"
775
- }
776
- );
777
- const fieldContent = !isCollapsed ? /* @__PURE__ */ jsx(
778
- FieldRenderer,
779
- {
780
- field: effectiveItemConfig,
781
- control,
782
- namePrefix: `${effectiveName}.${index}`
783
- }
784
- ) : null;
785
- const RowLayout = layout.arrayRowLayout;
786
- return /* @__PURE__ */ jsx(
787
- RowLayout,
788
- {
789
- buttons: {
790
- moveUp: moveUpButton,
791
- moveDown: moveDownButton,
792
- duplicate: duplicateButton,
793
- remove: removeButton,
794
- collapse: collapseButton
795
- },
796
- index,
797
- rowCount: rows.length,
798
- children: fieldContent
750
+ const {
751
+ add: AddBtn,
752
+ remove: RemoveBtn,
753
+ moveUp: MoveUpBtn,
754
+ moveDown: MoveDownBtn,
755
+ duplicate: DuplicateBtn,
756
+ collapse: CollapseBtn
757
+ } = layout.arrayButtons;
758
+ const ArrayFieldLayout = layout.arrayFieldLayout;
759
+ const RowLayout = layout.arrayRowLayout;
760
+ const renderedRows = rows.map((row, index) => {
761
+ const isCollapsed = showCollapse && collapsed.has(index);
762
+ const collapseButton = showCollapse ? /* @__PURE__ */ jsxs(
763
+ CollapseBtn,
764
+ {
765
+ type: "button",
766
+ className: classNames.arrayCollapse,
767
+ onClick: () => toggleCollapse(index),
768
+ "aria-label": isCollapsed ? `Expand item ${index + 1}` : `Collapse item ${index + 1}`,
769
+ isCollapsed,
770
+ children: [
771
+ isCollapsed ? labels.arrayExpand ?? "\u25BC" : labels.arrayCollapse ?? "\u25B6",
772
+ " ",
773
+ /* @__PURE__ */ jsx(
774
+ CollapseSummary,
775
+ {
776
+ control,
777
+ effectiveName,
778
+ index,
779
+ itemConfig,
780
+ isCollapsed
781
+ }
782
+ )
783
+ ]
784
+ }
785
+ ) : null;
786
+ const moveUpButton = showMove && rows.length > 1 ? /* @__PURE__ */ jsx(
787
+ MoveUpBtn,
788
+ {
789
+ type: "button",
790
+ className: classNames.arrayMove,
791
+ onClick: () => move(index, index - 1),
792
+ disabled: index === 0,
793
+ "aria-label": `Move item ${index + 1} up`,
794
+ children: labels.arrayMoveUp ?? "\u2191"
795
+ }
796
+ ) : null;
797
+ const moveDownButton = showMove && rows.length > 1 ? /* @__PURE__ */ jsx(
798
+ MoveDownBtn,
799
+ {
800
+ type: "button",
801
+ className: classNames.arrayMove,
802
+ onClick: () => move(index, index + 1),
803
+ disabled: index === rows.length - 1,
804
+ "aria-label": `Move item ${index + 1} down`,
805
+ children: labels.arrayMoveDown ?? "\u2193"
806
+ }
807
+ ) : null;
808
+ const duplicateButton = showDuplicate && !atMax ? /* @__PURE__ */ jsx(
809
+ DuplicateBtn,
810
+ {
811
+ type: "button",
812
+ className: classNames.arrayDuplicate,
813
+ onClick: () => {
814
+ const values = Object.fromEntries(
815
+ Object.entries(row).filter(([k]) => k !== "id")
816
+ );
817
+ insert(index + 1, values);
799
818
  },
800
- row.id
801
- );
802
- }),
803
- /* @__PURE__ */ jsx(
804
- "button",
819
+ "aria-label": `Duplicate item ${index + 1}`,
820
+ children: labels.arrayDuplicate ?? "Duplicate"
821
+ }
822
+ ) : null;
823
+ const removeButton = /* @__PURE__ */ jsx(
824
+ RemoveBtn,
805
825
  {
806
826
  type: "button",
807
- className: classNames.arrayAdd,
808
- disabled: atMax,
809
- onClick: () => append(getDefaultValue(itemConfig)),
810
- children: labels.arrayAdd ?? "Add"
827
+ className: classNames.arrayRemove,
828
+ onClick: () => remove(index),
829
+ disabled: atMin,
830
+ "aria-label": `Remove item ${index + 1}`,
831
+ children: labels.arrayRemove ?? "Remove"
811
832
  }
812
- )
813
- ] });
833
+ );
834
+ const fieldContent = !isCollapsed ? /* @__PURE__ */ jsx(
835
+ FieldRenderer,
836
+ {
837
+ field: effectiveItemConfig,
838
+ control,
839
+ namePrefix: `${effectiveName}.${index}`
840
+ }
841
+ ) : null;
842
+ return /* @__PURE__ */ jsx(
843
+ RowLayout,
844
+ {
845
+ buttons: {
846
+ moveUp: moveUpButton,
847
+ moveDown: moveDownButton,
848
+ duplicate: duplicateButton,
849
+ remove: removeButton,
850
+ collapse: collapseButton
851
+ },
852
+ index,
853
+ rowCount: rows.length,
854
+ children: fieldContent
855
+ },
856
+ row.id
857
+ );
858
+ });
859
+ const addButton = /* @__PURE__ */ jsx(
860
+ AddBtn,
861
+ {
862
+ type: "button",
863
+ className: classNames.arrayAdd,
864
+ disabled: atMax,
865
+ onClick: () => append(getDefaultValue(itemConfig)),
866
+ children: labels.arrayAdd ?? "Add"
867
+ }
868
+ );
869
+ const content = /* @__PURE__ */ jsx(
870
+ ArrayFieldLayout,
871
+ {
872
+ rows: renderedRows,
873
+ addButton,
874
+ rowCount: rows.length,
875
+ canAdd: !atMax
876
+ }
877
+ );
814
878
  if (field.meta.section) {
815
879
  return content;
816
880
  }
817
- return /* @__PURE__ */ jsxs("fieldset", { children: [
818
- field.label && /* @__PURE__ */ jsx("legend", { children: field.label }),
819
- content
820
- ] });
881
+ const ArrayWrapper = field.meta.wrapper ?? layout.arrayWrapper;
882
+ return /* @__PURE__ */ jsx(
883
+ ArrayWrapper,
884
+ {
885
+ label: field.label,
886
+ className: classNames.arrayFieldset,
887
+ labelClassName: classNames.arrayLegend,
888
+ children: content
889
+ }
890
+ );
821
891
  }
822
892
  function CollapseSummary({
823
893
  control,
@@ -1384,11 +1454,7 @@ function AutoForm(props) {
1384
1454
  onSubmitRef
1385
1455
  ]
1386
1456
  );
1387
- React3.useImperativeHandle(
1388
- ref,
1389
- () => ({ ...formMethods, isSubmitting: formState.isSubmitting }),
1390
- [formMethods, formState.isSubmitting]
1391
- );
1457
+ React3.useImperativeHandle(ref, () => formMethods, [formMethods]);
1392
1458
  const setFieldMeta = React3.useCallback(
1393
1459
  (field, meta) => {
1394
1460
  if (Object.keys(meta).length) {
@@ -1429,22 +1495,39 @@ function AutoForm(props) {
1429
1495
  }, [onValuesChangeRef, allValues]);
1430
1496
  const visibleFields = useConditionalFields(fieldsWithDynamic, control);
1431
1497
  const sections = useSectionGrouping(visibleFields);
1432
- const resolvedLayout = React3.useMemo(
1433
- () => ({
1498
+ const resolvedLayout = React3.useMemo(() => {
1499
+ const base = layout?.arrayButtons?.base ?? DefaultArrayButton;
1500
+ const slots = layout?.arrayButtons;
1501
+ return {
1434
1502
  formWrapper: layout?.formWrapper ?? DefaultFormWrapper,
1435
1503
  sectionWrapper: layout?.sectionWrapper ?? DefaultSectionWrapper,
1436
1504
  submitButton: layout?.submitButton ?? DefaultSubmitButton,
1437
1505
  arrayRowLayout: layout?.arrayRowLayout ?? DefaultArrayRowLayout,
1506
+ arrayFieldLayout: layout?.arrayFieldLayout ?? DefaultArrayFieldLayout,
1507
+ objectWrapper: layout?.objectWrapper ?? DefaultObjectWrapper,
1508
+ arrayWrapper: layout?.arrayWrapper ?? DefaultArrayWrapper,
1509
+ arrayButtons: {
1510
+ base,
1511
+ add: slots?.add ?? base,
1512
+ remove: slots?.remove ?? base,
1513
+ moveUp: slots?.moveUp ?? base,
1514
+ moveDown: slots?.moveDown ?? base,
1515
+ duplicate: slots?.duplicate ?? base,
1516
+ collapse: slots?.collapse ?? DefaultArrayCollapseButton
1517
+ },
1438
1518
  loadingFallback: layout?.loadingFallback ?? /* @__PURE__ */ jsx("p", { children: "Loading\u2026" })
1439
- }),
1440
- [
1441
- layout?.formWrapper,
1442
- layout?.sectionWrapper,
1443
- layout?.submitButton,
1444
- layout?.arrayRowLayout,
1445
- layout?.loadingFallback
1446
- ]
1447
- );
1519
+ };
1520
+ }, [
1521
+ layout?.formWrapper,
1522
+ layout?.sectionWrapper,
1523
+ layout?.submitButton,
1524
+ layout?.arrayRowLayout,
1525
+ layout?.arrayFieldLayout,
1526
+ layout?.objectWrapper,
1527
+ layout?.arrayWrapper,
1528
+ layout?.arrayButtons,
1529
+ layout?.loadingFallback
1530
+ ]);
1448
1531
  const resolvedFieldWrapper = fieldWrapper ?? DefaultFieldWrapper;
1449
1532
  const FormWrapper = resolvedLayout.formWrapper;
1450
1533
  const SectionWrapper = resolvedLayout.sectionWrapper;
@@ -1622,6 +1705,6 @@ function createForm(schema) {
1622
1705
  return new UniForm(schema);
1623
1706
  }
1624
1707
 
1625
- export { AutoForm, DefaultCheckbox, DefaultFieldWrapper, DefaultInput, DefaultSelect, DefaultSubmitButton, FieldRenderer, UniForm, coerceValue, createAutoForm, createForm, defaultCoercionMap, defaultRegistry, introspectObjectSchema, introspectSchema, mergeRegistries, useAutoFormContext, useConditionalFields, useFormPersistence, useSectionGrouping };
1708
+ export { AutoForm, DefaultArrayButton, DefaultArrayCollapseButton, DefaultArrayFieldLayout, DefaultArrayRowLayout, DefaultArrayWrapper, DefaultCheckbox, DefaultFieldWrapper, DefaultInput, DefaultObjectWrapper, DefaultSelect, DefaultSubmitButton, FieldRenderer, UniForm, coerceValue, createAutoForm, createForm, defaultCoercionMap, defaultRegistry, introspectObjectSchema, introspectSchema, mergeRegistries, useAutoFormContext, useConditionalFields, useFormPersistence, useSectionGrouping };
1626
1709
  //# sourceMappingURL=index.mjs.map
1627
1710
  //# sourceMappingURL=index.mjs.map