Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export class EmailLinkAuthFormComponent {
effect(() => {
this.form.update({
validators: {
onBlur: this.formSchema(),
onChange: this.formSchema(),
onSubmitAsync: async ({ value }) => {
try {
await sendSignInLinkToEmail(this.ui(), value.email);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export class ForgotPasswordAuthFormComponent {
effect(() => {
this.form.update({
validators: {
onBlur: this.formSchema(),
onChange: this.formSchema(),
onSubmitAsync: async ({ value }) => {
try {
await sendPasswordResetEmail(this.ui(), value.email);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export class SmsMultiFactorAssertionVerifyFormComponent {
effect(() => {
this.form.update({
validators: {
onBlur: this.formSchema(),
onChange: this.formSchema(),
onSubmit: this.formSchema(),
onSubmitAsync: async ({ value }) => {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export class SmsMultiFactorEnrollmentFormComponent {
effect(() => {
this.phoneForm.update({
validators: {
onBlur: this.phoneFormSchema(),
onChange: this.phoneFormSchema(),
onSubmit: this.phoneFormSchema(),
onSubmitAsync: async ({ value }) => {
try {
Expand Down Expand Up @@ -192,7 +192,7 @@ export class SmsMultiFactorEnrollmentFormComponent {
effect(() => {
this.verificationForm.update({
validators: {
onBlur: this.verificationFormSchema(),
onChange: this.verificationFormSchema(),
onSubmit: this.verificationFormSchema(),
onSubmitAsync: async ({ value }) => {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class TotpMultiFactorAssertionFormComponent {
effect(() => {
this.form.update({
validators: {
onBlur: this.formSchema(),
onChange: this.formSchema(),
onSubmitAsync: async ({ value }) => {
try {
const assertion = TotpMultiFactorGenerator.assertionForSignIn(this.hint().uid, value.verificationCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class TotpMultiFactorSecretGenerationFormComponent {
effect(() => {
this.form.update({
validators: {
onBlur: this.formSchema(),
onChange: this.formSchema(),
onSubmit: this.formSchema(),
onSubmitAsync: async ({ value }) => {
try {
Expand Down Expand Up @@ -191,7 +191,7 @@ export class TotpMultiFactorVerificationFormComponent {
effect(() => {
this.form.update({
validators: {
onBlur: this.formSchema(),
onChange: this.formSchema(),
onSubmit: this.formSchema(),
onSubmitAsync: async ({ value }) => {
try {
Expand Down
4 changes: 2 additions & 2 deletions packages/angular/src/lib/auth/forms/phone-auth-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export class PhoneNumberFormComponent {
effect(() => {
this.form.update({
validators: {
onBlur: this.formSchema(),
onChange: this.formSchema(),
onSubmitAsync: async ({ value }) => {
const selectedCountry = countryData.find((c) => c.code === this.country());
const formattedNumber = formatPhoneNumber(value.phoneNumber, selectedCountry!);
Expand Down Expand Up @@ -226,7 +226,7 @@ export class VerificationFormComponent {
effect(() => {
this.form.update({
validators: {
onBlur: this.formSchema(),
onChange: this.formSchema(),
onSubmitAsync: async ({ value }) => {
try {
const credential = await confirmPhoneNumber(this.ui(), this.verificationId(), value.verificationCode);
Expand Down
2 changes: 1 addition & 1 deletion packages/angular/src/lib/auth/forms/sign-in-auth-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export class SignInAuthFormComponent {
effect(() => {
this.form.update({
validators: {
onBlur: this.formSchema(),
onChange: this.formSchema(),
onSubmitAsync: async ({ value }) => {
try {
const credential = await signInWithEmailAndPassword(this.ui(), value.email, value.password);
Expand Down
2 changes: 1 addition & 1 deletion packages/angular/src/lib/auth/forms/sign-up-auth-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export class SignUpAuthFormComponent {
effect(() => {
this.form.update({
validators: {
onBlur: this.formSchema(),
onChange: this.formSchema(),
onSubmitAsync: async ({ value }) => {
try {
const credential = await createUserWithEmailAndPassword(
Expand Down
30 changes: 5 additions & 25 deletions packages/angular/src/lib/components/form.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,13 @@ import {
import { ButtonComponent } from "./button";

@Component({
template: `<fui-form-metadata [field]="field()"></fui-form-metadata>`,
template: `<fui-form-metadata [isTouched]="isTouched()" [errors]="errors()"></fui-form-metadata>`,
standalone: true,
imports: [FormMetadataComponent],
})
class TestFormMetadataHostComponent {
field = signal({
state: {
meta: {
isTouched: true,
errors: [{ message: "Test error" }],
},
},
} as any);
isTouched = signal(true);
errors = signal([{ message: "Test error" }]);
}

@Component({
Expand Down Expand Up @@ -90,14 +84,7 @@ describe("Form Components", () => {
it("does not render error message when field has no errors", async () => {
const component = await render(TestFormMetadataHostComponent);

component.fixture.componentInstance.field.set({
state: {
meta: {
isTouched: true,
errors: [],
},
},
} as any);
component.fixture.componentInstance.errors.set([]);
component.fixture.detectChanges();

const errorElement = screen.queryByRole("alert");
Expand All @@ -107,14 +94,7 @@ describe("Form Components", () => {
it("does not render error message when field is not touched", async () => {
const component = await render(TestFormMetadataHostComponent);

component.fixture.componentInstance.field.set({
state: {
meta: {
isTouched: false,
errors: [{ message: "Test error" }],
},
},
} as any);
component.fixture.componentInstance.isTouched.set(false);
component.fixture.detectChanges();

const errorElement = screen.queryByRole("alert");
Expand Down
37 changes: 23 additions & 14 deletions packages/angular/src/lib/components/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* limitations under the License.
*/

import { Component, computed, input } from "@angular/core";
import { AnyFieldApi, AnyFormState, injectField } from "@tanstack/angular-form";
import { ChangeDetectorRef, Component, computed, inject, input, OnChanges, SimpleChanges } from "@angular/core";
import { AnyFormState, injectField } from "@tanstack/angular-form";
import { ButtonComponent } from "./button";

@Component({
Expand All @@ -25,10 +25,10 @@ import { ButtonComponent } from "./button";
style: "display: block;",
},
template: `
@if (field().state.meta.isTouched && errors().length > 0) {
@if (isTouched() && errors().length > 0) {
<div>
<div role="alert" aria-live="polite" class="fui-error">
{{ errors() }}
{{ errorMessage() }}
</div>
</div>
}
Expand All @@ -38,13 +38,14 @@ import { ButtonComponent } from "./button";
* A component that displays form field metadata, such as validation errors.
*/
export class FormMetadataComponent {
/** The form field API instance. */
field = input.required<AnyFieldApi>();
errors = computed(() =>
this.field()
.state.meta.errors.map((error) => error.message)
.join(", ")
);
isTouched = input.required<boolean>();
errors = input.required<Array<{ message: string }>>();

errorMessage(): string {
return this.errors()
.map((error) => error.message)
.join(", ");
}
}

@Component({
Expand All @@ -70,27 +71,35 @@ export class FormMetadataComponent {
[id]="field.api.name"
[name]="field.api.name"
[value]="field.api.state.value"
(blur)="field.api.handleBlur()"
(input)="field.api.handleChange($any($event).target.value)"
[type]="type()"
/>
</div>
<ng-content></ng-content>
<fui-form-metadata [field]="field.api"></fui-form-metadata>
<fui-form-metadata
[isTouched]="field.api.state.meta.isTouched"
[errors]="field.api.state.meta.errors"
></fui-form-metadata>
</label>
`,
})
/**
* A form input component with label, description, and validation support.
*/
export class FormInputComponent {
export class FormInputComponent implements OnChanges {
field = injectField<string>();
private cdr = inject(ChangeDetectorRef);
/** The label text for the input field. */
label = input.required<string>();
/** The input type (e.g., "text", "email", "password"). */
type = input<string>("text");
/** Optional description text displayed below the label. */
description = input<string>();

ngOnChanges(_changes: SimpleChanges): void {
// Trigger change detection when any input changes
this.cdr.markForCheck();
}
}

@Component({
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { hasBehavior } from "./behaviors";
*/
export function createSignInAuthFormSchema(ui: FirebaseUI) {
return z.object({
email: z.email(getTranslation(ui, "errors", "invalidEmail")),
email: z.string().email(getTranslation(ui, "errors", "invalidEmail")),
password: z.string().min(6, getTranslation(ui, "errors", "weakPassword")),
});
}
Expand All @@ -48,7 +48,7 @@ export function createSignUpAuthFormSchema(ui: FirebaseUI) {
const displayNameRequiredMessage = getTranslation(ui, "errors", "displayNameRequired");

return z.object({
email: z.email(getTranslation(ui, "errors", "invalidEmail")),
email: z.string().email(getTranslation(ui, "errors", "invalidEmail")),
password: z.string().min(6, getTranslation(ui, "errors", "weakPassword")),
displayName: requireDisplayName
? z.string().min(1, displayNameRequiredMessage)
Expand All @@ -66,7 +66,7 @@ export function createSignUpAuthFormSchema(ui: FirebaseUI) {
*/
export function createForgotPasswordAuthFormSchema(ui: FirebaseUI) {
return z.object({
email: z.email(getTranslation(ui, "errors", "invalidEmail")),
email: z.string().email(getTranslation(ui, "errors", "invalidEmail")),
});
}

Expand All @@ -80,7 +80,7 @@ export function createForgotPasswordAuthFormSchema(ui: FirebaseUI) {
*/
export function createEmailLinkAuthFormSchema(ui: FirebaseUI) {
return z.object({
email: z.email(getTranslation(ui, "errors", "invalidEmail")),
email: z.string().email(getTranslation(ui, "errors", "invalidEmail")),
});
}

Expand Down
6 changes: 3 additions & 3 deletions packages/react/src/auth/forms/email-link-auth-form.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ describe("<EmailLinkAuthForm />", () => {
expect(onSignInMock).not.toHaveBeenCalled();
});

it("should trigger validation errors when the form is blurred", () => {
it("should trigger validation errors when the form changes", async () => {
const mockUI = createMockUI();

const { container } = render(
Expand All @@ -307,9 +307,9 @@ describe("<EmailLinkAuthForm />", () => {
const input = screen.getByRole("textbox", { name: /email/i });

act(() => {
fireEvent.blur(input);
fireEvent.change(input, { target: { value: "invalid" } });
});

expect(screen.getByText("Please enter a valid email address")).toBeInTheDocument();
expect(await screen.findByText("Please enter a valid email address")).toBeInTheDocument();
});
});
2 changes: 1 addition & 1 deletion packages/react/src/auth/forms/email-link-auth-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export function useEmailLinkAuthForm(onSuccess?: EmailLinkAuthFormProps["onEmail
email: "",
},
validators: {
onBlur: schema,
onChange: schema,
onSubmitAsync: async ({ value }) => {
try {
await action(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ describe("<ForgotPasswordAuthForm />", () => {
expect(onBackToSignInClickMock).toHaveBeenCalled();
});

it("should trigger validation errors when the form is blurred", () => {
it("should trigger validation errors when the form changes", () => {
const mockUI = createMockUI();

const { container } = render(
Expand All @@ -213,7 +213,7 @@ describe("<ForgotPasswordAuthForm />", () => {
const input = screen.getByRole("textbox", { name: /email/i });

act(() => {
fireEvent.blur(input);
fireEvent.change(input, { target: { value: "invalid" } });
});

expect(screen.getByText("Please enter a valid email address")).toBeInTheDocument();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export function useForgotPasswordAuthForm(onSuccess?: ForgotPasswordAuthFormProp
email: "",
},
validators: {
onBlur: schema,
onChange: schema,
onSubmitAsync: async ({ value }) => {
try {
await action(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ export function useSmsMultiFactorAssertionVerifyForm({
verificationCode: "",
},
validators: {
onBlur: schema,
onChange: schema,
onSubmitAsync: async ({ value }) => {
try {
const credential = await action(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export function useSmsMultiFactorEnrollmentPhoneNumberForm({
phoneNumber: "",
},
validators: {
onBlur: schema,
onChange: schema,
onSubmitAsync: async ({ value }) => {
try {
const formatted = formatPhoneNumber ? formatPhoneNumber(value.phoneNumber) : value.phoneNumber;
Expand Down Expand Up @@ -201,7 +201,7 @@ export function useMultiFactorEnrollmentVerifyPhoneNumberForm({
verificationCode: "",
},
validators: {
onBlur: schema,
onChange: schema,
onSubmitAsync: async ({ value }) => {
try {
await action({ ...value, displayName });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function useTotpMultiFactorAssertionForm({ hint, onSuccess }: UseTotpMult
verificationCode: "",
},
validators: {
onBlur: schema,
onChange: schema,
onSubmitAsync: async ({ value }) => {
try {
const credential = await action({ verificationCode: value.verificationCode, hint });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function useTotpMultiFactorSecretGenerationForm({ onSuccess }: UseTotpMul
displayName: "",
},
validators: {
onBlur: schema,
onChange: schema,
onSubmitAsync: async ({ value }) => {
try {
const secret = await action();
Expand Down Expand Up @@ -160,7 +160,7 @@ export function useMultiFactorEnrollmentVerifyTotpForm({
verificationCode: "",
},
validators: {
onBlur: schema,
onChange: schema,
onSubmitAsync: async ({ value }) => {
try {
await action({ secret, verificationCode: value.verificationCode, displayName });
Expand Down
Loading