-
Notifications
You must be signed in to change notification settings - Fork 195
/
Copy pathCustomAmount.tsx
65 lines (61 loc) · 2.4 KB
/
CustomAmount.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import classNames from 'classnames';
import {__} from '@wordpress/i18n';
import CurrencyInput from 'react-currency-input-field';
import {CurrencyInputOnChangeValues} from 'react-currency-input-field/dist/components/CurrencyInputProps';
/**
* @since 3.0.0
*/
type CustomAmountProps = {
fieldError?: string;
currency?: string;
currencySymbol?: string;
defaultValue?: string;
value?: string;
onValueChange?: (value: string, name?: string, values?: CurrencyInputOnChangeValues) => void;
};
const formatter = new Intl.NumberFormat(navigator.language);
const groupSeparator = formatter.format(1000).replace(/[0-9]/g, '');
const decimalSeparator = formatter.format(1.1).replace(/[0-9]/g, '');
/**
* @since 3.0.0
*/
const CustomAmount = ({defaultValue, fieldError, currency, value, onValueChange}: CustomAmountProps) => {
return (
<div className={classNames('givewp-fields-amount__input-container', {invalid: fieldError})}>
<CurrencyInput
ref={(input) => {
if (input && fieldError) {
input.focus();
}
}}
intlConfig={{
locale: navigator.language,
currency,
}}
disableAbbreviations
decimalSeparator={decimalSeparator}
groupSeparator={
/**
* Replace non-breaking space to avoid conflict with the suffix separator.
* @link https://github.com/cchanxzy/react-currency-input-field/issues/266
*/
groupSeparator.replace(/\u00A0/g, ' ')
}
className="givewp-fields-amount__input givewp-fields-amount__input-custom"
aria-label={__('Enter custom amount', 'give')}
aria-describedby={fieldError ? 'givewp-field-error-amount' : undefined}
aria-invalid={fieldError ? 'true' : 'false'}
id="amount-custom"
name="amount-custom"
placeholder={__('Enter custom amount', 'give')}
defaultValue={defaultValue}
value={value}
decimalsLimit={2}
onValueChange={(value) => {
onValueChange(value !== undefined ? value : '');
}}
/>
</div>
);
};
export default CustomAmount;