Skip to content

Commit

Permalink
Merge pull request #39311 from Krishna2323/krishna2323/issue/36862
Browse files Browse the repository at this point in the history
fix: When amount is highlighted, clicking on the page does not deselect the text
  • Loading branch information
Hayata Suenaga authored Apr 8, 2024
2 parents 327bfd3 + 782002b commit a6532ae
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/components/AmountForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,13 @@ function AmountForm(
if (!ids.includes(relatedTargetId)) {
return;
}

event.preventDefault();
setSelection({
start: selection.end,
end: selection.end,
});

if (!textInput.current) {
return;
}
Expand Down
22 changes: 22 additions & 0 deletions src/pages/iou/steps/MoneyRequestAmountForm.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {useIsFocused} from '@react-navigation/core';
import type {ForwardedRef} from 'react';
import React, {useCallback, useEffect, useRef, useState} from 'react';
import {View} from 'react-native';
Expand All @@ -8,6 +9,7 @@ import FormHelpMessage from '@components/FormHelpMessage';
import ScrollView from '@components/ScrollView';
import TextInputWithCurrencySymbol from '@components/TextInputWithCurrencySymbol';
import useLocalize from '@hooks/useLocalize';
import usePrevious from '@hooks/usePrevious';
import useThemeStyles from '@hooks/useThemeStyles';
import useWindowDimensions from '@hooks/useWindowDimensions';
import * as Browser from '@libs/Browser';
Expand Down Expand Up @@ -97,6 +99,9 @@ function MoneyRequestAmountForm(
const [formError, setFormError] = useState<MaybePhraseKey>('');
const [shouldUpdateSelection, setShouldUpdateSelection] = useState(true);

const isFocused = useIsFocused();
const wasFocused = usePrevious(isFocused);

const [selection, setSelection] = useState({
start: selectedAmountAsString.length,
end: selectedAmountAsString.length,
Expand All @@ -116,6 +121,11 @@ function MoneyRequestAmountForm(
}

event.preventDefault();
setSelection({
start: selection.end,
end: selection.end,
});

if (!textInput.current) {
return;
}
Expand Down Expand Up @@ -191,6 +201,18 @@ function MoneyRequestAmountForm(
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [setNewAmount]);

// Removes text selection if user visits currency selector with selection and comes back
useEffect(() => {
if (!isFocused || wasFocused) {
return;
}

setSelection({
start: selection.end,
end: selection.end,
});
}, [selection.end, isFocused, selection, wasFocused]);

/**
* Update amount with number or Backspace pressed for BigNumberPad.
* Validate new amount with decimal number regex up to 6 digits and 2 decimal digit to enable Next button
Expand Down

0 comments on commit a6532ae

Please sign in to comment.