Skip to content

Commit d645f24

Browse files
committed
refactor(segment-button): update when the errors are displayed
1 parent 21f49b0 commit d645f24

File tree

1 file changed

+25
-58
lines changed

1 file changed

+25
-58
lines changed

core/src/components/segment-button/segment-button.tsx

Lines changed: 25 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { ComponentInterface } from '@stencil/core';
22
import { Component, Element, Host, Prop, Method, State, Watch, forceUpdate, h } from '@stencil/core';
33
import type { ButtonInterface } from '@utils/element-interface';
44
import type { Attributes } from '@utils/helpers';
5-
import { addEventListener, removeEventListener, inheritAttributes, getNextSiblingOfType } from '@utils/helpers';
5+
import { addEventListener, removeEventListener, inheritAttributes } from '@utils/helpers';
66
import { hostContext } from '@utils/theme';
77

88
import { getIonMode } from '../../global/ionic-global';
@@ -65,58 +65,40 @@ export class SegmentButton implements ComponentInterface, ButtonInterface {
6565
this.updateState();
6666
}
6767

68-
private waitForSegmentContent(ionSegment: HTMLIonSegmentElement | null, contentId: string): Promise<HTMLElement> {
69-
return new Promise((resolve, reject) => {
70-
let timeoutId: NodeJS.Timeout | undefined = undefined;
71-
let animationFrameId: number;
72-
73-
const check = () => {
74-
if (!ionSegment) {
75-
reject(new Error(`Segment not found when looking for Segment Content`));
76-
return;
77-
}
78-
79-
const segmentView = getNextSiblingOfType<HTMLIonSegmentViewElement>(ionSegment); // Skip the text nodes
80-
const segmentContent = segmentView?.querySelector(
81-
`ion-segment-content[id="${contentId}"]`
82-
) as HTMLIonSegmentContentElement | null;
83-
if (segmentContent && timeoutId) {
84-
clearTimeout(timeoutId); // Clear the timeout if the segmentContent is found
85-
cancelAnimationFrame(animationFrameId);
86-
resolve(segmentContent);
87-
} else {
88-
animationFrameId = requestAnimationFrame(check); // Keep checking on the next animation frame
89-
}
90-
};
91-
92-
check();
93-
94-
// Set a timeout to reject the promise
95-
timeoutId = setTimeout(() => {
96-
cancelAnimationFrame(animationFrameId);
97-
reject(new Error(`Unable to find Segment Content with id="${contentId} within 1000 ms`));
98-
}, 1000);
99-
});
100-
}
101-
102-
async connectedCallback() {
68+
connectedCallback() {
10369
const segmentEl = (this.segmentEl = this.el.closest('ion-segment'));
10470
if (segmentEl) {
10571
this.updateState();
10672
addEventListener(segmentEl, 'ionSelect', this.updateState);
10773
addEventListener(segmentEl, 'ionStyle', this.updateStyle);
10874
}
75+
}
76+
77+
78+
79+
disconnectedCallback() {
80+
const segmentEl = this.segmentEl;
81+
if (segmentEl) {
82+
removeEventListener(segmentEl, 'ionSelect', this.updateState);
83+
removeEventListener(segmentEl, 'ionStyle', this.updateStyle);
84+
this.segmentEl = null;
85+
}
86+
}
87+
88+
componentWillLoad() {
89+
this.inheritedAttributes = {
90+
...inheritAttributes(this.el, ['aria-label']),
91+
};
10992

11093
// Return if there is no contentId defined
11194
if (!this.contentId) return;
11295

113-
let segmentContent;
114-
try {
115-
// Attempt to find the Segment Content by its contentId
116-
segmentContent = await this.waitForSegmentContent(segmentEl, this.contentId);
117-
} catch (error) {
118-
// If no associated Segment Content exists, log an error and return
119-
console.error('Segment Button: ', (error as Error).message);
96+
// Attempt to find the Segment Content by its contentId
97+
const segmentContent = document.getElementById(this.contentId) as HTMLIonSegmentContentElement | null;
98+
99+
// If no associated Segment Content exists, log an error and return
100+
if (!segmentContent) {
101+
console.error(`Segment Button: Unable to find Segment Content with id="${this.contentId}".`);
120102
return;
121103
}
122104

@@ -133,21 +115,6 @@ export class SegmentButton implements ComponentInterface, ButtonInterface {
133115
}
134116
}
135117

136-
disconnectedCallback() {
137-
const segmentEl = this.segmentEl;
138-
if (segmentEl) {
139-
removeEventListener(segmentEl, 'ionSelect', this.updateState);
140-
removeEventListener(segmentEl, 'ionStyle', this.updateStyle);
141-
this.segmentEl = null;
142-
}
143-
}
144-
145-
componentWillLoad() {
146-
this.inheritedAttributes = {
147-
...inheritAttributes(this.el, ['aria-label']),
148-
};
149-
}
150-
151118
private get hasLabel() {
152119
return !!this.el.querySelector('ion-label');
153120
}

0 commit comments

Comments
 (0)