Skip to content

Commit f7f4eb2

Browse files
li-jia-nanzombieJ
andauthored
refactor: rc-component/form (#747)
* refactor: rc-component/form * fix: fix * fix: fix * fix: fix * fix: fix * fix: fix * chore: bump father plugin --------- Co-authored-by: 二货机器人 <smith3816@gmail.com>
1 parent 811792a commit f7f4eb2

13 files changed

+37
-42
lines changed

package.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "rc-field-form",
3-
"version": "2.7.0",
2+
"name": "@rc-component/form",
3+
"version": "1.0.0-0",
44
"description": "React Form Component",
55
"typings": "es/index.d.ts",
66
"engines": {
@@ -49,18 +49,17 @@
4949
"react-dom": ">=16.9.0"
5050
},
5151
"dependencies": {
52-
"@babel/runtime": "^7.18.0",
5352
"@rc-component/async-validator": "^5.0.3",
54-
"rc-util": "^5.32.2"
53+
"@rc-component/util": "^1.1.0"
5554
},
5655
"devDependencies": {
57-
"@rc-component/father-plugin": "^1.0.0",
56+
"@rc-component/father-plugin": "^2.0.1",
5857
"@testing-library/jest-dom": "^6.1.4",
5958
"@testing-library/react": "^16.0.0",
6059
"@types/jest": "^29.2.5",
6160
"@types/lodash": "^4.14.135",
6261
"@types/node": "^22.0.2",
63-
"@types/react": "^18.0.0",
62+
"@types/react": "^19.0.6",
6463
"@types/react-dom": "^19.0.1",
6564
"@umijs/fabric": "^4.0.1",
6665
"dumi": "^2.0.0",

src/Field.tsx

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import toChildrenArray from 'rc-util/lib/Children/toArray';
2-
import isEqual from 'rc-util/lib/isEqual';
3-
import warning from 'rc-util/lib/warning';
1+
import toChildrenArray from '@rc-component/util/lib/Children/toArray';
2+
import isEqual from '@rc-component/util/lib/isEqual';
3+
import warning from '@rc-component/util/lib/warning';
44
import * as React from 'react';
55
import FieldContext, { HOOK_MARK } from './FieldContext';
66
import type {
@@ -112,11 +112,6 @@ export interface FieldState {
112112
class Field extends React.Component<InternalFieldProps, FieldState> implements FieldEntity {
113113
public static contextType = FieldContext;
114114

115-
public static defaultProps = {
116-
trigger: 'onChange',
117-
valuePropName: 'value',
118-
};
119-
120115
public state = {
121116
resetCount: 0,
122117
};
@@ -548,9 +543,10 @@ class Field extends React.Component<InternalFieldProps, FieldState> implements F
548543
}
549544

550545
// Filed element only
551-
const childList = toChildrenArray(children);
546+
const childList = toChildrenArray(children as any);
547+
552548
if (childList.length !== 1 || !React.isValidElement(childList[0])) {
553-
return { child: childList, isFunction: false };
549+
return { child: childList as React.ReactNode, isFunction: false };
554550
}
555551

556552
return { child: childList[0], isFunction: false };
@@ -566,11 +562,11 @@ class Field extends React.Component<InternalFieldProps, FieldState> implements F
566562
public getControlled = (childProps: ChildProps = {}) => {
567563
const {
568564
name,
569-
trigger,
565+
trigger = 'onChange',
570566
validateTrigger,
571567
getValueFromEvent,
572568
normalize,
573-
valuePropName,
569+
valuePropName = 'value',
574570
getValueProps,
575571
fieldContext,
576572
} = this.props;

src/FieldContext.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import warning from 'rc-util/lib/warning';
1+
import warning from '@rc-component/util/lib/warning';
22
import * as React from 'react';
33
import type { InternalFormInstance } from './interface';
44

src/Form.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import ListContext from './ListContext';
1717

1818
type BaseFormProps = Omit<React.FormHTMLAttributes<HTMLFormElement>, 'onSubmit' | 'children'>;
1919

20-
type RenderProps = (values: Store, form: FormInstance) => JSX.Element | React.ReactNode;
20+
type RenderProps = (values: Store, form: FormInstance) => React.ReactNode;
2121

2222
export interface FormProps<Values = any> extends BaseFormProps {
2323
initialValues?: Store;
@@ -138,7 +138,7 @@ const Form: React.ForwardRefRenderFunction<FormRef, FormProps> = (
138138
useSubscribe(!childrenRenderProps);
139139

140140
// Listen if fields provided. We use ref to save prev data here to avoid additional render
141-
const prevFieldsRef = React.useRef<FieldData[] | undefined>();
141+
const prevFieldsRef = React.useRef<FieldData[] | undefined>(null);
142142
React.useEffect(() => {
143143
if (!isSimilar(prevFieldsRef.current || [], fields || [])) {
144144
formInstance.setFields(fields || []);

src/List.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from 'react';
2-
import warning from 'rc-util/lib/warning';
2+
import warning from '@rc-component/util/lib/warning';
33
import type { InternalNamePath, NamePath, StoreValue, ValidatorRule, Meta } from './interface';
44
import FieldContext from './FieldContext';
55
import Field from './Field';
@@ -24,11 +24,7 @@ export interface ListProps<Values = any> {
2424
rules?: ValidatorRule[];
2525
validateTrigger?: string | string[] | false;
2626
initialValue?: any[];
27-
children?: (
28-
fields: ListField[],
29-
operations: ListOperations,
30-
meta: Meta,
31-
) => JSX.Element | React.ReactNode;
27+
children?: (fields: ListField[], operations: ListOperations, meta: Meta) => React.ReactNode;
3228

3329
/** @private Passed by Form.List props. Do not use since it will break by path check. */
3430
isListField?: boolean;

src/useForm.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { merge } from 'rc-util/lib/utils/set';
2-
import warning from 'rc-util/lib/warning';
1+
import { merge } from '@rc-component/util/lib/utils/set';
2+
import warning from '@rc-component/util/lib/warning';
33
import * as React from 'react';
44
import { HOOK_MARK } from './FieldContext';
55
import type {
@@ -1027,7 +1027,7 @@ export class FormStore {
10271027
}
10281028

10291029
function useForm<Values = any>(form?: FormInstance<Values>): [FormInstance<Values>] {
1030-
const formRef = React.useRef<FormInstance>();
1030+
const formRef = React.useRef<FormInstance>(null);
10311031
const [, forceUpdate] = React.useState({});
10321032

10331033
if (!formRef.current) {

src/useWatch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import warning from 'rc-util/lib/warning';
1+
import warning from '@rc-component/util/lib/warning';
22
import { useContext, useEffect, useMemo, useRef, useState } from 'react';
33
import FieldContext, { HOOK_MARK } from './FieldContext';
44
import type {

src/utils/validateUtil.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import RawAsyncValidator from '@rc-component/async-validator';
22
import * as React from 'react';
3-
import warning from 'rc-util/lib/warning';
3+
import warning from '@rc-component/util/lib/warning';
44
import type {
55
InternalNamePath,
66
InternalValidateOptions,
@@ -9,7 +9,7 @@ import type {
99
RuleError,
1010
} from '../interface';
1111
import { defaultValidateMessages } from './messages';
12-
import { merge } from 'rc-util/lib/utils/set';
12+
import { merge } from '@rc-component/util/lib/utils/set';
1313

1414
// Remove incorrect original ts define
1515
const AsyncValidator: any = RawAsyncValidator;

src/utils/valueUtil.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import getValue from 'rc-util/lib/utils/get';
2-
import setValue from 'rc-util/lib/utils/set';
1+
import getValue from '@rc-component/util/lib/utils/get';
2+
import setValue from '@rc-component/util/lib/utils/set';
33
import type { InternalNamePath, NamePath, Store, EventArgs } from '../interface';
44
import { toArray } from './typeUtil';
55

tests/index.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { act, fireEvent, render } from '@testing-library/react';
2-
import { resetWarned } from 'rc-util/lib/warning';
2+
import { resetWarned } from '@rc-component/util/lib/warning';
33
import React from 'react';
44
import type { FormInstance } from '../src';
55
import Form, { Field, useForm } from '../src';

tests/initialValue.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { act, fireEvent, render } from '@testing-library/react';
2-
import { resetWarned } from 'rc-util/lib/warning';
2+
import { resetWarned } from '@rc-component/util/lib/warning';
33
import React, { useState } from 'react';
44
import Form, { Field, List, useForm, type FormInstance } from '../src';
55
import { changeValue, getInput } from './common';

tests/list.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22
import { fireEvent, render, act } from '@testing-library/react';
3-
import { resetWarned } from 'rc-util/lib/warning';
3+
import { resetWarned } from '@rc-component/util/lib/warning';
44
import Form, { Field, List } from '../src';
55
import type { FormProps } from '../src';
66
import type { ListField, ListOperations, ListProps } from '../src/List';

tests/useWatch.test.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,13 @@ describe('useWatch', () => {
288288
const more = Form.useWatch(['age', 'name', 'gender'], form);
289289
const demo = Form.useWatch<string>(['demo']);
290290

291-
const values2 = Form.useWatch(values => ({ newName: values.name, newAge: values.age }), form);
292-
const values3 = Form.useWatch<FieldType, { newName?: string }>(values => ({
293-
newName: values.name,
291+
const values2 = Form.useWatch(
292+
_values => ({ newName: _values.name, newAge: _values.age }),
293+
form,
294+
);
295+
296+
const values3 = Form.useWatch<FieldType, { newName?: string }>(_values => ({
297+
newName: _values.name,
294298
}));
295299

296300
return (
@@ -393,7 +397,7 @@ describe('useWatch', () => {
393397
it('first undefined', () => {
394398
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
395399
const Demo: React.FC = () => {
396-
const formRef = useRef<FormInstance>();
400+
const formRef = useRef<FormInstance>(undefined);
397401
const name = Form.useWatch('name', formRef.current);
398402
const [, setUpdate] = useState({});
399403
return (

0 commit comments

Comments
 (0)