Skip to content

Commit 87ba282

Browse files
authored
refactor: getFieldValues default only get registered field (#77)
* getFieldsValue default get all the preserve values * fix Field usage perf
1 parent 56a084d commit 87ba282

File tree

6 files changed

+30
-31
lines changed

6 files changed

+30
-31
lines changed

examples/basic.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export default class Demo extends React.Component {
3636
<h4>Show additional field when `username` is `111`</h4>
3737
<Field dependencies={['username']}>
3838
{(control, meta, context) => {
39-
const { username } = context.getFieldsValue();
39+
const { username } = context.getFieldsValue(true);
4040
console.log('my render!', username);
4141
return username === '111' && <Input {...control} placeholder="I am secret!" />;
4242
}}

examples/validate.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export default class Demo extends React.Component {
9292
{ required: true },
9393
context => ({
9494
validator(rule, value, callback) {
95-
const { password } = context.getFieldsValue();
95+
const { password } = context.getFieldsValue(true);
9696
if (password !== value) {
9797
callback('Not Same as password1!!!');
9898
}

src/Field.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ class Field extends React.Component<FieldProps, FieldState> implements FieldEnti
171171
) => {
172172
const { shouldUpdate, dependencies = [], onReset } = this.props;
173173
const { getFieldsValue }: FormInstance = this.context;
174-
const values = getFieldsValue();
174+
const values = getFieldsValue(true);
175175
const namePath = this.getNamePath();
176176
const prevValue = this.getValue(prevStore);
177177
const curValue = this.getValue();
@@ -355,7 +355,7 @@ class Field extends React.Component<FieldProps, FieldState> implements FieldEnti
355355
public getValue = (store?: Store) => {
356356
const { getFieldsValue }: FormInstance = this.context;
357357
const namePath = this.getNamePath();
358-
return getValue(store || getFieldsValue(), namePath);
358+
return getValue(store || getFieldsValue(true), namePath);
359359
};
360360

361361
public getControlled = (childProps: ChildProps = {}) => {
@@ -386,7 +386,7 @@ class Field extends React.Component<FieldProps, FieldState> implements FieldEnti
386386
}
387387

388388
if (normalize) {
389-
newValue = normalize(newValue, value, getFieldsValue());
389+
newValue = normalize(newValue, value, getFieldsValue(true));
390390
}
391391

392392
dispatch({

src/Form.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ const Form: React.FunctionComponent<FormProps> = (
106106
let childrenNode = children;
107107
const childrenRenderProps = typeof children === 'function';
108108
if (childrenRenderProps) {
109-
const values = formInstance.getFieldsValue();
109+
const values = formInstance.getFieldsValue(true);
110110
childrenNode = (children as RenderProps)(values, formInstance);
111111
}
112112

src/interface.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,7 @@ export interface ValidateErrorEntity {
8080
}
8181

8282
export interface FieldEntity {
83-
onStoreChange: (
84-
store: Store,
85-
namePathList: InternalNamePath[] | null,
86-
info: NotifyInfo,
87-
) => void;
83+
onStoreChange: (store: Store, namePathList: InternalNamePath[] | null, info: NotifyInfo) => void;
8884
isFieldTouched: () => boolean;
8985
isFieldValidating: () => boolean;
9086
validateRules: (options?: ValidateOptions) => Promise<string[]>;
@@ -158,10 +154,7 @@ export interface InternalHooks {
158154
export interface FormInstance {
159155
// Origin Form API
160156
getFieldValue: (name: NamePath) => StoreValue;
161-
getFieldsValue: (
162-
nameList?: NamePath[],
163-
filterFunc?: (meta: Meta) => boolean,
164-
) => Store;
157+
getFieldsValue: (nameList?: NamePath[] | true, filterFunc?: (meta: Meta) => boolean) => Store;
165158
getFieldError: (name: NamePath) => string[];
166159
getFieldsError: (nameList?: NamePath[]) => FieldError[];
167160
isFieldsTouched(nameList?: NamePath[], allFieldsTouched?: boolean): boolean;

src/useForm.ts

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ import {
3232
setValues,
3333
} from './utils/valueUtil';
3434

35+
type InvalidateFieldEntity = { INVALIDATE_NAME_PATH: InternalNamePath };
36+
3537
interface UpdateAction {
3638
type: 'updateValue';
3739
namePath: InternalNamePath;
@@ -162,38 +164,42 @@ export class FormStore {
162164
return cache;
163165
};
164166

165-
private getFieldEntitiesForNamePathList = (nameList?: NamePath[]) => {
167+
private getFieldEntitiesForNamePathList = (
168+
nameList?: NamePath[],
169+
): (FieldEntity | InvalidateFieldEntity)[] => {
166170
if (!nameList) {
167171
return this.getFieldEntities(true);
168172
}
169173
const cache = this.getFieldsMap(true);
170174
return nameList.map(name => {
171175
const namePath = getNamePath(name);
172-
return cache.get(namePath);
176+
return cache.get(namePath) || { INVALIDATE_NAME_PATH: getNamePath(name) };
173177
});
174178
};
175179

176-
private getFieldsValue = (
177-
nameList?: NamePath[],
178-
filterFunc?: (meta: Meta) => boolean,
179-
) => {
180+
private getFieldsValue = (nameList?: NamePath[] | true, filterFunc?: (meta: Meta) => boolean) => {
180181
this.warningUnhooked();
181182

182-
if (!nameList && !filterFunc) {
183+
if (nameList === true && !filterFunc) {
183184
return this.store;
184185
}
185-
if (!filterFunc) {
186-
return cloneByNamePathList(this.store, nameList.map(getNamePath));
187-
}
188186

189-
const fieldEntities = this.getFieldEntitiesForNamePathList(nameList);
187+
const fieldEntities = this.getFieldEntitiesForNamePathList(
188+
Array.isArray(nameList) ? nameList : null,
189+
);
190190

191191
const filteredNameList: NamePath[] = [];
192-
fieldEntities.forEach((field: FieldEntity) => {
193-
const namePath = field.getNamePath();
194-
const meta = field.getMeta();
195-
if (filterFunc(meta)) {
192+
fieldEntities.forEach((entity: FieldEntity | InvalidateFieldEntity) => {
193+
const namePath =
194+
'INVALIDATE_NAME_PATH' in entity ? entity.INVALIDATE_NAME_PATH : entity.getNamePath();
195+
196+
if (!filterFunc) {
196197
filteredNameList.push(namePath);
198+
} else {
199+
const meta: Meta = 'getMeta' in entity ? entity.getMeta() : null;
200+
if (filterFunc(meta)) {
201+
filteredNameList.push(namePath);
202+
}
197203
}
198204
});
199205

@@ -213,7 +219,7 @@ export class FormStore {
213219
const fieldEntities = this.getFieldEntitiesForNamePathList(nameList);
214220

215221
return fieldEntities.map((entity, index) => {
216-
if (entity) {
222+
if (entity && !('INVALIDATE_NAME_PATH' in entity)) {
217223
return {
218224
name: entity.getNamePath(),
219225
errors: entity.getErrors(),

0 commit comments

Comments
 (0)