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/10731 auto focus input #51172

Merged
merged 24 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
38 changes: 21 additions & 17 deletions src/components/Composer/implementation/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -281,24 +281,28 @@ function Composer(
onClear(currentText);
}, [onClear, onSelectionChange]);

useImperativeHandle(ref, () => {
const textInputRef = textInput.current;
if (!textInputRef) {
throw new Error('textInputRef is not available. This should never happen and indicates a developer error.');
}
useImperativeHandle(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@christianwen Is there any new update on this file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no new update, just moved to the new file

ref,
() => {
const textInputRef = textInput.current;
if (!textInputRef) {
throw new Error('textInputRef is not available. This should never happen and indicates a developer error.');
}

return {
...textInputRef,
// Overwrite clear with our custom implementation, which mimics how the native TextInput's clear method works
clear,
// We have to redefine these methods as they are inherited by prototype chain and are not accessible directly
blur: () => textInputRef.blur(),
focus: () => textInputRef.focus(),
get scrollTop() {
return textInputRef.scrollTop;
},
};
}, [clear]);
return {
...textInputRef,
// Overwrite clear with our custom implementation, which mimics how the native TextInput's clear method works
clear,
// We have to redefine these methods as they are inherited by prototype chain and are not accessible directly
blur: () => textInputRef.blur(),
focus: () => textInputRef.focus(),
get scrollTop() {
return textInputRef.scrollTop;
},
};
},
[clear],
);

const handleKeyPress = useCallback(
(e: NativeSyntheticEvent<TextInputKeyPressEventData>) => {
Expand Down
5 changes: 4 additions & 1 deletion src/pages/home/ReportScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ function ReportScreen({route, currentReportID = '', navigation}: ReportScreenPro
const policy = policies?.[`${ONYXKEYS.COLLECTION.POLICY}${report?.policyID ?? '-1'}`];
const isTopMostReportId = currentReportID === reportIDFromRoute;
const didSubscribeToReportLeavingEvents = useRef(false);
const [showSoftInputOnFocus, setShowSoftInputOnFocus] = useState(false);

useEffect(() => {
if (!report?.reportID || shouldHideReport) {
Expand Down Expand Up @@ -711,7 +712,7 @@ function ReportScreen({route, currentReportID = '', navigation}: ReportScreenPro
<ScreenWrapper
navigation={navigation}
style={screenWrapperStyle}
shouldEnableKeyboardAvoidingView={isTopMostReportId || isInNarrowPaneModal}
shouldEnableKeyboardAvoidingView={(isTopMostReportId || isInNarrowPaneModal) && showSoftInputOnFocus}
testID={`report-screen-${reportID ?? ''}`}
>
<FullPageNotFoundView
Expand Down Expand Up @@ -808,6 +809,8 @@ function ReportScreen({route, currentReportID = '', navigation}: ReportScreenPro
isComposerFullSize={!!isComposerFullSize}
lastReportAction={lastReportAction}
workspaceTooltip={workspaceTooltip}
showSoftInputOnFocus={showSoftInputOnFocus}
setShowSoftInputOnFocus={setShowSoftInputOnFocus}
/>
) : null}
</View>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ type ComposerWithSuggestionsProps = Partial<ChildrenProps> & {

/** policy ID of the report */
policyID: string;

/** Whether the soft keyboard is open */
christianwen marked this conversation as resolved.
Show resolved Hide resolved
showSoftInputOnFocus: boolean;

/** A method to update showSoftInputOnFocus */
setShowSoftInputOnFocus: (value: boolean) => void;
};

type SwitchToCurrentReportProps = {
Expand Down Expand Up @@ -225,6 +231,8 @@ function ComposerWithSuggestions(

// For testing
children,
showSoftInputOnFocus,
setShowSoftInputOnFocus,
}: ComposerWithSuggestionsProps,
ref: ForwardedRef<ComposerRef>,
) {
Expand Down Expand Up @@ -274,8 +282,6 @@ function ComposerWithSuggestions(

const [composerHeight, setComposerHeight] = useState(0);

const [showSoftInputOnFocus, setShowSoftInputOnFocus] = useState(false);

const textInputRef = useRef<TextInput | null>(null);

const syncSelectionWithOnChangeTextRef = useRef<SyncSelection | null>(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ type ReportActionComposeProps = Pick<ComposerWithSuggestionsProps, 'reportID' |

/** Should show educational tooltip */
shouldShowEducationalTooltip?: boolean;

/** Whether the soft keyboard is open */
showSoftInputOnFocus: boolean;

/** A method to update showSoftInputOnFocus */
setShowSoftInputOnFocus: (value: boolean) => void;
};

const willBlurTextInputOnTapOutside = willBlurTextInputOnTapOutsideFunc();
Expand All @@ -104,8 +110,10 @@ function ReportActionCompose({
isReportReadyForDisplay = true,
lastReportAction,
shouldShowEducationalTooltip,
showSoftInputOnFocus,
onComposerFocus,
onComposerBlur,
setShowSoftInputOnFocus,
}: ReportActionComposeProps) {
const theme = useTheme();
const styles = useThemeStyles();
Expand Down Expand Up @@ -487,6 +495,8 @@ function ReportActionCompose({
}
validateCommentMaxLength(value, {reportID});
}}
showSoftInputOnFocus={showSoftInputOnFocus}
setShowSoftInputOnFocus={setShowSoftInputOnFocus}
/>
<ReportDropUI
onDrop={(event: DragEvent) => {
Expand Down
11 changes: 11 additions & 0 deletions src/pages/home/report/ReportFooter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ type ReportFooterProps = {

/** A method to call when the input is blur */
onComposerBlur: () => void;

/** Whether the soft keyboard is open */
showSoftInputOnFocus: boolean;

/** A method to update showSoftInputOnFocus */
setShowSoftInputOnFocus: (value: boolean) => void;
};

function ReportFooter({
Expand All @@ -73,8 +79,10 @@ function ReportFooter({
isReportReadyForDisplay = true,
isComposerFullSize = false,
workspaceTooltip,
showSoftInputOnFocus,
onComposerBlur,
onComposerFocus,
setShowSoftInputOnFocus,
}: ReportFooterProps) {
const styles = useThemeStyles();
const {isOffline} = useNetwork();
Expand Down Expand Up @@ -225,6 +233,8 @@ function ReportFooter({
isComposerFullSize={isComposerFullSize}
isReportReadyForDisplay={isReportReadyForDisplay}
shouldShowEducationalTooltip={didScreenTransitionEnd && shouldShowEducationalTooltip}
showSoftInputOnFocus={showSoftInputOnFocus}
setShowSoftInputOnFocus={setShowSoftInputOnFocus}
/>
</SwipeableView>
</View>
Expand All @@ -244,6 +254,7 @@ export default memo(
prevProps.lastReportAction === nextProps.lastReportAction &&
prevProps.isReportReadyForDisplay === nextProps.isReportReadyForDisplay &&
prevProps.workspaceTooltip?.shouldShow === nextProps.workspaceTooltip?.shouldShow &&
prevProps.showSoftInputOnFocus === nextProps.showSoftInputOnFocus &&
lodashIsEqual(prevProps.reportMetadata, nextProps.reportMetadata) &&
lodashIsEqual(prevProps.policy?.employeeList, nextProps.policy?.employeeList) &&
lodashIsEqual(prevProps.policy?.role, nextProps.policy?.role),
Expand Down
2 changes: 2 additions & 0 deletions tests/perf-test/ReportActionCompose.perf-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ function ReportActionComposeWrapper() {
disabled={false}
report={LHNTestUtils.getFakeReport()}
isComposerFullSize
showSoftInputOnFocus={false}
setShowSoftInputOnFocus={() => {}}
/>
</ComposeProviders>
);
Expand Down
Loading