Skip to content

Commit

Permalink
fix(segment-button): update checked state on render (#27183)
Browse files Browse the repository at this point in the history
This backports #26970
to v6.

resolves #26830

Co-authored-by: Sean Perkins <sean@ionic.io>
  • Loading branch information
liamdebeasi and sean-perkins authored Apr 12, 2023
1 parent d619b85 commit ae9689b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
6 changes: 5 additions & 1 deletion core/src/components/segment-button/segment-button.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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'));
Expand Down
24 changes: 24 additions & 0 deletions core/src/components/segment/test/segment.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});

0 comments on commit ae9689b

Please sign in to comment.