mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2026-03-13 10:22:08 +08:00
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:
@@ -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'));
|
||||
|
||||
24
core/src/components/segment/test/segment.spec.ts
Normal file
24
core/src/components/segment/test/segment.spec.ts
Normal 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);
|
||||
});
|
||||
Reference in New Issue
Block a user