From ccc4f0153db9098819d0bf728c828585132dd2f8 Mon Sep 17 00:00:00 2001 From: Peter Argany Date: Tue, 13 Oct 2020 11:13:34 -0700 Subject: [PATCH] Fabric Picker Native Command support Summary: This adds support for a controlled `` component . Changelog: [iOS][Fabric] Fabric Picker support Reviewed By: sammy-SC Differential Revision: D24005475 fbshipit-source-id: c50e9918f74f6ef5cdfbfe67cb6c132c12d64916 --- .../Picker/RCTPickerComponentView.mm | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/React/Fabric/Mounting/ComponentViews/Picker/RCTPickerComponentView.mm b/React/Fabric/Mounting/ComponentViews/Picker/RCTPickerComponentView.mm index 50b27554c7d..d1a2037473a 100644 --- a/React/Fabric/Mounting/ComponentViews/Picker/RCTPickerComponentView.mm +++ b/React/Fabric/Mounting/ComponentViews/Picker/RCTPickerComponentView.mm @@ -8,6 +8,7 @@ #import "RCTPickerComponentView.h" #import +#import #import #import #import @@ -75,17 +76,21 @@ using namespace facebook::react; { const auto &oldPickerProps = *std::static_pointer_cast(_props); const auto &newPickerProps = *std::static_pointer_cast(props); + bool needsToReload = false; if (oldPickerProps.items != newPickerProps.items) { _items = newPickerProps.items; + needsToReload = true; } if (oldPickerProps.selectedIndex != newPickerProps.selectedIndex) { _selectedIndex = newPickerProps.selectedIndex; + [self setSelectedIndex]; } if (oldPickerProps.textAttributes != newPickerProps.textAttributes) { _textAttributes = RCTNSTextAttributesFromTextAttributes(newPickerProps.getEffectiveTextAttributes()); + needsToReload = true; } // TODO (T75217510) - Figure out testID. @@ -96,12 +101,33 @@ using namespace facebook::react; _accessibilityLabel = [NSString stringWithUTF8String:newPickerProps.accessibilityLabel.c_str()]; } + if (needsToReload) { + [_pickerView reloadAllComponents]; + } + [super updateProps:props oldProps:oldProps]; } -// TODO (T75217510) - Handle Native Commands #pragma mark - Native Commands +- (void)handleCommand:(const NSString *)commandName args:(const NSArray *)args +{ + if ([commandName isEqualToString:@"setNativeSelectedIndex"] && [args objectAtIndex:0]) { + NSNumber *selectedIndex = [args objectAtIndex:0]; + if (_selectedIndex != selectedIndex.integerValue) { + [self setSelectedIndex]; + } + } else { + RCTLogWarn(@"Attempting to send unknown command to Picker component: %@", commandName); + } +} + +- (void)setSelectedIndex +{ + BOOL animated = _selectedIndex != NSNotFound; // Don't animate the initial value. + [_pickerView selectRow:_selectedIndex inComponent:0 animated:animated]; +} + #pragma mark - UIPickerViewDataSource protocol - (NSInteger)numberOfComponentsInPickerView:(__unused UIPickerView *)pickerView