Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method to mark FormField and FormGroup as PRISTINE #49

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -38,6 +38,7 @@ export type FormField<Value = unknown> = {
readOnly: Signal<boolean>;
markAsTouched: () => void;
markAsDirty: () => void;
markAsPristine: () => void;
reset: () => void;
hasError: (errorKey: string) => boolean;
errorMessage: (errorKey: string) => string | undefined,
Expand Down Expand Up @@ -157,6 +158,7 @@ export function createFormField<Value>(
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),
Expand Down
22 changes: 17 additions & 5 deletions packages/platform/src/lib/form-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ import {
export type FormGroup<Fields extends FormGroupCreatorOrSignal = {}> = {
__type: 'FormGroup';
value: Signal<UnwrappedFormGroup<Fields>>;
controls: Fields extends WritableSignal<FormGroup<infer G>[]>
? FormGroupFields<Fields> & WritableSignal<FormGroup<G>[]>
: Fields extends WritableSignal<infer F>
? FormGroupFields<Fields> & WritableSignal<F>
controls: Fields extends WritableSignal<FormGroup<infer G>[]>
? FormGroupFields<Fields> & WritableSignal<FormGroup<G>[]>
: Fields extends WritableSignal<infer F>
? FormGroupFields<Fields> & WritableSignal<F>
: FormGroupFields<Fields>;
valid: Signal<boolean>;
state: Signal<ValidationState>;
Expand All @@ -43,6 +43,7 @@ export type FormGroup<Fields extends FormGroupCreatorOrSignal = {}> = {
hasError: (errorKey: string) => boolean;
errorMessage: (errorKey: string) => string | undefined;
markAllAsTouched: () => void;
markAllAsPristine: () => void;
reset: () => void;
};

Expand Down Expand Up @@ -195,6 +196,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 All @@ -205,7 +217,7 @@ export function createFormGroup<FormFields extends FormGroupCreator>(
(formFieldsMapOrSignal as WritableSignal<any[]>).set([
...initialArrayControls,
]);

for (const ctrl of initialArrayControls) {
ctrl.reset();
}
Expand Down
Loading