Home >Web Front-end >JS Tutorial >How to Build a Centered Input Field for Amounts in React Native
A clean, centered input field for entering amounts is a small but significant part of any financial app. It enhances user experience, provides clarity, and ensures ease of use. In this article, we’ll walk through creating a reusable CurrencySection component in React Native.
This component is simple yet powerful, featuring:
Let’s dive into the details!
The CurrencySection component is designed to:
Here’s the full implementation of the CurrencySection component:
/* eslint-disable react-native/no-inline-styles */ import React, {useCallback, useState} from 'react'; import { Keyboard, NativeSyntheticEvent, Platform, StyleSheet, TextInput, TextInputContentSizeChangeEventData, TextInputProps, View, } from 'react-native'; import { formatAmountInCurrency, MAX_AMOUNT_LENGTH_CURRENCY_SECTION, trimNumber, } from '../../utils/functions'; import colors from '../../utils/helpers/colors'; import {InlineError} from '../../utils/helpers/Reusables'; import AppText from '../texts/appText'; interface CurrencySectionProps extends TextInputProps { title?: string; amount?: string; error?: string; onChangeAmount?: (amount: string) => void; isEditable?: boolean; currency?: string; } const CurrencySection: React.FC<CurrencySectionProps> = ({ title = 'Enter amount', amount = '', error = '', onChangeAmount, isEditable = true, currency = '₦', ...props }) => { const [height, setHeight] = useState(42); const onChangeText = useCallback( (text: string) => { onChangeAmount?.(trimNumber(text)); }, [onChangeAmount], ); const onBlur = useCallback(() => { if (amount) { const formattedAmount = formatAmountInCurrency( Number(amount.replaceAll(',', '')), ).replaceAll(',', ''); onChangeAmount?.(formattedAmount); } }, [amount, onChangeAmount]); const handleContentSizeChange = useCallback( (e: NativeSyntheticEvent<TextInputContentSizeChangeEventData>) => { setHeight(e.nativeEvent.contentSize.height); }, [], ); return ( <View> <hr> <p><img src="https://img.php.cn/upload/article/000/000/000/173624596347120.jpg" alt="How to Build a Centered Input Field for Amounts in React Native"></p><h3> Highlights of the CurrencySection Component </h3> <ol> <li><p><strong>Dynamic Height Adjustment</strong><br><br> The input field dynamically resizes to match its content size, ensuring a clean and adaptive design. </p></li> <li><p><strong>Automatic Currency Formatting</strong><br><br> The formatAmountInCurrency function ensures amounts are formatted appropriately, making them easier to read. </p></li> <li><p><strong>Customizable and Reusable</strong><br><br> With props for titles, currency symbols, and error messages, this component is flexible enough for various use cases. </p></li> <li><p><strong>Error Feedback</strong><br><br> Inline error messages provide immediate feedback without disrupting the flow of the UI. </p></li> </ol> <hr> <h3> Styling Choices </h3>
This CurrencySection component is a simple yet effective addition to any financial app. It’s designed to handle core input requirements while keeping the UI visually appealing and functional.
Give it a try, customize it to suit your app’s style, and let your users enjoy a seamless experience!
Feel free to share your thoughts or questions in the comments.
The above is the detailed content of How to Build a Centered Input Field for Amounts in React Native. For more information, please follow other related articles on the PHP Chinese website!