Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 { ButtonInterface } from '../../utils/element-interface';
Expand Down Expand Up @@ -54,6 +54,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
20 changes: 20 additions & 0 deletions core/src/components/segment/test/segment.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,23 @@ it('should disable segment buttons added to disabled segment async', async () =>
const segmentButton = page.body.querySelector('ion-segment-button');
expect(segmentButton.disabled).toBe(true);
});

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);
});