Skip to content

Commit bae480c

Browse files
committed
fix(menu): enhance menu selection and also mounting issue child views
1 parent 7ec7dcf commit bae480c

File tree

1 file changed

+50
-3
lines changed

1 file changed

+50
-3
lines changed

ios/MenuView.mm

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,18 @@ - (void)unmountChildComponentView:(UIView<RCTComponentViewProtocol> *)childCompo
7474

7575
- (void)setupChildViewAsMenuTrigger:(UIView *)childView
7676
{
77-
// Add the child view to our view hierarchy
78-
[self addSubview:childView];
77+
// Don't manually add the child view - React already added it via mountChildComponentView
78+
// Just ensure it has the correct constraints
79+
childView.translatesAutoresizingMaskIntoConstraints = NO;
80+
81+
// Remove any existing constraints on the child view to avoid conflicts
82+
[childView.constraints enumerateObjectsUsingBlock:^(NSLayoutConstraint *constraint, NSUInteger idx, BOOL *stop) {
83+
if (constraint.firstItem == childView || constraint.secondItem == childView) {
84+
[constraint setActive:NO];
85+
}
86+
}];
7987

8088
// Setup constraints to fill the container
81-
childView.translatesAutoresizingMaskIntoConstraints = NO;
8289
[NSLayoutConstraint activateConstraints:@[
8390
[childView.topAnchor constraintEqualToAnchor:self.topAnchor],
8491
[childView.leadingAnchor constraintEqualToAnchor:self.leadingAnchor],
@@ -181,7 +188,12 @@ - (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &
181188
_menuItems = [items copy];
182189
[self updateMenuItems:_menuItems selectedIdentifier:currentSelectedIdentifier];
183190
} else if (selectedIdentifierChanged) {
191+
// Always update the menu when selectedIdentifier changes
184192
[self updateMenuItems:_menuItems selectedIdentifier:currentSelectedIdentifier];
193+
} else if (_menuButton && _menuButton.menu) {
194+
// Even if nothing changed, ensure the menu reflects current selectedIdentifier
195+
// This handles cases where the component was remounted after being unmounted
196+
[self updateMenuSelection:currentSelectedIdentifier];
185197
}
186198

187199
[super updateProps:props oldProps:oldProps];
@@ -224,6 +236,41 @@ - (void)updateMenuItems:(NSArray<NSDictionary *> *)menuItems selectedIdentifier:
224236
_menuButton.menu = menu;
225237
}
226238

239+
- (void)updateMenuSelection:(NSString *)selectedIdentifier
240+
{
241+
if (!_menuButton || !_menuButton.menu) {
242+
return;
243+
}
244+
245+
// Recreate the menu with updated selection states
246+
NSMutableArray<UIAction *> *actions = [[NSMutableArray alloc] init];
247+
248+
for (UIMenuElement *element in _menuButton.menu.children) {
249+
if ([element isKindOfClass:[UIAction class]]) {
250+
UIAction *oldAction = (UIAction *)element;
251+
252+
UIAction *newAction = [UIAction actionWithTitle:oldAction.title
253+
image:oldAction.image
254+
identifier:oldAction.identifier
255+
handler:^(__kindof UIAction * _Nonnull action) {
256+
[self selectMenuItem:action.identifier title:action.title];
257+
}];
258+
259+
// Update state based on current selection
260+
if (selectedIdentifier != nil && ![selectedIdentifier isEqualToString:@""] && [oldAction.identifier isEqualToString:selectedIdentifier]) {
261+
newAction.state = UIMenuElementStateOn;
262+
} else {
263+
newAction.state = UIMenuElementStateOff;
264+
}
265+
266+
[actions addObject:newAction];
267+
}
268+
}
269+
270+
UIMenu *menu = [UIMenu menuWithTitle:@"" children:actions];
271+
_menuButton.menu = menu;
272+
}
273+
227274
- (void)selectMenuItem:(NSString *)identifier title:(NSString *)title
228275
{
229276
[self sendMenuSelection:identifier title:title];

0 commit comments

Comments
 (0)