Skip to content

Commit ca49213

Browse files
committed
isFieldsTouched support second arg
1 parent 12af40b commit ca49213

File tree

4 files changed

+33
-9
lines changed

4 files changed

+33
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class Demo extends React.Component {
122122
| getFieldsValue | Get list of field values by name path list | (nameList?: [NamePath](#namepath)[]) => any |
123123
| getFieldError | Get field errors by name path | (name: [NamePath](#namepath)) => string[] |
124124
| getFieldsError | Get list of field errors by name path list | (nameList?: [NamePath](#namepath)[]) => FieldError[] |
125-
| isFieldsTouched | Check if list of fields are touched | (nameList?: [NamePath](#namepath)[]) => boolean |
125+
| isFieldsTouched | Check if list of fields are touched | (nameList?: [NamePath](#namepath)[], allTouched?: boolean) => boolean |
126126
| isFieldTouched | Check if a field is touched | (name: [NamePath](#namepath)) => boolean |
127127
| isFieldValidating | Check if a field is validating | (name: [NamePath](#namepath)) => boolean |
128128
| resetFields | Reset fields status | (fields?: [NamePath](#namepath)[]) => void |

src/interface.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,17 @@ export interface InternalHooks {
142142
setValidateMessages: (validateMessages: ValidateMessages) => void;
143143
}
144144

145+
export type IsFieldsTouched =
146+
| ((allFieldsTouched?: boolean) => boolean)
147+
| ((nameList: NamePath[], allFieldsTouched?: boolean) => boolean);
148+
145149
export interface FormInstance {
146150
// Origin Form API
147151
getFieldValue: (name: NamePath) => StoreValue;
148152
getFieldsValue: (nameList?: NamePath[]) => Store;
149153
getFieldError: (name: NamePath) => string[];
150154
getFieldsError: (nameList?: NamePath[]) => FieldError[];
151-
isFieldsTouched: (nameList?: NamePath[]) => boolean;
155+
isFieldsTouched: IsFieldsTouched;
152156
isFieldTouched: (name: NamePath) => boolean;
153157
isFieldValidating: (name: NamePath) => boolean;
154158
isFieldsValidating: (nameList: NamePath[]) => boolean;

src/useForm.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,27 @@ export class FormStore {
172172
return [];
173173
};
174174

175-
private isFieldsTouched = (nameList?: NamePath[]) => {
176-
let namePathList: InternalNamePath[] | null = null;
177-
if (nameList) {
178-
namePathList = nameList.map(getNamePath);
175+
private isFieldsTouched = (...args) => {
176+
const [arg0, arg1] = args;
177+
let namePathList: InternalNamePath[] | null;
178+
let isAllFieldsTouched = false;
179+
180+
if (args.length === 0) {
181+
namePathList = null;
182+
} else if (args.length === 1) {
183+
if (Array.isArray(arg0)) {
184+
namePathList = arg0.map(getNamePath);
185+
isAllFieldsTouched = false;
186+
} else {
187+
namePathList = null;
188+
isAllFieldsTouched = arg0;
189+
}
190+
} else {
191+
namePathList = arg0.map(getNamePath);
192+
isAllFieldsTouched = arg1;
179193
}
180194

181-
return this.getFieldEntities().some((field: FieldEntity) => {
195+
const testTouched = (field: FieldEntity) => {
182196
// Not provide `nameList` will check all the fields
183197
if (!namePathList) {
184198
return field.isFieldTouched();
@@ -189,7 +203,11 @@ export class FormStore {
189203
return field.isFieldTouched();
190204
}
191205
return false;
192-
});
206+
};
207+
208+
return isAllFieldsTouched
209+
? this.getFieldEntities().every(testTouched)
210+
: this.getFieldEntities().some(testTouched);
193211
};
194212

195213
private isFieldTouched = (name: NamePath) => this.isFieldsTouched([name]);

tests/index.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22
import { mount } from 'enzyme';
3-
import Form, { Field, useForm } from '../src';
3+
import Form, { Field } from '../src';
44
import InfoField, { Input } from './common/InfoField';
55
import { changeValue, getField, matchError } from './common';
66
import timeout from './common/timeout';
@@ -76,6 +76,8 @@ describe('Basic', () => {
7676
await changeValue(getField(wrapper, 0), 'Bamboo');
7777
expect(form.isFieldsTouched()).toBeTruthy();
7878
expect(form.isFieldsTouched(['username', 'password'])).toBeTruthy();
79+
expect(form.isFieldsTouched(true)).toBeFalsy();
80+
expect(form.isFieldsTouched(['username', 'password'], true)).toBeFalsy();
7981
});
8082

8183
describe('reset form', () => {

0 commit comments

Comments
 (0)