mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-29 04:35:36 +08:00
[ReactNative] OSS Picker
This commit is contained in:
11
ReactKit/Views/RCTPicker.h
Normal file
11
ReactKit/Views/RCTPicker.h
Normal file
@@ -0,0 +1,11 @@
|
||||
// Copyright 2004-present Facebook. All Rights Reserved.
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
@class RCTEventDispatcher;
|
||||
|
||||
@interface RCTPicker : UIPickerView
|
||||
|
||||
- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher NS_DESIGNATED_INITIALIZER;
|
||||
|
||||
@end
|
||||
91
ReactKit/Views/RCTPicker.m
Normal file
91
ReactKit/Views/RCTPicker.m
Normal file
@@ -0,0 +1,91 @@
|
||||
// Copyright 2004-present Facebook. All Rights Reserved.
|
||||
|
||||
#import "RCTPicker.h"
|
||||
|
||||
#import "RCTConvert.h"
|
||||
#import "RCTEventDispatcher.h"
|
||||
#import "RCTUtils.h"
|
||||
#import "UIView+ReactKit.h"
|
||||
|
||||
const NSInteger UNINITIALIZED_INDEX = -1;
|
||||
|
||||
@interface RCTPicker() <UIPickerViewDataSource, UIPickerViewDelegate>
|
||||
{
|
||||
RCTEventDispatcher *_eventDispatcher;
|
||||
NSArray *_items;
|
||||
NSInteger _selectedIndex;
|
||||
}
|
||||
@end
|
||||
|
||||
@implementation RCTPicker
|
||||
|
||||
- (id)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher
|
||||
{
|
||||
if (self = [super initWithFrame:CGRectZero]) {
|
||||
_eventDispatcher = eventDispatcher;
|
||||
_selectedIndex = UNINITIALIZED_INDEX;
|
||||
self.delegate = self;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)setItems:(NSArray *)items
|
||||
{
|
||||
if (_items != items) {
|
||||
_items = [items copy];
|
||||
[self setNeedsLayout];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)setSelectedIndex:(NSInteger)selectedIndex
|
||||
{
|
||||
if (_selectedIndex != selectedIndex) {
|
||||
BOOL animated = _selectedIndex != UNINITIALIZED_INDEX; // Don't animate the initial value
|
||||
_selectedIndex = selectedIndex;
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
[self selectRow:selectedIndex inComponent:0 animated:animated];
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark - UIPickerViewDataSource protocol
|
||||
|
||||
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
|
||||
{
|
||||
return [_items count];
|
||||
}
|
||||
|
||||
#pragma mark - UIPickerViewDelegate methods
|
||||
|
||||
- (NSDictionary *)itemForRow:(NSInteger)row
|
||||
{
|
||||
return (NSDictionary*)[_items objectAtIndex:row];
|
||||
}
|
||||
|
||||
- (id)valueForRow:(NSInteger)row
|
||||
{
|
||||
return [self itemForRow:row][@"value"];
|
||||
}
|
||||
|
||||
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
|
||||
{
|
||||
return [self itemForRow:row][@"label"];
|
||||
}
|
||||
|
||||
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
|
||||
{
|
||||
_selectedIndex = row;
|
||||
NSDictionary *event = @{
|
||||
@"target": self.reactTag,
|
||||
@"newIndex": @(row),
|
||||
@"newValue": [self valueForRow:row]
|
||||
};
|
||||
|
||||
[_eventDispatcher sendInputEventWithName:@"topChange" body:event];
|
||||
}
|
||||
@end
|
||||
7
ReactKit/Views/RCTPickerManager.h
Normal file
7
ReactKit/Views/RCTPickerManager.h
Normal file
@@ -0,0 +1,7 @@
|
||||
// Copyright 2004-present Facebook. All Rights Reserved.
|
||||
|
||||
#import "RCTViewManager.h"
|
||||
|
||||
@interface RCTPickerManager : RCTViewManager
|
||||
|
||||
@end
|
||||
28
ReactKit/Views/RCTPickerManager.m
Normal file
28
ReactKit/Views/RCTPickerManager.m
Normal file
@@ -0,0 +1,28 @@
|
||||
// Copyright 2004-present Facebook. All Rights Reserved.
|
||||
|
||||
#import "RCTPickerManager.h"
|
||||
|
||||
#import "RCTBridge.h"
|
||||
#import "RCTConvert.h"
|
||||
#import "RCTPicker.h"
|
||||
|
||||
@implementation RCTPickerManager
|
||||
|
||||
- (UIView *)view
|
||||
{
|
||||
return [[RCTPicker alloc] initWithEventDispatcher:self.bridge.eventDispatcher];
|
||||
}
|
||||
|
||||
RCT_EXPORT_VIEW_PROPERTY(items)
|
||||
RCT_EXPORT_VIEW_PROPERTY(selectedIndex)
|
||||
|
||||
- (NSDictionary *)constantsToExport
|
||||
{
|
||||
RCTPicker *pv = [[RCTPicker alloc] init];
|
||||
return @{
|
||||
@"ComponentHeight": @(CGRectGetHeight(pv.frame)),
|
||||
@"ComponentWidth": @(CGRectGetWidth(pv.frame))
|
||||
};
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user