-
Notifications
You must be signed in to change notification settings - Fork 24.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: Fixes #534: ![screen shot 2015-03-31 at 7 52 10 pm](https://cloud.githubusercontent.com/assets/153704/6934038/742ddd34-d7e3-11e4-8f55-3eb7d9d3f1cd.png) ```jsx <SegmentedControlIOS tintColor="#ff0000" values={['One', 'Two', 'Three', 'Four']} selectedtIndex={0} momentary={false} enabled={true} onValueChange={ (value) => console.log(value) } /> ``` This only supports string-based segments, not images. Also doesn't support full customization (no separator images etc); I figure this is a good MVP to lock-down a basic API I also included a snapshot test case, but the images keep coming out funky. When I look at the sim, I see that the text labels show up for the selected segment, but the snapshot keeps coming out with no text on those segments. I tried forcing a delay, but same result. Is that explainable? Obviously happy to change anything about the API, code-style nitpicks, etc Closes #564 Github Author: Clay Allsopp <clay.allsopp@gmail.com> Test Plan: Imported from GitHub, without a `Test Plan:` line.
- Loading branch information
1 parent
c82c6a0
commit 11f2047
Showing
9 changed files
with
433 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
/** | ||
* The examples provided by Facebook are for non-commercial testing and | ||
* evaluation purposes only. | ||
* | ||
* Facebook reserves all rights not expressly granted. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | ||
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL | ||
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN | ||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
* | ||
* @flow | ||
*/ | ||
'use strict'; | ||
|
||
var React = require('react-native'); | ||
var { | ||
SegmentedControlIOS, | ||
Text, | ||
View, | ||
StyleSheet | ||
} = React; | ||
|
||
var BasicSegmentedControlExample = React.createClass({ | ||
render() { | ||
return ( | ||
<View> | ||
<View style={{marginBottom: 10}}> | ||
<SegmentedControlIOS values={['One', 'Two']} /> | ||
</View> | ||
<View> | ||
<SegmentedControlIOS values={['One', 'Two', 'Three', 'Four', 'Five']} /> | ||
</View> | ||
</View> | ||
); | ||
} | ||
}); | ||
|
||
var PreSelectedSegmentedControlExample = React.createClass({ | ||
render() { | ||
return ( | ||
<View> | ||
<View> | ||
<SegmentedControlIOS values={['One', 'Two']} selectedIndex={0} /> | ||
</View> | ||
</View> | ||
); | ||
} | ||
}); | ||
|
||
var MomentarySegmentedControlExample = React.createClass({ | ||
render() { | ||
return ( | ||
<View> | ||
<View> | ||
<SegmentedControlIOS values={['One', 'Two']} momentary={true} /> | ||
</View> | ||
</View> | ||
); | ||
} | ||
}); | ||
|
||
var DisabledSegmentedControlExample = React.createClass({ | ||
render() { | ||
return ( | ||
<View> | ||
<View> | ||
<SegmentedControlIOS enabled={false} values={['One', 'Two']} selectedIndex={1} /> | ||
</View> | ||
</View> | ||
); | ||
}, | ||
}); | ||
|
||
var ColorSegmentedControlExample = React.createClass({ | ||
render() { | ||
return ( | ||
<View> | ||
<View style={{marginBottom: 10}}> | ||
<SegmentedControlIOS tintColor="#ff0000" values={['One', 'Two', 'Three', 'Four']} selectedIndex={0} /> | ||
</View> | ||
<View> | ||
<SegmentedControlIOS tintColor="#00ff00" values={['One', 'Two', 'Three']} selectedIndex={1} /> | ||
</View> | ||
</View> | ||
); | ||
}, | ||
}); | ||
|
||
var EventSegmentedControlExample = React.createClass({ | ||
getInitialState() { | ||
return { | ||
values: ['One', 'Two', 'Three'], | ||
value: 'Not selected', | ||
selectedIndex: undefined | ||
}; | ||
}, | ||
|
||
render() { | ||
return ( | ||
<View> | ||
<Text style={styles.text} > | ||
Value: {this.state.value} | ||
</Text> | ||
<Text style={styles.text} > | ||
Index: {this.state.selectedIndex} | ||
</Text> | ||
<SegmentedControlIOS | ||
values={this.state.values} | ||
selectedIndex={this.state.selectedIndex} | ||
onChange={this._onChange} | ||
onValueChange={this._onValueChange} /> | ||
</View> | ||
); | ||
}, | ||
|
||
_onChange(event) { | ||
this.setState({ | ||
selectedIndex: event.nativeEvent.selectedIndex, | ||
}); | ||
}, | ||
|
||
_onValueChange(value) { | ||
this.setState({ | ||
value: value, | ||
}); | ||
} | ||
}); | ||
|
||
var styles = StyleSheet.create({ | ||
text: { | ||
fontSize: 14, | ||
textAlign: 'center', | ||
fontWeight: '500', | ||
margin: 10, | ||
}, | ||
}); | ||
|
||
exports.title = '<SegmentedControlIOS>'; | ||
exports.displayName = 'SegmentedControlExample'; | ||
exports.description = 'Native segmented control'; | ||
exports.examples = [ | ||
{ | ||
title: 'Segmented controls can have values', | ||
render(): ReactElement { return <BasicSegmentedControlExample />; } | ||
}, | ||
{ | ||
title: 'Segmented controls can have a pre-selected value', | ||
render(): ReactElement { return <PreSelectedSegmentedControlExample />; } | ||
}, | ||
{ | ||
title: 'Segmented controls can be momentary', | ||
render(): ReactElement { return <MomentarySegmentedControlExample />; } | ||
}, | ||
{ | ||
title: 'Segmented controls can be disabled', | ||
render(): ReactElement { return <DisabledSegmentedControlExample />; } | ||
}, | ||
{ | ||
title: 'Custom colors can be provided', | ||
render(): ReactElement { return <ColorSegmentedControlExample />; } | ||
}, | ||
{ | ||
title: 'Change events can be detected', | ||
render(): ReactElement { return <EventSegmentedControlExample />; } | ||
} | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
Libraries/Components/SegmentedControlIOS/SegmentedControlIOS.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* @providesModule SegmentedControlIOS | ||
* @flow | ||
*/ | ||
'use strict'; | ||
|
||
var NativeMethodsMixin = require('NativeMethodsMixin'); | ||
var NativeModules = require('NativeModules'); | ||
var PropTypes = require('ReactPropTypes'); | ||
var React = require('React'); | ||
var StyleSheet = require('StyleSheet'); | ||
|
||
var requireNativeComponent = require('requireNativeComponent'); | ||
var verifyPropTypes = require('verifyPropTypes'); | ||
|
||
type DefaultProps = { | ||
values: Array<string>; | ||
enabled: boolean; | ||
}; | ||
|
||
var SEGMENTED_CONTROL_REFERENCE = 'segmentedcontrol'; | ||
|
||
type Event = Object; | ||
|
||
/** | ||
* Use `SegmentedControlIOS` to render a UISegmentedControl iOS. | ||
*/ | ||
var SegmentedControlIOS = React.createClass({ | ||
mixins: [NativeMethodsMixin], | ||
|
||
propTypes: { | ||
/** | ||
* The labels for the control's segment buttons, in order. | ||
*/ | ||
values: PropTypes.arrayOf(PropTypes.string), | ||
|
||
/** | ||
* The index in `props.values` of the segment to be pre-selected | ||
*/ | ||
selectedIndex: PropTypes.number, | ||
|
||
/** | ||
* Callback that is called when the user taps a segment; | ||
* passes the segment's value as an argument | ||
*/ | ||
onValueChange: PropTypes.func, | ||
|
||
/** | ||
* Callback that is called when the user taps a segment; | ||
* passes the event as an argument | ||
*/ | ||
onChange: PropTypes.func, | ||
|
||
/** | ||
* If false the user won't be able to interact with the control. | ||
* Default value is true. | ||
*/ | ||
enabled: PropTypes.bool, | ||
|
||
/** | ||
* Accent color of the control. | ||
*/ | ||
tintColor: PropTypes.string, | ||
|
||
/** | ||
* If true, then selecting a segment won't persist visually. | ||
* The `onValueChange` callback will still work as expected. | ||
*/ | ||
momentary: PropTypes.bool | ||
}, | ||
|
||
getDefaultProps: function(): DefaultProps { | ||
return { | ||
values: [], | ||
enabled: true | ||
}; | ||
}, | ||
|
||
_onChange: function(event: Event) { | ||
this.props.onChange && this.props.onChange(event); | ||
this.props.onValueChange && this.props.onValueChange(event.nativeEvent.value); | ||
}, | ||
|
||
render: function() { | ||
return ( | ||
<RCTSegmentedControl | ||
{...this.props} | ||
ref={SEGMENTED_CONTROL_REFERENCE} | ||
style={[styles.segmentedControl, this.props.style]} | ||
onChange={this._onChange} | ||
/> | ||
); | ||
} | ||
}); | ||
|
||
var styles = StyleSheet.create({ | ||
segmentedControl: { | ||
height: NativeModules.SegmentedControlManager.ComponentHeight | ||
}, | ||
}); | ||
|
||
var RCTSegmentedControl = requireNativeComponent( | ||
'RCTSegmentedControl', | ||
null | ||
); | ||
if (__DEV__) { | ||
verifyPropTypes( | ||
RCTSegmentedControl, | ||
RCTSegmentedControl.viewConfig | ||
); | ||
} | ||
|
||
module.exports = SegmentedControlIOS; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// | ||
// RCTSegmentedControl.h | ||
// React | ||
// | ||
// Created by Clay Allsopp on 3/31/15. | ||
// Copyright (c) 2015 Facebook. All rights reserved. | ||
// | ||
|
||
#import <UIKit/UIKit.h> | ||
|
||
@class RCTEventDispatcher; | ||
|
||
@interface RCTSegmentedControl : UISegmentedControl | ||
|
||
- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher NS_DESIGNATED_INITIALIZER; | ||
|
||
@property (nonatomic, copy) NSArray *values; | ||
@property (nonatomic, assign) NSInteger selectedIndex; | ||
|
||
@end |
Oops, something went wrong.