From ae9689bf3e185749dd2e0b27864b1c99b129820e Mon Sep 17 00:00:00 2001 From: Liam DeBeasi Date: Wed, 12 Apr 2023 12:16:19 -0400 Subject: [PATCH] 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 --- .../segment-button/segment-button.tsx | 6 ++++- .../components/segment/test/segment.spec.ts | 24 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 core/src/components/segment/test/segment.spec.ts diff --git a/core/src/components/segment-button/segment-button.tsx b/core/src/components/segment-button/segment-button.tsx index 710af58c3f..019fbc4859 100644 --- a/core/src/components/segment-button/segment-button.tsx +++ b/core/src/components/segment-button/segment-button.tsx @@ -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')); diff --git a/core/src/components/segment/test/segment.spec.ts b/core/src/components/segment/test/segment.spec.ts new file mode 100644 index 0000000000..693d0649a5 --- /dev/null +++ b/core/src/components/segment/test/segment.spec.ts @@ -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: ` + + First + + `, + }); + + 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); +});