mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-26 23:05:00 +08:00
Add SegmentedControlIOS
Summary: Fixes #534:  ```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 https://github.com/facebook/react-native/pull/564 Github Author: Clay Allsopp <clay.allsopp@gmail.com> Test Plan: Imported from GitHub, without a `Test Plan:` line.
This commit is contained in:
57
React/Views/RCTSegmentedControl.m
Normal file
57
React/Views/RCTSegmentedControl.m
Normal file
@@ -0,0 +1,57 @@
|
||||
//
|
||||
// RCTSegmentedControl.m
|
||||
// React
|
||||
//
|
||||
// Created by Clay Allsopp on 3/31/15.
|
||||
// Copyright (c) 2015 Facebook. All rights reserved.
|
||||
//
|
||||
|
||||
#import "RCTSegmentedControl.h"
|
||||
|
||||
#import "RCTConvert.h"
|
||||
#import "RCTEventDispatcher.h"
|
||||
#import "UIView+React.h"
|
||||
|
||||
@implementation RCTSegmentedControl
|
||||
{
|
||||
RCTEventDispatcher *_eventDispatcher;
|
||||
}
|
||||
|
||||
- (id)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher
|
||||
{
|
||||
if ((self = [super initWithFrame:CGRectZero])) {
|
||||
_eventDispatcher = eventDispatcher;
|
||||
_selectedIndex = self.selectedSegmentIndex;
|
||||
[self addTarget:self action:@selector(onChange:)
|
||||
forControlEvents:UIControlEventValueChanged];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)setValues:(NSArray *)values
|
||||
{
|
||||
_values = [values copy];
|
||||
[self removeAllSegments];
|
||||
for (NSString *value in values) {
|
||||
[self insertSegmentWithTitle:value atIndex:self.numberOfSegments animated:NO];
|
||||
}
|
||||
super.selectedSegmentIndex = _selectedIndex;
|
||||
}
|
||||
|
||||
- (void)setSelectedIndex:(NSInteger)selectedIndex
|
||||
{
|
||||
_selectedIndex = selectedIndex;
|
||||
super.selectedSegmentIndex = selectedIndex;
|
||||
}
|
||||
|
||||
- (void)onChange:(UISegmentedControl *)sender
|
||||
{
|
||||
NSDictionary *event = @{
|
||||
@"target": self.reactTag,
|
||||
@"value": [self titleForSegmentAtIndex:sender.selectedSegmentIndex],
|
||||
@"selectedSegmentIndex": @(sender.selectedSegmentIndex)
|
||||
};
|
||||
[_eventDispatcher sendInputEventWithName:@"topChange" body:event];
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user