-
Notifications
You must be signed in to change notification settings - Fork 13.4k
fix(input): improve error text accessibility #30635
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
Open
ShaneK
wants to merge
11
commits into
main
Choose a base branch
from
ionic-49
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,764
−17
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ec83531
fix(input): add aria-live attributes to error text
ShaneK e358cab
Merge branch 'main' of github.com:ionic-team/ionic-framework into ion…
ShaneK a7295b4
fix(input): making validation reading work more consistently across b…
ShaneK fc05815
fix(lint): fixing lint in test files
ShaneK f1bb778
Merge branch 'main' of github.com:ionic-team/ionic-framework into ion…
ShaneK 9ebada9
App validation tests
ShaneK 1baf39e
fix(input): improve validation state reactivity
ShaneK 5760856
fix(input): fixing input validation accessibility for template-driven…
ShaneK 85b8cbf
fix(input): cleanup on input validation
ShaneK 1d0c191
fix(cleanup): removing file
ShaneK 65dd166
Restoring tests
ShaneK File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,8 +79,15 @@ export class Input implements ComponentInterface { | |
*/ | ||
@State() hasFocus = false; | ||
|
||
/** | ||
* Track validation state for proper aria-live announcements | ||
*/ | ||
@State() isInvalid = false; | ||
|
||
@Element() el!: HTMLIonInputElement; | ||
|
||
private validationObserver?: MutationObserver; | ||
|
||
/** | ||
* The color to use from your application's color palette. | ||
* Default options are: `"primary"`, `"secondary"`, `"tertiary"`, `"success"`, `"warning"`, `"danger"`, `"light"`, `"medium"`, and `"dark"`. | ||
|
@@ -396,6 +403,24 @@ export class Input implements ComponentInterface { | |
}; | ||
} | ||
|
||
/** | ||
* Checks if the input is in an invalid state based on validation classes | ||
*/ | ||
private checkValidationState(): boolean { | ||
// Check for both Ionic and Angular validation classes on the element itself | ||
// Angular applies ng-touched/ng-invalid directly to the host element with ngModel | ||
const hasIonTouched = this.el.classList.contains('ion-touched'); | ||
const hasIonInvalid = this.el.classList.contains('ion-invalid'); | ||
const hasNgTouched = this.el.classList.contains('ng-touched'); | ||
const hasNgInvalid = this.el.classList.contains('ng-invalid'); | ||
|
||
// Return true if we have both touched and invalid states from either framework | ||
const isTouched = hasIonTouched || hasNgTouched; | ||
const isInvalid = hasIonInvalid || hasNgInvalid; | ||
|
||
return isTouched && isInvalid; | ||
} | ||
|
||
connectedCallback() { | ||
const { el } = this; | ||
|
||
|
@@ -406,6 +431,26 @@ export class Input implements ComponentInterface { | |
() => this.labelSlot | ||
); | ||
|
||
// Watch for class changes to update validation state | ||
if (Build.isBrowser && typeof MutationObserver !== 'undefined') { | ||
this.validationObserver = new MutationObserver(() => { | ||
const newIsInvalid = this.checkValidationState(); | ||
if (this.isInvalid !== newIsInvalid) { | ||
this.isInvalid = newIsInvalid; | ||
// Force a re-render to update aria-describedby immediately | ||
forceUpdate(this); | ||
} | ||
}); | ||
|
||
this.validationObserver.observe(el, { | ||
attributes: true, | ||
attributeFilter: ['class'], | ||
}); | ||
} | ||
|
||
// Always set initial state | ||
this.isInvalid = this.checkValidationState(); | ||
|
||
this.debounceChanged(); | ||
if (Build.isBrowser) { | ||
document.dispatchEvent( | ||
|
@@ -451,6 +496,12 @@ export class Input implements ComponentInterface { | |
this.notchController.destroy(); | ||
this.notchController = undefined; | ||
} | ||
|
||
// Clean up validation observer to prevent memory leaks | ||
if (this.validationObserver) { | ||
this.validationObserver.disconnect(); | ||
this.validationObserver = undefined; | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -549,6 +600,20 @@ export class Input implements ComponentInterface { | |
this.didInputClearOnEdit = false; | ||
|
||
this.ionBlur.emit(ev); | ||
|
||
/** | ||
* Check validation state after blur to handle framework-managed classes. | ||
* Frameworks like Angular update classes asynchronously, often using | ||
* requestAnimationFrame or promises. Using setTimeout ensures we check | ||
* after all microtasks and animation frames have completed. | ||
*/ | ||
setTimeout(() => { | ||
const newIsInvalid = this.checkValidationState(); | ||
if (this.isInvalid !== newIsInvalid) { | ||
this.isInvalid = newIsInvalid; | ||
forceUpdate(this); | ||
} | ||
}, 100); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is 100 needed here? Does no number work? |
||
}; | ||
|
||
private onFocus = (ev: FocusEvent) => { | ||
|
@@ -626,22 +691,22 @@ export class Input implements ComponentInterface { | |
* Renders the helper text or error text values | ||
*/ | ||
private renderHintText() { | ||
const { helperText, errorText, helperTextId, errorTextId } = this; | ||
const { helperText, errorText, helperTextId, errorTextId, isInvalid } = this; | ||
|
||
return [ | ||
<div id={helperTextId} class="helper-text"> | ||
{helperText} | ||
<div id={helperTextId} class="helper-text" aria-live="polite"> | ||
{!isInvalid ? helperText : null} | ||
</div>, | ||
<div id={errorTextId} class="error-text"> | ||
{errorText} | ||
<div id={errorTextId} class="error-text" role="alert"> | ||
{isInvalid ? errorText : null} | ||
</div>, | ||
]; | ||
} | ||
|
||
private getHintTextID(): string | undefined { | ||
const { el, helperText, errorText, helperTextId, errorTextId } = this; | ||
const { isInvalid, helperText, errorText, helperTextId, errorTextId } = this; | ||
|
||
if (el.classList.contains('ion-touched') && el.classList.contains('ion-invalid') && errorText) { | ||
if (isInvalid && errorText) { | ||
return errorTextId; | ||
} | ||
|
||
|
@@ -864,7 +929,7 @@ export class Input implements ComponentInterface { | |
onCompositionstart={this.onCompositionStart} | ||
onCompositionend={this.onCompositionEnd} | ||
aria-describedby={this.getHintTextID()} | ||
aria-invalid={this.getHintTextID() === this.errorTextId} | ||
aria-invalid={this.isInvalid ? 'true' : undefined} | ||
{...this.inheritedAttributes} | ||
/> | ||
{this.clearInput && !readonly && !disabled && ( | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We shouldn't have Angular checks in core, Angular should be adding
ion-touched
andion-invalid
at the same time. If we need to adjust this we should do it in Angular.