Skip to content

Commit ae9689b

Browse files
fix(segment-button): update checked state on render (#27183)
This backports #26970 to v6. resolves #26830 Co-authored-by: Sean Perkins <[email protected]>
1 parent d619b85 commit ae9689b

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { ComponentInterface } from '@stencil/core';
2-
import { Component, Element, Host, Prop, Method, State, forceUpdate, h } from '@stencil/core';
2+
import { Component, Element, Host, Prop, Method, State, Watch, forceUpdate, h } from '@stencil/core';
33

44
import { getIonMode } from '../../global/ionic-global';
55
import type { SegmentButtonLayout } from '../../interface';
@@ -53,6 +53,10 @@ export class SegmentButton implements ComponentInterface, ButtonInterface {
5353
* The value of the segment button.
5454
*/
5555
@Prop() value: string = 'ion-sb-' + ids++;
56+
@Watch('value')
57+
valueChanged() {
58+
this.updateState();
59+
}
5660

5761
connectedCallback() {
5862
const segmentEl = (this.segmentEl = this.el.closest('ion-segment'));
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { newSpecPage } from '@stencil/core/testing';
2+
3+
import { SegmentButton } from '../../segment-button/segment-button';
4+
import { Segment } from '../segment';
5+
6+
it('should set checked state when value is set asynchronously', async () => {
7+
const page = await newSpecPage({
8+
components: [Segment, SegmentButton],
9+
html: `
10+
<ion-segment value="first">
11+
<ion-segment-button>First</ion-segment-button>
12+
</ion-segment>
13+
`,
14+
});
15+
16+
const segmentButton = page.root.querySelector('ion-segment-button');
17+
18+
expect(segmentButton.classList.contains('segment-button-checked')).toBe(false);
19+
20+
segmentButton.value = 'first';
21+
await page.waitForChanges();
22+
23+
expect(segmentButton.classList.contains('segment-button-checked')).toBe(true);
24+
});

0 commit comments

Comments
 (0)