Skip to content

Commit 16f89cf

Browse files
committed
feat: standardize the style of obtaining status-related information
1 parent a6363a2 commit 16f89cf

File tree

2 files changed

+56
-80
lines changed

2 files changed

+56
-80
lines changed

primitives/filed-form /src/FieldContext.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ export interface StateOptions<Values = any> {
2727
getFieldError: (name: AllPaths<Values>) => string[];
2828
getFields: (names?: AllPaths<Values>[]) => Meta<AllPaths<Values>, PathToDeepType<Values, AllPaths<Values>>>[];
2929
getFieldsError: (...name: AllPaths<Values>[]) => Record<AllPaths<Values>, string[]>;
30+
getFieldsTouched: (...name: AllPaths<Values>[]) => boolean;
31+
getFieldsValidated: (...name: AllPaths<Values>[]) => boolean;
32+
getFieldsValidating: (...name: AllPaths<Values>[]) => boolean;
3033
getFieldsWarning: (...name: AllPaths<Values>[]) => Record<AllPaths<Values>, string[]>;
34+
getFieldTouched: (name: AllPaths<Values>) => boolean;
35+
getFieldValidated: (name: AllPaths<Values>) => boolean;
36+
getFieldValidating: (name: AllPaths<Values>) => boolean;
3137
getFieldWarning: (name: AllPaths<Values>) => string[];
3238
getFormState: () => FormState;
33-
isFieldsTouched: (...name: AllPaths<Values>[]) => boolean;
34-
isFieldsValidating: (...name: AllPaths<Values>[]) => boolean;
35-
isFieldTouched: (name: AllPaths<Values>) => boolean;
36-
isFieldValidating: (name: AllPaths<Values>) => boolean;
3739
}
3840

3941
export interface ValidateFieldsOptions extends ValidateOptions {

primitives/filed-form /src/createStore.ts

Lines changed: 50 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,32 @@ const matchTrigger = (rule: Rule, trig?: string | string[]) => {
3333
return trigList.some(t => list.includes(t));
3434
};
3535

36+
function getValueByNames<T>(source: Map<string, T>, names?: NamePath[]): T | Record<string, T> | undefined {
37+
if (!names || names.length === 0) {
38+
return Object.fromEntries(source) as Record<string, T>;
39+
}
40+
41+
if (names.length === 1) {
42+
return source.get(keyOfName(names[0]));
43+
}
44+
45+
const out: Record<string, T> = {};
46+
for (const n of names) {
47+
const k = keyOfName(n);
48+
const v = source.get(k);
49+
if (v !== undefined) out[k] = v;
50+
}
51+
return out;
52+
}
53+
54+
function getFlag(bucket: Set<string>, names?: NamePath[]) {
55+
if (!names || names.length === 0) {
56+
return bucket.size > 0;
57+
}
58+
59+
return anyOn(bucket, names);
60+
}
61+
3662
class FormStore {
3763
// ------------------------------------------------
3864
// Fields (State & registries)
@@ -216,18 +242,6 @@ class FormStore {
216242
};
217243
}
218244

219-
private getInitialValues = (...namePath: NonNullable<NamePath>[]) => {
220-
if (namePath.length === 0) {
221-
return this._initial;
222-
}
223-
224-
return namePath.reduce((acc, name) => {
225-
acc[name as string] = get(this._initial, name);
226-
227-
return acc;
228-
}, {} as Store);
229-
};
230-
231245
private getInitialValue = (name: NamePath) => {
232246
return get(this._initial, keyOfName(name));
233247
};
@@ -504,6 +518,7 @@ class FormStore {
504518

505519
private setFieldValue = (name: NamePath, value: StoreValue, validate = false) => {
506520
const key = keyOfName(name);
521+
507522
const before = get(this._store, key);
508523

509524
if (isEqual(before, value)) return; // no change
@@ -576,83 +591,40 @@ class FormStore {
576591
const key = keyOfName(name);
577592

578593
return {
579-
errors: this.getFieldError(key) || [],
594+
errors: this.getFieldError(key),
580595
name: key,
581-
touched: this.isFieldTouched(name),
582-
validated: this._validated.has(key),
583-
validating: this.isFieldValidating(key),
596+
touched: this.getFieldTouched(key),
597+
validated: this.getFieldValidated(key),
598+
validating: this.getFieldValidating(key),
584599
value: this.getFieldValue(key),
585-
warnings: this.getFieldWarning(key) || []
600+
warnings: this.getFieldWarning(key)
586601
};
587602
};
588603

589604
// ===== FieldError =====
590-
private getFieldError = (name: NamePath) => {
591-
return this._errors.get(keyOfName(name)) || [];
592-
};
593-
594-
private getFieldsError = (...nameList: NamePath[]) => {
595-
if (nameList.length === 0) {
596-
return Object.fromEntries(this._errors);
597-
}
605+
private getFieldError = (name: NamePath) => this._errors.get(keyOfName(name)) || [];
598606

599-
const nameListArray = toArray(nameList);
600-
601-
return nameListArray.reduce(
602-
(acc, name) => {
603-
const key = keyOfName(name);
604-
acc[key] = this._errors.get(key) || [];
605-
606-
return acc;
607-
},
608-
{} as Record<string, string[]>
609-
);
610-
};
607+
private getFieldsError = (names: NamePath[]) => getValueByNames(this._errors, names);
611608

612609
// ===== FieldWarning =====
613-
private getFieldWarning = (name: NamePath) => {
614-
return this._warnings.get(keyOfName(name)) || [];
615-
};
616-
617-
private getFieldsWarning = (...nameList: NonNullable<NamePath>[]) => {
618-
if (nameList.length === 0) {
619-
return Object.fromEntries(this._warnings);
620-
}
610+
private getFieldWarning = (name: NamePath) => this._warnings.get(keyOfName(name)) || [];
621611

622-
return nameList.reduce(
623-
(acc, name) => {
624-
acc[name as string] = this._warnings.get(keyOfName(name)) || [];
625-
626-
return acc;
627-
},
628-
{} as Record<string, string[]>
629-
);
630-
};
612+
private getFieldsWarning = (names: NonNullable<NamePath>[]) => getValueByNames(this._warnings, names);
631613

632614
// ===== FieldValidating =====
633-
private isFieldsValidating = (...nameList: NonNullable<NamePath>[]) => {
634-
if (nameList.length === 0) {
635-
return this._validating.size > 0;
636-
}
615+
private getFieldsValidating = (names: NonNullable<NamePath>[]) => getFlag(this._validating, names);
637616

638-
return anyOn(this._validating, nameList);
639-
};
617+
private getFieldValidating = (name: NamePath) => isOn(this._validating, name);
640618

641-
private isFieldValidating = (name: NamePath) => {
642-
return isOn(this._validating, name);
643-
};
619+
// ===== FieldValidated =====
620+
private getFieldsValidated = (names: NonNullable<NamePath>[]) => getFlag(this._validated, names);
621+
622+
private getFieldValidated = (name: NamePath) => isOn(this._validated, name);
644623

645624
// ===== FieldTouched =====
646-
private isFieldsTouched = (...nameList: NonNullable<NamePath>[]) => {
647-
if (nameList.length === 0) {
648-
return this._touched.size > 0;
649-
}
650-
return anyOn(this._touched, nameList);
651-
};
625+
private getFieldsTouched = (names: NonNullable<NamePath>[]) => getFlag(this._touched, names);
652626

653-
private isFieldTouched = (name: NamePath) => {
654-
return isOn(this._touched, name);
655-
};
627+
private getFieldTouched = (name: NamePath) => isOn(this._touched, name);
656628

657629
// ===== Rules =====
658630
private setFieldRules = (name: NamePath, rules?: Rule[]) => {
@@ -1121,16 +1093,18 @@ class FormStore {
11211093
getFieldError: this.getFieldError,
11221094
getFields: this.getFields,
11231095
getFieldsError: this.getFieldsError,
1096+
getFieldsTouched: this.getFieldsTouched,
1097+
getFieldsValidated: this.getFieldsValidated,
1098+
getFieldsValidating: this.getFieldsValidating,
11241099
getFieldsValue: this.getFieldsValue,
11251100
getFieldsWarning: this.getFieldsWarning,
1101+
getFieldTouched: this.getFieldTouched,
1102+
getFieldValidated: this.getFieldValidated,
1103+
getFieldValidating: this.getFieldValidating,
11261104
getFieldValue: this.getFieldValue,
11271105
getFieldWarning: this.getFieldWarning,
11281106
getFormState: this.getFormState,
11291107
getInternalHooks: this.getInternalHooks,
1130-
isFieldsTouched: this.isFieldsTouched,
1131-
isFieldsValidating: this.isFieldsValidating,
1132-
isFieldTouched: this.isFieldTouched,
1133-
isFieldValidating: this.isFieldValidating,
11341108
resetFields: (names: NonNullable<NamePath>[] = []) => this.dispatch({ names, type: 'reset' }),
11351109
setFieldsValue: (values: Store, validate = false) => this.dispatch({ type: 'setFieldsValue', validate, values }),
11361110
setFieldValue: (name: NamePath, value: StoreValue, validate = false) =>

0 commit comments

Comments
 (0)