Skip to content

Commit 52e2d9c

Browse files
authored
fix(fabric): Add focus and blur to View commands (#2703)
## Summary: Followup to #2692 , we need to add `focus` and `blur` to the View native components' commands, and implement them so they can be called by JS. This matches what is later implemented upstream in facebook@3e58380 ## Test Plan: `ref.current?.focus()` actually works.
1 parent cd44df2 commit 52e2d9c

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

packages/react-native/Libraries/Components/View/ViewNativeComponent.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ const ViewNativeComponent: HostComponent<Props> =
2121
}));
2222

2323
interface NativeCommands {
24+
+focus: () => void; // [macOS]
25+
+blur: () => void; // [macOS]
2426
+hotspotUpdate: (viewRef: HostInstance, x: number, y: number) => void;
2527
+setPressed: (viewRef: HostInstance, pressed: boolean) => void;
2628
}
2729

2830
export const Commands: NativeCommands = codegenNativeCommands<NativeCommands>({
29-
supportedCommands: ['hotspotUpdate', 'setPressed'],
31+
supportedCommands: ['focus', 'blur', 'hotspotUpdate', 'setPressed'], // [macOS]
3032
});
3133

3234
export default ViewNativeComponent;

packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4096,6 +4096,8 @@ export type AccessibilityProps = $ReadOnly<{
40964096
exports[`public API should not change unintentionally Libraries/Components/View/ViewNativeComponent.js 1`] = `
40974097
"declare const ViewNativeComponent: HostComponent<Props>;
40984098
interface NativeCommands {
4099+
+focus: () => void;
4100+
+blur: () => void;
40994101
+hotspotUpdate: (viewRef: HostInstance, x: number, y: number) => void;
41004102
+setPressed: (viewRef: HostInstance, pressed: boolean) => void;
41014103
}

packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,6 +1512,19 @@ - (BOOL)didActivateAccessibilityCustomAction:(UIAccessibilityCustomAction *)acti
15121512

15131513
#if TARGET_OS_OSX // [macOS
15141514

1515+
- (void)handleCommand:(const NSString *)commandName args:(const NSArray *)args
1516+
{
1517+
if ([commandName isEqualToString:@"focus"]) {
1518+
[self focus];
1519+
return;
1520+
}
1521+
1522+
if ([commandName isEqualToString:@"blur"]) {
1523+
[self blur];
1524+
return;
1525+
}
1526+
}
1527+
15151528
# pragma mark - Focus Ring
15161529

15171530
- (CGRect)focusRingMaskBounds
@@ -1537,6 +1550,16 @@ - (void)drawFocusRingMask
15371550

15381551

15391552
#pragma mark - Focus Events
1553+
1554+
- (void)focus
1555+
{
1556+
[[self window] makeFirstResponder:self];
1557+
}
1558+
1559+
- (void)blur
1560+
{
1561+
[[self window] resignFirstResponder];
1562+
}
15401563

15411564
- (BOOL)needsPanelToBecomeKey
15421565
{

0 commit comments

Comments
 (0)