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