Skip to content

Commit

Permalink
feat: add markAsPristine to mark FormField and FormGroup as pristine (
Browse files Browse the repository at this point in the history
  • Loading branch information
crapStone authored Nov 8, 2024
1 parent b8e2153 commit d702b31
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/platform/src/lib/form-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export type FormField<Value = unknown> = {
readOnly: Signal<boolean>;
markAsTouched: () => void;
markAsDirty: () => void;
markAsPristine: () => void;
reset: () => void;
hasError: (errorKey: string) => boolean;
hasValidator: (validator: Validator) => boolean;
Expand Down Expand Up @@ -159,6 +160,7 @@ export function createFormField<Value>(
readOnly: readOnlySignal,
markAsTouched: () => touchedStateSignal.set('TOUCHED'),
markAsDirty: () => dirtyStateSignal.set('DIRTY'),
markAsPristine: () => dirtyStateSignal.set('PRISTINE'),
hasError: (errorKey: string) => !!errorsSignal()[errorKey],
hasValidator: (validator: Validator) => {
if (finalOptions !== undefined) {
Expand Down
12 changes: 12 additions & 0 deletions packages/platform/src/lib/form-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export type FormGroup<Fields extends FormGroupCreatorOrSignal = {}> = {
hasValidator: (validator: Validator) => boolean;
errorMessage: (errorKey: string) => string | undefined;
markAllAsTouched: () => void;
markAllAsPristine: () => void;
reset: () => void;
};

Expand Down Expand Up @@ -204,6 +205,17 @@ export function createFormGroup<FormFields extends FormGroupCreator>(
}
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()
Expand Down

0 comments on commit d702b31

Please sign in to comment.