2015-03-23 22:07:33 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2015-03-23 22:07:33 +00:00
|
|
|
*/
|
2015-03-11 02:11:28 +00:00
|
|
|
|
|
|
|
#import "RCTPicker.h"
|
|
|
|
|
2015-12-08 15:44:56 +00:00
|
|
|
#import "RCTConvert.h"
|
2015-03-11 02:11:28 +00:00
|
|
|
#import "RCTUtils.h"
|
|
|
|
|
|
|
|
@interface RCTPicker() <UIPickerViewDataSource, UIPickerViewDelegate>
|
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])) {
|
2015-12-08 15:44:56 +00:00
|
|
|
_color = [UIColor blackColor];
|
|
|
|
_font = [UIFont systemFontOfSize:21]; // TODO: selected title default should be 23.5
|
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
|
|
|
_selectedIndex = NSNotFound;
|
2015-12-08 15:44:56 +00:00
|
|
|
_textAlign = NSTextAlignmentCenter;
|
2015-03-11 02:11:28 +00:00
|
|
|
self.delegate = self;
|
2018-07-21 01:59:10 +00:00
|
|
|
[self selectRow:0 inComponent:0 animated:YES]; // Workaround for missing selection indicator lines (see https://stackoverflow.com/questions/39564660/uipickerview-selection-indicator-not-visible-in-ios10)
|
2015-03-11 02:11:28 +00:00
|
|
|
}
|
|
|
|
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-11-03 22:45:46 +00:00
|
|
|
- (void)setItems:(NSArray<NSDictionary *> *)items
|
2015-03-11 02:11:28 +00:00
|
|
|
{
|
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
|
|
|
|
|
2015-12-08 15:44:56 +00:00
|
|
|
- (NSString *)pickerView:(__unused UIPickerView *)pickerView
|
|
|
|
titleForRow:(NSInteger)row
|
|
|
|
forComponent:(__unused NSInteger)component
|
2015-03-11 02:11:28 +00:00
|
|
|
{
|
2015-12-08 15:44:56 +00:00
|
|
|
return [RCTConvert NSString:_items[row][@"label"]];
|
2015-03-11 02:11:28 +00:00
|
|
|
}
|
|
|
|
|
2017-08-24 03:27:48 +00:00
|
|
|
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
|
|
|
|
return _font.pointSize + 19;
|
|
|
|
}
|
|
|
|
|
2015-12-08 15:44:56 +00:00
|
|
|
- (UIView *)pickerView:(UIPickerView *)pickerView
|
|
|
|
viewForRow:(NSInteger)row
|
|
|
|
forComponent:(NSInteger)component
|
|
|
|
reusingView:(UILabel *)label
|
2015-03-11 02:11:28 +00:00
|
|
|
{
|
2015-12-08 15:44:56 +00:00
|
|
|
if (!label) {
|
|
|
|
label = [[UILabel alloc] initWithFrame:(CGRect){
|
|
|
|
CGPointZero,
|
|
|
|
{
|
|
|
|
[pickerView rowSizeForComponent:component].width,
|
|
|
|
[pickerView rowSizeForComponent:component].height,
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
}
|
2015-03-11 02:11:28 +00:00
|
|
|
|
2015-12-08 15:44:56 +00:00
|
|
|
label.font = _font;
|
2016-12-04 02:46:20 +00:00
|
|
|
|
|
|
|
label.textColor = [RCTConvert UIColor:_items[row][@"textColor"]] ?: _color;
|
|
|
|
|
2015-12-08 15:44:56 +00:00
|
|
|
label.textAlignment = _textAlign;
|
|
|
|
label.text = [self pickerView:pickerView titleForRow:row forComponent:component];
|
|
|
|
return label;
|
2015-03-11 02:11:28 +00:00
|
|
|
}
|
|
|
|
|
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;
|
2017-03-23 18:25:28 +00:00
|
|
|
if (_onChange && _items.count > (NSUInteger)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
|
|
|
_onChange(@{
|
|
|
|
@"newIndex": @(row),
|
2015-12-08 15:44:56 +00:00
|
|
|
@"newValue": RCTNullIfNil(_items[row][@"value"]),
|
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
|
|
|
});
|
|
|
|
}
|
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
|