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 current selection for web TextInputWithCurrencySymbol #51325

Merged
merged 6 commits into from
Nov 4, 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
10 changes: 5 additions & 5 deletions src/components/AmountForm.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {ForwardedRef} from 'react';
import React, {forwardRef, useCallback, useEffect, useMemo, useRef, useState} from 'react';
import type {NativeSyntheticEvent, TextInputSelectionChangeEventData} from 'react-native';
import type {NativeSyntheticEvent} from 'react-native';
import {View} from 'react-native';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
Expand All @@ -16,7 +16,7 @@ import TextInput from './TextInput';
import isTextInputFocused from './TextInput/BaseTextInput/isTextInputFocused';
import type {BaseTextInputProps, BaseTextInputRef} from './TextInput/BaseTextInput/types';
import TextInputWithCurrencySymbol from './TextInputWithCurrencySymbol';
import type TextInputWithCurrencySymbolProps from './TextInputWithCurrencySymbol/types';
import type BaseTextInputWithCurrencySymbolProps from './TextInputWithCurrencySymbol/types';

type AmountFormProps = {
/** Amount supplied by the FormProvider */
Expand Down Expand Up @@ -51,7 +51,7 @@ type AmountFormProps = {

/** Number of decimals to display */
fixedDecimals?: number;
} & Pick<TextInputWithCurrencySymbolProps, 'hideCurrencySymbol' | 'extraSymbol'> &
} & Pick<BaseTextInputWithCurrencySymbolProps, 'hideCurrencySymbol' | 'extraSymbol'> &
Pick<BaseTextInputProps, 'autoFocus'>;

/**
Expand Down Expand Up @@ -292,11 +292,11 @@ function AmountForm(
}}
selectedCurrencyCode={currency}
selection={selection}
onSelectionChange={(e: NativeSyntheticEvent<TextInputSelectionChangeEventData>) => {
onSelectionChange={(start, end) => {
if (!shouldUpdateSelection) {
return;
}
setSelection(e.nativeEvent.selection);
setSelection({start, end});
}}
onKeyPress={textInputKeyPress}
isCurrencyPressable={isCurrencyPressable}
Expand Down
8 changes: 4 additions & 4 deletions src/components/MoneyRequestAmountInput.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {ForwardedRef} from 'react';
import React, {useCallback, useEffect, useImperativeHandle, useMemo, useRef, useState} from 'react';
import type {NativeSyntheticEvent, StyleProp, TextInputSelectionChangeEventData, TextStyle, ViewStyle} from 'react-native';
import type {NativeSyntheticEvent, StyleProp, TextStyle, ViewStyle} from 'react-native';
import useLocalize from '@hooks/useLocalize';
import {useMouseContext} from '@hooks/useMouseContext';
import * as Browser from '@libs/Browser';
Expand Down Expand Up @@ -308,7 +308,7 @@ function MoneyRequestAmountInput(
}}
selectedCurrencyCode={currency}
selection={selection}
onSelectionChange={(e: NativeSyntheticEvent<TextInputSelectionChangeEventData>) => {
onSelectionChange={(selectionStart, selectionEnd) => {
if (shouldIgnoreSelectionWhenUpdatedManually && willSelectionBeUpdatedManually.current) {
willSelectionBeUpdatedManually.current = false;
return;
Expand All @@ -320,8 +320,8 @@ function MoneyRequestAmountInput(
// When the amount is updated in setNewAmount on iOS, in onSelectionChange formattedAmount stores the value before the update. Using amountRef allows us to read the updated value
const maxSelection = amountRef.current?.length ?? formattedAmount.length;
amountRef.current = undefined;
const start = Math.min(e.nativeEvent.selection.start, maxSelection);
const end = Math.min(e.nativeEvent.selection.end, maxSelection);
const start = Math.min(selectionStart, maxSelection);
const end = Math.min(selectionEnd, maxSelection);
setSelection({start, end});
}}
onKeyPress={textInputKeyPress}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import useThemeStyles from '@hooks/useThemeStyles';
import * as CurrencyUtils from '@libs/CurrencyUtils';
import * as MoneyRequestUtils from '@libs/MoneyRequestUtils';
import type {BaseTextInputRef} from '@src/components/TextInput/BaseTextInput/types';
import type TextInputWithCurrencySymbolProps from './types';
import type BaseTextInputWithCurrencySymbolProps from './types';

function BaseTextInputWithCurrencySymbol(
{
Expand All @@ -24,7 +24,7 @@ function BaseTextInputWithCurrencySymbol(
extraSymbol,
style,
...rest
}: TextInputWithCurrencySymbolProps,
}: BaseTextInputWithCurrencySymbolProps,
ref: React.ForwardedRef<BaseTextInputRef>,
) {
const {fromLocaleDigit} = useLocalize();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, {useEffect, useState} from 'react';
import type {NativeSyntheticEvent, TextInputSelectionChangeEventData} from 'react-native';
import type {BaseTextInputRef} from '@src/components/TextInput/BaseTextInput/types';
import BaseTextInputWithCurrencySymbol from './BaseTextInputWithCurrencySymbol';
import type TextInputWithCurrencySymbolProps from './types';
import type {TextInputWithCurrencySymbolProps} from './types';

function TextInputWithCurrencySymbol({onSelectionChange = () => {}, ...props}: TextInputWithCurrencySymbolProps, ref: React.ForwardedRef<BaseTextInputRef>) {
const [skipNextSelectionChange, setSkipNextSelectionChange] = useState(false);
Expand All @@ -21,7 +21,7 @@ function TextInputWithCurrencySymbol({onSelectionChange = () => {}, ...props}: T
setSkipNextSelectionChange(false);
return;
}
onSelectionChange(event);
onSelectionChange(event.nativeEvent.selection.start, event.nativeEvent.selection.end);
}}
/>
);
Expand Down
8 changes: 6 additions & 2 deletions src/components/TextInputWithCurrencySymbol/index.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import React from 'react';
import type {NativeSyntheticEvent, TextInputSelectionChangeEventData} from 'react-native';
import type {BaseTextInputRef} from '@src/components/TextInput/BaseTextInput/types';
import BaseTextInputWithCurrencySymbol from './BaseTextInputWithCurrencySymbol';
import type TextInputWithCurrencySymbolProps from './types';
import type {TextInputWithCurrencySymbolProps} from './types';

function TextInputWithCurrencySymbol(props: TextInputWithCurrencySymbolProps, ref: React.ForwardedRef<BaseTextInputRef>) {
function TextInputWithCurrencySymbol({onSelectionChange = () => {}, ...props}: TextInputWithCurrencySymbolProps, ref: React.ForwardedRef<BaseTextInputRef>) {
return (
<BaseTextInputWithCurrencySymbol
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
ref={ref}
onSelectionChange={(event: NativeSyntheticEvent<TextInputSelectionChangeEventData>) => {
onSelectionChange(event.nativeEvent.selection.start, event.nativeEvent.selection.end);
}}
/>
);
}
Expand Down
42 changes: 42 additions & 0 deletions src/components/TextInputWithCurrencySymbol/index.web.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import React, {useRef} from 'react';
import type {NativeSyntheticEvent, TextInputSelectionChangeEventData} from 'react-native';
import BaseTextInputWithCurrencySymbol from './BaseTextInputWithCurrencySymbol';
import type {TextInputWithCurrencySymbolProps} from './types';

function TextInputWithCurrencySymbol({onSelectionChange = () => {}, ...props}: TextInputWithCurrencySymbolProps, ref: React.ForwardedRef<HTMLFormElement>) {
const textInputRef = useRef<HTMLFormElement | null>(null);

return (
<BaseTextInputWithCurrencySymbol
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
ref={(element) => {
textInputRef.current = element as HTMLFormElement;

if (!ref) {
return;
}

if (typeof ref === 'function') {
ref(element as HTMLFormElement);
return;
}

// eslint-disable-next-line no-param-reassign
ref.current = element as HTMLFormElement;
}}
onSelectionChange={(event: NativeSyntheticEvent<TextInputSelectionChangeEventData>) => {
onSelectionChange(event.nativeEvent.selection.start, event.nativeEvent.selection.end);
}}
onPress={() => {
const selectionStart = (textInputRef.current?.selectionStart as number) ?? 0;
const selectionEnd = (textInputRef.current?.selectionEnd as number) ?? 0;
onSelectionChange(selectionStart, selectionEnd);
}}
/>
);
}

TextInputWithCurrencySymbol.displayName = 'TextInputWithCurrencySymbol';

export default React.forwardRef(TextInputWithCurrencySymbol);
12 changes: 9 additions & 3 deletions src/components/TextInputWithCurrencySymbol/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {NativeSyntheticEvent, StyleProp, TextInputFocusEventData, TextInput
import type {TextSelection} from '@components/Composer/types';
import type {BaseTextInputProps} from '@components/TextInput/BaseTextInput/types';

type TextInputWithCurrencySymbolProps = {
type BaseTextInputWithCurrencySymbolProps = {
/** Formatted amount in local currency */
formattedAmount: string;

Expand Down Expand Up @@ -77,6 +77,12 @@ type TextInputWithCurrencySymbolProps = {

/** Hide the focus styles on TextInput */
hideFocusedState?: boolean;
} & Pick<BaseTextInputProps, 'autoFocus' | 'autoGrow' | 'contentWidth' | 'regex'>;
} & Pick<BaseTextInputProps, 'autoFocus' | 'autoGrow' | 'contentWidth' | 'regex' | 'onPress'>;

export default TextInputWithCurrencySymbolProps;
type TextInputWithCurrencySymbolProps = Omit<BaseTextInputWithCurrencySymbolProps, 'onSelectionChange'> & {
onSelectionChange?: (start: number, end: number) => void;
};

export type {TextInputWithCurrencySymbolProps};

export default BaseTextInputWithCurrencySymbolProps;
Loading