From 9e5ed948c5c130f775c8bb6970c043cb418f8669 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A0=97=E5=98=89=E7=94=B7?= <574980606@qq.com> Date: Mon, 13 Jan 2025 08:50:58 +0800 Subject: [PATCH 1/7] refactor: rc-component/form --- package.json | 6 +++--- src/Field.tsx | 15 +++++---------- src/FieldContext.ts | 2 +- src/List.tsx | 2 +- src/useForm.ts | 4 ++-- src/useWatch.ts | 2 +- src/utils/validateUtil.ts | 4 ++-- src/utils/valueUtil.ts | 4 ++-- tests/index.test.tsx | 2 +- tests/initialValue.test.tsx | 2 +- tests/list.test.tsx | 2 +- 11 files changed, 20 insertions(+), 25 deletions(-) diff --git a/package.json b/package.json index cfdc08de7..b1d405b8b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "rc-field-form", - "version": "2.7.0", + "name": "@rc-component/form", + "version": "1.0.0", "description": "React Form Component", "typings": "es/index.d.ts", "engines": { @@ -51,7 +51,7 @@ "dependencies": { "@babel/runtime": "^7.18.0", "@rc-component/async-validator": "^5.0.3", - "rc-util": "^5.32.2" + "@rc-component/util": "^1.1.0" }, "devDependencies": { "@rc-component/father-plugin": "^1.0.0", diff --git a/src/Field.tsx b/src/Field.tsx index afdf55d3e..73883031d 100644 --- a/src/Field.tsx +++ b/src/Field.tsx @@ -1,6 +1,6 @@ -import toChildrenArray from 'rc-util/lib/Children/toArray'; -import isEqual from 'rc-util/lib/isEqual'; -import warning from 'rc-util/lib/warning'; +import toChildrenArray from '@rc-component/util/lib/Children/toArray'; +import isEqual from '@rc-component/util/lib/isEqual'; +import warning from '@rc-component/util/lib/warning'; import * as React from 'react'; import FieldContext, { HOOK_MARK } from './FieldContext'; import type { @@ -112,11 +112,6 @@ export interface FieldState { class Field extends React.Component implements FieldEntity { public static contextType = FieldContext; - public static defaultProps = { - trigger: 'onChange', - valuePropName: 'value', - }; - public state = { resetCount: 0, }; @@ -566,11 +561,11 @@ class Field extends React.Component implements F public getControlled = (childProps: ChildProps = {}) => { const { name, - trigger, + trigger = 'onChange', validateTrigger, getValueFromEvent, normalize, - valuePropName, + valuePropName = 'value', getValueProps, fieldContext, } = this.props; diff --git a/src/FieldContext.ts b/src/FieldContext.ts index dbe08038c..d2b6f4d5d 100644 --- a/src/FieldContext.ts +++ b/src/FieldContext.ts @@ -1,4 +1,4 @@ -import warning from 'rc-util/lib/warning'; +import warning from '@rc-component/util/lib/warning'; import * as React from 'react'; import type { InternalFormInstance } from './interface'; diff --git a/src/List.tsx b/src/List.tsx index 99548bfac..f2f1a29c3 100644 --- a/src/List.tsx +++ b/src/List.tsx @@ -1,5 +1,5 @@ import * as React from 'react'; -import warning from 'rc-util/lib/warning'; +import warning from '@rc-component/util/lib/warning'; import type { InternalNamePath, NamePath, StoreValue, ValidatorRule, Meta } from './interface'; import FieldContext from './FieldContext'; import Field from './Field'; diff --git a/src/useForm.ts b/src/useForm.ts index 7879cf85f..bb89d43b6 100644 --- a/src/useForm.ts +++ b/src/useForm.ts @@ -1,5 +1,5 @@ -import { merge } from 'rc-util/lib/utils/set'; -import warning from 'rc-util/lib/warning'; +import { merge } from '@rc-component/util/lib/utils/set'; +import warning from '@rc-component/util/lib/warning'; import * as React from 'react'; import { HOOK_MARK } from './FieldContext'; import type { diff --git a/src/useWatch.ts b/src/useWatch.ts index 127c04d5a..08e8c5344 100644 --- a/src/useWatch.ts +++ b/src/useWatch.ts @@ -1,4 +1,4 @@ -import warning from 'rc-util/lib/warning'; +import warning from '@rc-component/util/lib/warning'; import { useContext, useEffect, useMemo, useRef, useState } from 'react'; import FieldContext, { HOOK_MARK } from './FieldContext'; import type { diff --git a/src/utils/validateUtil.ts b/src/utils/validateUtil.ts index 09106d1d2..3522f4cea 100644 --- a/src/utils/validateUtil.ts +++ b/src/utils/validateUtil.ts @@ -1,6 +1,6 @@ import RawAsyncValidator from '@rc-component/async-validator'; import * as React from 'react'; -import warning from 'rc-util/lib/warning'; +import warning from '@rc-component/util/lib/warning'; import type { InternalNamePath, InternalValidateOptions, @@ -9,7 +9,7 @@ import type { RuleError, } from '../interface'; import { defaultValidateMessages } from './messages'; -import { merge } from 'rc-util/lib/utils/set'; +import { merge } from '@rc-component/util/lib/utils/set'; // Remove incorrect original ts define const AsyncValidator: any = RawAsyncValidator; diff --git a/src/utils/valueUtil.ts b/src/utils/valueUtil.ts index eb64966aa..7aeb00953 100644 --- a/src/utils/valueUtil.ts +++ b/src/utils/valueUtil.ts @@ -1,5 +1,5 @@ -import getValue from 'rc-util/lib/utils/get'; -import setValue from 'rc-util/lib/utils/set'; +import getValue from '@rc-component/util/lib/utils/get'; +import setValue from '@rc-component/util/lib/utils/set'; import type { InternalNamePath, NamePath, Store, EventArgs } from '../interface'; import { toArray } from './typeUtil'; diff --git a/tests/index.test.tsx b/tests/index.test.tsx index a9a0e740c..8cab35d8e 100644 --- a/tests/index.test.tsx +++ b/tests/index.test.tsx @@ -1,5 +1,5 @@ import { act, fireEvent, render } from '@testing-library/react'; -import { resetWarned } from 'rc-util/lib/warning'; +import { resetWarned } from '@rc-component/util/lib/warning'; import React from 'react'; import type { FormInstance } from '../src'; import Form, { Field, useForm } from '../src'; diff --git a/tests/initialValue.test.tsx b/tests/initialValue.test.tsx index cfb736c69..bc5f0a105 100644 --- a/tests/initialValue.test.tsx +++ b/tests/initialValue.test.tsx @@ -1,5 +1,5 @@ import { act, fireEvent, render } from '@testing-library/react'; -import { resetWarned } from 'rc-util/lib/warning'; +import { resetWarned } from '@rc-component/util/lib/warning'; import React, { useState } from 'react'; import Form, { Field, List, useForm, type FormInstance } from '../src'; import { changeValue, getInput } from './common'; diff --git a/tests/list.test.tsx b/tests/list.test.tsx index 170164e13..598eb5f54 100644 --- a/tests/list.test.tsx +++ b/tests/list.test.tsx @@ -1,6 +1,6 @@ import React from 'react'; import { fireEvent, render, act } from '@testing-library/react'; -import { resetWarned } from 'rc-util/lib/warning'; +import { resetWarned } from '@rc-component/util/lib/warning'; import Form, { Field, List } from '../src'; import type { FormProps } from '../src'; import type { ListField, ListOperations, ListProps } from '../src/List'; From e4e73264f3f1b1e3e3fa07a2e25b12cba103d717 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A0=97=E5=98=89=E7=94=B7?= <574980606@qq.com> Date: Mon, 13 Jan 2025 09:03:51 +0800 Subject: [PATCH 2/7] fix: fix --- package.json | 4 ++-- src/Field.tsx | 5 +++-- src/Form.tsx | 2 +- src/useForm.ts | 2 +- tests/useWatch.test.tsx | 12 ++++++++---- 5 files changed, 15 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index b1d405b8b..4abb6bf12 100644 --- a/package.json +++ b/package.json @@ -60,8 +60,8 @@ "@types/jest": "^29.2.5", "@types/lodash": "^4.14.135", "@types/node": "^22.0.2", - "@types/react": "^18.0.0", - "@types/react-dom": "^18.0.0", + "@types/react": "^19.0.6", + "@types/react-dom": "^19.0.1", "@umijs/fabric": "^4.0.1", "dumi": "^2.0.0", "eslint": "^8.54.0", diff --git a/src/Field.tsx b/src/Field.tsx index 73883031d..69e680ce8 100644 --- a/src/Field.tsx +++ b/src/Field.tsx @@ -543,9 +543,10 @@ class Field extends React.Component implements F } // Filed element only - const childList = toChildrenArray(children); + const childList = toChildrenArray(children as any); + if (childList.length !== 1 || !React.isValidElement(childList[0])) { - return { child: childList, isFunction: false }; + return { child: childList as React.ReactNode, isFunction: false }; } return { child: childList[0], isFunction: false }; diff --git a/src/Form.tsx b/src/Form.tsx index 24972de5c..8c19210a5 100644 --- a/src/Form.tsx +++ b/src/Form.tsx @@ -138,7 +138,7 @@ const Form: React.ForwardRefRenderFunction = ( useSubscribe(!childrenRenderProps); // Listen if fields provided. We use ref to save prev data here to avoid additional render - const prevFieldsRef = React.useRef(); + const prevFieldsRef = React.useRef(null); React.useEffect(() => { if (!isSimilar(prevFieldsRef.current || [], fields || [])) { formInstance.setFields(fields || []); diff --git a/src/useForm.ts b/src/useForm.ts index bb89d43b6..fe8c80697 100644 --- a/src/useForm.ts +++ b/src/useForm.ts @@ -1027,7 +1027,7 @@ export class FormStore { } function useForm(form?: FormInstance): [FormInstance] { - const formRef = React.useRef(); + const formRef = React.useRef(null); const [, forceUpdate] = React.useState({}); if (!formRef.current) { diff --git a/tests/useWatch.test.tsx b/tests/useWatch.test.tsx index 0c08ab190..a52005dbd 100644 --- a/tests/useWatch.test.tsx +++ b/tests/useWatch.test.tsx @@ -288,9 +288,13 @@ describe('useWatch', () => { const more = Form.useWatch(['age', 'name', 'gender'], form); const demo = Form.useWatch(['demo']); - const values2 = Form.useWatch(values => ({ newName: values.name, newAge: values.age }), form); - const values3 = Form.useWatch(values => ({ - newName: values.name, + const values2 = Form.useWatch( + _values => ({ newName: _values.name, newAge: _values.age }), + form, + ); + + const values3 = Form.useWatch(_values => ({ + newName: _values.name, })); return ( @@ -393,7 +397,7 @@ describe('useWatch', () => { it('first undefined', () => { const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); const Demo: React.FC = () => { - const formRef = useRef(); + const formRef = useRef(null); const name = Form.useWatch('name', formRef.current); const [, setUpdate] = useState({}); return ( From 1a123022b8837b7b4d2039e1513dda3044f90be2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A0=97=E5=98=89=E7=94=B7?= <574980606@qq.com> Date: Mon, 13 Jan 2025 09:06:55 +0800 Subject: [PATCH 3/7] fix: fix --- src/Form.tsx | 2 +- src/List.tsx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Form.tsx b/src/Form.tsx index 8c19210a5..200282a78 100644 --- a/src/Form.tsx +++ b/src/Form.tsx @@ -17,7 +17,7 @@ import ListContext from './ListContext'; type BaseFormProps = Omit, 'onSubmit' | 'children'>; -type RenderProps = (values: Store, form: FormInstance) => JSX.Element | React.ReactNode; +type RenderProps = (values: Store, form: FormInstance) => React.JSX.Element | React.ReactNode; export interface FormProps extends BaseFormProps { initialValues?: Store; diff --git a/src/List.tsx b/src/List.tsx index f2f1a29c3..9a0e6ce6b 100644 --- a/src/List.tsx +++ b/src/List.tsx @@ -28,7 +28,7 @@ export interface ListProps { fields: ListField[], operations: ListOperations, meta: Meta, - ) => JSX.Element | React.ReactNode; + ) => React.JSX.Element | React.ReactNode; /** @private Passed by Form.List props. Do not use since it will break by path check. */ isListField?: boolean; From a751221916bd693a0b7c54f861320a97fdd53e37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A0=97=E5=98=89=E7=94=B7?= <574980606@qq.com> Date: Mon, 13 Jan 2025 09:08:26 +0800 Subject: [PATCH 4/7] fix: fix --- src/Form.tsx | 2 +- src/List.tsx | 6 +----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/Form.tsx b/src/Form.tsx index 200282a78..a883397c6 100644 --- a/src/Form.tsx +++ b/src/Form.tsx @@ -17,7 +17,7 @@ import ListContext from './ListContext'; type BaseFormProps = Omit, 'onSubmit' | 'children'>; -type RenderProps = (values: Store, form: FormInstance) => React.JSX.Element | React.ReactNode; +type RenderProps = (values: Store, form: FormInstance) => React.ReactNode; export interface FormProps extends BaseFormProps { initialValues?: Store; diff --git a/src/List.tsx b/src/List.tsx index 9a0e6ce6b..7bb22d012 100644 --- a/src/List.tsx +++ b/src/List.tsx @@ -24,11 +24,7 @@ export interface ListProps { rules?: ValidatorRule[]; validateTrigger?: string | string[] | false; initialValue?: any[]; - children?: ( - fields: ListField[], - operations: ListOperations, - meta: Meta, - ) => React.JSX.Element | React.ReactNode; + children?: (fields: ListField[], operations: ListOperations, meta: Meta) => React.ReactNode; /** @private Passed by Form.List props. Do not use since it will break by path check. */ isListField?: boolean; From e1815c40126e0d6228cc8d38f44658cb4eb060b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A0=97=E5=98=89=E7=94=B7?= <574980606@qq.com> Date: Mon, 13 Jan 2025 09:12:34 +0800 Subject: [PATCH 5/7] fix: fix --- tests/useWatch.test.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/useWatch.test.tsx b/tests/useWatch.test.tsx index a52005dbd..86be6cdb1 100644 --- a/tests/useWatch.test.tsx +++ b/tests/useWatch.test.tsx @@ -397,7 +397,7 @@ describe('useWatch', () => { it('first undefined', () => { const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); const Demo: React.FC = () => { - const formRef = useRef(null); + const formRef = useRef(undefined); const name = Form.useWatch('name', formRef.current); const [, setUpdate] = useState({}); return ( From 2ea86f801544c165cde30403d7dc4556cf3ac4e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A0=97=E5=98=89=E7=94=B7?= <574980606@qq.com> Date: Tue, 14 Jan 2025 16:26:58 +0800 Subject: [PATCH 6/7] fix: fix --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 4abb6bf12..af8afa5ee 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@rc-component/form", - "version": "1.0.0", + "version": "1.0.0-0", "description": "React Form Component", "typings": "es/index.d.ts", "engines": { From 667dca8bb5c1083b4ea4330bc6e2ede90f52dcbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E8=B4=A7=E6=9C=BA=E5=99=A8=E4=BA=BA?= Date: Fri, 17 Jan 2025 17:14:25 +0800 Subject: [PATCH 7/7] chore: bump father plugin --- package.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/package.json b/package.json index af8afa5ee..781ebfedb 100644 --- a/package.json +++ b/package.json @@ -49,12 +49,11 @@ "react-dom": ">=16.9.0" }, "dependencies": { - "@babel/runtime": "^7.18.0", "@rc-component/async-validator": "^5.0.3", "@rc-component/util": "^1.1.0" }, "devDependencies": { - "@rc-component/father-plugin": "^1.0.0", + "@rc-component/father-plugin": "^2.0.1", "@testing-library/jest-dom": "^6.1.4", "@testing-library/react": "^16.0.0", "@types/jest": "^29.2.5",