Skip to content

Commit eb1f0ab

Browse files
committed
feat: function for adding collection sub-paths
1 parent c082b96 commit eb1f0ab

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

primitives/filed-form /src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ export { default as List } from './List';
1010

1111
export type { InternalFieldProps } from './types/field';
1212

13+
export type { FormInstance } from './FieldContext';
14+
1315
export { default as useForm } from './useForm';

primitives/filed-form /src/utils/util.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,31 @@ export const isUnderPrefix = (key: string, prefix: string) => {
118118
if (key === prefix) return true; // 自身节点
119119
return key.startsWith(`${prefix}.`); // 子节点(点分隔)
120120
};
121+
122+
export function collectDeepKeys(obj: any, prefix: string = ''): string[] {
123+
if (obj === null || obj === undefined) {
124+
// 叶子节点(值是 null/undefined)
125+
return [prefix];
126+
}
127+
128+
if (typeof obj !== 'object' || obj instanceof Date) {
129+
// 基础值(string/number/boolean/function/Date...)
130+
return [prefix];
131+
}
132+
133+
// 对象/数组:即使值是 undefined/null,也要保留路径
134+
const keys: string[] = [];
135+
136+
// 如果是空对象/数组,也要把自己 push 出来
137+
if (Object.keys(obj).length === 0) {
138+
keys.push(prefix);
139+
return keys;
140+
}
141+
142+
for (const k of Object.keys(obj)) {
143+
const path = prefix ? `${prefix}.${k}` : k;
144+
keys.push(...collectDeepKeys(obj[k], path));
145+
}
146+
147+
return keys;
148+
}

0 commit comments

Comments
 (0)