From 62c59d711e2e03c50a500e9c2be6c8a14f1f2d47 Mon Sep 17 00:00:00 2001
From: ChenJiYuan <447334358@qq.com>
Date: Mon, 11 Mar 2024 07:39:22 +0000
Subject: [PATCH] feat: support scoped form instance
- add hook Form.useFormInstance
- Form.useWatch add param "scoped: true"
---
docs/demo/scopedForm.md | 3 +
docs/examples/scopedForm.tsx | 162 ++++++++++++
src/FieldContext.ts | 2 +
src/Form.tsx | 9 +-
src/FormInstanceContext.tsx | 6 +
src/index.tsx | 7 +-
src/interface.ts | 8 +
src/useForm.ts | 2 +
src/useFormInstance.ts | 270 ++++++++++++++++++++
src/useWatch.ts | 19 +-
tests/scopedForm.test.tsx | 477 +++++++++++++++++++++++++++++++++++
11 files changed, 954 insertions(+), 11 deletions(-)
create mode 100644 docs/demo/scopedForm.md
create mode 100644 docs/examples/scopedForm.tsx
create mode 100644 src/FormInstanceContext.tsx
create mode 100644 src/useFormInstance.ts
create mode 100644 tests/scopedForm.test.tsx
diff --git a/docs/demo/scopedForm.md b/docs/demo/scopedForm.md
new file mode 100644
index 00000000..f3db51fc
--- /dev/null
+++ b/docs/demo/scopedForm.md
@@ -0,0 +1,3 @@
+## scopedForm
+
+
diff --git a/docs/examples/scopedForm.tsx b/docs/examples/scopedForm.tsx
new file mode 100644
index 00000000..4a72d8a2
--- /dev/null
+++ b/docs/examples/scopedForm.tsx
@@ -0,0 +1,162 @@
+import React from 'react';
+import Form from 'rc-field-form';
+import Input from './components/Input';
+import { isEqual } from 'lodash';
+
+const ChildrenContent = (props: { name: number }) => {
+
+ const { name } = props;
+
+ const scopedForm = Form.useFormInstance({ scoped: true });
+ const college = Form.useWatch([name, 'college'], scopedForm);
+ const location = Form.useWatch([name, 'location'], { scoped: true });
+ const [, forceUpdate] = React.useState({});
+
+ React.useEffect(() => {
+ scopedForm.setFieldValue([name, 'nonexistent'], 'nonexistent');
+ }, [scopedForm, name]);
+
+ return (
+