From 985a827e70d7b924c19d77609ee254a3c01682b4 Mon Sep 17 00:00:00 2001 From: Ismar Besic <46565776+ismarbesic@users.noreply.github.com> Date: Tue, 15 Oct 2024 14:41:02 +0200 Subject: [PATCH] fix: first tab being rendered when onSwitchToTab and a custom currentTabIndex is set (#7922) --- lib/ios/RNNBottomTabsController.m | 5 +++ .../NavigationTests/RNNCommandsHandlerTest.m | 34 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/lib/ios/RNNBottomTabsController.m b/lib/ios/RNNBottomTabsController.m index 2de2f0fdc8..d870018c78 100644 --- a/lib/ios/RNNBottomTabsController.m +++ b/lib/ios/RNNBottomTabsController.m @@ -29,6 +29,11 @@ - (instancetype)initWithLayoutInfo:(RNNLayoutInfo *)layoutInfo _bottomTabPresenter = bottomTabPresenter; _dotIndicatorPresenter = dotIndicatorPresenter; + if ([options.bottomTabs.currentTabIndex hasValue]) { + _currentTabIndex = [options.bottomTabs.currentTabIndex get]; + _previousTabIndex = _currentTabIndex; + } + self = [super initWithLayoutInfo:layoutInfo creator:creator options:options diff --git a/playground/ios/NavigationTests/RNNCommandsHandlerTest.m b/playground/ios/NavigationTests/RNNCommandsHandlerTest.m index dc451ef0df..89d9b4bfc7 100644 --- a/playground/ios/NavigationTests/RNNCommandsHandlerTest.m +++ b/playground/ios/NavigationTests/RNNCommandsHandlerTest.m @@ -521,6 +521,40 @@ - (void)testSetRoot_withBottomTabsAttachModeOnSwitchToTab { XCTAssertTrue(_vc2.isViewLoaded); } +- (void)testSetRoot_withBottomTabsAttachModeOnSwitchToTabWithCustomIndex { + [self.uut setReadyToReceiveCommands:true]; + RNNNavigationOptions *options = [RNNNavigationOptions emptyOptions]; + options.bottomTabs.tabsAttachMode = + [[BottomTabsAttachMode alloc] initWithValue:@"onSwitchToTab"]; + options.animations.setRoot.waitForRender = [[Bool alloc] initWithBOOL:YES]; + options.bottomTabs.currentTabIndex = [IntNumber withValue:@1]; + + BottomTabsBaseAttacher *attacher = + [[[BottomTabsAttachModeFactory alloc] initWithDefaultOptions:nil] fromOptions:options]; + RNNBottomTabsController *tabBarController = + [[RNNBottomTabsController alloc] initWithLayoutInfo:nil + creator:nil + options:options + defaultOptions:[RNNNavigationOptions emptyOptions] + presenter:[RNNBasePresenter new] + bottomTabPresenter:nil + dotIndicatorPresenter:nil + eventEmitter:_eventEmmiter + childViewControllers:@[ _vc1, _vc2 ] + bottomTabsAttacher:attacher]; + [tabBarController viewWillAppear:YES]; + OCMStub([self.controllerFactory createLayout:[OCMArg any]]).andReturn(tabBarController); + + [self.uut setRoot:@{} + commandId:@"" + completion:^(NSString *componentId){ + }]; + XCTAssertFalse(_vc1.isViewLoaded); + XCTAssertTrue(_vc2.isViewLoaded); + [tabBarController setSelectedIndex:0]; + XCTAssertTrue(_vc1.isViewLoaded); +} + - (void)testSetRoot_withBottomTabsAttachModeAfterInitialTab { [self.uut setReadyToReceiveCommands:true]; RNNNavigationOptions *options = [RNNNavigationOptions emptyOptions];