Skip to content

Commit 72b74ec

Browse files
committed
refactor(form): remove legacy createFormStore and refactor FormStore class to support submit state & count
1 parent f69e563 commit 72b74ec

File tree

3 files changed

+71
-24
lines changed

3 files changed

+71
-24
lines changed

primitives/filed-form /src/copy.tsx

Lines changed: 0 additions & 16 deletions
This file was deleted.

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

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,11 @@ class FormStore {
167167
/** Flag to preserve field values after unmount */
168168
private _preserve = false;
169169

170+
private _submitCount = 0;
171+
private _isSubmitting = false;
172+
private _isSubmitted = false;
173+
private _isSubmitSuccessful = false;
174+
170175
constructor() {
171176
this.rebindMiddlewares();
172177
}
@@ -318,14 +323,40 @@ class FormStore {
318323
* Gets current form state including validation status
319324
*/
320325
private getFormState() {
326+
const errors = Object.fromEntries(this._errors);
327+
const warnings = Object.fromEntries(this._warnings);
328+
329+
const dirtyFields = Object.fromEntries(Array.from(this._dirty).map(k => [k, true]));
330+
331+
const touchedFields = Object.fromEntries(Array.from(this._touched).map(k => [k, true]));
332+
333+
const validatingFields = Object.fromEntries(Array.from(this._validating).map(k => [k, true]));
334+
335+
const validatedFields = Object.fromEntries(Array.from(this._validated).map(k => [k, true]));
336+
321337
return {
322-
errors: Object.fromEntries(this._errors),
338+
dirtyFields,
339+
// meta maps
340+
errors,
341+
323342
initialValues: this._initial,
343+
// booleans
324344
isDirty: this._dirty.size > 0,
345+
isSubmitSuccessful: this._isSubmitSuccessful,
346+
isSubmitted: this._isSubmitted,
347+
isSubmitting: this._isSubmitting,
325348
isValid: Array.from(this._errors.values()).every(arr => !arr?.length),
349+
326350
isValidating: this._validating.size > 0,
351+
// counters
352+
submitCount: this._submitCount,
353+
touchedFields,
354+
validatedFields,
355+
validatingFields,
356+
// raw values
327357
values: this._store,
328-
warnings: Object.fromEntries(this._warnings)
358+
359+
warnings
329360
};
330361
}
331362

@@ -1181,9 +1212,19 @@ class FormStore {
11811212
* Calls onFinish if validation passes, onFinishFailed if it fails
11821213
*/
11831214
private submit = () => {
1215+
this._isSubmitted = true;
1216+
this._isSubmitting = true;
1217+
this._submitCount++;
1218+
11841219
this.validateFields().then(ok => {
1185-
if (ok) this._callbacks.onFinish?.(this._store);
1186-
else this._callbacks.onFinishFailed?.(this.buildFailedPayload());
1220+
this._isSubmitting = false;
1221+
if (ok) {
1222+
this._isSubmitSuccessful = true;
1223+
this._callbacks.onFinish?.(this._store);
1224+
} else {
1225+
this._isSubmitSuccessful = false;
1226+
this._callbacks.onFinishFailed?.(this.buildFailedPayload());
1227+
}
11871228
});
11881229
};
11891230

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,9 @@ import type {
1515

1616
import type { ChangeMask } from '../../form-core/event';
1717
import type { Action, ArrayOpArgs, Middleware } from '../../form-core/middleware';
18-
import type { FieldEntity, StoreValue } from '../../form-core/types';
18+
import type { FieldEntity, Meta, StoreValue } from '../../form-core/types';
1919
import type { ValidateMessages } from '../../form-core/validate';
2020
import type { Rule, ValidateOptions } from '../../form-core/validation';
21-
import type { Meta } from '../../types/shared-types';
22-
23-
import type { FormState } from './types';
2421

2522
export type ListRenderItem = {
2623
key: string;
@@ -45,6 +42,31 @@ export type MetaShapeFromPaths<T, Ps extends readonly string[]> = Ps extends nev
4542
? { [K in keyof T]: Meta<K & string, PathToDeepType<T, K & string>> }
4643
: MergeUnion<Ps[number] extends infer P ? (P extends string ? BuildMetaShape<T, P> : never) : never>;
4744

45+
export interface FormState<Values = any> {
46+
dirtyFields: Record<AllPathsKeys<Values>, boolean>;
47+
errors: Record<AllPathsKeys<Values>, string[]>;
48+
initialValues: DeepPartial<Values>;
49+
// === Global booleans ===
50+
isDirty: boolean;
51+
52+
isSubmitSuccessful: boolean;
53+
// === Submission lifecycle ===
54+
isSubmitted: boolean;
55+
isSubmitting: boolean;
56+
isValid: boolean;
57+
58+
isValidating: boolean;
59+
submitCount: number;
60+
// === Field-level states ===
61+
touchedFields: Record<AllPathsKeys<Values>, boolean>;
62+
63+
validatedFields: Record<AllPathsKeys<Values>, boolean>;
64+
validatingFields: Record<AllPathsKeys<Values>, boolean>;
65+
// === Meta states ===
66+
values: Values;
67+
warnings: Record<AllPathsKeys<Values>, string[]>;
68+
}
69+
4870
export interface ValuesOptions<Values = any> {
4971
arrayOp: <K extends ArrayKeys<Values>>(
5072
name: K

0 commit comments

Comments
 (0)