7575< h4 > < a class ="anchor " aria-hidden ="true " id ="programmatically-changing-selected-index "> </ a > < a href ="#programmatically-changing-selected-index " aria-hidden ="true " class ="hash-link "> < svg class ="hash-link-icon " aria-hidden ="true " height ="16 " version ="1.1 " viewBox ="0 0 16 16 " width ="16 "> < path fill-rule ="evenodd " d ="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z "> </ path > </ svg > </ a > Programmatically changing selected index</ h4 >
7676< p > The selected index can be changed on the fly by assigning the selectedIndex prop to a state variable, then changing that variable. Note that the state variable would need to be updated as the user selects a value and changes the index, as shown in the example below.</ p >
7777< h2 > < a class ="anchor " aria-hidden ="true " id ="example "> </ a > < a href ="#example " aria-hidden ="true " class ="hash-link "> < svg class ="hash-link-icon " aria-hidden ="true " height ="16 " version ="1.1 " viewBox ="0 0 16 16 " width ="16 "> < path fill-rule ="evenodd " d ="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z "> </ path > </ svg > </ a > Example</ h2 >
78- < pre > < code class ="hljs css language-jsx "> < span class ="token tag "> < span class ="token tag "> < span class ="token punctuation "> <</ span > < span class ="token class-name "> SegmentedControlIOS</ span > </ span >
79- < span class ="token attr-name "> values</ span > < span class ="token script language-javascript "> < span class ="token script-punctuation punctuation "> =</ span > < span class ="token punctuation "> {</ span > < span class ="token punctuation "> [</ span > < span class ="token string "> 'One'</ span > < span class ="token punctuation "> ,</ span > < span class ="token string "> 'Two'</ span > < span class ="token punctuation "> ]</ span > < span class ="token punctuation "> }</ span > </ span >
80- < span class ="token attr-name "> selectedIndex</ span > < span class ="token script language-javascript "> < span class ="token script-punctuation punctuation "> =</ span > < span class ="token punctuation "> {</ span > < span class ="token keyword "> this</ span > < span class ="token punctuation "> .</ span > state< span class ="token punctuation "> .</ span > selectedIndex< span class ="token punctuation "> }</ span > </ span >
81- < span class ="token attr-name "> onChange</ span > < span class ="token script language-javascript "> < span class ="token script-punctuation punctuation "> =</ span > < span class ="token punctuation "> {</ span > < span class ="token punctuation "> (</ span > < span class ="token parameter "> event</ span > < span class ="token punctuation "> )</ span > < span class ="token operator "> => </ span > < span class ="token punctuation "> {</ span >
82- < span class ="token keyword "> this</ span > < span class ="token punctuation "> .</ span > < span class ="token function "> setState</ span > < span class ="token punctuation "> (</ span > < span class ="token punctuation "> {</ span > selectedIndex< span class ="token punctuation "> :</ span > event< span class ="token punctuation "> .</ span > nativeEvent< span class ="token punctuation "> .</ span > selectedSegmentIndex< span class ="token punctuation "> }</ span > < span class ="token punctuation "> )</ span > < span class ="token punctuation "> ;</ span >
83- < span class ="token punctuation "> }</ span > < span class ="token punctuation "> }</ span > </ span >
84- < span class ="token punctuation "> /> </ span > </ span >
85- </ code > </ pre >
86- < p > < center > < img src ="/docs/assets/SegmentedControlIOS/example.gif " width ="360 "> </ img > </ center > </ p >
87- < hr >
78+ < div class ="snack-player "> < div class ="mobile-friendly-snack " style ="display: none "> < pre > < code class ="hljs css javascript "> < span class ="hljs-keyword "> import</ span > React, { useState } < span class ="hljs-keyword "> from</ span > < span class ="hljs-string "> "react"</ span > ;
79+ < span class ="hljs-keyword "> import</ span > { SegmentedControlIOS, StyleSheet, Text, View } < span class ="hljs-keyword "> from</ span > < span class ="hljs-string "> "react-native"</ span > ;
80+
81+ < span class ="hljs-keyword "> export</ span > < span class ="hljs-keyword "> default</ span > App = < span class ="hljs-function "> < span class ="hljs-params "> ()</ span > =></ span > {
82+ < span class ="hljs-keyword "> const</ span > [index, setIndex] = useState(< span class ="hljs-number "> 0</ span > );
83+ < span class ="hljs-keyword "> return</ span > (
84+ < span class ="xml "> < span class ="hljs-tag "> << span class ="hljs-name "> View</ span > < span class ="hljs-attr "> style</ span > =< span class ="hljs-string "> {styles.container}</ span > ></ span >
85+ < span class ="hljs-tag "> << span class ="hljs-name "> SegmentedControlIOS</ span >
86+ < span class ="hljs-attr "> values</ span > =< span class ="hljs-string "> {[</ span > '< span class ="hljs-attr "> One</ span > ', '< span class ="hljs-attr "> Two</ span > ']}
87+ < span class ="hljs-attr "> selectedIndex</ span > =< span class ="hljs-string "> {index}</ span >
88+ < span class ="hljs-attr "> onChange</ span > =< span class ="hljs-string "> {(event)</ span > =></ span > {
89+ setIndex(event.nativeEvent.selectedSegmentIndex);
90+ }}
91+ />
92+ < span class ="hljs-tag "> << span class ="hljs-name "> Text</ span > < span class ="hljs-attr "> style</ span > =< span class ="hljs-string "> {styles.text}</ span > ></ span >
93+ Selected index: {index}
94+ < span class ="hljs-tag "> </< span class ="hljs-name "> Text</ span > ></ span >
95+ < span class ="hljs-tag "> </< span class ="hljs-name "> View</ span > ></ span >
96+ );
97+ };
98+
99+ const styles = StyleSheet.create({
100+ container: {
101+ flex: 1,
102+ padding: 24,
103+ justifyContent: "center"
104+ },
105+ text: {
106+ marginTop: 24
107+ }
108+ });
109+ </ span > </ code > </ pre > </ div > < div class ="desktop-friendly-snack " style ="margin-top: 15px; margin-bottom: 15px "> < div
110+ data-snack-name ="SegmentedControlIOS Example "
111+ data-snack-description ="Example usage "
112+ data-snack-code ="import%20React%2C%20%7B%20useState%20%7D%20from%20%22react%22%3B%0Aimport%20%7B%20SegmentedControlIOS%2C%20StyleSheet%2C%20Text%2C%20View%20%7D%20from%20%22react-native%22%3B%0A%0Aexport%20default%20App%20%3D%20()%20%3D%3E%20%7B%0A%20%20const%20%5Bindex%2C%20setIndex%5D%20%3D%20useState(0)%3B%0A%20%20return%20(%0A%20%20%20%20%3CView%20style%3D%7Bstyles.container%7D%3E%0A%20%20%20%20%20%20%3CSegmentedControlIOS%0A%20%20%20%20%20%20%20%20values%3D%7B%5B'One'%2C%20'Two'%5D%7D%0A%20%20%20%20%20%20%20%20selectedIndex%3D%7Bindex%7D%0A%20%20%20%20%20%20%20%20onChange%3D%7B(event)%20%3D%3E%20%7B%0A%20%20%20%20%20%20%20%20%20%20setIndex(event.nativeEvent.selectedSegmentIndex)%3B%0A%20%20%20%20%20%20%20%20%7D%7D%0A%20%20%20%20%20%20%2F%3E%0A%20%20%20%20%20%20%3CText%20style%3D%7Bstyles.text%7D%3E%0A%20%20%20%20%20%20%20%20Selected%20index%3A%20%7Bindex%7D%0A%20%20%20%20%20%20%3C%2FText%3E%0A%20%20%20%20%3C%2FView%3E%0A%20%20)%3B%0A%7D%3B%0A%0Aconst%20styles%20%3D%20StyleSheet.create(%7B%0A%20%20container%3A%20%7B%0A%20%20%20%20flex%3A%201%2C%0A%20%20%20%20padding%3A%2024%2C%0A%20%20%20%20justifyContent%3A%20%22center%22%0A%20%20%7D%2C%0A%20%20text%3A%20%7B%0A%20%20%20%20marginTop%3A%2024%0A%20%20%7D%0A%7D)%3B%0A "
113+ data-snack-platform ="web "
114+ data-snack-supported-platforms =ios
115+ data-snack-preview ="true "
116+ style ="
117+ overflow: hidden;
118+ background: #fafafa;
119+ border: 1px solid rgba(0,0,0,.16);
120+ border-radius: 4px;
121+ height: 514px;
122+ width: 100%;
123+ "
124+ > </ div > </ div > </ div > < hr >
88125< h1 > < a class ="anchor " aria-hidden ="true " id ="reference "> </ a > < a href ="#reference " aria-hidden ="true " class ="hash-link "> < svg class ="hash-link-icon " aria-hidden ="true " height ="16 " version ="1.1 " viewBox ="0 0 16 16 " width ="16 "> < path fill-rule ="evenodd " d ="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z "> </ path > </ svg > </ a > Reference</ h1 >
89126< h2 > < a class ="anchor " aria-hidden ="true " id ="props "> </ a > < a href ="#props " aria-hidden ="true " class ="hash-link "> < svg class ="hash-link-icon " aria-hidden ="true " height ="16 " version ="1.1 " viewBox ="0 0 16 16 " width ="16 "> < path fill-rule ="evenodd " d ="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z "> </ path > </ svg > </ a > Props</ h2 >
90127< p > Inherits < a href ="/docs/0.61/view#props "> View Props</ a > .</ p >
@@ -98,7 +135,6 @@ <h3><a class="anchor" aria-hidden="true" id="enabled"></a><a href="#enabled" ari
98135< tr > < td > bool</ td > < td > No</ td > </ tr >
99136</ tbody >
100137</ table >
101- < p > < center > < img src ="/docs/assets/SegmentedControlIOS/enabled.png " width ="360 "> </ img > </ center > </ p >
102138< hr >
103139< h3 > < a class ="anchor " aria-hidden ="true " id ="momentary "> </ a > < a href ="#momentary " aria-hidden ="true " class ="hash-link "> < svg class ="hash-link-icon " aria-hidden ="true " height ="16 " version ="1.1 " viewBox ="0 0 16 16 " width ="16 "> < path fill-rule ="evenodd " d ="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z "> </ path > </ svg > </ a > < code > momentary</ code > </ h3 >
104140< p > If true, then selecting a segment won't persist visually. The < code > onValueChange</ code > callback will still work as expected.</ p >
@@ -110,7 +146,6 @@ <h3><a class="anchor" aria-hidden="true" id="momentary"></a><a href="#momentary"
110146< tr > < td > bool</ td > < td > No</ td > </ tr >
111147</ tbody >
112148</ table >
113- < p > < center > < img src ="/docs/assets/SegmentedControlIOS/momentary.gif " width ="360 "> </ img > </ center > </ p >
114149< hr >
115150< h3 > < a class ="anchor " aria-hidden ="true " id ="onchange "> </ a > < a href ="#onchange " aria-hidden ="true " class ="hash-link "> < svg class ="hash-link-icon " aria-hidden ="true " height ="16 " version ="1.1 " viewBox ="0 0 16 16 " width ="16 "> < path fill-rule ="evenodd " d ="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z "> </ path > </ svg > </ a > < code > onChange</ code > </ h3 >
116151< p > Callback that is called when the user taps a segment; passes the event as an argument</ p >
@@ -155,7 +190,6 @@ <h3><a class="anchor" aria-hidden="true" id="tintcolor"></a><a href="#tintcolor"
155190< tr > < td > string</ td > < td > No</ td > </ tr >
156191</ tbody >
157192</ table >
158- < p > < center > < img src ="/docs/assets/SegmentedControlIOS/tintColor.png " width ="360 "> </ img > </ center > </ p >
159193< hr >
160194< h3 > < a class ="anchor " aria-hidden ="true " id ="values "> </ a > < a href ="#values " aria-hidden ="true " class ="hash-link "> < svg class ="hash-link-icon " aria-hidden ="true " height ="16 " version ="1.1 " viewBox ="0 0 16 16 " width ="16 "> < path fill-rule ="evenodd " d ="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z "> </ path > </ svg > </ a > < code > values</ code > </ h3 >
161195< p > The labels for the control's segment buttons, in order.</ p >
0 commit comments