Skip to content

Commit

Permalink
feat: support Form.STRICT (#605)
Browse files Browse the repository at this point in the history
* feat: support Form.STRICT

* refactor: use config
  • Loading branch information
zombieJ authored Jul 26, 2023
1 parent 135549e commit e8ce503
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 7 deletions.
7 changes: 6 additions & 1 deletion src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,16 @@ type RecursivePartial<T> = NonNullable<T> extends object
}
: T;

export type FilterFunc = (meta: Meta) => boolean;

export type GetFieldsValueConfig = { strict?: boolean; filter?: FilterFunc };

export interface FormInstance<Values = any> {
// Origin Form API
getFieldValue: (name: NamePath) => StoreValue;
getFieldsValue: (() => Values) &
((nameList: NamePath[] | true, filterFunc?: (meta: Meta) => boolean) => any);
((nameList: NamePath[] | true, filterFunc?: FilterFunc) => any) &
((config: GetFieldsValueConfig) => any);
getFieldError: (name: NamePath) => string[];
getFieldsError: (nameList?: NamePath[]) => FieldError[];
getFieldWarning: (name: NamePath) => string[];
Expand Down
34 changes: 28 additions & 6 deletions src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import type {
InternalValidateOptions,
ValuedNotifyInfo,
WatchCallBack,
FilterFunc,
GetFieldsValueConfig,
} from './interface';
import { allPromiseFinish } from './utils/asyncUtil';
import { merge } from 'rc-util/lib/utils/set';
Expand Down Expand Up @@ -265,15 +267,31 @@ export class FormStore {
});
};

private getFieldsValue = (nameList?: NamePath[] | true, filterFunc?: (meta: Meta) => boolean) => {
private getFieldsValue = (
nameList?: NamePath[] | true | GetFieldsValueConfig,
filterFunc?: FilterFunc,
) => {
this.warningUnhooked();

if (nameList === true && !filterFunc) {
// Fill args
let mergedNameList: NamePath[] | true;
let mergedFilterFunc: FilterFunc;
let mergedStrict: boolean;

if (nameList === true || Array.isArray(nameList)) {
mergedNameList = nameList;
mergedFilterFunc = filterFunc;
} else if (nameList && typeof nameList === 'object') {
mergedStrict = nameList.strict;
mergedFilterFunc = nameList.filter;
}

if (mergedNameList === true && !mergedFilterFunc) {
return this.store;
}

const fieldEntities = this.getFieldEntitiesForNamePathList(
Array.isArray(nameList) ? nameList : null,
Array.isArray(mergedNameList) ? mergedNameList : null,
);

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

// Ignore when it's a list item and not specific the namePath,
// since parent field is already take in count
if (!nameList && (entity as FieldEntity).isListField?.()) {
if (mergedStrict) {
if ((entity as FieldEntity).isList?.()) {
return;
}
} else if (!mergedNameList && (entity as FieldEntity).isListField?.()) {
return;
}

if (!filterFunc) {
if (!mergedFilterFunc) {
filteredNameList.push(namePath);
} else {
const meta: Meta = 'getMeta' in entity ? entity.getMeta() : null;
if (filterFunc(meta)) {
if (mergedFilterFunc(meta)) {
filteredNameList.push(namePath);
}
}
Expand Down
33 changes: 33 additions & 0 deletions tests/list.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -855,4 +855,37 @@ describe('Form.List', () => {

expect(onValuesChange).toHaveBeenCalledWith({ name: 'little' }, { name: 'little', age: 2 });
});

it('getFieldsValue with Strict mode', () => {
const formRef = React.createRef<FormInstance>();

const initialValues = { list: [{ bamboo: 1, light: 3 }], little: 9 };

mount(
<div>
<Form ref={formRef} initialValues={initialValues}>
<Field name="little">
<Input />
</Field>
<Form.List name="list">
{fields =>
fields.map(field => (
<Field key={field.key} name={[field.name, 'bamboo']}>
<Input />
</Field>
))
}
</Form.List>
</Form>
</div>,
);

// expect(formRef.current.getFieldsValue()).toEqual(initialValues);

// Strict only return field not list
expect(formRef.current.getFieldsValue({ strict: true })).toEqual({
list: [{ bamboo: 1 }],
little: 9,
});
});
});

0 comments on commit e8ce503

Please sign in to comment.