Skip to content

Commit d9ebc63

Browse files
authored
Fix TextInput focus when inside a FocusZone (#3849)
* Fix TextInput focus when inside a FocusZone When a TextInput is not inside a FocusZone, it properly participates in the key view loop in that Tab\Shift+Tab will place focus on the inner `RCTUITextView`, instead of the outer `RCTBaseTextInputView` subclass. This is due to [overriding](https://github.com/microsoft/react-native-macos/blob/b7033b3513d98b20a08ec20abfe2b9bb712c68d4/packages/react-native/Libraries/Text/TextInput/RCTBaseTextInputView.mm#L1147C1-L1150C2) the `canBecomeKeyView` in that base class. However, the FocusZone control does not look at that property -- instead it only considers `canBecomeFirstResponder`, as seen in #2329. Although it seems like the logical next step to use `canBecomeKeyView` in the FocusZone, that was previously done with #2267 but later reverted in #2322 since it broke things downstream. Instead, we'll make a compromise and leak some of the text input implementation details into the focus zone -- if we're considering focusing a subclass of `RCTBaseTextInputView`, we'll instead use the containing `backedTextInputView`. Testing: * TI inside FZ is now properly focused (using the new case added to the FZ page) * Verified all other cases on the FZ page * Verified in the downstream scenario that prompted this investigation Notes: * I noticed that Shift+Ctrl+Tab does not move focus out of the containing FZ, but that has been determined to be a pre-existing issue, and thus isn't being addressed in this change.
1 parent 5ca24ac commit d9ebc63

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

apps/fluent-tester/src/TestComponents/FocusZone/FocusZoneTest.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from 'react';
2-
import { View, ScrollView, Pressable } from 'react-native';
2+
import { View, ScrollView, Pressable, TextInput } from 'react-native';
33

44
import type { FocusZoneDirection, FocusZoneTabNavigation } from '@fluentui/react-native';
55
import { FocusZone, MenuButton, Text, useOnPressWithFocus } from '@fluentui/react-native';
@@ -207,6 +207,19 @@ const FocusZoneGrid: React.FunctionComponent = () => {
207207
);
208208
};
209209

210+
const FocusZoneTextInput: React.FunctionComponent = () => {
211+
return (
212+
<FocusZoneListWrapper>
213+
<>
214+
<Text>FocusZone Grid</Text>
215+
<FocusZone>
216+
<TextInput multiline={true} />
217+
</FocusZone>
218+
</>
219+
</FocusZoneListWrapper>
220+
);
221+
};
222+
210223
const focusZoneSections: TestSection[] = [
211224
{
212225
name: 'Common FocusZone Usage',
@@ -245,6 +258,10 @@ const focusZoneSections: TestSection[] = [
245258
name: 'FocusZone Grid',
246259
component: FocusZoneGrid,
247260
},
261+
{
262+
name: 'FocusZone with TextInput',
263+
component: FocusZoneTextInput,
264+
},
248265
];
249266

250267
const e2eSections: TestSection[] = [
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "patch",
3+
"comment": "Fix TextInput focus when inside a FocusZone",
4+
"packageName": "@fluentui-react-native/focus-zone",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "patch",
3+
"comment": "Add TextInput inside FocusZone case",
4+
"packageName": "@fluentui-react-native/tester",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}

packages/components/FocusZone/macos/RCTFocusZone.m

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#import "KeyCodes.h"
2+
#import <React/RCTBaseTextInputView.h>
23
#import "RCTFocusZone.h"
34
#import "RCTi18nUtil.h"
45

@@ -61,7 +62,14 @@ static inline CGFloat GetMinDistanceBetweenRectVerticesAndPoint(NSRect rect, NSP
6162

6263
for (NSView *view in [parentView subviews]) {
6364
if ([view acceptsFirstResponder]) {
64-
return view;
65+
if ([view isKindOfClass:[RCTBaseTextInputView class]]) {
66+
RCTUIView *backedTextInputView = [(RCTBaseTextInputView *)view backedTextInputView];
67+
if ([backedTextInputView acceptsFirstResponder]) {
68+
return backedTextInputView;
69+
}
70+
} else {
71+
return view;
72+
}
6573
}
6674

6775
NSView *match = GetFirstFocusableViewWithin(view);

0 commit comments

Comments
 (0)