Skip to content

Commit 3a5d486

Browse files
committed
refactor(select): move function to utils
1 parent 59b350e commit 3a5d486

File tree

4 files changed

+22
-27
lines changed

4 files changed

+22
-27
lines changed

core/src/components/input/input.tsx

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
h,
1515
} from '@stencil/core';
1616
import type { NotchController } from '@utils/forms';
17-
import { createNotchController } from '@utils/forms';
17+
import { createNotchController, checkInvalidState } from '@utils/forms';
1818
import type { Attributes } from '@utils/helpers';
1919
import { inheritAriaAttributes, debounceEvent, inheritAttributes, componentOnReady } from '@utils/helpers';
2020
import { createSlotMutationController } from '@utils/slot-mutation-controller';
@@ -403,16 +403,6 @@ export class Input implements ComponentInterface {
403403
};
404404
}
405405

406-
/**
407-
* Checks if the input is in an invalid state based on Ionic validation classes
408-
*/
409-
private checkInvalidState(): boolean {
410-
const hasIonTouched = this.el.classList.contains('ion-touched');
411-
const hasIonInvalid = this.el.classList.contains('ion-invalid');
412-
413-
return hasIonTouched && hasIonInvalid;
414-
}
415-
416406
connectedCallback() {
417407
const { el } = this;
418408

@@ -426,7 +416,7 @@ export class Input implements ComponentInterface {
426416
// Watch for class changes to update validation state
427417
if (Build.isBrowser && typeof MutationObserver !== 'undefined') {
428418
this.validationObserver = new MutationObserver(() => {
429-
const newIsInvalid = this.checkInvalidState();
419+
const newIsInvalid = checkInvalidState(el);
430420
if (this.isInvalid !== newIsInvalid) {
431421
this.isInvalid = newIsInvalid;
432422
// Force a re-render to update aria-describedby immediately
@@ -441,7 +431,7 @@ export class Input implements ComponentInterface {
441431
}
442432

443433
// Always set initial state
444-
this.isInvalid = this.checkInvalidState();
434+
this.isInvalid = checkInvalidState(el);
445435

446436
this.debounceChanged();
447437
if (Build.isBrowser) {

core/src/components/select/select.tsx

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { ComponentInterface, EventEmitter } from '@stencil/core';
22
import { Build, Component, Element, Event, Host, Method, Prop, State, Watch, h, forceUpdate } from '@stencil/core';
33
import type { NotchController } from '@utils/forms';
4-
import { compareOptions, createNotchController, isOptionSelected } from '@utils/forms';
4+
import { compareOptions, createNotchController, isOptionSelected, checkInvalidState } from '@utils/forms';
55
import { focusVisibleElement, renderHiddenInput, inheritAttributes } from '@utils/helpers';
66
import type { Attributes } from '@utils/helpers';
77
import { printIonWarning } from '@utils/logging';
@@ -310,7 +310,7 @@ export class Select implements ComponentInterface {
310310
// Watch for class changes to update validation state.
311311
if (Build.isBrowser && typeof MutationObserver !== 'undefined') {
312312
this.validationObserver = new MutationObserver(() => {
313-
const newIsInvalid = this.checkInvalidState();
313+
const newIsInvalid = checkInvalidState(this.el);
314314
if (this.isInvalid !== newIsInvalid) {
315315
this.isInvalid = newIsInvalid;
316316
/**
@@ -342,7 +342,7 @@ export class Select implements ComponentInterface {
342342
}
343343

344344
// Always set initial state
345-
this.isInvalid = this.checkInvalidState();
345+
this.isInvalid = checkInvalidState(this.el);
346346
}
347347

348348
componentWillLoad() {
@@ -1168,17 +1168,6 @@ export class Select implements ComponentInterface {
11681168
return <div class="select-bottom">{this.renderHintText()}</div>;
11691169
}
11701170

1171-
/**
1172-
* Checks if the input is in an invalid state based
1173-
* on Ionic validation classes.
1174-
*/
1175-
private checkInvalidState(): boolean {
1176-
const hasIonTouched = this.el.classList.contains('ion-touched');
1177-
const hasIonInvalid = this.el.classList.contains('ion-invalid');
1178-
1179-
return hasIonTouched && hasIonInvalid;
1180-
}
1181-
11821171
render() {
11831172
const {
11841173
disabled,

core/src/utils/forms/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './notch-controller';
22
export * from './compare-with-utils';
3+
export * from './validity';

core/src/utils/forms/validity.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
type FormElement = HTMLIonInputElement | HTMLIonSelectElement;
2+
3+
/**
4+
* Checks if the form element is in an invalid state based on
5+
* Ionic validation classes.
6+
*
7+
* @param el The form element to check.
8+
* @returns `true` if the element is invalid, `false` otherwise.
9+
*/
10+
export const checkInvalidState = (el: FormElement): boolean => {
11+
const hasIonTouched = el.classList.contains('ion-touched');
12+
const hasIonInvalid = el.classList.contains('ion-invalid');
13+
14+
return hasIonTouched && hasIonInvalid;
15+
};

0 commit comments

Comments
 (0)