Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
ec83531
fix(input): add aria-live attributes to error text
ShaneK Aug 14, 2025
e358cab
Merge branch 'main' of github.com:ionic-team/ionic-framework into ion…
ShaneK Aug 19, 2025
a7295b4
fix(input): making validation reading work more consistently across b…
ShaneK Aug 19, 2025
fc05815
fix(lint): fixing lint in test files
ShaneK Aug 19, 2025
f1bb778
Merge branch 'main' of github.com:ionic-team/ionic-framework into ion…
ShaneK Aug 21, 2025
9ebada9
App validation tests
ShaneK Aug 21, 2025
1baf39e
fix(input): improve validation state reactivity
ShaneK Aug 21, 2025
5760856
fix(input): fixing input validation accessibility for template-driven…
ShaneK Aug 21, 2025
85b8cbf
fix(input): cleanup on input validation
ShaneK Aug 26, 2025
1d0c191
fix(cleanup): removing file
ShaneK Aug 26, 2025
65dd166
Restoring tests
ShaneK Aug 26, 2025
a9ff301
Merge branch 'main' of github.com:ionic-team/ionic-framework into ion…
ShaneK Sep 2, 2025
65867ce
refactor(input): removing debug code
ShaneK Sep 2, 2025
df8fa7d
refactor(input): removing angular-specific code from core
ShaneK Sep 2, 2025
0180454
fix(lint): fixing lint in core
ShaneK Sep 2, 2025
7fc8cbc
Update packages/angular/test/base/src/app/standalone/validation/input…
ShaneK Sep 3, 2025
2db2b1f
Update core/src/components/input/test/validation/index.html
ShaneK Sep 3, 2025
7926f08
Update core/src/components/textarea/test/validation/index.html
ShaneK Sep 3, 2025
1493bf7
refactor(tests, input): doing cleanup across input and tests
ShaneK Sep 4, 2025
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
81 changes: 73 additions & 8 deletions core/src/components/input/input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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"`.
Expand Down Expand Up @@ -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;

Expand All @@ -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(
Expand Down Expand Up @@ -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;
}
}

/**
Expand Down Expand Up @@ -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);
};

private onFocus = (ev: FocusEvent) => {
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 && (
Expand Down
Loading
Loading