diff --git a/packages/platform/src/lib/form-field.ts b/packages/platform/src/lib/form-field.ts index d87e379..80b8112 100644 --- a/packages/platform/src/lib/form-field.ts +++ b/packages/platform/src/lib/form-field.ts @@ -38,6 +38,7 @@ export type FormField = { readOnly: Signal; markAsTouched: () => void; markAsDirty: () => void; + markAsPristine: () => void; reset: () => void; hasError: (errorKey: string) => boolean; errorMessage: (errorKey: string) => string | undefined, @@ -157,6 +158,7 @@ export function createFormField( readOnly: readOnlySignal, markAsTouched: () => touchedStateSignal.set('TOUCHED'), markAsDirty: () => dirtyStateSignal.set('DIRTY'), + markAsPristine: () => dirtyStateSignal.set('PRISTINE'), hasError: (errorKey: string) => !!errorsSignal()[errorKey], errorMessage: (errorKey: string) => errorsArraySignal().find(e => e.key === errorKey)?.message, registerOnReset: (fn: (value: Value) => void) => (onReset = fn), diff --git a/packages/platform/src/lib/form-group.ts b/packages/platform/src/lib/form-group.ts index ec80fff..85c20a3 100644 --- a/packages/platform/src/lib/form-group.ts +++ b/packages/platform/src/lib/form-group.ts @@ -27,10 +27,10 @@ import { export type FormGroup = { __type: 'FormGroup'; value: Signal>; - controls: Fields extends WritableSignal[]> - ? FormGroupFields & WritableSignal[]> - : Fields extends WritableSignal - ? FormGroupFields & WritableSignal + controls: Fields extends WritableSignal[]> + ? FormGroupFields & WritableSignal[]> + : Fields extends WritableSignal + ? FormGroupFields & WritableSignal : FormGroupFields; valid: Signal; state: Signal; @@ -43,6 +43,7 @@ export type FormGroup = { hasError: (errorKey: string) => boolean; errorMessage: (errorKey: string) => string | undefined; markAllAsTouched: () => void; + markAllAsPristine: () => void; reset: () => void; }; @@ -195,6 +196,17 @@ export function createFormGroup( } Object.values(fg).forEach((f) => markFormControlAsTouched(f)); }, + markAllAsPristine: () => { + const fg = isSignal(formFieldsMapOrSignal) + ? formFieldsMapOrSignal() + : formFieldsMapOrSignal; + + if (Array.isArray(fg)) { + fg.forEach((f) => f.markAsPristine()); + return; + } + Object.values(fg).forEach((f) => f.markAsPristine()); + }, reset: () => { const fg = isSignal(formFieldsMapOrSignal) ? formFieldsMapOrSignal() @@ -205,7 +217,7 @@ export function createFormGroup( (formFieldsMapOrSignal as WritableSignal).set([ ...initialArrayControls, ]); - + for (const ctrl of initialArrayControls) { ctrl.reset(); }