fix(segment-button): update checked state on render (#27183)

This backports https://github.com/ionic-team/ionic-framework/pull/26970
to v6.

resolves https://github.com/ionic-team/ionic-framework/issues/26830

Co-authored-by: Sean Perkins <sean@ionic.io>
This commit is contained in:
Liam DeBeasi
2023-04-12 12:16:19 -04:00
committed by GitHub
parent d619b85a54
commit ae9689bf3e
2 changed files with 29 additions and 1 deletions

View File

@@ -1,5 +1,5 @@
import type { ComponentInterface } from '@stencil/core';
import { Component, Element, Host, Prop, Method, State, forceUpdate, h } from '@stencil/core';
import { Component, Element, Host, Prop, Method, State, Watch, forceUpdate, h } from '@stencil/core';
import { getIonMode } from '../../global/ionic-global';
import type { SegmentButtonLayout } from '../../interface';
@@ -53,6 +53,10 @@ export class SegmentButton implements ComponentInterface, ButtonInterface {
* The value of the segment button.
*/
@Prop() value: string = 'ion-sb-' + ids++;
@Watch('value')
valueChanged() {
this.updateState();
}
connectedCallback() {
const segmentEl = (this.segmentEl = this.el.closest('ion-segment'));

View File

@@ -0,0 +1,24 @@
import { newSpecPage } from '@stencil/core/testing';
import { SegmentButton } from '../../segment-button/segment-button';
import { Segment } from '../segment';
it('should set checked state when value is set asynchronously', async () => {
const page = await newSpecPage({
components: [Segment, SegmentButton],
html: `
<ion-segment value="first">
<ion-segment-button>First</ion-segment-button>
</ion-segment>
`,
});
const segmentButton = page.root.querySelector('ion-segment-button');
expect(segmentButton.classList.contains('segment-button-checked')).toBe(false);
segmentButton.value = 'first';
await page.waitForChanges();
expect(segmentButton.classList.contains('segment-button-checked')).toBe(true);
});