Fabric Picker onChange event

Summary:
Implements `onChange` prop for `<Picker/>`.

Changelog: [iOS][Fabric] Fabric Picker support

Reviewed By: sammy-SC

Differential Revision: D23975689

fbshipit-source-id: 7aa81c203d420a8971e4309911a41ecfd377a318
This commit is contained in:
Peter Argany
2020-10-13 11:19:29 -07:00
committed by Facebook GitHub Bot
parent c2e7ac1b35
commit 251d5e1bbe
3 changed files with 35 additions and 9 deletions
@@ -10,6 +10,7 @@
#import <React/RCTConvert.h>
#import <UIKit/UIKit.h>
#import <react/renderer/components/iospicker/PickerComponentDescriptor.h>
#import <react/renderer/components/iospicker/PickerEventEmitter.h>
#import <react/renderer/components/iospicker/PickerProps.h>
#import "FBRCTFabricComponentsPlugins.h"
@@ -35,8 +36,6 @@ using namespace facebook::react;
{
if (self = [super initWithFrame:frame]) {
_pickerView = [[UIPickerView alloc] initWithFrame:self.bounds];
// TODO (T75217510) - Handle and test onChange, something like:
// [_pickerView addTarget:self action:@selector(onChange:) forControlEvents:UIControlEventValueChanged];
self.contentView = _pickerView;
[self setPropsToDefault];
}
@@ -98,11 +97,6 @@ using namespace facebook::react;
[super updateProps:props oldProps:oldProps];
}
- (void)onChange:(UISwitch *)sender
{
// TODO (T75217510) - Handle and test onChange
}
// TODO (T75217510) - Handle Native Commands
#pragma mark - Native Commands
@@ -125,7 +119,6 @@ using namespace facebook::react;
forComponent:(__unused NSInteger)component
{
return [NSString stringWithUTF8String:_items[row].label.c_str()];
;
}
- (CGFloat)pickerView:(__unused UIPickerView *)pickerView rowHeightForComponent:(NSInteger)__unused component
@@ -157,7 +150,10 @@ using namespace facebook::react;
inComponent:(__unused NSInteger)component
{
_selectedIndex = row;
// TODO (T75217510) - Handle and test onChange
PickerEventEmitter::PickerIOSChangeEvent event = {.newValue = _items[row].value, .newIndex = (int)row};
if (_eventEmitter) {
std::static_pointer_cast<PickerEventEmitter const>(_eventEmitter)->onChange(event);
}
}
#pragma mark - UIPickerViewAccessibilityDelegate protocol
@@ -0,0 +1,23 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "PickerEventEmitter.h"
namespace facebook {
namespace react {
void PickerEventEmitter::onChange(PickerIOSChangeEvent event) const {
dispatchEvent("change", [event = std::move(event)](jsi::Runtime &runtime) {
auto payload = jsi::Object(runtime);
payload.setProperty(runtime, "newValue", event.newValue);
payload.setProperty(runtime, "newIndex", event.newIndex);
return payload;
});
}
} // namespace react
} // namespace facebook
@@ -15,6 +15,13 @@ namespace react {
class PickerEventEmitter : public ViewEventEmitter {
public:
using ViewEventEmitter::ViewEventEmitter;
struct PickerIOSChangeEvent {
std::string newValue;
int newIndex;
};
void onChange(PickerIOSChangeEvent event) const;
};
} // namespace react