Skip to content

Commit 9103c40

Browse files
committed
docs(segment-view): document setContent method and add example
1 parent e6f76d5 commit 9103c40

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

core/src/components.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2723,6 +2723,11 @@ export namespace Components {
27232723
* If `true`, the segment view cannot be interacted with.
27242724
*/
27252725
"disabled": boolean;
2726+
/**
2727+
* This method is used to programmatically set the displayed segment content in the segment view. Calling this method will update the `value` of the corresponding segment button.
2728+
* @param id : The id of the segment content to display.
2729+
* @param smoothScroll : Whether to animate the scroll transition.
2730+
*/
27262731
"setContent": (id: string, smoothScroll?: boolean) => Promise<void>;
27272732
}
27282733
interface IonSelect {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ export class SegmentView implements ComponentInterface {
3636
}
3737
}
3838

39+
/**
40+
* This method is used to programmatically set the displayed segment content
41+
* in the segment view. Calling this method will update the `value` of the
42+
* corresponding segment button.
43+
* @param id: The id of the segment content to display.
44+
* @param smoothScroll: Whether to animate the scroll transition.
45+
*/
3946
@Method()
4047
async setContent(id: string, smoothScroll = true) {
4148
const contents = this.getSegmentContents();

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

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848

4949
<ion-content>
5050
<ion-toolbar>
51-
<ion-segment>
51+
<ion-segment id="noValueSegment">
5252
<ion-segment-button content-id="no" value="no">
5353
<ion-label>No</ion-label>
5454
</ion-segment-button>
@@ -57,7 +57,7 @@
5757
</ion-segment-button>
5858
</ion-segment>
5959
</ion-toolbar>
60-
<ion-segment-view>
60+
<ion-segment-view id="noValueSegmentView">
6161
<ion-segment-content id="no">No</ion-segment-content>
6262
<ion-segment-content id="value">Value</ion-segment-content>
6363
</ion-segment-view>
@@ -76,6 +76,7 @@
7676
<ion-segment-content id="favorites">Favorites</ion-segment-content>
7777
</ion-segment-view>
7878

79+
<ion-toolbar>
7980
<ion-segment value="free">
8081
<ion-segment-button content-id="paid" value="paid">
8182
<ion-label>Paid</ion-label>
@@ -112,13 +113,41 @@
112113
<ion-segment-content id="reading-list">Reading List</ion-segment-content>
113114
<ion-segment-content id="shared-links">Shared Links</ion-segment-content>
114115
</ion-segment-view>
116+
117+
<button class="expand" onClick="changeSegmentContent()">
118+
Change Segment Content
119+
</button>
120+
121+
<button class="expand" onClick="clearSegmentValue()">
122+
Clear Segment Value
123+
</button>
115124
</ion-content>
116125

117126
<ion-footer>
118127
<ion-toolbar>
119128
<ion-title>Footer</ion-title>
120129
</ion-toolbar>
121130
</ion-footer>
131+
132+
<script>
133+
let currentValue;
134+
135+
function changeSegmentContent() {
136+
const segmentView = document.querySelector('#noValueSegmentView');
137+
138+
if (currentValue === 'value') {
139+
currentValue = 'no';
140+
} else {
141+
currentValue = 'value';
142+
}
143+
segmentView.setContent(currentValue);
144+
}
145+
146+
function clearSegmentValue() {
147+
const segment = document.querySelector('#noValueSegment');
148+
segment.value = undefined;
149+
}
150+
</script>
122151
</ion-app>
123152
</body>
124153
</html>

0 commit comments

Comments
 (0)