Skip to content

Commit

Permalink
update API for selectedSegmentIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
clayallsopp committed Apr 1, 2015
1 parent 67a91e0 commit 06c2d95
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 37 deletions.
36 changes: 28 additions & 8 deletions Examples/UIExplorer/SegmentedControlIOSExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var PreSelectedSegmentedControlExample = React.createClass({
return (
<View>
<View>
<SegmentedControlIOS values={["One", "Two"]} selectedValue="One" />
<SegmentedControlIOS values={["One", "Two"]} selectedSegmentIndex={0} />
</View>
</View>
);
Expand All @@ -67,7 +67,7 @@ var DisabledSegmentedControlExample = React.createClass({
return (
<View>
<View>
<SegmentedControlIOS disabled={true} values={["One", "Two"]} selectedValue="Two" />
<SegmentedControlIOS disabled={true} values={["One", "Two"]} selectedSegmentIndex={1} />
</View>
</View>
);
Expand All @@ -79,10 +79,10 @@ var ColorSegmentedControlExample = React.createClass({
return (
<View>
<View style={{marginBottom: 10}}>
<SegmentedControlIOS tintColor="#ff0000" values={["One", "Two", "Three", "Four"]} selectedValue={"One"} />
<SegmentedControlIOS tintColor="#ff0000" values={["One", "Two", "Three", "Four"]} selectedSegmentIndex={0} />
</View>
<View>
<SegmentedControlIOS tintColor="#00ff00" values={["One", "Two", "Three"]} selectedValue={"Two"} />
<SegmentedControlIOS tintColor="#00ff00" values={["One", "Two", "Three"]} selectedSegmentIndex={1} />
</View>
</View>
);
Expand All @@ -92,20 +92,40 @@ var ColorSegmentedControlExample = React.createClass({
var EventSegmentedControlExample = React.createClass({
getInitialState() {
return {
value: 'Not selected'
values: ["One", "Two", "Three"],
value: 'Not selected',
selectedSegmentIndex: undefined
};
},

render() {
return (
<View>
<Text style={styles.text} >
{this.state.value}
Value: {this.state.value}
</Text>
<Text style={styles.text} >
Index: {this.state.selectedSegmentIndex}
</Text>
<SegmentedControlIOS
values={["One", "Two", "Three"]}
onValueChange={(value) => this.setState({value: value})} />
values={this.state.values}
selectedSegmentIndex={this.state.selectedSegmentIndex}
onChange={this._onChange}
onValueChange={this._onValueChange} />
</View>
);
},

_onChange(event) {
this.setState({
selectedSegmentIndex: event.nativeEvent.selectedSegmentIndex,
});
},

_onValueChange(value) {
this.setState({
value: value,
});
}
});

Expand Down
8 changes: 4 additions & 4 deletions IntegrationTests/SegmentedControlIOSSnapshotTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@ var SegmentedControlIOSSnapshotTest = React.createClass({
<SegmentedControlIOS values={["One", "Two"]} />
</View>
<View style={styles.testRow}>
<SegmentedControlIOS values={["One", "Two"]} selectedValue="One" />
<SegmentedControlIOS values={["One", "Two"]} selectedSegmentIndex={0} />
</View>
<View style={styles.testRow}>
<SegmentedControlIOS values={["One", "Two", "Three", "Four", "Five"]} />
</View>
<View style={styles.testRow}>
<SegmentedControlIOS disabled={true} values={["One", "Two"]} selectedValue="Two" />
<SegmentedControlIOS disabled={true} values={["One", "Two"]} selectedSegmentIndex={1} />
</View>
<View style={styles.testRow}>
<SegmentedControlIOS tintColor="#ff0000" values={["One", "Two", "Three", "Four"]} selectedValue={"One"} />
<SegmentedControlIOS tintColor="#ff0000" values={["One", "Two", "Three", "Four"]} selectedSegmentIndex={0} />
</View>
<View style={styles.testRow}>
<SegmentedControlIOS tintColor="#00ff00" values={["One", "Two", "Three"]} selectedValue={"Two"} />
<SegmentedControlIOS tintColor="#00ff00" values={["One", "Two", "Three"]} selectedSegmentIndex={1} />
</View>
</View>
);
Expand Down
17 changes: 8 additions & 9 deletions Libraries/Components/SegmentedControlIOS/SegmentedControlIOS.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@ var SegmentedControlIOS = React.createClass({
values: PropTypes.arrayOf(PropTypes.string),

/**
* The segmented to be pre-selected; if not in `props.values`, then
* has no effect.
* The index in `props.values` of the segment to be pre-selected
*/
selectedValue: PropTypes.string,
selectedSegmentIndex: PropTypes.number,

/**
* Used to style and layout the `SegmentedControl`. See `StyleSheet.js` and
Expand Down Expand Up @@ -100,16 +99,16 @@ var SegmentedControlIOS = React.createClass({
},

render: function() {
var valuesAndSelectedValue = {
selectedValue: this.props.selectedValue,
var valuesAndSelectedSegmentIndex = {
selectedSegmentIndex: this.props.selectedSegmentIndex,
values: this.props.values
}
};
return (
<RCTSegmentedControl
ref={SEGMENTED_CONTROL_REFERENCE}
style={[styles.segmentedControl, this.props.style]}
enabled={!this.props.disabled}
valuesAndSelectedValue={valuesAndSelectedValue}
valuesAndSelectedSegmentIndex={valuesAndSelectedSegmentIndex}
tintColor={this.props.tintColor}
onChange={this._onChange}
momentary={this.props.momentary}
Expand All @@ -131,8 +130,8 @@ var rkSegmentedControlAttributes = merge(ReactIOSViewAttributes.UIView, {
momentary: true,
enabled: true,
// Send both values simultaneously, to avoid race condition where
// `selectedValue` is set before `values`
valuesAndSelectedValue: true
// `selectedSegmentIndex` is set before `values`
valuesAndSelectedSegmentIndex: true
});


Expand Down
2 changes: 1 addition & 1 deletion React/Views/RCTSegmentedControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@

- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher NS_DESIGNATED_INITIALIZER;

- (void)setValuesAndSelectedValue:(NSDictionary *)valuesAndSelectedValue;
- (void)setValuesAndSelectedSegmentIndex:(NSDictionary *)valuesAndSelectedValue;

@end
22 changes: 8 additions & 14 deletions React/Views/RCTSegmentedControl.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,30 @@ - (id)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher
}


- (void)setValuesAndSelectedValue:(NSDictionary *)valuesAndSelectedValue
- (void)setValuesAndSelectedSegmentIndex:(NSDictionary *)valuesAndSelectedSegmentIndex
{
[self removeAllSegments];

NSArray *values = valuesAndSelectedValue[@"values"];
NSArray *values = valuesAndSelectedSegmentIndex[@"values"];
NSNumber *selectedSegmentIndex = valuesAndSelectedSegmentIndex[@"selectedSegmentIndex"];

NSUInteger insertAtIndex = 0;
for (NSString *value in values) {
[self insertSegmentWithTitle:value atIndex:insertAtIndex animated:NO];
insertAtIndex += 1;
}

NSString *selectedValue = valuesAndSelectedValue[@"selectedValue"];
NSUInteger selectedSegmentIndex = NSNotFound;
for (NSUInteger i = 0; i < self.numberOfSegments; i++) {
NSString *segmentValue = [self titleForSegmentAtIndex:i];
if ([segmentValue isEqualToString:selectedValue]) {
selectedSegmentIndex = i;
break;
}
}
if (selectedSegmentIndex != NSNotFound) {
[self setSelectedSegmentIndex:selectedSegmentIndex];
if (selectedSegmentIndex) {
[self setSelectedSegmentIndex:[selectedSegmentIndex integerValue]];
}
}

- (void)onChange:(UISegmentedControl *)sender
{
NSDictionary *event = @{
@"target": self.reactTag,
@"value": [self titleForSegmentAtIndex:sender.selectedSegmentIndex]
@"value": [self titleForSegmentAtIndex:sender.selectedSegmentIndex],
@"selectedSegmentIndex": @(sender.selectedSegmentIndex)
};
[_eventDispatcher sendInputEventWithName:@"topChange" body:event];
}
Expand Down
2 changes: 1 addition & 1 deletion React/Views/RCTSegmentedControlManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ - (UIView *)view
return [[RCTSegmentedControl alloc] initWithEventDispatcher:self.bridge.eventDispatcher];
}

RCT_EXPORT_VIEW_PROPERTY(valuesAndSelectedValue, NSDictionary);
RCT_EXPORT_VIEW_PROPERTY(valuesAndSelectedSegmentIndex, NSDictionary);
RCT_EXPORT_VIEW_PROPERTY(tintColor, UIColor);
RCT_EXPORT_VIEW_PROPERTY(momentary, BOOL);
RCT_EXPORT_VIEW_PROPERTY(enabled, BOOL);
Expand Down

0 comments on commit 06c2d95

Please sign in to comment.