Skip to content

Commit

Permalink
Refactor AmountTextInput types
Browse files Browse the repository at this point in the history
  • Loading branch information
WojtekBoman committed Jan 4, 2024
1 parent ddce776 commit 09b88e3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 23 deletions.
29 changes: 8 additions & 21 deletions src/components/AmountTextInput.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
import type {ForwardedRef} from 'react';
import React from 'react';
import type {StyleProp, TextInput, TextStyle, ViewStyle} from 'react-native';
import type {StyleProp, TextStyle, ViewStyle} from 'react-native';
import useThemeStyles from '@hooks/useThemeStyles';
import CONST from '@src/CONST';
import type TextInputSelection from '@src/types/utils/TextInputSelection';
import TextInputComponent from './TextInput';
import type {TextSelection} from './Composer/types';
import TextInput from './TextInput';
import type {BaseTextInputRef} from './TextInput/BaseTextInput/types';

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

/** A ref to forward to amount text input */
forwardedRef?: ForwardedRef<TextInput>;

/** Function to call when amount in text input is changed */
onChangeAmount: (amount: string) => void;

/** Placeholder value for amount text input */
placeholder: string;

/** Selection Object */
selection?: TextInputSelection;
selection?: TextSelection;

/** Function to call when selection in text input is changed */
onSelectionChange?: () => void;
Expand All @@ -35,27 +32,17 @@ type AmountTextInputProps = {
onKeyPress?: () => void;
};

function AmountTextInput({
formattedAmount,
forwardedRef = undefined,
onChangeAmount,
placeholder,
selection = undefined,
onSelectionChange = () => {},
style = {},
containerStyles = {},
onKeyPress = () => {},
}: AmountTextInputProps) {
function AmountTextInput({formattedAmount, onChangeAmount, placeholder, selection, onSelectionChange, style, containerStyles, onKeyPress}: AmountTextInputProps, ref: BaseTextInputRef) {
const styles = useThemeStyles();
return (
<TextInputComponent
<TextInput
disableKeyboard
autoGrow
hideFocusedState
inputStyle={[styles.iouAmountTextInput, styles.p0, styles.noLeftBorderRadius, styles.noRightBorderRadius, style]}
textInputContainerStyles={[styles.borderNone, styles.noLeftBorderRadius, styles.noRightBorderRadius]}
onChangeText={onChangeAmount}
ref={forwardedRef}
ref={ref}
value={formattedAmount}
placeholder={placeholder}
inputMode={CONST.INPUT_MODE.NUMERIC}
Expand Down
8 changes: 6 additions & 2 deletions src/libs/ComposerUtils/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import * as DeviceCapabilities from '@libs/DeviceCapabilities';
import type TextInputSelection from '@src/types/utils/TextInputSelection';
import getNumberOfLines from './getNumberOfLines';
import updateNumberOfLines from './updateNumberOfLines';

type Selection = {
start: number;
end: number;
};

/**
* Replace substring between selection with a text.
*/
function insertText(text: string, selection: TextInputSelection, textToInsert: string): string {
function insertText(text: string, selection: Selection, textToInsert: string): string {
return text.slice(0, selection.start) + textToInsert + text.slice(selection.end, text.length);
}

Expand Down

0 comments on commit 09b88e3

Please sign in to comment.