Skip to content

Commit ba28530

Browse files
committed
fix(segment): set the view to the initial value without scrolling
1 parent 44e8374 commit ba28530

File tree

5 files changed

+64
-21
lines changed

5 files changed

+64
-21
lines changed

core/api.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1610,7 +1610,8 @@ ion-segment-button,part,native
16101610
ion-segment-content,shadow
16111611

16121612
ion-segment-view,shadow
1613-
ion-segment-view,method,setContent,setContent(id: string) => Promise<void>
1613+
ion-segment-view,prop,disabled,boolean,false,false,false
1614+
ion-segment-view,method,setContent,setContent(id: string, smoothScroll?: boolean) => Promise<void>
16141615

16151616
ion-select,shadow
16161617
ion-select,prop,cancelText,string,'Cancel',false,false

core/src/components.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2723,7 +2723,7 @@ export namespace Components {
27232723
* If `true`, the segment view cannot be interacted with.
27242724
*/
27252725
"disabled": boolean;
2726-
"setContent": (id: string) => Promise<void>;
2726+
"setContent": (id: string, smoothScroll?: boolean) => Promise<void>;
27272727
}
27282728
interface IonSelect {
27292729
/**

core/src/components/segment-view/segment-view.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class SegmentView implements ComponentInterface {
3737
}
3838

3939
@Method()
40-
async setContent(id: string) {
40+
async setContent(id: string, smoothScroll = true) {
4141
const contents = this.getSegmentContents();
4242
const index = contents.findIndex((content) => content.id === id);
4343

@@ -47,7 +47,7 @@ export class SegmentView implements ComponentInterface {
4747
this.el.scrollTo({
4848
top: 0,
4949
left: index * contentWidth,
50-
behavior: 'smooth',
50+
behavior: smoothScroll ? 'smooth' : 'auto',
5151
});
5252
}
5353

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

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,26 @@
1515

1616
<style>
1717
ion-segment-view {
18-
height: 200px;
18+
height: 100px;
1919
}
2020

21-
ion-segment-content:nth-of-type(even) {
22-
background: lightblue;
21+
ion-segment-content {
22+
display: flex;
23+
justify-content: center;
24+
align-items: center;
2325
}
2426

25-
ion-segment-content:nth-of-type(odd) {
27+
ion-segment-content:nth-of-type(1) {
2628
background: lightpink;
2729
}
30+
31+
ion-segment-content:nth-of-type(2) {
32+
background: lightblue;
33+
}
34+
35+
ion-segment-content:nth-of-type(3) {
36+
background: lightgreen;
37+
}
2838
</style>
2939
</head>
3040

@@ -38,14 +48,42 @@
3848

3949
<ion-content>
4050
<ion-toolbar>
41-
<ion-segment value="Paid">
42-
<ion-segment-button content-id="paid" value="Paid">
51+
<ion-segment>
52+
<ion-segment-button content-id="no" value="no">
53+
<ion-label>No</ion-label>
54+
</ion-segment-button>
55+
<ion-segment-button content-id="value" value="value">
56+
<ion-label>Value</ion-label>
57+
</ion-segment-button>
58+
</ion-segment>
59+
</ion-toolbar>
60+
<ion-segment-view>
61+
<ion-segment-content id="no">No</ion-segment-content>
62+
<ion-segment-content id="value">Value</ion-segment-content>
63+
</ion-segment-view>
64+
65+
<ion-segment value="all">
66+
<ion-segment-button content-id="all" value="all">
67+
<ion-label>All</ion-label>
68+
</ion-segment-button>
69+
<ion-segment-button content-id="favorites" value="favorites">
70+
<ion-label>Favorites</ion-label>
71+
</ion-segment-button>
72+
</ion-segment>
73+
</ion-toolbar>
74+
<ion-segment-view>
75+
<ion-segment-content id="all">All</ion-segment-content>
76+
<ion-segment-content id="favorites">Favorites</ion-segment-content>
77+
</ion-segment-view>
78+
79+
<ion-segment value="free">
80+
<ion-segment-button content-id="paid" value="paid">
4381
<ion-label>Paid</ion-label>
4482
</ion-segment-button>
45-
<ion-segment-button content-id="free" value="Free">
83+
<ion-segment-button content-id="free" value="free">
4684
<ion-label>Free</ion-label>
4785
</ion-segment-button>
48-
<ion-segment-button content-id="top" value="Top">
86+
<ion-segment-button content-id="top" value="top">
4987
<ion-label>Top</ion-label>
5088
</ion-segment-button>
5189
</ion-segment>
@@ -57,14 +95,14 @@
5795
</ion-segment-view>
5896

5997
<ion-toolbar>
60-
<ion-segment disabled color="danger">
61-
<ion-segment-button content-id="bookmarks" value="Bookmarks">
98+
<ion-segment disabled value="reading-list">
99+
<ion-segment-button content-id="bookmarks" value="bookmarks">
62100
<ion-label>Bookmarks</ion-label>
63101
</ion-segment-button>
64-
<ion-segment-button content-id="reading-list" value="Reading List">
102+
<ion-segment-button content-id="reading-list" value="reading-list">
65103
<ion-label>Reading List</ion-label>
66104
</ion-segment-button>
67-
<ion-segment-button content-id="shared-links" value="Shared Links">
105+
<ion-segment-button content-id="shared-links" value="shared-links">
68106
<ion-label>Shared Links</ion-label>
69107
</ion-segment-button>
70108
</ion-segment>

core/src/components/segment/segment.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ export class Segment implements ComponentInterface {
180180
if (this.disabled) {
181181
this.disabledChanged();
182182
}
183+
184+
// Update segment view based on the initial value,
185+
// but do not animate the scroll
186+
this.updateSegmentView(false);
183187
}
184188

185189
onStart(detail: GestureDetail) {
@@ -322,11 +326,11 @@ export class Segment implements ComponentInterface {
322326
/**
323327
* Finds the related segment view and sets its current content
324328
* based on the selected segment button. This method
325-
* should be called only after the gesture is completed
326-
* (if dragging between segments) or when a segment button
327-
* is clicked directly.
329+
* should be called on initial load of the segment,
330+
* after the gesture is completed (if dragging between segments)
331+
* and when a segment button is clicked directly.
328332
*/
329-
private updateSegmentView() {
333+
private updateSegmentView(smoothScroll = true) {
330334
const buttons = this.getButtons();
331335
const button = buttons.find((btn) => btn.value === this.value);
332336

@@ -340,7 +344,7 @@ export class Segment implements ComponentInterface {
340344
const segmentView = content?.closest('ion-segment-view');
341345

342346
if (segmentView) {
343-
segmentView.setContent(button.contentId);
347+
segmentView.setContent(button.contentId, smoothScroll);
344348
}
345349
}
346350

0 commit comments

Comments
 (0)