@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/README.md +19 -16
- package/dist/{field-DPgaGkOL.d.mts → field-KKjnXn-d.d.mts} +47 -29
- package/dist/{field-DPgaGkOL.d.ts → field-KKjnXn-d.d.ts} +47 -29
- package/dist/index.d.mts +48 -7
- package/dist/index.d.ts +48 -7
- package/dist/index.js +87 -37
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +88 -39
- package/dist/index.mjs.map +1 -1
- package/dist/locales/en.d.mts +1 -1
- package/dist/locales/en.d.ts +1 -1
- package/dist/locales/es.d.mts +1 -1
- package/dist/locales/es.d.ts +1 -1
- package/dist/locales/he.d.mts +1 -1
- package/dist/locales/he.d.ts +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as z from 'zod/v4/core';
|
|
2
2
|
import * as React3 from 'react';
|
|
3
3
|
import { useMemo, useRef, useEffect, useCallback, useState } from 'react';
|
|
4
|
-
import {
|
|
4
|
+
import { useWatch, useFormState, useForm, useFieldArray, Controller } from 'react-hook-form';
|
|
5
5
|
import { zodResolver } from '@hookform/resolvers/zod';
|
|
6
6
|
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
|
|
7
7
|
|
|
@@ -635,6 +635,24 @@ function SelectField({
|
|
|
635
635
|
}
|
|
636
636
|
);
|
|
637
637
|
}
|
|
638
|
+
function useConditionalFields(fields, control, scopeName) {
|
|
639
|
+
const allValues = useWatch({ control });
|
|
640
|
+
const scopedValues = useWatch({ control, name: scopeName });
|
|
641
|
+
const values = scopeName ? scopedValues : allValues;
|
|
642
|
+
return useMemo(() => {
|
|
643
|
+
return fields.filter((field) => {
|
|
644
|
+
if (field.meta.hidden) return false;
|
|
645
|
+
if (typeof field.meta.condition === "function") {
|
|
646
|
+
return field.meta.condition(values);
|
|
647
|
+
}
|
|
648
|
+
return true;
|
|
649
|
+
}).sort((a, b) => {
|
|
650
|
+
const orderA = typeof a.meta.order === "number" ? a.meta.order : Infinity;
|
|
651
|
+
const orderB = typeof b.meta.order === "number" ? b.meta.order : Infinity;
|
|
652
|
+
return orderA - orderB;
|
|
653
|
+
});
|
|
654
|
+
}, [fields, values]);
|
|
655
|
+
}
|
|
638
656
|
function ObjectField({
|
|
639
657
|
field,
|
|
640
658
|
control,
|
|
@@ -643,7 +661,7 @@ function ObjectField({
|
|
|
643
661
|
shouldUnregister
|
|
644
662
|
}) {
|
|
645
663
|
const { classNames, layout } = useAutoFormContext();
|
|
646
|
-
const children = field.children;
|
|
664
|
+
const children = useConditionalFields(field.children, control, namePrefix);
|
|
647
665
|
const content = children.map((child, idx) => /* @__PURE__ */ jsx(
|
|
648
666
|
FieldRenderer,
|
|
649
667
|
{
|
|
@@ -759,7 +777,7 @@ function ArrayField({ field, control, effectiveName }) {
|
|
|
759
777
|
const RowLayout = layout.arrayRowLayout;
|
|
760
778
|
const renderedRows = rows.map((row, index) => {
|
|
761
779
|
const isCollapsed = showCollapse && collapsed.has(index);
|
|
762
|
-
const collapseButton = showCollapse ? /* @__PURE__ */ jsxs(
|
|
780
|
+
const collapseButton = showCollapse && CollapseBtn ? /* @__PURE__ */ jsxs(
|
|
763
781
|
CollapseBtn,
|
|
764
782
|
{
|
|
765
783
|
type: "button",
|
|
@@ -783,7 +801,7 @@ function ArrayField({ field, control, effectiveName }) {
|
|
|
783
801
|
]
|
|
784
802
|
}
|
|
785
803
|
) : null;
|
|
786
|
-
const moveUpButton = showMove && rows.length > 1 ? /* @__PURE__ */ jsx(
|
|
804
|
+
const moveUpButton = showMove && rows.length > 1 && MoveUpBtn ? /* @__PURE__ */ jsx(
|
|
787
805
|
MoveUpBtn,
|
|
788
806
|
{
|
|
789
807
|
type: "button",
|
|
@@ -794,7 +812,7 @@ function ArrayField({ field, control, effectiveName }) {
|
|
|
794
812
|
children: labels.arrayMoveUp ?? "\u2191"
|
|
795
813
|
}
|
|
796
814
|
) : null;
|
|
797
|
-
const moveDownButton = showMove && rows.length > 1 ? /* @__PURE__ */ jsx(
|
|
815
|
+
const moveDownButton = showMove && rows.length > 1 && MoveDownBtn ? /* @__PURE__ */ jsx(
|
|
798
816
|
MoveDownBtn,
|
|
799
817
|
{
|
|
800
818
|
type: "button",
|
|
@@ -805,7 +823,7 @@ function ArrayField({ field, control, effectiveName }) {
|
|
|
805
823
|
children: labels.arrayMoveDown ?? "\u2193"
|
|
806
824
|
}
|
|
807
825
|
) : null;
|
|
808
|
-
const duplicateButton = showDuplicate && !atMax ? /* @__PURE__ */ jsx(
|
|
826
|
+
const duplicateButton = showDuplicate && !atMax && DuplicateBtn ? /* @__PURE__ */ jsx(
|
|
809
827
|
DuplicateBtn,
|
|
810
828
|
{
|
|
811
829
|
type: "button",
|
|
@@ -820,7 +838,7 @@ function ArrayField({ field, control, effectiveName }) {
|
|
|
820
838
|
children: labels.arrayDuplicate ?? "Duplicate"
|
|
821
839
|
}
|
|
822
840
|
) : null;
|
|
823
|
-
const removeButton = /* @__PURE__ */ jsx(
|
|
841
|
+
const removeButton = RemoveBtn ? /* @__PURE__ */ jsx(
|
|
824
842
|
RemoveBtn,
|
|
825
843
|
{
|
|
826
844
|
type: "button",
|
|
@@ -830,7 +848,7 @@ function ArrayField({ field, control, effectiveName }) {
|
|
|
830
848
|
"aria-label": labels.arrayAriaRemove?.(index) ?? `Remove item ${index + 1}`,
|
|
831
849
|
children: labels.arrayRemove ?? "Remove"
|
|
832
850
|
}
|
|
833
|
-
);
|
|
851
|
+
) : null;
|
|
834
852
|
const fieldContent = !isCollapsed ? /* @__PURE__ */ jsx(
|
|
835
853
|
FieldRenderer,
|
|
836
854
|
{
|
|
@@ -856,7 +874,7 @@ function ArrayField({ field, control, effectiveName }) {
|
|
|
856
874
|
row.id
|
|
857
875
|
);
|
|
858
876
|
});
|
|
859
|
-
const addButton = /* @__PURE__ */ jsx(
|
|
877
|
+
const addButton = AddBtn ? /* @__PURE__ */ jsx(
|
|
860
878
|
AddBtn,
|
|
861
879
|
{
|
|
862
880
|
type: "button",
|
|
@@ -865,7 +883,7 @@ function ArrayField({ field, control, effectiveName }) {
|
|
|
865
883
|
onClick: () => append(getDefaultValue(itemConfig)),
|
|
866
884
|
children: labels.arrayAdd ?? "Add"
|
|
867
885
|
}
|
|
868
|
-
);
|
|
886
|
+
) : null;
|
|
869
887
|
const content = /* @__PURE__ */ jsx(
|
|
870
888
|
ArrayFieldLayout,
|
|
871
889
|
{
|
|
@@ -1027,22 +1045,6 @@ function FieldRenderer({
|
|
|
1027
1045
|
}
|
|
1028
1046
|
);
|
|
1029
1047
|
}
|
|
1030
|
-
function useConditionalFields(fields, control) {
|
|
1031
|
-
const values = useWatch({ control });
|
|
1032
|
-
return useMemo(() => {
|
|
1033
|
-
return fields.filter((field) => {
|
|
1034
|
-
if (field.meta.hidden) return false;
|
|
1035
|
-
if (typeof field.meta.condition === "function") {
|
|
1036
|
-
return field.meta.condition(values);
|
|
1037
|
-
}
|
|
1038
|
-
return true;
|
|
1039
|
-
}).sort((a, b) => {
|
|
1040
|
-
const orderA = typeof a.meta.order === "number" ? a.meta.order : Infinity;
|
|
1041
|
-
const orderB = typeof b.meta.order === "number" ? b.meta.order : Infinity;
|
|
1042
|
-
return orderA - orderB;
|
|
1043
|
-
});
|
|
1044
|
-
}, [fields, values]);
|
|
1045
|
-
}
|
|
1046
1048
|
function useSectionGrouping(fields) {
|
|
1047
1049
|
return useMemo(() => {
|
|
1048
1050
|
const ungrouped = [];
|
|
@@ -1281,6 +1283,12 @@ function buildDefaults(fields) {
|
|
|
1281
1283
|
}
|
|
1282
1284
|
return result;
|
|
1283
1285
|
}
|
|
1286
|
+
|
|
1287
|
+
// src/utils/resolveNullableSlot.ts
|
|
1288
|
+
function resolveNullableSlot(slot, fallback) {
|
|
1289
|
+
if (slot === null) return null;
|
|
1290
|
+
return slot ?? fallback;
|
|
1291
|
+
}
|
|
1284
1292
|
function AutoForm(props) {
|
|
1285
1293
|
const {
|
|
1286
1294
|
form: uniForm,
|
|
@@ -1505,24 +1513,33 @@ function AutoForm(props) {
|
|
|
1505
1513
|
const visibleFields = useConditionalFields(fieldsWithDynamic, control);
|
|
1506
1514
|
const sections = useSectionGrouping(visibleFields);
|
|
1507
1515
|
const resolvedLayout = React3.useMemo(() => {
|
|
1508
|
-
const base =
|
|
1516
|
+
const base = resolveNullableSlot(
|
|
1517
|
+
layout?.arrayButtons?.base,
|
|
1518
|
+
DefaultArrayButton
|
|
1519
|
+
);
|
|
1509
1520
|
const slots = layout?.arrayButtons;
|
|
1510
1521
|
return {
|
|
1511
1522
|
formWrapper: layout?.formWrapper ?? DefaultFormWrapper,
|
|
1512
1523
|
sectionWrapper: layout?.sectionWrapper ?? DefaultSectionWrapper,
|
|
1513
|
-
submitButton:
|
|
1524
|
+
submitButton: resolveNullableSlot(
|
|
1525
|
+
layout?.submitButton,
|
|
1526
|
+
DefaultSubmitButton
|
|
1527
|
+
),
|
|
1514
1528
|
arrayRowLayout: layout?.arrayRowLayout ?? DefaultArrayRowLayout,
|
|
1515
1529
|
arrayFieldLayout: layout?.arrayFieldLayout ?? DefaultArrayFieldLayout,
|
|
1516
1530
|
objectWrapper: layout?.objectWrapper ?? DefaultObjectWrapper,
|
|
1517
1531
|
arrayWrapper: layout?.arrayWrapper ?? DefaultArrayWrapper,
|
|
1518
1532
|
arrayButtons: {
|
|
1519
1533
|
base,
|
|
1520
|
-
add: slots?.add
|
|
1521
|
-
remove: slots?.remove
|
|
1522
|
-
moveUp: slots?.moveUp
|
|
1523
|
-
moveDown: slots?.moveDown
|
|
1524
|
-
duplicate: slots?.duplicate
|
|
1525
|
-
collapse:
|
|
1534
|
+
add: resolveNullableSlot(slots?.add, base),
|
|
1535
|
+
remove: resolveNullableSlot(slots?.remove, base),
|
|
1536
|
+
moveUp: resolveNullableSlot(slots?.moveUp, base),
|
|
1537
|
+
moveDown: resolveNullableSlot(slots?.moveDown, base),
|
|
1538
|
+
duplicate: resolveNullableSlot(slots?.duplicate, base),
|
|
1539
|
+
collapse: resolveNullableSlot(
|
|
1540
|
+
slots?.collapse,
|
|
1541
|
+
DefaultArrayCollapseButton
|
|
1542
|
+
)
|
|
1526
1543
|
},
|
|
1527
1544
|
loadingFallback: layout?.loadingFallback ?? /* @__PURE__ */ jsx("p", { children: "Loading\u2026" })
|
|
1528
1545
|
};
|
|
@@ -1544,6 +1561,7 @@ function AutoForm(props) {
|
|
|
1544
1561
|
const contextValue = React3.useMemo(
|
|
1545
1562
|
() => ({
|
|
1546
1563
|
registry,
|
|
1564
|
+
fieldConfigs: mergedFields,
|
|
1547
1565
|
fieldOverrides: fieldOverridesProp,
|
|
1548
1566
|
fieldWrapper: resolvedFieldWrapper,
|
|
1549
1567
|
layout: resolvedLayout,
|
|
@@ -1552,10 +1570,12 @@ function AutoForm(props) {
|
|
|
1552
1570
|
coercions,
|
|
1553
1571
|
messages,
|
|
1554
1572
|
labels,
|
|
1555
|
-
formMethods
|
|
1573
|
+
formMethods,
|
|
1574
|
+
control
|
|
1556
1575
|
}),
|
|
1557
1576
|
[
|
|
1558
1577
|
registry,
|
|
1578
|
+
mergedFields,
|
|
1559
1579
|
fieldOverridesProp,
|
|
1560
1580
|
resolvedFieldWrapper,
|
|
1561
1581
|
resolvedLayout,
|
|
@@ -1564,7 +1584,8 @@ function AutoForm(props) {
|
|
|
1564
1584
|
coercions,
|
|
1565
1585
|
messages,
|
|
1566
1586
|
labels,
|
|
1567
|
-
formMethods
|
|
1587
|
+
formMethods,
|
|
1588
|
+
control
|
|
1568
1589
|
]
|
|
1569
1590
|
);
|
|
1570
1591
|
if (isLoadingDefaults) {
|
|
@@ -1608,13 +1629,13 @@ function AutoForm(props) {
|
|
|
1608
1629
|
section.title
|
|
1609
1630
|
);
|
|
1610
1631
|
}),
|
|
1611
|
-
/* @__PURE__ */ jsx(
|
|
1632
|
+
SubmitButton ? /* @__PURE__ */ jsx(
|
|
1612
1633
|
SubmitButton,
|
|
1613
1634
|
{
|
|
1614
1635
|
isSubmitting: formState.isSubmitting,
|
|
1615
1636
|
label: labels.submit ?? "Submit"
|
|
1616
1637
|
}
|
|
1617
|
-
)
|
|
1638
|
+
) : null
|
|
1618
1639
|
] })
|
|
1619
1640
|
}
|
|
1620
1641
|
) });
|
|
@@ -1713,7 +1734,35 @@ var UniForm = class {
|
|
|
1713
1734
|
function createForm(schema) {
|
|
1714
1735
|
return new UniForm(schema);
|
|
1715
1736
|
}
|
|
1737
|
+
function findArrayConfig(fields, name) {
|
|
1738
|
+
for (const field of fields) {
|
|
1739
|
+
if (field.name === name) {
|
|
1740
|
+
return field.type === "array" ? field : void 0;
|
|
1741
|
+
}
|
|
1742
|
+
if (field.type === "object") {
|
|
1743
|
+
const found = findArrayConfig(field.children, name);
|
|
1744
|
+
if (found) return found;
|
|
1745
|
+
}
|
|
1746
|
+
}
|
|
1747
|
+
return void 0;
|
|
1748
|
+
}
|
|
1749
|
+
function useArrayField(fieldName) {
|
|
1750
|
+
const { control, fieldConfigs } = useAutoFormContext();
|
|
1751
|
+
const result = useFieldArray({ control, name: fieldName });
|
|
1752
|
+
const rowCount = result.fields.length;
|
|
1753
|
+
const config = findArrayConfig(fieldConfigs, fieldName);
|
|
1754
|
+
const minItems = config?.minItems;
|
|
1755
|
+
const maxItems = config?.maxItems;
|
|
1756
|
+
const canAdd = maxItems == null || rowCount < maxItems;
|
|
1757
|
+
const atMin = minItems != null && rowCount <= minItems;
|
|
1758
|
+
return {
|
|
1759
|
+
...result,
|
|
1760
|
+
rowCount,
|
|
1761
|
+
canAdd,
|
|
1762
|
+
atMin
|
|
1763
|
+
};
|
|
1764
|
+
}
|
|
1716
1765
|
|
|
1717
|
-
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 };
|
|
1766
|
+
export { AutoForm, DefaultArrayButton, DefaultArrayCollapseButton, DefaultArrayFieldLayout, DefaultArrayRowLayout, DefaultArrayWrapper, DefaultCheckbox, DefaultFieldWrapper, DefaultInput, DefaultObjectWrapper, DefaultSelect, DefaultSubmitButton, FieldRenderer, UniForm, coerceValue, createAutoForm, createForm, defaultCoercionMap, defaultRegistry, introspectObjectSchema, introspectSchema, mergeRegistries, useArrayField, useAutoFormContext, useConditionalFields, useFormPersistence, useSectionGrouping };
|
|
1718
1767
|
//# sourceMappingURL=index.mjs.map
|
|
1719
1768
|
//# sourceMappingURL=index.mjs.map
|