Skip to content

Commit 44be424

Browse files
fix(segment): segment drag would set disabled segment button checked (#31112)
Issue number: resolves internal --------- <!-- Please do not submit updates to dependencies unless it fixes an issue. --> <!-- Please try to limit your pull request to one type (bugfix, feature, etc). Submit multiple pull requests if needed. --> ## What is the current behavior? - When the first segment is disabled and the last item is selected, if the user swipes the `segment-view`, the first `segment-button` gets checked even if it is disabled, and the view is presenting other `segment-view`. ## What is the new behavior? - A validation was added to ensure that the `segment-button` can only be checked if it is not disabled ## Does this introduce a breaking change? - [ ] Yes - [x] No <!-- If this introduces a breaking change: 1. Describe the impact and migration path for existing applications below. 2. Update the BREAKING.md file with the breaking change. 3. Add "BREAKING CHANGE: [...]" to the commit description when merging. See https://github.com/ionic-team/ionic-framework/blob/main/docs/CONTRIBUTING.md#footer for more information. -->
1 parent c18502f commit 44be424

3 files changed

Lines changed: 53 additions & 0 deletions

File tree

core/src/components/segment-view/test/basic/index.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,22 @@
7979
<ion-segment-content id="top">Top</ion-segment-content>
8080
</ion-segment-view>
8181

82+
<ion-segment id="disabledFirstSegment" value="third">
83+
<ion-segment-button value="first" disabled>
84+
<ion-label>First</ion-label>
85+
</ion-segment-button>
86+
<ion-segment-button content-id="second-content" value="second">
87+
<ion-label>Second</ion-label>
88+
</ion-segment-button>
89+
<ion-segment-button content-id="third-content" value="third">
90+
<ion-label>Third</ion-label>
91+
</ion-segment-button>
92+
</ion-segment>
93+
<ion-segment-view id="disabledFirstSegmentView">
94+
<ion-segment-content id="second-content">Second Content</ion-segment-content>
95+
<ion-segment-content id="third-content">Third Content</ion-segment-content>
96+
</ion-segment-view>
97+
8298
<ion-segment value="peach" scrollable>
8399
<ion-segment-button content-id="orange" value="orange">
84100
<ion-label>Orange</ion-label>

core/src/components/segment-view/test/basic/segment-view.e2e.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,5 +169,38 @@ configs({ modes: ['md'] }).forEach(({ title, config }) => {
169169
const segmentContent = page.locator('ion-segment-content[id="top"]');
170170
await expect(segmentContent).toBeInViewport();
171171
});
172+
test('should not select a disabled first segment button when dragging segment-view from last content', async ({
173+
page,
174+
}) => {
175+
await page.setContent(
176+
`
177+
<ion-segment id="disabledFirstSegment" scrollable="true" value="third">
178+
<ion-segment-button value="first" disabled>
179+
<ion-label>First</ion-label>
180+
</ion-segment-button>
181+
<ion-segment-button content-id="second-content" value="second">
182+
<ion-label>Second</ion-label>
183+
</ion-segment-button>
184+
<ion-segment-button content-id="third-content" value="third">
185+
<ion-label>Third</ion-label>
186+
</ion-segment-button>
187+
</ion-segment>
188+
<ion-segment-view id="disabledFirstSegmentView">
189+
<ion-segment-content id="second-content">Second Content</ion-segment-content>
190+
<ion-segment-content id="third-content">Third Content</ion-segment-content>
191+
</ion-segment-view>
192+
`,
193+
config
194+
);
195+
await page.waitForChanges();
196+
197+
await page.locator('ion-segment-view').evaluate((el: HTMLElement) => {
198+
const max = el.scrollWidth - el.clientWidth;
199+
el.scrollLeft = max;
200+
});
201+
await page.waitForChanges();
202+
203+
await expect(page.locator('ion-segment-button[value="first"]')).not.toHaveClass(/segment-button-checked/);
204+
});
172205
});
173206
});

core/src/components/segment/segment.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,10 @@ export class Segment implements ComponentInterface {
400400

401401
const nextIndex = Math.round(scrollRatio * (buttons.length - 1));
402402

403+
if (buttons[nextIndex]?.disabled) {
404+
return;
405+
}
406+
403407
if (this.lastNextIndex === undefined || this.lastNextIndex !== nextIndex) {
404408
this.lastNextIndex = nextIndex;
405409
this.triggerScrollOnValueChange = false;

0 commit comments

Comments
 (0)