Skip to content

Commit 1d60c03

Browse files
committed
chore: merge master
2 parents 7a8800a + 0ec1a4e commit 1d60c03

File tree

9 files changed

+55
-20
lines changed

9 files changed

+55
-20
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
/yarn-error.log
77
/yarn.lock
88
/package-lock.json
9+
pnpm-lock.yaml
910

1011
# production
1112
/dist
@@ -29,4 +30,4 @@
2930
.dumi/
3031
package-lock.json
3132

32-
bun.lockb
33+
bun.lockb

docs/examples/useWatch-selector.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/no-shadow */
12
import React from 'react';
23
import Form, { Field } from 'rc-field-form';
34
import Input from './components/Input';
@@ -10,11 +11,15 @@ type FieldType = {
1011

1112
export default () => {
1213
const [form] = Form.useForm<FieldType>();
14+
const firstEmptyObject = Form.useWatch(values => ({ newName: values.name }), form);
15+
console.log('firstEmptyObject', firstEmptyObject);
16+
1317
const values = Form.useWatch(
1418
values => ({ init: values.init, newName: values.name, newAge: values.age }),
1519
{ form, preserve: true },
1620
);
1721
console.log('values', values);
22+
1823
return (
1924
<>
2025
<Form form={form} initialValues={{ init: 'init', name: 'aaa' }}>

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@rc-component/form",
3-
"version": "1.3.1",
3+
"version": "1.3.2",
44
"description": "React Form Component",
55
"typings": "es/index.d.ts",
66
"engines": {
@@ -50,7 +50,8 @@
5050
},
5151
"dependencies": {
5252
"@rc-component/async-validator": "^5.0.3",
53-
"@rc-component/util": "^1.1.0"
53+
"@rc-component/util": "^1.3.0",
54+
"clsx": "^2.1.1"
5455
},
5556
"devDependencies": {
5657
"@rc-component/father-plugin": "^2.0.1",

src/Form.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import type { FormContextProps } from './FormContext';
1414
import FormContext from './FormContext';
1515
import { isSimilar } from './utils/valueUtil';
1616
import ListContext from './ListContext';
17-
import BatchUpdate, { type BatchTask, type BatchUpdateRef } from './BatchUpdate';
17+
import type { BatchTask, BatchUpdateRef } from './BatchUpdate';
18+
import BatchUpdate from './BatchUpdate';
1819

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

@@ -185,7 +186,7 @@ const Form: React.ForwardRefRenderFunction<FormRef, FormProps> = (
185186
}, [fields, formInstance]);
186187

187188
// =========================== Render ===========================
188-
const formContextValue = React.useMemo(
189+
const formContextValue = React.useMemo<InternalFormInstance>(
189190
() => ({
190191
...(formInstance as InternalFormInstance),
191192
validateTrigger,

src/List.tsx

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import * as React from 'react';
22
import warning from '@rc-component/util/lib/warning';
3-
import type { InternalNamePath, NamePath, StoreValue, ValidatorRule, Meta } from './interface';
3+
import type {
4+
InternalNamePath,
5+
NamePath,
6+
StoreValue,
7+
ValidatorRule,
8+
Meta,
9+
InternalFormInstance,
10+
} from './interface';
411
import FieldContext from './FieldContext';
512
import Field from './Field';
613
import { move, getNamePath } from './utils/valueUtil';
@@ -40,18 +47,19 @@ function List<Values = any>({
4047
}: ListProps<Values>) {
4148
const context = React.useContext(FieldContext);
4249
const wrapperListContext = React.useContext(ListContext);
43-
const keyRef = React.useRef({
44-
keys: [],
45-
id: 0,
46-
});
50+
const keyRef = React.useRef({ keys: [], id: 0 });
51+
4752
const keyManager = keyRef.current;
4853

49-
const prefixName: InternalNamePath = React.useMemo(() => {
54+
const prefixName = React.useMemo<InternalNamePath>(() => {
5055
const parentPrefixName = getNamePath(context.prefixName) || [];
5156
return [...parentPrefixName, ...getNamePath(name)];
5257
}, [context.prefixName, name]);
5358

54-
const fieldContext = React.useMemo(() => ({ ...context, prefixName }), [context, prefixName]);
59+
const fieldContext = React.useMemo<InternalFormInstance>(
60+
() => ({ ...context, prefixName }),
61+
[context, prefixName],
62+
);
5563

5664
// List context
5765
const listContext = React.useMemo<ListContextProps>(
@@ -62,7 +70,7 @@ function List<Values = any>({
6270
return [keyManager.keys[pathName], namePath.slice(len + 1)];
6371
},
6472
}),
65-
[prefixName],
73+
[keyManager, prefixName],
6674
);
6775

6876
// User should not pass `children` as other type.

src/useWatch.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type GetGeneric<TForm extends FormInstance> = ReturnPromise<ReturnType<TForm['va
1818
export function stringify(value: any) {
1919
try {
2020
return JSON.stringify(value);
21-
} catch (err) {
21+
} catch {
2222
return Math.random();
2323
}
2424
}
@@ -92,7 +92,9 @@ function useWatch(
9292
const options = isFormInstance(_form) ? { form: _form } : _form;
9393
const form = options.form;
9494

95-
const [value, setValue] = useState<any>();
95+
const [value, setValue] = useState<any>(() =>
96+
typeof dependencies === 'function' ? dependencies({}) : undefined,
97+
);
9698

9799
const valueStr = useMemo(() => stringify(value), [value]);
98100
const valueStrRef = useRef(valueStr);

src/utils/validateUtil.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ async function validateRule(
9090
}
9191
}
9292

93-
if (!result.length && subRuleField) {
93+
if (!result.length && subRuleField && Array.isArray(value) && value.length > 0) {
9494
const subResults: string[][] = await Promise.all(
9595
(value as StoreValue[]).map((subValue: StoreValue, i: number) =>
9696
validateRule(`${name}.${i}`, subValue, subRuleField, options, messageVariables),

tests/common/InfoField.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,17 @@ const InfoField: React.FC<InfoFieldProps> = ({ children, ...props }) => (
1717
<Field {...props}>
1818
{(control, info) => {
1919
const { errors, warnings, validating } = info;
20-
2120
return (
22-
<div className='field'>
21+
<div className="field">
2322
{children ? React.cloneElement(children, control) : <Input {...control} />}
2423
<ul className="errors">
2524
{errors.map((error, index) => (
26-
<li key={index}>{error}</li>
25+
<li key={`error-${index}`}>{error}</li>
2726
))}
2827
</ul>
2928
<ul className="warnings">
3029
{warnings.map((warning, index) => (
31-
<li key={index}>{warning}</li>
30+
<li key={`warning-${index}`}>{warning}</li>
3231
))}
3332
</ul>
3433
{validating && <span className="validating" />}

tests/useWatch.test.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,4 +518,22 @@ describe('useWatch', () => {
518518
await changeValue(input[0], 'bamboo2');
519519
expect(container.querySelector<HTMLDivElement>('.values')?.textContent).toEqual('bamboo2');
520520
});
521+
it('selector by first no undefined', async () => {
522+
const list: any[] = [];
523+
const Demo = () => {
524+
const [form] = Form.useForm<{ name?: string }>();
525+
const data = Form.useWatch(values => values, form);
526+
list.push(data);
527+
return (
528+
<Form form={form}>
529+
<Field name="name" initialValue="bamboo">
530+
<Input />
531+
</Field>
532+
</Form>
533+
);
534+
};
535+
render(<Demo />);
536+
expect(list[0]).toEqual({});
537+
expect(list[1]).toEqual({ name: 'bamboo' });
538+
});
521539
});

0 commit comments

Comments
 (0)