22 type Component ,
33 type RefObject ,
44 useImperativeHandle ,
5+ useLayoutEffect ,
56 useMemo ,
67 useRef ,
78} from 'react' ;
@@ -18,20 +19,23 @@ import EnrichedTextInputNativeComponent, {
1819 type OnMentionDetectedInternal ,
1920 type MentionStyleProperties ,
2021} from './EnrichedTextInputNativeComponent' ;
21- import type {
22- ColorValue ,
23- HostInstance ,
24- MeasureInWindowOnSuccessCallback ,
25- MeasureLayoutOnSuccessCallback ,
26- MeasureOnSuccessCallback ,
27- NativeMethods ,
28- NativeSyntheticEvent ,
29- TextStyle ,
30- ViewProps ,
31- ViewStyle ,
22+ import {
23+ type ColorValue ,
24+ type HostInstance ,
25+ type MeasureInWindowOnSuccessCallback ,
26+ type MeasureLayoutOnSuccessCallback ,
27+ type MeasureOnSuccessCallback ,
28+ type NativeMethods ,
29+ type NativeSyntheticEvent ,
30+ type TextStyle ,
31+ type ViewProps ,
32+ type ViewStyle ,
3233} from 'react-native' ;
3334import { normalizeHtmlStyle } from './normalizeHtmlStyle' ;
3435
36+ //@ts -ignore
37+ import TextInputState from 'react-native/Libraries/Components/TextInput/TextInputState' ;
38+
3539export interface EnrichedTextInputInstance extends NativeMethods {
3640 // General commands
3741 focus : ( ) => void ;
@@ -297,6 +301,19 @@ export const EnrichedTextInput = ({
297301 } ,
298302 } ) ) ;
299303
304+ useLayoutEffect ( ( ) => {
305+ const inputRef = nativeRef . current ;
306+ TextInputState . registerInput ( inputRef ) ;
307+
308+ return ( ) => {
309+ TextInputState . unregisterInput ( inputRef ) ;
310+
311+ if ( TextInputState . currentlyFocusedInput ( ) === inputRef ) {
312+ inputRef ?. blur ( ) ;
313+ }
314+ } ;
315+ } , [ ] ) ;
316+
300317 const handleMentionEvent = ( e : NativeSyntheticEvent < OnMentionEvent > ) => {
301318 const mentionText = e . nativeEvent . text ;
302319 const mentionIndicator = e . nativeEvent . indicator ;
@@ -325,6 +342,16 @@ export const EnrichedTextInput = ({
325342 onMentionDetected ?.( { text, indicator, attributes } ) ;
326343 } ;
327344
345+ const _onFocus = ( ) => {
346+ TextInputState . focusInput ( nativeRef ?. current ) ;
347+ onFocus ?.( ) ;
348+ } ;
349+
350+ const _onBlur = ( ) => {
351+ TextInputState . blurInput ( nativeRef ?. current ) ;
352+ onBlur ?.( ) ;
353+ } ;
354+
328355 return (
329356 < EnrichedTextInputNativeComponent
330357 ref = { nativeRef }
@@ -339,8 +366,8 @@ export const EnrichedTextInput = ({
339366 style = { style }
340367 autoCapitalize = { autoCapitalize }
341368 htmlStyle = { normalizedHtmlStyle }
342- onInputFocus = { onFocus }
343- onInputBlur = { onBlur }
369+ onInputFocus = { _onFocus }
370+ onInputBlur = { _onBlur }
344371 onChangeText = { onChangeText }
345372 onChangeHtml = { onChangeHtml }
346373 isOnChangeHtmlSet = { onChangeHtml !== undefined }
0 commit comments