Skip to content

Commit 0b1ce8c

Browse files
committed
feat: update the comments in English to enhance the readability of the code
1 parent df49f0e commit 0b1ce8c

File tree

7 files changed

+36
-36
lines changed

7 files changed

+36
-36
lines changed

primitives/filed-form /src/form-core/types.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export interface Rule {
2222
validateTrigger?: ValidateTrigger | ValidateTrigger[];
2323
validator?: (value: any, allValues: Store) => string | Promise<string>;
2424
warningOnly?: boolean;
25-
whitespace?: boolean; // true 时,空白字符串视为“无值”
25+
whitespace?: boolean; // When true, whitespace-only strings are treated as "empty"
2626
}
2727

2828
export interface FieldEntity {
@@ -62,8 +62,8 @@ export interface Callbacks<Values = any> {
6262

6363
export type ChangeTag =
6464
| 'errors'
65-
| 'meta' // touched/dirty 变化
66-
| 'mount' // 校验进行中状态变化
67-
| 'unmount' // 错误/警告变化
65+
| 'meta' // touched/dirty changes
66+
| 'mount' // validating state changes
67+
| 'unmount' // error/warning changes
6868
| 'validating'
69-
| 'value'; // 字段注册/注销
69+
| 'value'; // field register/unregister

primitives/filed-form /src/react/components/ComputedField.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function ComputedField<Values = any>({
4141
useEffect(() => {
4242
const unregister = registerComputed(name, deps, compute);
4343

44-
// 订阅当前字段的变化 → 强制刷新
44+
// Subscribe to current field changes → force refresh
4545
const unsub = registerField({
4646
changeValue: newValue => {
4747
updateValue(newValue);

primitives/filed-form /src/react/components/List.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ function List<Values = any>(props: ListProps<Values>) {
6161
key = keyManager.keys[i];
6262
}
6363
return {
64-
key: String(key), // 稳定 key
64+
key: String(key), // Stable key
6565
name: `${keyOfName(name)}.${i}`
6666
};
6767
});

primitives/filed-form /src/react/hooks/FieldContext.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import type { Meta } from '../../types/shared-types';
1313

1414
import type { FormState } from './types';
1515

16-
// 核心改造:给每条路径套上 Meta
16+
// Core change: wrap each path with Meta
1717
type BuildMetaShape<T, P extends string> = P extends `${infer K}.${infer R}`
1818
? K extends keyof T
1919
? T[K] extends readonly (infer U)[]
@@ -26,7 +26,7 @@ type BuildMetaShape<T, P extends string> = P extends `${infer K}.${infer R}`
2626
: never
2727
: never;
2828

29-
// 把路径数组转成 Meta 层级结构
29+
// Convert an array of paths into a hierarchical Meta structure
3030
export type MetaShapeFromPaths<T, Ps extends readonly string[]> = Ps extends never[] | []
3131
? { [K in keyof T]: Meta<K & string, PathToDeepType<T, K & string>> }
3232
: MergeUnion<Ps[number] extends infer P ? (P extends string ? BuildMetaShape<T, P> : never) : never>;
@@ -67,13 +67,13 @@ export interface OperationOptions<Values = any> {
6767
}
6868

6969
export interface ValidateErrorEntity<Values = any> {
70-
// 便于 UI 直接滚动
71-
errorCount: number; // 可选:按 _pruneForSubmit 过滤后的值(给埋点/回放)
72-
errorFields: Meta<string, any>[]; // 逐字段列表(用于滚动到第一个错误、逐项显示)
73-
errorMap: Record<string, string[]>; // 同上:警告
74-
firstErrorName?: string; // 全量当前值(未裁剪)
70+
// For UI to scroll directly
71+
errorCount: number; // Optional: values filtered by _pruneForSubmit (for analytics/replay)
72+
errorFields: Meta<string, any>[]; // Per-field list (used to scroll to the first error and show items)
73+
errorMap: Record<string, string[]>; // Same as above: warnings
74+
firstErrorName?: string; // Full current values (not pruned)
7575
submittedAt: number;
76-
values: Values; // 快速索引(表头红点、侧边分组统计)
76+
values: Values; // Fast index (header red dot, side group statistics)
7777
warningMap: Record<string, string[]>;
7878
}
7979

@@ -128,7 +128,7 @@ export interface InternalFormContext<Values = any> extends FormInstance<Values>
128128
}
129129

130130
export interface InternalFormInstance<Values = any> extends InternalFormContext<Values> {
131-
/** 内部 API,不建议外部使用 */
131+
/** Internal API, not recommended for external use */
132132
getInternalHooks: () => InternalFormHooks<Values>;
133133
}
134134

primitives/filed-form /src/react/hooks/useFieldState.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ function useFieldState<Values = any, T extends AllPathsKeys<Values> = AllPathsKe
3030
opts?: UseFormFieldsStateOpts<Values>
3131
): MetaShapeFromPaths<Values, T[]>;
3232

33-
// 无参数:全量嵌套对象
33+
// No arguments: return the full nested object
3434
function useFieldState<Values = any>(): MetaShapeFromPaths<Values, []>;
3535

36-
// form 参数:全量嵌套对象
36+
// With a form instance: return the full nested object
3737
function useFieldState<Values = any>(form: FormInstance<Values>): MetaShapeFromPaths<Values, []>;
3838

3939
function useFieldState<Values = any>(
@@ -44,7 +44,7 @@ function useFieldState<Values = any>(
4444

4545
const isFormInstance = isObject(names) && 'getFields' in names;
4646

47-
const form = isFormInstance ? names : (opts?.form ?? context); // 优先外部传入,否则走 context
47+
const form = isFormInstance ? names : (opts?.form ?? context); // Prefer external form, otherwise use context
4848

4949
if (!form) {
5050
throw new Error('Can not find FormContext. Please make sure you wrap Field under Form or provide a form instance.');
@@ -53,16 +53,16 @@ function useFieldState<Values = any>(
5353
let subscribeNames: AllPathsKeys<Values>[] | undefined;
5454

5555
if (isFormInstance) {
56-
// 外部传 form → 订阅全部
56+
// External form provided → subscribe to all
5757
subscribeNames = undefined;
5858
} else if (isNil(names)) {
59-
// 未传 names → 订阅全部
59+
// No names provided → subscribe to all
6060
subscribeNames = undefined;
6161
} else if (isArray(names)) {
62-
// 多字段
62+
// Multiple fields
6363
subscribeNames = names;
6464
} else {
65-
// 单字段
65+
// Single field
6666
subscribeNames = [names];
6767
}
6868

@@ -99,14 +99,14 @@ function useFieldState<Values = any>(
9999
}, []);
100100

101101
if (!subscribeNames) {
102-
// names 为空返回 Map 形式
102+
// names emptyreturn Map shape
103103
return state;
104104
}
105105
if (subscribeNames.length === 1) {
106-
// 单字段 → 直接返回该字段的 meta
106+
// Single field → return that field's meta directly
107107
return get(state, subscribeNames[0]);
108108
}
109-
// 多字段 → 返回对象
109+
// Multiple fields → return object
110110
return state;
111111
}
112112

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export function unset<T>(obj: T, path: NamePath, options: SetOptions = { safeKey
7575
if (Array.isArray(node)) {
7676
const arr = node.slice();
7777
if (isLast) {
78-
arr.splice(key as number, 1); // 删除该元素并前移
78+
arr.splice(key as number, 1); // Remove the element and shift left
7979
return arr;
8080
}
8181
arr[key as number] = unsetImpl(arr[key as number], i + 1);
@@ -87,7 +87,7 @@ export function unset<T>(obj: T, path: NamePath, options: SetOptions = { safeKey
8787
for (const k in node as any) {
8888
if (Object.hasOwn(node, k)) {
8989
if (isLast && k === key) {
90-
// 跳过该 key,不写入 out
90+
// Skip this key; do not write to out
9191
continue;
9292
}
9393
out[k] = k === key ? unsetImpl((node as any)[k], i + 1) : (node as any)[k];

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,28 +73,28 @@ export const anyOn = (set: Set<string>, names?: NamePath[]) =>
7373
export const allOn = (set: Set<string>, names?: NamePath[]) =>
7474
!names || names.length === 0 ? set.size > 0 : names.every(n => set.has(keyOfName(n)));
7575

76-
// ✅ 递归收集变更路径(会把数组也往里走)
76+
// ✅ Recursively collect changed paths (descend into arrays as well)
7777
export const collectChangedLeafPaths = (
7878
input: any,
7979
prefix: (string | number)[] = [],
8080
out: (string | number)[][] = []
8181
) => {
8282
if (Array.isArray(input)) {
83-
// 数组节点本身也视为变更(用于 List 层级)
83+
// Array nodes themselves are also treated as changes (used by List level)
8484
out.push([...prefix]);
8585
input.forEach((item, i) => collectChangedLeafPaths(item, [...prefix, keyOfName(i)], out));
8686
} else if (input && typeof input === 'object') {
8787
Object.keys(input).forEach(k => {
8888
collectChangedLeafPaths(input[keyOfName(k)], [...prefix, keyOfName(k)], out);
8989
});
9090
} else {
91-
// 原子值,叶子
91+
// Atomic value, leaf
9292
out.push([...prefix]);
9393
}
9494
return out;
9595
};
9696

97-
// ✅ 如果想覆盖“删除/缩短数组”的场景(通知旧叶子),可以把旧值的叶子也并上
97+
// ✅ To cover cases like array deletion/shortening (to notify old leaves), union with the old value's leaves as well
9898
export const unionPaths = (a: (string | number)[][], b: (string | number)[][]) => {
9999
const s = new Set<string>();
100100
const res: (string | number)[][] = [];
@@ -123,19 +123,19 @@ export const isUnderPrefix = (key: string, prefix: string): boolean => {
123123

124124
export function collectDeepKeys(obj: any, prefix: string = ''): string[] {
125125
if (obj === null || obj === undefined) {
126-
// 叶子节点(值是 null/undefined
126+
// Leaf node (value is null/undefined)
127127
return [prefix];
128128
}
129129

130130
if (typeof obj !== 'object' || obj instanceof Date) {
131-
// 基础值(string/number/boolean/function/Date...
131+
// Primitive value (string/number/boolean/function/Date...)
132132
return [prefix];
133133
}
134134

135-
// 对象/数组:即使值是 undefined/null,也要保留路径
135+
// Object/Array: keep the path even if value is undefined/null
136136
const keys: string[] = [];
137137

138-
// 如果是空对象/数组,也要把自己 push 出来
138+
// If empty object/array, also push itself
139139
if (Object.keys(obj).length === 0) {
140140
keys.push(prefix);
141141
return keys;

0 commit comments

Comments
 (0)