2015-03-23 22:07:33 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2015-03-11 02:11:28 +00:00
|
|
|
|
|
|
|
#import "RCTPicker.h"
|
|
|
|
|
|
|
|
#import "RCTUtils.h"
|
2015-03-26 13:32:01 +00:00
|
|
|
#import "UIView+React.h"
|
2015-03-11 02:11:28 +00:00
|
|
|
|
|
|
|
@interface RCTPicker() <UIPickerViewDataSource, UIPickerViewDelegate>
|
2015-03-26 13:32:01 +00:00
|
|
|
|
Added mechanism for directly mapping JS event handlers to blocks
Summary:
Currently, the system for mapping JS event handlers to blocks is quite clean on the JS side, but is clunky on the native side. The event property is passed as a boolean, which can then be checked by the native side, and if true, the native side is supposed to send an event via the event dispatcher.
This diff adds the facility to declare the property as a block instead. This means that the event side can simply call the block, and it will automatically send the event. Because the blocks for bubbling and direct events are named differently, we can also use this to generate the event registration data and get rid of the arrays of event names.
The name of the event is inferred from the property name, which means that the property for an event called "load" must be called `onLoad` or the mapping won't work. This can be optionally remapped to a different property name on the view itself if necessary, e.g.
RCT_REMAP_VIEW_PROPERTY(onLoad, loadEventBlock, RCTDirectEventBlock)
If you don't want to use this mechanism then for now it is still possible to declare the property as a BOOL instead and use the old mechanism (this approach is now deprecated however, and may eventually be removed altogether).
2015-09-02 12:58:10 +00:00
|
|
|
@property (nonatomic, copy) NSArray *items;
|
|
|
|
@property (nonatomic, assign) NSInteger selectedIndex;
|
|
|
|
@property (nonatomic, copy) RCTBubblingEventBlock onChange;
|
|
|
|
|
2015-03-26 13:32:01 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTPicker
|
2015-03-11 02:11:28 +00:00
|
|
|
|
Added mechanism for directly mapping JS event handlers to blocks
Summary:
Currently, the system for mapping JS event handlers to blocks is quite clean on the JS side, but is clunky on the native side. The event property is passed as a boolean, which can then be checked by the native side, and if true, the native side is supposed to send an event via the event dispatcher.
This diff adds the facility to declare the property as a block instead. This means that the event side can simply call the block, and it will automatically send the event. Because the blocks for bubbling and direct events are named differently, we can also use this to generate the event registration data and get rid of the arrays of event names.
The name of the event is inferred from the property name, which means that the property for an event called "load" must be called `onLoad` or the mapping won't work. This can be optionally remapped to a different property name on the view itself if necessary, e.g.
RCT_REMAP_VIEW_PROPERTY(onLoad, loadEventBlock, RCTDirectEventBlock)
If you don't want to use this mechanism then for now it is still possible to declare the property as a BOOL instead and use the old mechanism (this approach is now deprecated however, and may eventually be removed altogether).
2015-09-02 12:58:10 +00:00
|
|
|
- (instancetype)initWithFrame:(CGRect)frame
|
2015-03-11 02:11:28 +00:00
|
|
|
{
|
Added mechanism for directly mapping JS event handlers to blocks
Summary:
Currently, the system for mapping JS event handlers to blocks is quite clean on the JS side, but is clunky on the native side. The event property is passed as a boolean, which can then be checked by the native side, and if true, the native side is supposed to send an event via the event dispatcher.
This diff adds the facility to declare the property as a block instead. This means that the event side can simply call the block, and it will automatically send the event. Because the blocks for bubbling and direct events are named differently, we can also use this to generate the event registration data and get rid of the arrays of event names.
The name of the event is inferred from the property name, which means that the property for an event called "load" must be called `onLoad` or the mapping won't work. This can be optionally remapped to a different property name on the view itself if necessary, e.g.
RCT_REMAP_VIEW_PROPERTY(onLoad, loadEventBlock, RCTDirectEventBlock)
If you don't want to use this mechanism then for now it is still possible to declare the property as a BOOL instead and use the old mechanism (this approach is now deprecated however, and may eventually be removed altogether).
2015-09-02 12:58:10 +00:00
|
|
|
if ((self = [super initWithFrame:frame])) {
|
|
|
|
_selectedIndex = NSNotFound;
|
2015-03-11 02:11:28 +00:00
|
|
|
self.delegate = self;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-08-24 10:14:33 +00:00
|
|
|
RCT_NOT_IMPLEMENTED(- (instancetype)initWithCoder:(NSCoder *)aDecoder)
|
2015-06-15 14:53:45 +00:00
|
|
|
|
2015-03-11 02:11:28 +00:00
|
|
|
- (void)setItems:(NSArray *)items
|
|
|
|
{
|
2015-06-15 14:53:45 +00:00
|
|
|
_items = [items copy];
|
|
|
|
[self setNeedsLayout];
|
2015-03-11 02:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setSelectedIndex:(NSInteger)selectedIndex
|
|
|
|
{
|
|
|
|
if (_selectedIndex != selectedIndex) {
|
Added mechanism for directly mapping JS event handlers to blocks
Summary:
Currently, the system for mapping JS event handlers to blocks is quite clean on the JS side, but is clunky on the native side. The event property is passed as a boolean, which can then be checked by the native side, and if true, the native side is supposed to send an event via the event dispatcher.
This diff adds the facility to declare the property as a block instead. This means that the event side can simply call the block, and it will automatically send the event. Because the blocks for bubbling and direct events are named differently, we can also use this to generate the event registration data and get rid of the arrays of event names.
The name of the event is inferred from the property name, which means that the property for an event called "load" must be called `onLoad` or the mapping won't work. This can be optionally remapped to a different property name on the view itself if necessary, e.g.
RCT_REMAP_VIEW_PROPERTY(onLoad, loadEventBlock, RCTDirectEventBlock)
If you don't want to use this mechanism then for now it is still possible to declare the property as a BOOL instead and use the old mechanism (this approach is now deprecated however, and may eventually be removed altogether).
2015-09-02 12:58:10 +00:00
|
|
|
BOOL animated = _selectedIndex != NSNotFound; // Don't animate the initial value
|
2015-03-11 02:11:28 +00:00
|
|
|
_selectedIndex = selectedIndex;
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
[self selectRow:selectedIndex inComponent:0 animated:animated];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - UIPickerViewDataSource protocol
|
|
|
|
|
2015-06-15 14:53:45 +00:00
|
|
|
- (NSInteger)numberOfComponentsInPickerView:(__unused UIPickerView *)pickerView
|
2015-03-11 02:11:28 +00:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-06-15 14:53:45 +00:00
|
|
|
- (NSInteger)pickerView:(__unused UIPickerView *)pickerView
|
|
|
|
numberOfRowsInComponent:(__unused NSInteger)component
|
2015-03-11 02:11:28 +00:00
|
|
|
{
|
2015-03-26 13:32:01 +00:00
|
|
|
return _items.count;
|
2015-03-11 02:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - UIPickerViewDelegate methods
|
|
|
|
|
|
|
|
- (NSDictionary *)itemForRow:(NSInteger)row
|
|
|
|
{
|
2015-03-26 13:32:01 +00:00
|
|
|
return _items[row];
|
2015-03-11 02:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id)valueForRow:(NSInteger)row
|
|
|
|
{
|
|
|
|
return [self itemForRow:row][@"value"];
|
|
|
|
}
|
|
|
|
|
2015-06-15 14:53:45 +00:00
|
|
|
- (NSString *)pickerView:(__unused UIPickerView *)pickerView
|
|
|
|
titleForRow:(NSInteger)row forComponent:(__unused NSInteger)component
|
2015-03-11 02:11:28 +00:00
|
|
|
{
|
|
|
|
return [self itemForRow:row][@"label"];
|
|
|
|
}
|
|
|
|
|
2015-06-15 14:53:45 +00:00
|
|
|
- (void)pickerView:(__unused UIPickerView *)pickerView
|
|
|
|
didSelectRow:(NSInteger)row inComponent:(__unused NSInteger)component
|
2015-03-11 02:11:28 +00:00
|
|
|
{
|
|
|
|
_selectedIndex = row;
|
Added mechanism for directly mapping JS event handlers to blocks
Summary:
Currently, the system for mapping JS event handlers to blocks is quite clean on the JS side, but is clunky on the native side. The event property is passed as a boolean, which can then be checked by the native side, and if true, the native side is supposed to send an event via the event dispatcher.
This diff adds the facility to declare the property as a block instead. This means that the event side can simply call the block, and it will automatically send the event. Because the blocks for bubbling and direct events are named differently, we can also use this to generate the event registration data and get rid of the arrays of event names.
The name of the event is inferred from the property name, which means that the property for an event called "load" must be called `onLoad` or the mapping won't work. This can be optionally remapped to a different property name on the view itself if necessary, e.g.
RCT_REMAP_VIEW_PROPERTY(onLoad, loadEventBlock, RCTDirectEventBlock)
If you don't want to use this mechanism then for now it is still possible to declare the property as a BOOL instead and use the old mechanism (this approach is now deprecated however, and may eventually be removed altogether).
2015-09-02 12:58:10 +00:00
|
|
|
if (_onChange) {
|
|
|
|
_onChange(@{
|
|
|
|
@"newIndex": @(row),
|
|
|
|
@"newValue": [self valueForRow:row]
|
|
|
|
});
|
|
|
|
}
|
2015-03-11 02:11:28 +00:00
|
|
|
}
|
2015-06-15 14:53:45 +00:00
|
|
|
|
2015-03-11 02:11:28 +00:00
|
|
|
@end
|