Skip to content

Commit 92ec5cc

Browse files
committed
feat: rename the "updateValue" in diapatch to "setFieldValue"
1 parent f458f0e commit 92ec5cc

File tree

4 files changed

+67
-45
lines changed

4 files changed

+67
-45
lines changed

primitives/filed-form /src/Field.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ function Field<Values = any>(props: InternalFieldProps<Values>) {
9999
if (newValue !== oldValue) {
100100
dispatch({
101101
name,
102-
type: 'updateValue',
102+
type: 'setFieldValue',
103103
value: newValue
104104
});
105105
}

primitives/filed-form /src/FieldContext.ts

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,19 @@ export interface InternalCallbacks<Values = any> {
8282

8383
export interface InternalFieldHooks<Values = any> {
8484
dispatch: (action: Action) => void;
85-
subscribeField: (name: AllPaths<Values>, cb: () => void, opt?: { includeChildren?: boolean; mask?: ChangeMask }) => () => void;
86-
subscribeFields: (names: AllPaths<Values>[], cb: () => void, opt?: { includeChildren?: boolean; mask?: ChangeMask }) => () => void;
8785
getInitialValue: <T extends AllPaths<Values>>(name: T) => PathToDeepType<Values, T>;
8886
registerField: (entity: FieldEntity) => () => void;
8987
setFieldRules: (name: AllPaths<Values>, rules?: Rule[]) => void;
88+
subscribeField: (
89+
name: AllPaths<Values>,
90+
cb: () => void,
91+
opt?: { includeChildren?: boolean; mask?: ChangeMask }
92+
) => () => void;
93+
subscribeFields: (
94+
names: AllPaths<Values>[],
95+
cb: () => void,
96+
opt?: { includeChildren?: boolean; mask?: ChangeMask }
97+
) => () => void;
9098
}
9199

92100
export interface FormInstance<Values = any>
@@ -168,8 +176,8 @@ export const useFieldsState = <Values = any>(
168176
form: FormInstance<Values>,
169177
names: AllPaths<Values>[],
170178
opts?: {
171-
mask?: SubscribeMaskOptions;
172179
includeChildren?: boolean;
180+
mask?: SubscribeMaskOptions;
173181
}
174182
) => {
175183
const state = form.getFields(names);
@@ -178,13 +186,16 @@ export const useFieldsState = <Values = any>(
178186

179187
const { subscribeFields } = getInternalHooks();
180188

181-
const { mask = {
182-
errors: true,
183-
touched: true,
184-
validated: true,
185-
validating: true,
186-
warnings: true
187-
}, includeChildren } = opts || {};
189+
const {
190+
includeChildren,
191+
mask = {
192+
errors: true,
193+
touched: true,
194+
validated: true,
195+
validating: true,
196+
warnings: true
197+
}
198+
} = opts || {};
188199

189200
// eslint-disable-next-line react/hook-use-state
190201
const [_, forceUpdate] = useState({});
@@ -198,7 +209,7 @@ export const useFieldsState = <Values = any>(
198209
});
199210
},
200211
{
201-
includeChildren: includeChildren,
212+
includeChildren,
202213
mask: toMask(mask)
203214
}
204215
);
@@ -257,7 +268,7 @@ function useWatch<Values = any>(
257268

258269
const nameIsArray = isArray(names) || !names;
259270

260-
useFieldsState<Values>(form, namesArray, {mask: {value: true}, includeChildren: includeChildren || !names });
271+
useFieldsState<Values>(form, namesArray, { includeChildren: includeChildren || !names, mask: { value: true } });
261272

262273
return nameIsArray ? form.getFieldsValue(namesArray) : form.getFieldValue(names);
263274
}

primitives/filed-form /src/createStore.ts

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ class FormStore {
140140
// ===== dispatch =====
141141
private baseDispatch = (a: Action) => {
142142
switch (a.type) {
143-
case 'updateValue':
144-
this.updateValue(a.name, a.value, { validate: a.validate });
143+
case 'setFieldValue':
144+
this.setFieldValue(a.name, a.value, a.validate);
145145
break;
146146
case 'setFieldsValue':
147147
this.setFieldsValue(a.values, a.validate);
@@ -433,7 +433,7 @@ class FormStore {
433433
const nextVal = def.compute(getKey, this._store);
434434
const prevVal = getKey(k);
435435
if (!isEqual(prevVal, nextVal)) {
436-
this.updateValue(keyOfName(k), nextVal, { markTouched: false }); // 不标记 touched
436+
this.setFieldValue(keyOfName(k), nextVal);
437437
}
438438
}
439439
});
@@ -504,54 +504,64 @@ class FormStore {
504504
};
505505

506506
private setFieldValue = (name: NamePath, value: StoreValue, validate = false) => {
507-
this.dispatch({ name, type: 'updateValue', validate, value });
508-
};
509-
510-
private updateValue = (
511-
name: NamePath,
512-
value: StoreValue,
513-
{ markTouched = true, validate = false }: { markTouched?: boolean; validate?: boolean } = {}
514-
) => {
515507
const key = keyOfName(name);
516-
517508
const before = get(this._store, key);
518509

519-
if (isEqual(before, value)) return; // value not changed, do not trigger
520-
521-
this.updateStore(set(this._store, name, value));
522-
523-
this._validated.delete(key);
510+
if (isEqual(before, value)) return; // no change
524511

525-
// touched
526-
let mask = ChangeTag.Value;
527-
528-
if (markTouched && !this._touched.has(key)) {
529-
this._touched.add(key);
530-
mask |= ChangeTag.Touched;
531-
}
532-
// dirty: compare with initial
533512
const initV = this.getInitialValue(key);
534513

535-
isEqual(value, initV) ? this._dirty.delete(key) : this._dirty.add(key);
514+
// 1. 更新 store
515+
this.updateStore(set(this._store, name, value));
536516

537-
mask |= ChangeTag.Dirty;
517+
// 2. 更新元状态(dirty/touched/validated)
518+
const mask = this.updateMetaState(key, value, initV);
538519

520+
// 3. 通知订阅者
539521
this.enqueueNotify([name], mask);
540522

541-
// callback
523+
// 4. 执行回调
542524
this._callbacks.onValuesChange?.(set({}, key, value), this._store);
543-
544525
this.triggerOnFieldsChange([key]);
545526

527+
// 5. 触发依赖计算
546528
const affected = this.collectDependents([key]);
547-
548529
this.recomputeTargets(affected);
549530

531+
// 6. 是否立即校验
550532
if (validate) {
551533
this.dispatch({ name, type: 'validateField' });
552534
}
553535
};
554536

537+
private updateMetaState = (key: string, value: StoreValue, initV: StoreValue): ChangeMask => {
538+
let mask = ChangeTag.Value;
539+
540+
// touched
541+
if (!this._touched.has(key)) {
542+
this._touched.add(key);
543+
mask |= ChangeTag.Touched;
544+
}
545+
546+
// dirty
547+
const wasDirty = this._dirty.has(key);
548+
const isDirty = !isEqual(value, initV);
549+
if (isDirty && !wasDirty) {
550+
this._dirty.add(key);
551+
mask |= ChangeTag.Dirty;
552+
} else if (!isDirty && wasDirty) {
553+
this._dirty.delete(key);
554+
mask |= ChangeTag.Dirty;
555+
}
556+
557+
// validated 一旦值变了 → 作废
558+
if (this._validated.has(key)) {
559+
this._validated.delete(key);
560+
}
561+
562+
return mask;
563+
};
564+
555565
// ------------------------------------------------
556566
// Fields State (errors/touched/validating/warnings, selectors)
557567
// ------------------------------------------------
@@ -1121,7 +1131,8 @@ class FormStore {
11211131
isFieldValidating: this.isFieldValidating,
11221132
resetFields: (names: NonNullable<NamePath>[] = []) => this.dispatch({ names, type: 'reset' }),
11231133
setFieldsValue: (values: Store, validate = false) => this.dispatch({ type: 'setFieldsValue', validate, values }),
1124-
setFieldValue: this.setFieldValue,
1134+
setFieldValue: (name: NamePath, value: StoreValue, validate = false) =>
1135+
this.dispatch({ name, type: 'setFieldValue', validate, value }),
11251136
submit: this.submit,
11261137
use: this.use,
11271138
validateField: (name: NamePath, opts?: ValidateOptions) => this.dispatch({ name, opts, type: 'validateField' }),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export interface ValidateFieldsOptions extends ValidateOptions {
88
}
99

1010
export type Action =
11-
| { name: NamePath; type: 'updateValue'; validate?: boolean; value: StoreValue }
11+
| { name: NamePath; type: 'setFieldValue'; validate?: boolean; value: StoreValue }
1212
| { type: 'setFieldsValue'; validate?: boolean; values: Store }
1313
| { names?: NonNullable<NamePath>[]; type: 'reset' }
1414
| {

0 commit comments

Comments
 (0)