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

feat: add hasError helper #31

Merged
merged 1 commit into from
Jan 27, 2024
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
6 changes: 6 additions & 0 deletions apps/example/src/app/simple-form/simple-form.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ import { CustomErrorComponent } from '../custom-input-error.component';
[debounce]="700"
[formField]="form.controls.username"
/>
@if(form.controls.username.hasError('required')) {
<small>
Username is required
</small>
}

</div>

<div>
Expand Down
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> = {
markAsTouched: () => void;
markAsDirty: () => void;
reset: () => void;
hasError: (errorKey: string) => boolean;
registerOnReset: (fn: (value: Value) => void) => void;
};

Expand Down Expand Up @@ -136,6 +137,7 @@ export function createFormField<Value>(
disabled: disabledSignal,
markAsTouched: () => touchedStateSignal.set('TOUCHED'),
markAsDirty: () => dirtyStateSignal.set('DIRTY'),
hasError: (errorKey: string) => !!errorsSignal()[errorKey],
registerOnReset: (fn: (value: Value) => void) => (onReset = fn),
reset: () => {
valueSignal.set(defaultValue);
Expand Down
2 changes: 2 additions & 0 deletions packages/platform/src/lib/form-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export type FormGroup<Fields extends FormGroupCreatorOrSignal = {}> = {
touched: Signal<boolean>;
errors: Signal<{}>;
errorsArray: Signal<InvalidDetails[]>;
hasError: (errorKey: string) => boolean;
markAllAsTouched: () => void;
reset: () => void;
};
Expand Down Expand Up @@ -172,6 +173,7 @@ export function createFormGroup<FormFields extends FormGroupCreator>(
});
return myErrors.concat(...childErrors);
}),
hasError: (errorKey: string) => !!errorsSignal()[errorKey],
dirtyState: dirtyStateSignal,
dirty: dirtySignal,
touchedState: touchedStateSignal,
Expand Down
2 changes: 1 addition & 1 deletion packages/platform/src/lib/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export function computeValidateState(
return computed(() => validateSignal().map((v) => v()));
}

export function computeErrors(validateSignal: Signal<ValidateState[]>) {
export function computeErrors(validateSignal: Signal<ValidateState[]>): Signal<Record<string, ValidationError>> {
return computed(() => {
return validateSignal().reduce((acc, errors) => {
if (!errors.errors) {
Expand Down
Loading