Skip to content

Commit 6421d7c

Browse files
committed
feat: wip focus consistency
1 parent f678b77 commit 6421d7c

3 files changed

Lines changed: 49 additions & 16 deletions

File tree

example/ios/Podfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2690,9 +2690,9 @@ SPEC CHECKSUMS:
26902690
ReactAppDependencyProvider: 77a7129540c0e06ab6be9a2bcb887d3d2e594431
26912691
ReactCodegen: b62625187ce853918021a7a9178cc406e9820d56
26922692
ReactCommon: d07170f92e0e853091a70b2741b7c43f5dfdea73
2693-
ReactNativeEnriched: b11d66700889cd36c9938a825736de1929349b24
2693+
ReactNativeEnriched: ce7a893fdefce993826a3177da6811b513e26526
26942694
SocketRocket: d4aabe649be1e368d1318fdf28a022d714d65748
2695-
Yoga: 6af5d1e0290903c82b3e0cb6836a5f898c1c4634
2695+
Yoga: 47264cc34431da6b66b3687348be283084924f46
26962696

26972697
PODFILE CHECKSUM: 1b876ae6d1ea0597d954f2089c1213dd3ade28bc
26982698

example/src/App.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
Text,
55
type NativeSyntheticEvent,
66
ScrollView,
7+
Keyboard,
78
} from 'react-native';
89
import {
910
EnrichedTextInput,
@@ -62,7 +63,7 @@ const DEFAULT_LINK_STATE = {
6263
end: 0,
6364
};
6465

65-
const DEBUG_SCROLLABLE = false;
66+
const DEBUG_SCROLLABLE = true;
6667

6768
// Enabling this prop fixes input flickering while auto growing.
6869
// However, it's still experimental and not tested well.
@@ -293,6 +294,11 @@ export default function App() {
293294
onPress={openValueModal}
294295
style={styles.valueButton}
295296
/>
297+
<Button
298+
title="Dismiss keyboard"
299+
onPress={Keyboard.dismiss}
300+
style={styles.valueButton}
301+
/>
296302
<HtmlSection currentHtml={currentHtml} />
297303
{DEBUG_SCROLLABLE && <View style={styles.scrollPlaceholder} />}
298304
</ScrollView>

src/EnrichedTextInput.tsx

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
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';
3334
import { normalizeHtmlStyle } from './normalizeHtmlStyle';
3435

36+
//@ts-ignore
37+
import TextInputState from 'react-native/Libraries/Components/TextInput/TextInputState';
38+
3539
export 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

Comments
 (0)