Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use useAutoFocusInput for native composer #47202

Merged
merged 6 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/components/Composer/index.native.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type {MarkdownStyle} from '@expensify/react-native-live-markdown';
import type {ForwardedRef} from 'react';
import React, {useCallback, useMemo, useRef} from 'react';
import React, {useCallback, useEffect, useMemo, useRef} from 'react';
import type {NativeSyntheticEvent, TextInput, TextInputChangeEventData, TextInputPasteEventData} from 'react-native';
import {StyleSheet} from 'react-native';
import type {FileObject} from '@components/AttachmentModal';
import type {AnimatedMarkdownTextInputRef} from '@components/RNMarkdownTextInput';
import RNMarkdownTextInput from '@components/RNMarkdownTextInput';
import useAutoFocusInput from '@hooks/useAutoFocusInput';
import useMarkdownStyle from '@hooks/useMarkdownStyle';
import useResetComposerFocus from '@hooks/useResetComposerFocus';
import useStyleUtils from '@hooks/useStyleUtils';
Expand Down Expand Up @@ -46,6 +47,15 @@ function Composer(
const styles = useThemeStyles();
const StyleUtils = useStyleUtils();

const {inputCallbackRef, inputRef: autoFocusInputRef} = useAutoFocusInput();

useEffect(() => {
if (autoFocus === !!autoFocusInputRef.current) {
return;
}
inputCallbackRef(autoFocus ? textInput.current : null);
}, [autoFocus, inputCallbackRef, autoFocusInputRef]);

/**
* Set the TextInput Ref
* @param {Element} el
Expand All @@ -57,6 +67,10 @@ function Composer(
return;
}

if (autoFocus) {
inputCallbackRef(el);
}

// This callback prop is used by the parent component using the constructor to
// get a ref to the inner textInput element e.g. if we do
// <constructor ref={el => this.textInput = el} /> this will not
Expand Down
3 changes: 2 additions & 1 deletion tests/perf-test/ReportActionCompose.perf-test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {fireEvent, screen} from '@testing-library/react-native';
import type {ComponentType} from 'react';
import type {ComponentType, EffectCallback} from 'react';
import React from 'react';
import Onyx from 'react-native-onyx';
import type Animated from 'react-native-reanimated';
Expand Down Expand Up @@ -36,6 +36,7 @@ jest.mock('@react-navigation/native', () => {
}),
useIsFocused: () => true,
useNavigationState: () => {},
useFocusEffect: (cb: EffectCallback) => cb(),
};
});

Expand Down
Loading