react-f0rm 0.10.0 → 0.11.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/README.md +52 -1
- package/dist/devtools/index.cjs.js.map +1 -1
- package/dist/devtools/index.mjs.map +1 -1
- package/dist/form-BQsb2CuG.mjs.map +1 -1
- package/dist/{form-BNqHYba-.d.ts → form-DSwruZIg.d.ts} +5 -1
- package/dist/form-DZK8HgZ7.cjs.js.map +1 -1
- package/dist/index.cjs.js +1 -1
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +65 -6
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +83 -1
- package/dist/index.umd.js.map +1 -1
- package/dist/index.umd.min.js +2 -2
- package/dist/index.umd.min.js.map +1 -1
- package/dist/resolvers/yup.d.ts +1 -1
- package/dist/resolvers/zod.d.ts +1 -1
- package/dist/{validate-CbteZ-Jb.d.ts → validate-CqRu0f-o.d.ts} +1 -1
- package/package.json +1 -1
package/dist/index.umd.js
CHANGED
|
@@ -1557,6 +1557,21 @@
|
|
|
1557
1557
|
function generateId() {
|
|
1558
1558
|
return `_${++idCounter}`;
|
|
1559
1559
|
}
|
|
1560
|
+
const arrayIdsRegistry = /* @__PURE__ */ new WeakMap();
|
|
1561
|
+
function getArrayIds(form, key) {
|
|
1562
|
+
return arrayIdsRegistry.get(form)?.get(key);
|
|
1563
|
+
}
|
|
1564
|
+
function registerArrayIds(form, key, ids) {
|
|
1565
|
+
let registry = arrayIdsRegistry.get(form);
|
|
1566
|
+
if (!registry) {
|
|
1567
|
+
registry = /* @__PURE__ */ new Map();
|
|
1568
|
+
arrayIdsRegistry.set(form, registry);
|
|
1569
|
+
}
|
|
1570
|
+
registry.set(key, ids);
|
|
1571
|
+
}
|
|
1572
|
+
function bumpReducer(count) {
|
|
1573
|
+
return count + 1;
|
|
1574
|
+
}
|
|
1560
1575
|
function useFieldArrayCore(options, Context) {
|
|
1561
1576
|
const contextForm = React.useContext(Context);
|
|
1562
1577
|
const form = options.form || contextForm;
|
|
@@ -1592,6 +1607,14 @@
|
|
|
1592
1607
|
() => onPathEvent(form.emitter, "change", path, "branch", syncFields),
|
|
1593
1608
|
[form.emitter, path]
|
|
1594
1609
|
);
|
|
1610
|
+
registerArrayIds(form, path.key, idsRef.current);
|
|
1611
|
+
React.useEffect(() => {
|
|
1612
|
+
registerArrayIds(form, path.key, idsRef.current);
|
|
1613
|
+
return () => {
|
|
1614
|
+
const registry = arrayIdsRegistry.get(form);
|
|
1615
|
+
if (registry?.get(path.key) === idsRef.current) registry.delete(path.key);
|
|
1616
|
+
};
|
|
1617
|
+
}, [form, path.key]);
|
|
1595
1618
|
const append = useStageFn((value) => {
|
|
1596
1619
|
const arr = getArray();
|
|
1597
1620
|
idsRef.current.push(generateId());
|
|
@@ -1649,6 +1672,60 @@
|
|
|
1649
1672
|
function useFieldArray(options) {
|
|
1650
1673
|
return useFieldArrayCore(options, FormContext);
|
|
1651
1674
|
}
|
|
1675
|
+
function useFieldArrayItemCore(options, Context) {
|
|
1676
|
+
const contextForm = React.useContext(Context);
|
|
1677
|
+
const form = options.form || contextForm;
|
|
1678
|
+
if (!form) throw new Error("no form provided");
|
|
1679
|
+
const { id } = options;
|
|
1680
|
+
const arrayPath = usePath(options.name);
|
|
1681
|
+
const computeIndex = React.useCallback(
|
|
1682
|
+
() => getArrayIds(form, arrayPath.key)?.indexOf(id) ?? -1,
|
|
1683
|
+
[form, arrayPath.key, id]
|
|
1684
|
+
);
|
|
1685
|
+
const computeValue = React.useCallback(
|
|
1686
|
+
(index2) => {
|
|
1687
|
+
const arr = getValueByPath(form, arrayPath);
|
|
1688
|
+
return Array.isArray(arr) ? arr[index2] : void 0;
|
|
1689
|
+
},
|
|
1690
|
+
[form, arrayPath]
|
|
1691
|
+
);
|
|
1692
|
+
const index = computeIndex();
|
|
1693
|
+
const value = computeValue(index);
|
|
1694
|
+
const lastRef = React.useRef({ index, value });
|
|
1695
|
+
lastRef.current = { index, value };
|
|
1696
|
+
const computeRef = useStage({ computeIndex, computeValue });
|
|
1697
|
+
const [, bump] = React.useReducer(bumpReducer, 0);
|
|
1698
|
+
React.useEffect(
|
|
1699
|
+
() => onPathEvent(form.emitter, "change", arrayPath, "leaf", () => {
|
|
1700
|
+
const { computeIndex: idx, computeValue: val } = computeRef.current;
|
|
1701
|
+
const nextIndex = idx();
|
|
1702
|
+
if (nextIndex !== lastRef.current.index || !Object.is(val(nextIndex), lastRef.current.value))
|
|
1703
|
+
bump();
|
|
1704
|
+
}),
|
|
1705
|
+
[form.emitter, arrayPath, computeRef]
|
|
1706
|
+
);
|
|
1707
|
+
const itemPath = usePath([...arrayPath.value, index]);
|
|
1708
|
+
const errors = useFieldErrorsByPath(form, itemPath);
|
|
1709
|
+
const setValue = useStageFn((v) => {
|
|
1710
|
+
if (index < 0) return;
|
|
1711
|
+
const arr = [...getValueByPath(form, arrayPath) ?? []];
|
|
1712
|
+
if (index >= arr.length) return;
|
|
1713
|
+
arr[index] = v;
|
|
1714
|
+
setValueByPath(form, arrayPath, arr);
|
|
1715
|
+
});
|
|
1716
|
+
return {
|
|
1717
|
+
value,
|
|
1718
|
+
setValue,
|
|
1719
|
+
errors,
|
|
1720
|
+
error: errors[0]?.message,
|
|
1721
|
+
name: itemPath.key,
|
|
1722
|
+
index,
|
|
1723
|
+
form
|
|
1724
|
+
};
|
|
1725
|
+
}
|
|
1726
|
+
function useFieldArrayItem(options) {
|
|
1727
|
+
return useFieldArrayItemCore(options, FormContext);
|
|
1728
|
+
}
|
|
1652
1729
|
|
|
1653
1730
|
const FormContext = React.createContext(null);
|
|
1654
1731
|
const FormProvider = FormContext.Provider;
|
|
@@ -1676,12 +1753,16 @@
|
|
|
1676
1753
|
function useFieldArray(options) {
|
|
1677
1754
|
return useFieldArrayCore(options, Context);
|
|
1678
1755
|
}
|
|
1756
|
+
function useFieldArrayItem(options) {
|
|
1757
|
+
return useFieldArrayItemCore(options, Context);
|
|
1758
|
+
}
|
|
1679
1759
|
return {
|
|
1680
1760
|
context: Context,
|
|
1681
1761
|
FormProvider: FormProvider2,
|
|
1682
1762
|
useFormContext: useFormContext2,
|
|
1683
1763
|
useField,
|
|
1684
|
-
useFieldArray
|
|
1764
|
+
useFieldArray,
|
|
1765
|
+
useFieldArrayItem
|
|
1685
1766
|
};
|
|
1686
1767
|
}
|
|
1687
1768
|
const CheckboxGroupContext = React.createContext(null);
|
|
@@ -2012,6 +2093,7 @@
|
|
|
2012
2093
|
exports.useErrorByPath = useErrorByPath;
|
|
2013
2094
|
exports.useField = useField;
|
|
2014
2095
|
exports.useFieldArray = useFieldArray;
|
|
2096
|
+
exports.useFieldArrayItem = useFieldArrayItem;
|
|
2015
2097
|
exports.useFieldErrors = useFieldErrors;
|
|
2016
2098
|
exports.useFieldErrorsByPath = useFieldErrorsByPath;
|
|
2017
2099
|
exports.useForm = useForm;
|