mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-07 10:04:11 +00:00
Dynamic aria-describedby attribute
(cherry picked from commit e629d9edf0
)
This commit is contained in:

committed by
github-actions[bot]
![github-actions[bot]](/assets/img/avatar_default.png)
parent
a6b45f777c
commit
3b3fa4f643
@@ -16,7 +16,7 @@
|
|||||||
<input [className]="(email.invalid) && (email.dirty || email.touched) ? 'form-control is-invalid' :'form-control'"
|
<input [className]="(email.invalid) && (email.dirty || email.touched) ? 'form-control is-invalid' :'form-control'"
|
||||||
type="text" id="email" formControlName="email"
|
type="text" id="email" formControlName="email"
|
||||||
[attr.aria-label]="MESSAGE_PREFIX + '.aria.label' | translate"
|
[attr.aria-label]="MESSAGE_PREFIX + '.aria.label' | translate"
|
||||||
aria-describedby="email-errors-required email-error-not-valid"
|
[attr.aria-describedby]="ariaDescribedby"
|
||||||
[attr.aria-invalid]="email.invalid"/>
|
[attr.aria-invalid]="email.invalid"/>
|
||||||
<div *ngIf="email.invalid && (email.dirty || email.touched)"
|
<div *ngIf="email.invalid && (email.dirty || email.touched)"
|
||||||
class="invalid-feedback show-feedback">
|
class="invalid-feedback show-feedback">
|
||||||
|
@@ -192,4 +192,39 @@ describe('RegisterEmailFormComponent', () => {
|
|||||||
expect(router.navigate).not.toHaveBeenCalled();
|
expect(router.navigate).not.toHaveBeenCalled();
|
||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
|
describe('ariaDescribedby', () => {
|
||||||
|
it('should have required error message when email is empty', () => {
|
||||||
|
comp.form.patchValue({ email: '' });
|
||||||
|
comp.checkEmailValidity();
|
||||||
|
|
||||||
|
expect(comp.ariaDescribedby).toContain('email-errors-required');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should have invalid email error message when email is invalid', () => {
|
||||||
|
comp.form.patchValue({ email: 'invalid-email' });
|
||||||
|
comp.checkEmailValidity();
|
||||||
|
|
||||||
|
expect(comp.ariaDescribedby).toContain('email-error-not-valid');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should clear ariaDescribedby when email is valid', () => {
|
||||||
|
comp.form.patchValue({ email: 'valid@email.com' });
|
||||||
|
comp.checkEmailValidity();
|
||||||
|
|
||||||
|
expect(comp.ariaDescribedby).toBe('');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should update ariaDescribedby on value changes', () => {
|
||||||
|
spyOn(comp, 'checkEmailValidity').and.callThrough();
|
||||||
|
|
||||||
|
comp.form.patchValue({ email: '' });
|
||||||
|
expect(comp.ariaDescribedby).toContain('email-errors-required');
|
||||||
|
|
||||||
|
comp.form.patchValue({ email: 'invalid-email' });
|
||||||
|
expect(comp.ariaDescribedby).toContain('email-error-not-valid');
|
||||||
|
|
||||||
|
comp.form.patchValue({ email: 'valid@email.com' });
|
||||||
|
expect(comp.ariaDescribedby).toBe('');
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@@ -66,6 +66,11 @@ export class RegisterEmailFormComponent implements OnDestroy, OnInit {
|
|||||||
|
|
||||||
subscriptions: Subscription[] = [];
|
subscriptions: Subscription[] = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stores error messages related to the email field
|
||||||
|
*/
|
||||||
|
ariaDescribedby: string = '';
|
||||||
|
|
||||||
captchaVersion(): Observable<string> {
|
captchaVersion(): Observable<string> {
|
||||||
return this.googleRecaptchaService.captchaVersion();
|
return this.googleRecaptchaService.captchaVersion();
|
||||||
}
|
}
|
||||||
@@ -135,6 +140,13 @@ export class RegisterEmailFormComponent implements OnDestroy, OnInit {
|
|||||||
this.disableUntilChecked = res;
|
this.disableUntilChecked = res;
|
||||||
this.changeDetectorRef.detectChanges();
|
this.changeDetectorRef.detectChanges();
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Subscription to email field value changes
|
||||||
|
*/
|
||||||
|
this.subscriptions.push(this.email.valueChanges.subscribe(() => {
|
||||||
|
this.checkEmailValidity();
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -248,4 +260,18 @@ export class RegisterEmailFormComponent implements OnDestroy, OnInit {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
checkEmailValidity() {
|
||||||
|
const descriptions = [];
|
||||||
|
|
||||||
|
if (this.email.errors?.required) {
|
||||||
|
descriptions.push('email-errors-required');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.email.errors?.pattern || this.email.errors?.email) {
|
||||||
|
descriptions.push('email-error-not-valid');
|
||||||
|
}
|
||||||
|
|
||||||
|
this.ariaDescribedby = descriptions.join(' ');
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user