give
/
src
/
DonationForms
/
resources
/
registrars
/
templates
/
fields
/
Amount
/
CustomAmount.tsx
CurrencySwitcher.tsx
2 years ago
CustomAmount.tsx
1 year ago
DonationAmountCurrency.tsx
2 years ago
DonationAmountLevels.tsx
1 month ago
index.tsx
1 month ago
withCurrencySettings.tsx
1 month ago
CustomAmount.tsx
71 lines
| 1 | import classNames from 'classnames'; |
| 2 | import {__} from '@wordpress/i18n'; |
| 3 | import CurrencyInput from 'react-currency-input-field'; |
| 4 | import {CurrencyInputOnChangeValues} from 'react-currency-input-field/dist/components/CurrencyInputProps'; |
| 5 | |
| 6 | import {useEffect, useRef} from "react"; |
| 7 | /** |
| 8 | * @since 3.0.0 |
| 9 | */ |
| 10 | type CustomAmountProps = { |
| 11 | fieldError?: string; |
| 12 | currency?: string; |
| 13 | currencySymbol?: string; |
| 14 | defaultValue?: string; |
| 15 | value?: string; |
| 16 | onValueChange?: (value: string, name?: string, values?: CurrencyInputOnChangeValues) => void; |
| 17 | }; |
| 18 | |
| 19 | const formatter = new Intl.NumberFormat(navigator.language); |
| 20 | const groupSeparator = formatter.format(1000).replace(/[0-9]/g, ''); |
| 21 | const decimalSeparator = formatter.format(1.1).replace(/[0-9]/g, ''); |
| 22 | |
| 23 | /** |
| 24 | * @since 4.3.0 add module styles to override currency-input placeholder styling. |
| 25 | * @since 3.0.0 |
| 26 | */ |
| 27 | const CustomAmount = ({defaultValue, fieldError, currency, value, onValueChange}: CustomAmountProps) => { |
| 28 | const ref = useRef<HTMLInputElement | null>(null); |
| 29 | |
| 30 | useEffect(() => { |
| 31 | if (fieldError && ref.current) { |
| 32 | ref.current.focus(); |
| 33 | } |
| 34 | }, [fieldError]); |
| 35 | |
| 36 | return ( |
| 37 | <div className={classNames('givewp-fields-amount__input-container', {invalid: fieldError})}> |
| 38 | <CurrencyInput |
| 39 | ref={ref} |
| 40 | intlConfig={{ |
| 41 | locale: navigator.language, |
| 42 | currency, |
| 43 | }} |
| 44 | disableAbbreviations |
| 45 | decimalSeparator={decimalSeparator} |
| 46 | groupSeparator={ |
| 47 | /** |
| 48 | * Replace non-breaking space to avoid conflict with the suffix separator. |
| 49 | * @link https://github.com/cchanxzy/react-currency-input-field/issues/266 |
| 50 | */ |
| 51 | groupSeparator.replace(/\u00A0/g, ' ') |
| 52 | } |
| 53 | className='givewp-fields-amount__input givewp-fields-amount__input-custom' |
| 54 | aria-label={__('Enter custom amount', 'give')} |
| 55 | aria-describedby={fieldError ? 'givewp-field-error-amount' : undefined} |
| 56 | aria-invalid={fieldError ? 'true' : 'false'} |
| 57 | id="amount-custom" |
| 58 | placeholder={__('Enter custom amount', 'give')} |
| 59 | defaultValue={defaultValue} |
| 60 | value={value} |
| 61 | decimalsLimit={2} |
| 62 | onValueChange={(value) => { |
| 63 | onValueChange(value !== undefined ? value : ''); |
| 64 | }} |
| 65 | /> |
| 66 | </div> |
| 67 | ); |
| 68 | }; |
| 69 | |
| 70 | export default CustomAmount; |
| 71 |