Skip to content

Commit e8ce503

Browse files
authored
feat: support Form.STRICT (#605)
* feat: support Form.STRICT * refactor: use config
1 parent 135549e commit e8ce503

File tree

3 files changed

+67
-7
lines changed

3 files changed

+67
-7
lines changed

src/interface.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,16 @@ type RecursivePartial<T> = NonNullable<T> extends object
242242
}
243243
: T;
244244

245+
export type FilterFunc = (meta: Meta) => boolean;
246+
247+
export type GetFieldsValueConfig = { strict?: boolean; filter?: FilterFunc };
248+
245249
export interface FormInstance<Values = any> {
246250
// Origin Form API
247251
getFieldValue: (name: NamePath) => StoreValue;
248252
getFieldsValue: (() => Values) &
249-
((nameList: NamePath[] | true, filterFunc?: (meta: Meta) => boolean) => any);
253+
((nameList: NamePath[] | true, filterFunc?: FilterFunc) => any) &
254+
((config: GetFieldsValueConfig) => any);
250255
getFieldError: (name: NamePath) => string[];
251256
getFieldsError: (nameList?: NamePath[]) => FieldError[];
252257
getFieldWarning: (name: NamePath) => string[];

src/useForm.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import type {
2323
InternalValidateOptions,
2424
ValuedNotifyInfo,
2525
WatchCallBack,
26+
FilterFunc,
27+
GetFieldsValueConfig,
2628
} from './interface';
2729
import { allPromiseFinish } from './utils/asyncUtil';
2830
import { merge } from 'rc-util/lib/utils/set';
@@ -265,15 +267,31 @@ export class FormStore {
265267
});
266268
};
267269

268-
private getFieldsValue = (nameList?: NamePath[] | true, filterFunc?: (meta: Meta) => boolean) => {
270+
private getFieldsValue = (
271+
nameList?: NamePath[] | true | GetFieldsValueConfig,
272+
filterFunc?: FilterFunc,
273+
) => {
269274
this.warningUnhooked();
270275

271-
if (nameList === true && !filterFunc) {
276+
// Fill args
277+
let mergedNameList: NamePath[] | true;
278+
let mergedFilterFunc: FilterFunc;
279+
let mergedStrict: boolean;
280+
281+
if (nameList === true || Array.isArray(nameList)) {
282+
mergedNameList = nameList;
283+
mergedFilterFunc = filterFunc;
284+
} else if (nameList && typeof nameList === 'object') {
285+
mergedStrict = nameList.strict;
286+
mergedFilterFunc = nameList.filter;
287+
}
288+
289+
if (mergedNameList === true && !mergedFilterFunc) {
272290
return this.store;
273291
}
274292

275293
const fieldEntities = this.getFieldEntitiesForNamePathList(
276-
Array.isArray(nameList) ? nameList : null,
294+
Array.isArray(mergedNameList) ? mergedNameList : null,
277295
);
278296

279297
const filteredNameList: NamePath[] = [];
@@ -283,15 +301,19 @@ export class FormStore {
283301

284302
// Ignore when it's a list item and not specific the namePath,
285303
// since parent field is already take in count
286-
if (!nameList && (entity as FieldEntity).isListField?.()) {
304+
if (mergedStrict) {
305+
if ((entity as FieldEntity).isList?.()) {
306+
return;
307+
}
308+
} else if (!mergedNameList && (entity as FieldEntity).isListField?.()) {
287309
return;
288310
}
289311

290-
if (!filterFunc) {
312+
if (!mergedFilterFunc) {
291313
filteredNameList.push(namePath);
292314
} else {
293315
const meta: Meta = 'getMeta' in entity ? entity.getMeta() : null;
294-
if (filterFunc(meta)) {
316+
if (mergedFilterFunc(meta)) {
295317
filteredNameList.push(namePath);
296318
}
297319
}

tests/list.test.tsx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,4 +855,37 @@ describe('Form.List', () => {
855855

856856
expect(onValuesChange).toHaveBeenCalledWith({ name: 'little' }, { name: 'little', age: 2 });
857857
});
858+
859+
it('getFieldsValue with Strict mode', () => {
860+
const formRef = React.createRef<FormInstance>();
861+
862+
const initialValues = { list: [{ bamboo: 1, light: 3 }], little: 9 };
863+
864+
mount(
865+
<div>
866+
<Form ref={formRef} initialValues={initialValues}>
867+
<Field name="little">
868+
<Input />
869+
</Field>
870+
<Form.List name="list">
871+
{fields =>
872+
fields.map(field => (
873+
<Field key={field.key} name={[field.name, 'bamboo']}>
874+
<Input />
875+
</Field>
876+
))
877+
}
878+
</Form.List>
879+
</Form>
880+
</div>,
881+
);
882+
883+
// expect(formRef.current.getFieldsValue()).toEqual(initialValues);
884+
885+
// Strict only return field not list
886+
expect(formRef.current.getFieldsValue({ strict: true })).toEqual({
887+
list: [{ bamboo: 1 }],
888+
little: 9,
889+
});
890+
});
858891
});

0 commit comments

Comments
 (0)