Skip to content

Commit 07da421

Browse files
authored
fix: getFieldsValue can not get fully store (#774)
* test: test driven * chore: todo * fix: getFieldsValue logic * test: update test case * chore: clean up * chore: clean up * chore: clean up * chore: clean up
1 parent 31ef0b7 commit 07da421

File tree

3 files changed

+68
-3
lines changed

3 files changed

+68
-3
lines changed

src/hooks/useForm.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,11 @@ export class FormStore {
239239
return this.fieldEntities.filter(field => field.getNamePath().length);
240240
};
241241

242+
/**
243+
* Get a map of registered field entities with their name path as the key.
244+
* @param pure Only include fields which have a `name`. Default: false
245+
* @returns A NameMap containing field entities indexed by their name paths
246+
*/
242247
private getFieldsMap = (pure: boolean = false) => {
243248
const cache: NameMap<FieldEntity> = new NameMap();
244249
this.getFieldEntities(pure).forEach(field => {
@@ -248,14 +253,35 @@ export class FormStore {
248253
return cache;
249254
};
250255

251-
private getFieldEntitiesForNamePathList = (nameList?: NamePath[]): FlexibleFieldEntity[] => {
256+
/**
257+
* Get field entities based on a list of name paths.
258+
* @param nameList - Array of name paths to search for. If not provided, returns all field entities with names.
259+
* @param includesSubNamePath - Whether to include fields that have the given name path as a prefix.
260+
*/
261+
private getFieldEntitiesForNamePathList = (
262+
nameList?: NamePath[],
263+
includesSubNamePath = false,
264+
): FlexibleFieldEntity[] => {
252265
if (!nameList) {
253266
return this.getFieldEntities(true);
254267
}
255268
const cache = this.getFieldsMap(true);
256-
return nameList.map(name => {
269+
270+
if (!includesSubNamePath) {
271+
return nameList.map(name => {
272+
const namePath = getNamePath(name);
273+
return cache.get(namePath) || { INVALIDATE_NAME_PATH: getNamePath(name) };
274+
});
275+
}
276+
277+
return nameList.flatMap(name => {
257278
const namePath = getNamePath(name);
258-
return cache.get(namePath) || { INVALIDATE_NAME_PATH: getNamePath(name) };
279+
const fields: FlexibleFieldEntity[] = cache.getAsPrefix(namePath);
280+
281+
if (fields.length) {
282+
return fields;
283+
}
284+
return [{ INVALIDATE_NAME_PATH: namePath }];
259285
});
260286
};
261287

@@ -282,6 +308,7 @@ export class FormStore {
282308

283309
const fieldEntities = this.getFieldEntitiesForNamePathList(
284310
Array.isArray(mergedNameList) ? mergedNameList : null,
311+
true,
285312
);
286313

287314
const filteredNameList: NamePath[] = [];

src/utils/NameMap.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,25 @@ class NameMap<T> {
3333
return this.kvs.get(normalize(key));
3434
}
3535

36+
public getAsPrefix(key: InternalNamePath): T[] {
37+
const normalizedKey = normalize(key);
38+
const normalizedPrefix = normalizedKey + SPLIT;
39+
const results: T[] = [];
40+
41+
const current = this.kvs.get(normalizedKey);
42+
if (current !== undefined) {
43+
results.push(current);
44+
}
45+
46+
this.kvs.forEach((value, itemNormalizedKey) => {
47+
if (itemNormalizedKey.startsWith(normalizedPrefix)) {
48+
results.push(value);
49+
}
50+
});
51+
52+
return results;
53+
}
54+
3655
public update(key: InternalNamePath, updater: (origin: T) => T | null) {
3756
const origin = this.get(key);
3857
const next = updater(origin);

tests/list.test.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,4 +1171,23 @@ describe('Form.List', () => {
11711171
list: [{ name: 'A' }, { name: 'BB' }, { name: 'C' }, { name: 'D' }],
11721172
});
11731173
});
1174+
1175+
it('getFieldsValue(["list"]) should same as getFieldsValue().list', async () => {
1176+
generateForm(
1177+
fields =>
1178+
fields.map(field => (
1179+
<Field {...fields} name={[field.name, 'name']} key={field.key}>
1180+
<Input />
1181+
</Field>
1182+
)),
1183+
{
1184+
initialValues: {
1185+
list: [{ name: 'bamboo', notExist: 'little' }],
1186+
},
1187+
},
1188+
);
1189+
1190+
expect(form.current!.getFieldsValue()).toEqual({ list: [{ name: 'bamboo' }] });
1191+
expect(form.current!.getFieldsValue(['list'])).toEqual({ list: [{ name: 'bamboo' }] });
1192+
});
11741193
});

0 commit comments

Comments
 (0)