diff --git a/React/Fabric/Mounting/ComponentViews/Picker/RCTPickerComponentView.mm b/React/Fabric/Mounting/ComponentViews/Picker/RCTPickerComponentView.mm index 3001c423ea2..2cc534f9cbf 100644 --- a/React/Fabric/Mounting/ComponentViews/Picker/RCTPickerComponentView.mm +++ b/React/Fabric/Mounting/ComponentViews/Picker/RCTPickerComponentView.mm @@ -10,6 +10,7 @@ #import #import #import +#import #import #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(_eventEmitter)->onChange(event); + } } #pragma mark - UIPickerViewAccessibilityDelegate protocol diff --git a/ReactCommon/react/renderer/components/picker/iospicker/PickerEventEmitter.cpp b/ReactCommon/react/renderer/components/picker/iospicker/PickerEventEmitter.cpp new file mode 100644 index 00000000000..9e608966063 --- /dev/null +++ b/ReactCommon/react/renderer/components/picker/iospicker/PickerEventEmitter.cpp @@ -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 diff --git a/ReactCommon/react/renderer/components/picker/iospicker/PickerEventEmitter.h b/ReactCommon/react/renderer/components/picker/iospicker/PickerEventEmitter.h index df523632ced..fea36adf3f6 100644 --- a/ReactCommon/react/renderer/components/picker/iospicker/PickerEventEmitter.h +++ b/ReactCommon/react/renderer/components/picker/iospicker/PickerEventEmitter.h @@ -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