Skip to content

Commit 2d791fb

Browse files
committed
fix: getFieldsValue logic
1 parent 4e063b7 commit 2d791fb

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

src/hooks/useForm.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,11 @@ export class FormStore {
253253
return cache;
254254
};
255255

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+
*/
256261
private getFieldEntitiesForNamePathList = (
257262
nameList?: NamePath[],
258263
includesSubNamePath = false,
@@ -268,6 +273,16 @@ export class FormStore {
268273
return cache.get(namePath) || { INVALIDATE_NAME_PATH: getNamePath(name) };
269274
});
270275
}
276+
277+
return nameList.flatMap(name => {
278+
const namePath = getNamePath(name);
279+
const fields: FlexibleFieldEntity[] = cache.getAsPrefix(namePath);
280+
281+
if (fields.length) {
282+
return fields;
283+
}
284+
return [{ INVALIDATE_NAME_PATH: namePath }];
285+
});
271286
};
272287

273288
private getFieldsValue = (
@@ -293,6 +308,7 @@ export class FormStore {
293308

294309
const fieldEntities = this.getFieldEntitiesForNamePathList(
295310
Array.isArray(mergedNameList) ? mergedNameList : null,
311+
true,
296312
);
297313

298314
const filteredNameList: NamePath[] = [];

src/utils/NameMap.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,22 @@ class NameMap<T> {
3434
}
3535

3636
public getAsPrefix(key: InternalNamePath): T[] {
37-
// TODO: 实现前缀获取,如果 InternalNamePath 满足前缀则返回所有包含本身的集合
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;
3853
}
3954

4055
public update(key: InternalNamePath, updater: (origin: T) => T | null) {

0 commit comments

Comments
 (0)