Skip to content

Commit fe0ea55

Browse files
authored
fix: TypeError ruleErrors.forEach on rule type: 'array' with defaultField (#470)
* fix: TypeError ruleErrors.forEach on rule type: 'array' with defaultField close ant-design/ant-design#36436 * chore: remove console.log
1 parent ea6e6e1 commit fe0ea55

File tree

6 files changed

+41
-22
lines changed

6 files changed

+41
-22
lines changed

docs/examples/validate.tsx

+3-7
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ const { Field } = Form;
88

99
const Error = ({ children }) => (
1010
<ul style={{ color: 'red' }}>
11-
{children.map(error => (
12-
<li>{error}</li>
11+
{children.map((error, i) => (
12+
<li key={i}>{error}</li>
1313
))}
1414
</ul>
1515
);
@@ -38,10 +38,6 @@ export default () => {
3838
console.log('Finish:', values);
3939
};
4040

41-
const onNameError = (errors: string[]) => {
42-
console.log('🐞 Name Error Change:', errors);
43-
};
44-
4541
return (
4642
<div style={{ position: 'relative' }}>
4743
<h3>Validate Form</h3>
@@ -57,7 +53,7 @@ export default () => {
5753

5854
return (
5955
<React.Fragment>
60-
<Field name="username" rules={[{ required: true }]} onError={onNameError}>
56+
<Field name="username" rules={[{ required: true }]}>
6157
<Input
6258
placeholder="Username"
6359
onChange={({ target: { value } }) => {

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@
7878
"react-dnd-html5-backend": "^8.0.3",
7979
"react-dom": "^16.14.0",
8080
"react-redux": "^4.4.10",
81-
"react-router": "^3.0.0",
8281
"redux": "^3.7.2",
8382
"typescript": "^4.6.3"
8483
}

src/Field.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ class Field extends React.Component<InternalFieldProps, FieldState> implements F
401401
// Get errors & warnings
402402
const nextErrors: string[] = [];
403403
const nextWarnings: string[] = [];
404-
ruleErrors.forEach(({ rule: { warningOnly }, errors = EMPTY_ERRORS }) => {
404+
ruleErrors.forEach?.(({ rule: { warningOnly }, errors = EMPTY_ERRORS }) => {
405405
if (warningOnly) {
406406
nextWarnings.push(...errors);
407407
} else {

src/index.tsx

+5-12
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { FormInstance } from './interface';
33
import Field from './Field';
44
import List from './List';
55
import useForm from './useForm';
6-
import FieldForm, { FormProps } from './Form';
6+
import type { FormProps } from './Form';
7+
import FieldForm from './Form';
78
import { FormProvider } from './FormContext';
89
import FieldContext from './FieldContext';
910
import ListContext from './ListContext';
@@ -30,16 +31,8 @@ RefForm.List = List;
3031
RefForm.useForm = useForm;
3132
RefForm.useWatch = useWatch;
3233

33-
export {
34-
FormInstance,
35-
Field,
36-
List,
37-
useForm,
38-
FormProvider,
39-
FormProps,
40-
FieldContext,
41-
ListContext,
42-
useWatch,
43-
};
34+
export { FormInstance, Field, List, useForm, FormProvider, FieldContext, ListContext, useWatch };
35+
36+
export type { FormProps };
4437

4538
export default RefForm;

src/useForm.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,7 @@ export class FormStore {
894894
const mergedErrors: string[] = [];
895895
const mergedWarnings: string[] = [];
896896

897-
ruleErrors.forEach(({ rule: { warningOnly }, errors }) => {
897+
ruleErrors.forEach?.(({ rule: { warningOnly }, errors }) => {
898898
if (warningOnly) {
899899
mergedWarnings.push(...errors);
900900
} else {

tests/legacy/validate-array.test.js

+31
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,35 @@ describe('legacy.validate-array', () => {
5555
);
5656
}
5757
});
58+
59+
// https://github.com/ant-design/ant-design/issues/36436
60+
it('antd issue #36436', async () => {
61+
let form;
62+
63+
mount(
64+
<div>
65+
<Form
66+
ref={instance => {
67+
form = instance;
68+
}}
69+
>
70+
<Field
71+
name="tags"
72+
rules={[
73+
{
74+
type: 'array',
75+
defaultField: { type: 'string' },
76+
},
77+
]}
78+
>
79+
<input />
80+
</Field>
81+
</Form>
82+
</div>,
83+
);
84+
85+
expect(async () => {
86+
await form.validateFields();
87+
}).not.toThrow();
88+
});
5889
});

0 commit comments

Comments
 (0)