Files
react-native/Libraries/Components/View/View.js
T
Igor Klemenski cab4da7288 Add onFocus and onBlur to Pressable. (#30405)
Summary:
Starting to upstream keyboard-related features from React Native Windows - this is the Android implementation.
Exposing onFocus and onBlur callbacks on Pressable; they're already declared for View in ViewPropTypes.js, which Pressable wraps.

Registering event listeners in ReactViewManager to listen for native focus events.
## Changelog

<!-- Help reviewers and the release process by writing your own changelog entry. For an example, see:
https://github.com/facebook/react-native/wiki/Changelog
-->

[Android] [Added] - Add onFocus/onBlur for Pressable on Android.

Pull Request resolved: https://github.com/facebook/react-native/pull/30405

Test Plan:
Tested on v63-stable, since building master on Windows is now broken. Screenshots from RNTester running on the emulator:
![image](https://user-images.githubusercontent.com/12816515/99320373-59777e80-2820-11eb-91a8-704fff4aa13d.png)
![image](https://user-images.githubusercontent.com/12816515/99320412-6f853f00-2820-11eb-98f2-f9cd29e8aa0d.png)

Reviewed By: mdvacca

Differential Revision: D25444566

Pulled By: appden

fbshipit-source-id: ce0efd3e3b199a508df0ba1ce484b4de17471432
2021-01-08 13:59:48 -08:00

74 lines
1.9 KiB
JavaScript

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow strict-local
*/
'use strict';
import type {ViewProps} from './ViewPropTypes';
import type {BlurEvent, FocusEvent} from '../../Types/CoreEventTypes';
const React = require('react');
import ViewNativeComponent from './ViewNativeComponent';
const TextAncestor = require('../../Text/TextAncestor');
const TextInputState = require('../TextInput/TextInputState');
import type {HostComponent} from '../../Renderer/shims/ReactNativeTypes';
const setAndForwardRef = require('../../Utilities/setAndForwardRef');
const {useRef} = React;
export type Props = ViewProps;
/**
* The most fundamental component for building a UI, View is a container that
* supports layout with flexbox, style, some touch handling, and accessibility
* controls.
*
* @see https://reactnative.dev/docs/view.html
*/
const View: React.AbstractComponent<
ViewProps,
React.ElementRef<typeof ViewNativeComponent>,
> = React.forwardRef((props: ViewProps, forwardedRef) => {
const viewRef = useRef<null | React.ElementRef<HostComponent<mixed>>>(null);
const _setNativeRef = setAndForwardRef({
getForwardedRef: () => forwardedRef,
setLocalRef: ref => {
viewRef.current = ref;
},
});
const _onBlur = (event: BlurEvent) => {
TextInputState.blurInput(viewRef.current);
if (props.onBlur) {
props.onBlur(event);
}
};
const _onFocus = (event: FocusEvent) => {
TextInputState.focusInput(viewRef.current);
if (props.onFocus) {
props.onFocus(event);
}
};
return (
<TextAncestor.Provider value={false}>
<ViewNativeComponent
{...props}
onBlur={_onBlur}
onFocus={_onFocus}
ref={_setNativeRef}
/>
</TextAncestor.Provider>
);
});
View.displayName = 'View';
module.exports = View;