|
| 1 | +/** |
| 2 | + * The examples provided by Facebook are for non-commercial testing and |
| 3 | + * evaluation purposes only. |
| 4 | + * |
| 5 | + * Facebook reserves all rights not expressly granted. |
| 6 | + * |
| 7 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 8 | + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 9 | + * FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL |
| 10 | + * FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN |
| 11 | + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 12 | + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 13 | + * |
| 14 | + * @flow |
| 15 | + */ |
| 16 | +'use strict'; |
| 17 | + |
| 18 | +var React = require('react-native'); |
| 19 | +var { |
| 20 | + SegmentedControlIOS, |
| 21 | + Text, |
| 22 | + View, |
| 23 | + StyleSheet |
| 24 | +} = React; |
| 25 | + |
| 26 | +var BasicSegmentedControlExample = React.createClass({ |
| 27 | + render() { |
| 28 | + return ( |
| 29 | + <View> |
| 30 | + <View style={{marginBottom: 10}}> |
| 31 | + <SegmentedControlIOS values={['One', 'Two']} /> |
| 32 | + </View> |
| 33 | + <View> |
| 34 | + <SegmentedControlIOS values={['One', 'Two', 'Three', 'Four', 'Five']} /> |
| 35 | + </View> |
| 36 | + </View> |
| 37 | + ); |
| 38 | + } |
| 39 | +}); |
| 40 | + |
| 41 | +var PreSelectedSegmentedControlExample = React.createClass({ |
| 42 | + render() { |
| 43 | + return ( |
| 44 | + <View> |
| 45 | + <View> |
| 46 | + <SegmentedControlIOS values={['One', 'Two']} selectedIndex={0} /> |
| 47 | + </View> |
| 48 | + </View> |
| 49 | + ); |
| 50 | + } |
| 51 | +}); |
| 52 | + |
| 53 | +var MomentarySegmentedControlExample = React.createClass({ |
| 54 | + render() { |
| 55 | + return ( |
| 56 | + <View> |
| 57 | + <View> |
| 58 | + <SegmentedControlIOS values={['One', 'Two']} momentary={true} /> |
| 59 | + </View> |
| 60 | + </View> |
| 61 | + ); |
| 62 | + } |
| 63 | +}); |
| 64 | + |
| 65 | +var DisabledSegmentedControlExample = React.createClass({ |
| 66 | + render() { |
| 67 | + return ( |
| 68 | + <View> |
| 69 | + <View> |
| 70 | + <SegmentedControlIOS enabled={false} values={['One', 'Two']} selectedIndex={1} /> |
| 71 | + </View> |
| 72 | + </View> |
| 73 | + ); |
| 74 | + }, |
| 75 | +}); |
| 76 | + |
| 77 | +var ColorSegmentedControlExample = React.createClass({ |
| 78 | + render() { |
| 79 | + return ( |
| 80 | + <View> |
| 81 | + <View style={{marginBottom: 10}}> |
| 82 | + <SegmentedControlIOS tintColor="#ff0000" values={['One', 'Two', 'Three', 'Four']} selectedIndex={0} /> |
| 83 | + </View> |
| 84 | + <View> |
| 85 | + <SegmentedControlIOS tintColor="#00ff00" values={['One', 'Two', 'Three']} selectedIndex={1} /> |
| 86 | + </View> |
| 87 | + </View> |
| 88 | + ); |
| 89 | + }, |
| 90 | +}); |
| 91 | + |
| 92 | +var EventSegmentedControlExample = React.createClass({ |
| 93 | + getInitialState() { |
| 94 | + return { |
| 95 | + values: ['One', 'Two', 'Three'], |
| 96 | + value: 'Not selected', |
| 97 | + selectedIndex: undefined |
| 98 | + }; |
| 99 | + }, |
| 100 | + |
| 101 | + render() { |
| 102 | + return ( |
| 103 | + <View> |
| 104 | + <Text style={styles.text} > |
| 105 | + Value: {this.state.value} |
| 106 | + </Text> |
| 107 | + <Text style={styles.text} > |
| 108 | + Index: {this.state.selectedIndex} |
| 109 | + </Text> |
| 110 | + <SegmentedControlIOS |
| 111 | + values={this.state.values} |
| 112 | + selectedIndex={this.state.selectedIndex} |
| 113 | + onChange={this._onChange} |
| 114 | + onValueChange={this._onValueChange} /> |
| 115 | + </View> |
| 116 | + ); |
| 117 | + }, |
| 118 | + |
| 119 | + _onChange(event) { |
| 120 | + this.setState({ |
| 121 | + selectedIndex: event.nativeEvent.selectedIndex, |
| 122 | + }); |
| 123 | + }, |
| 124 | + |
| 125 | + _onValueChange(value) { |
| 126 | + this.setState({ |
| 127 | + value: value, |
| 128 | + }); |
| 129 | + } |
| 130 | +}); |
| 131 | + |
| 132 | +var styles = StyleSheet.create({ |
| 133 | + text: { |
| 134 | + fontSize: 14, |
| 135 | + textAlign: 'center', |
| 136 | + fontWeight: '500', |
| 137 | + margin: 10, |
| 138 | + }, |
| 139 | +}); |
| 140 | + |
| 141 | +exports.title = '<SegmentedControlIOS>'; |
| 142 | +exports.displayName = 'SegmentedControlExample'; |
| 143 | +exports.description = 'Native segmented control'; |
| 144 | +exports.examples = [ |
| 145 | + { |
| 146 | + title: 'Segmented controls can have values', |
| 147 | + render(): ReactElement { return <BasicSegmentedControlExample />; } |
| 148 | + }, |
| 149 | + { |
| 150 | + title: 'Segmented controls can have a pre-selected value', |
| 151 | + render(): ReactElement { return <PreSelectedSegmentedControlExample />; } |
| 152 | + }, |
| 153 | + { |
| 154 | + title: 'Segmented controls can be momentary', |
| 155 | + render(): ReactElement { return <MomentarySegmentedControlExample />; } |
| 156 | + }, |
| 157 | + { |
| 158 | + title: 'Segmented controls can be disabled', |
| 159 | + render(): ReactElement { return <DisabledSegmentedControlExample />; } |
| 160 | + }, |
| 161 | + { |
| 162 | + title: 'Custom colors can be provided', |
| 163 | + render(): ReactElement { return <ColorSegmentedControlExample />; } |
| 164 | + }, |
| 165 | + { |
| 166 | + title: 'Change events can be detected', |
| 167 | + render(): ReactElement { return <EventSegmentedControlExample />; } |
| 168 | + } |
| 169 | +]; |
0 commit comments