@transferwise/components 46.112.1 → 46.113.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/expressiveMoneyInput/ExpressiveMoneyInput.js +113 -0
- package/build/expressiveMoneyInput/ExpressiveMoneyInput.js.map +1 -0
- package/build/expressiveMoneyInput/ExpressiveMoneyInput.messages.js +17 -0
- package/build/expressiveMoneyInput/ExpressiveMoneyInput.messages.js.map +1 -0
- package/build/expressiveMoneyInput/ExpressiveMoneyInput.messages.mjs +13 -0
- package/build/expressiveMoneyInput/ExpressiveMoneyInput.messages.mjs.map +1 -0
- package/build/expressiveMoneyInput/ExpressiveMoneyInput.mjs +109 -0
- package/build/expressiveMoneyInput/ExpressiveMoneyInput.mjs.map +1 -0
- package/build/expressiveMoneyInput/amountInput/AmountInput.js +281 -0
- package/build/expressiveMoneyInput/amountInput/AmountInput.js.map +1 -0
- package/build/expressiveMoneyInput/amountInput/AmountInput.mjs +279 -0
- package/build/expressiveMoneyInput/amountInput/AmountInput.mjs.map +1 -0
- package/build/expressiveMoneyInput/amountInput/utils.js +87 -0
- package/build/expressiveMoneyInput/amountInput/utils.js.map +1 -0
- package/build/expressiveMoneyInput/amountInput/utils.mjs +78 -0
- package/build/expressiveMoneyInput/amountInput/utils.mjs.map +1 -0
- package/build/expressiveMoneyInput/animatedNumber/AnimatedNumber.js +50 -0
- package/build/expressiveMoneyInput/animatedNumber/AnimatedNumber.js.map +1 -0
- package/build/expressiveMoneyInput/animatedNumber/AnimatedNumber.mjs +48 -0
- package/build/expressiveMoneyInput/animatedNumber/AnimatedNumber.mjs.map +1 -0
- package/build/expressiveMoneyInput/chevron/Chevron.js +31 -0
- package/build/expressiveMoneyInput/chevron/Chevron.js.map +1 -0
- package/build/expressiveMoneyInput/chevron/Chevron.mjs +29 -0
- package/build/expressiveMoneyInput/chevron/Chevron.mjs.map +1 -0
- package/build/expressiveMoneyInput/currencySelector/CurrencySelector.js +160 -0
- package/build/expressiveMoneyInput/currencySelector/CurrencySelector.js.map +1 -0
- package/build/expressiveMoneyInput/currencySelector/CurrencySelector.mjs +157 -0
- package/build/expressiveMoneyInput/currencySelector/CurrencySelector.mjs.map +1 -0
- package/build/expressiveMoneyInput/hooks/useFocus.js +37 -0
- package/build/expressiveMoneyInput/hooks/useFocus.js.map +1 -0
- package/build/expressiveMoneyInput/hooks/useFocus.mjs +35 -0
- package/build/expressiveMoneyInput/hooks/useFocus.mjs.map +1 -0
- package/build/expressiveMoneyInput/hooks/useInputStyle.js +71 -0
- package/build/expressiveMoneyInput/hooks/useInputStyle.js.map +1 -0
- package/build/expressiveMoneyInput/hooks/useInputStyle.mjs +69 -0
- package/build/expressiveMoneyInput/hooks/useInputStyle.mjs.map +1 -0
- package/build/i18n/en.json +2 -0
- package/build/i18n/en.json.js +2 -0
- package/build/i18n/en.json.js.map +1 -1
- package/build/i18n/en.json.mjs +2 -0
- package/build/i18n/en.json.mjs.map +1 -1
- package/build/index.js +2 -0
- package/build/index.js.map +1 -1
- package/build/index.mjs +1 -0
- package/build/index.mjs.map +1 -1
- package/build/main.css +73 -7
- package/build/prompt/InlinePrompt/InlinePrompt.js +7 -0
- package/build/prompt/InlinePrompt/InlinePrompt.js.map +1 -1
- package/build/prompt/InlinePrompt/InlinePrompt.mjs +8 -1
- package/build/prompt/InlinePrompt/InlinePrompt.mjs.map +1 -1
- package/build/styles/expressiveMoneyInput/ExpressiveMoneyInput.css +58 -0
- package/build/styles/expressiveMoneyInput/amountInput/AmountInput.css +32 -0
- package/build/styles/expressiveMoneyInput/chevron/Chevron.css +12 -0
- package/build/styles/expressiveMoneyInput/currencySelector/CurrencySelector.css +6 -0
- package/build/styles/main.css +73 -7
- package/build/styles/moneyInput/MoneyInput.css +8 -0
- package/build/styles/prompt/InlinePrompt/InlinePrompt.css +7 -7
- package/build/types/expressiveMoneyInput/ExpressiveMoneyInput.d.ts +59 -0
- package/build/types/expressiveMoneyInput/ExpressiveMoneyInput.d.ts.map +1 -0
- package/build/types/expressiveMoneyInput/ExpressiveMoneyInput.messages.d.ts +12 -0
- package/build/types/expressiveMoneyInput/ExpressiveMoneyInput.messages.d.ts.map +1 -0
- package/build/types/expressiveMoneyInput/amountInput/AmountInput.d.ts +13 -0
- package/build/types/expressiveMoneyInput/amountInput/AmountInput.d.ts.map +1 -0
- package/build/types/expressiveMoneyInput/amountInput/utils.d.ts +22 -0
- package/build/types/expressiveMoneyInput/amountInput/utils.d.ts.map +1 -0
- package/build/types/expressiveMoneyInput/animatedNumber/AnimatedNumber.d.ts +9 -0
- package/build/types/expressiveMoneyInput/animatedNumber/AnimatedNumber.d.ts.map +1 -0
- package/build/types/expressiveMoneyInput/chevron/Chevron.d.ts +6 -0
- package/build/types/expressiveMoneyInput/chevron/Chevron.d.ts.map +1 -0
- package/build/types/expressiveMoneyInput/currencySelector/CurrencySelector.d.ts +30 -0
- package/build/types/expressiveMoneyInput/currencySelector/CurrencySelector.d.ts.map +1 -0
- package/build/types/expressiveMoneyInput/hooks/useFocus.d.ts +7 -0
- package/build/types/expressiveMoneyInput/hooks/useFocus.d.ts.map +1 -0
- package/build/types/expressiveMoneyInput/hooks/useInputStyle.d.ts +10 -0
- package/build/types/expressiveMoneyInput/hooks/useInputStyle.d.ts.map +1 -0
- package/build/types/expressiveMoneyInput/hooks/useSelectionRange.d.ts +10 -0
- package/build/types/expressiveMoneyInput/hooks/useSelectionRange.d.ts.map +1 -0
- package/build/types/expressiveMoneyInput/index.d.ts +3 -0
- package/build/types/expressiveMoneyInput/index.d.ts.map +1 -0
- package/build/types/index.d.ts +2 -0
- package/build/types/index.d.ts.map +1 -1
- package/build/types/prompt/InlinePrompt/InlinePrompt.d.ts +3 -2
- package/build/types/prompt/InlinePrompt/InlinePrompt.d.ts.map +1 -1
- package/build/types/test-utils/index.d.ts +4 -0
- package/build/types/test-utils/index.d.ts.map +1 -1
- package/package.json +6 -6
- package/src/expressiveMoneyInput/ExpressiveMoneyInput.autofocus.docs.mdx +12 -0
- package/src/expressiveMoneyInput/ExpressiveMoneyInput.css +58 -0
- package/src/expressiveMoneyInput/ExpressiveMoneyInput.less +13 -0
- package/src/expressiveMoneyInput/ExpressiveMoneyInput.messages.ts +13 -0
- package/src/expressiveMoneyInput/ExpressiveMoneyInput.story.tsx +232 -0
- package/src/expressiveMoneyInput/ExpressiveMoneyInput.tsx +156 -0
- package/src/expressiveMoneyInput/amountInput/AmountInput.css +32 -0
- package/src/expressiveMoneyInput/amountInput/AmountInput.less +43 -0
- package/src/expressiveMoneyInput/amountInput/AmountInput.tsx +353 -0
- package/src/expressiveMoneyInput/amountInput/utils.spec.ts +114 -0
- package/src/expressiveMoneyInput/amountInput/utils.ts +116 -0
- package/src/expressiveMoneyInput/animatedNumber/AnimatedNumber.tsx +40 -0
- package/src/expressiveMoneyInput/chevron/Chevron.css +12 -0
- package/src/expressiveMoneyInput/chevron/Chevron.less +13 -0
- package/src/expressiveMoneyInput/chevron/Chevron.tsx +35 -0
- package/src/expressiveMoneyInput/currencySelector/CurrencySelector.css +6 -0
- package/src/expressiveMoneyInput/currencySelector/CurrencySelector.less +7 -0
- package/src/expressiveMoneyInput/currencySelector/CurrencySelector.tsx +220 -0
- package/src/expressiveMoneyInput/hooks/useFocus.ts +35 -0
- package/src/expressiveMoneyInput/hooks/useInputStyle.ts +85 -0
- package/src/expressiveMoneyInput/hooks/useSelectionRange.ts +23 -0
- package/src/expressiveMoneyInput/index.ts +2 -0
- package/src/i18n/en.json +2 -0
- package/src/index.ts +2 -0
- package/src/main.css +73 -7
- package/src/main.less +1 -0
- package/src/moneyInput/MoneyInput.css +8 -0
- package/src/moneyInput/MoneyInput.less +5 -0
- package/src/prompt/InlinePrompt/InlinePrompt.css +7 -7
- package/src/prompt/InlinePrompt/InlinePrompt.less +7 -7
- package/src/prompt/InlinePrompt/InlinePrompt.spec.tsx +6 -0
- package/src/prompt/InlinePrompt/InlinePrompt.story.tsx +39 -0
- package/src/prompt/InlinePrompt/InlinePrompt.tsx +12 -2
- package/src/ssr.spec.tsx +1 -0
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
import { formatAmount } from '@transferwise/formatting';
|
|
2
|
+
import { clsx } from 'clsx';
|
|
3
|
+
import { AnimatePresence } from 'framer-motion';
|
|
4
|
+
import { type ChangeEvent, type KeyboardEvent, useEffect, useMemo, useRef, useState } from 'react';
|
|
5
|
+
import { useIntl } from 'react-intl';
|
|
6
|
+
|
|
7
|
+
import { Props as ExpressiveMoneyInputProps } from '../ExpressiveMoneyInput';
|
|
8
|
+
import { AnimatedNumber } from '../animatedNumber/AnimatedNumber';
|
|
9
|
+
import { useFocus } from '../hooks/useFocus';
|
|
10
|
+
import { useInputStyle } from '../hooks/useInputStyle';
|
|
11
|
+
import {
|
|
12
|
+
getDecimalCount,
|
|
13
|
+
getDecimalSeparator,
|
|
14
|
+
getEnteredDecimalsCount,
|
|
15
|
+
getFormattedString,
|
|
16
|
+
getGroupSeparator,
|
|
17
|
+
getUnformattedNumber,
|
|
18
|
+
isAllowedInputKey,
|
|
19
|
+
isInputPossiblyOverflowing,
|
|
20
|
+
} from './utils';
|
|
21
|
+
|
|
22
|
+
type Props = {
|
|
23
|
+
id: string;
|
|
24
|
+
describedById?: string;
|
|
25
|
+
amount?: number | null;
|
|
26
|
+
currency: string;
|
|
27
|
+
autoFocus?: boolean;
|
|
28
|
+
onChange: (amount: number | null) => void;
|
|
29
|
+
onFocusChange?: (focused: boolean) => void;
|
|
30
|
+
} & Pick<ExpressiveMoneyInputProps, 'loading'>;
|
|
31
|
+
|
|
32
|
+
export const AmountInput = ({
|
|
33
|
+
id,
|
|
34
|
+
describedById,
|
|
35
|
+
amount,
|
|
36
|
+
currency,
|
|
37
|
+
autoFocus,
|
|
38
|
+
onChange,
|
|
39
|
+
onFocusChange,
|
|
40
|
+
loading,
|
|
41
|
+
}: Props) => {
|
|
42
|
+
const intl = useIntl();
|
|
43
|
+
const { focus, setFocus, visualFocus, setVisualFocus } = useFocus();
|
|
44
|
+
|
|
45
|
+
const [value, setValue] = useState<string>(
|
|
46
|
+
amount
|
|
47
|
+
? getFormattedString({
|
|
48
|
+
value: amount,
|
|
49
|
+
currency,
|
|
50
|
+
locale: intl.locale,
|
|
51
|
+
})
|
|
52
|
+
: '',
|
|
53
|
+
);
|
|
54
|
+
const numericValue = useMemo(() => {
|
|
55
|
+
return getUnformattedNumber({
|
|
56
|
+
value,
|
|
57
|
+
currency,
|
|
58
|
+
locale: intl.locale,
|
|
59
|
+
});
|
|
60
|
+
}, [value, currency, intl.locale]);
|
|
61
|
+
|
|
62
|
+
const valueWithFullDecimals = useMemo(() => {
|
|
63
|
+
return getFormattedString({
|
|
64
|
+
value: numericValue ?? 0,
|
|
65
|
+
currency,
|
|
66
|
+
locale: intl.locale,
|
|
67
|
+
alwaysShowDecimals: true,
|
|
68
|
+
});
|
|
69
|
+
}, [numericValue, currency, intl.locale]);
|
|
70
|
+
|
|
71
|
+
const ref = useRef<HTMLInputElement>(null);
|
|
72
|
+
|
|
73
|
+
useEffect(() => {
|
|
74
|
+
if (autoFocus) {
|
|
75
|
+
ref.current?.focus();
|
|
76
|
+
}
|
|
77
|
+
}, []);
|
|
78
|
+
|
|
79
|
+
const placeholder = getPlaceholder(currency, intl.locale);
|
|
80
|
+
const groupSeparator = getGroupSeparator(currency, intl.locale);
|
|
81
|
+
const decimalSeparator = getDecimalSeparator(currency, intl.locale);
|
|
82
|
+
const maxDecimalCount = getDecimalCount(currency, intl.locale);
|
|
83
|
+
|
|
84
|
+
const decimalPart = getDecimalPart(value, decimalSeparator);
|
|
85
|
+
const decimalMode = decimalSeparator && value.includes(decimalSeparator);
|
|
86
|
+
|
|
87
|
+
useEffect(() => {
|
|
88
|
+
if (!focus) {
|
|
89
|
+
setValue(
|
|
90
|
+
amount
|
|
91
|
+
? getFormattedString({
|
|
92
|
+
value: amount,
|
|
93
|
+
currency,
|
|
94
|
+
locale: intl.locale,
|
|
95
|
+
})
|
|
96
|
+
: '',
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
100
|
+
}, [amount]);
|
|
101
|
+
|
|
102
|
+
useEffect(() => {
|
|
103
|
+
onFocusChange?.(visualFocus);
|
|
104
|
+
}, [visualFocus]);
|
|
105
|
+
|
|
106
|
+
const shouldReformatAfterUserInput = (newValue: string) => {
|
|
107
|
+
// don't reformat if formatting would wipe out user's input
|
|
108
|
+
if (reformatValue(newValue) === '') {
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const endsWithDecimalSeparator = decimalSeparator && newValue.endsWith(decimalSeparator);
|
|
113
|
+
const endsWithGroupSeparator = groupSeparator && newValue.endsWith(groupSeparator);
|
|
114
|
+
|
|
115
|
+
// if the user has entered a seperator to the end, formatting would delete it
|
|
116
|
+
if (endsWithDecimalSeparator || endsWithGroupSeparator) {
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const containsDecimalSeparator = decimalSeparator && newValue.includes(decimalSeparator);
|
|
121
|
+
|
|
122
|
+
if (containsDecimalSeparator) {
|
|
123
|
+
const enteredDecimalsCount = getEnteredDecimalsCount(newValue, decimalSeparator);
|
|
124
|
+
// don't reformat until user has entered all the allowed decimals
|
|
125
|
+
// for example, we don't want 1.1 to be reformatted to 1.10 immediately
|
|
126
|
+
if (enteredDecimalsCount < maxDecimalCount) {
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return true;
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
const reformatValue = (newValue: string) => {
|
|
135
|
+
const unformattedValue = getUnformattedNumber({
|
|
136
|
+
value: newValue,
|
|
137
|
+
currency,
|
|
138
|
+
locale: intl.locale,
|
|
139
|
+
});
|
|
140
|
+
const formattedValue = unformattedValue
|
|
141
|
+
? getFormattedString({
|
|
142
|
+
value: unformattedValue,
|
|
143
|
+
currency,
|
|
144
|
+
locale: intl.locale,
|
|
145
|
+
})
|
|
146
|
+
: '';
|
|
147
|
+
return formattedValue;
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
const handleChange = (newValue: string) => {
|
|
151
|
+
const oldCursorPosition = ref.current?.selectionStart ?? 0;
|
|
152
|
+
|
|
153
|
+
const newFormattedString = shouldReformatAfterUserInput(newValue)
|
|
154
|
+
? reformatValue(newValue)
|
|
155
|
+
: newValue;
|
|
156
|
+
setValue(newFormattedString);
|
|
157
|
+
|
|
158
|
+
const newNumber = getUnformattedNumber({
|
|
159
|
+
value: newFormattedString,
|
|
160
|
+
currency,
|
|
161
|
+
locale: intl.locale,
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
if (newNumber !== numericValue) {
|
|
165
|
+
if (numericValue || newNumber) {
|
|
166
|
+
onChange(newNumber);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const newCursorPosition = oldCursorPosition + (newFormattedString.length - newValue.length);
|
|
171
|
+
requestAnimationFrame(() => {
|
|
172
|
+
ref?.current?.setSelectionRange(newCursorPosition, newCursorPosition);
|
|
173
|
+
});
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
const handlePaste = (e: React.ClipboardEvent<HTMLInputElement>) => {
|
|
177
|
+
e.preventDefault();
|
|
178
|
+
|
|
179
|
+
const clipboardData = e.clipboardData?.getData('text/plain');
|
|
180
|
+
if (!clipboardData) {
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// need to sanitise the pasted value otherwise other validation logic will ignore the input entirely
|
|
185
|
+
const sanitisedValue = reformatValue(clipboardData);
|
|
186
|
+
|
|
187
|
+
handleChange(sanitisedValue);
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
const handleBlur = () => {
|
|
191
|
+
setFocus(false);
|
|
192
|
+
setValue(reformatValue(value));
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
const handleBackspace = (e: KeyboardEvent<HTMLInputElement>) => {
|
|
196
|
+
const input = e.target as HTMLInputElement;
|
|
197
|
+
// using the updated selection range after the backspace key has been processed, instead of the current selection range in state
|
|
198
|
+
const { value: currentValue, selectionStart, selectionEnd } = input;
|
|
199
|
+
|
|
200
|
+
if (selectionStart === selectionEnd && selectionStart && selectionStart > 0) {
|
|
201
|
+
const charBeforeCursor = currentValue[selectionStart - 1];
|
|
202
|
+
|
|
203
|
+
// if the user deletes a thousands separator, remove the digit before it as well
|
|
204
|
+
if (charBeforeCursor === groupSeparator) {
|
|
205
|
+
e.preventDefault();
|
|
206
|
+
const beforeCursor = currentValue.slice(0, selectionStart - 2);
|
|
207
|
+
const afterCursor = currentValue.slice(selectionStart);
|
|
208
|
+
const newValue = `${beforeCursor}${afterCursor}`;
|
|
209
|
+
input.setSelectionRange(beforeCursor.length, beforeCursor.length);
|
|
210
|
+
handleChange(newValue);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
const handleKeyDown = (e: KeyboardEvent<HTMLInputElement>) => {
|
|
216
|
+
setFocus(true);
|
|
217
|
+
if (!isAllowedInputKey(e)) {
|
|
218
|
+
e.preventDefault();
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (e.key === 'Backspace') {
|
|
222
|
+
handleBackspace(e);
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
const isAllowedInput = (e: ChangeEvent<HTMLInputElement>) => {
|
|
227
|
+
const hasMultipleDecimalSeparators =
|
|
228
|
+
decimalSeparator && e.target.value.split(decimalSeparator).length > 2;
|
|
229
|
+
if (hasMultipleDecimalSeparators) {
|
|
230
|
+
return false;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const newNumericValue = getUnformattedNumber({
|
|
234
|
+
value: e.target.value,
|
|
235
|
+
currency,
|
|
236
|
+
locale: intl.locale,
|
|
237
|
+
});
|
|
238
|
+
const maxLength = Number.MAX_SAFE_INTEGER.toString().length;
|
|
239
|
+
if (String(newNumericValue).length > maxLength) {
|
|
240
|
+
return false;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
return true;
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
const addonContent = useMemo((): string | null | undefined => {
|
|
247
|
+
// because we're using a separate "addon" element for the placeholder decimals, there is a possibility that the input itself will become scrollable
|
|
248
|
+
// and the decimals will appear on top of the input. Safest thing to do is to just hide the addon if there is not enough room
|
|
249
|
+
if (isInputPossiblyOverflowing({ ref, value })) {
|
|
250
|
+
return null;
|
|
251
|
+
}
|
|
252
|
+
if (!decimalSeparator || !value) {
|
|
253
|
+
return null;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// if the user has typed a decimal separator, show the full decimal part as a placeholder
|
|
257
|
+
// this returns a string even if there is no content, typing should replace the placeholder immediately without animation
|
|
258
|
+
// otherwise there is an ugly animation when going from 1.23 to 1.2 due to AnimatePresence
|
|
259
|
+
if (focus && decimalMode) {
|
|
260
|
+
// reuse getDecimalPart
|
|
261
|
+
const fullDecimalPart = getDecimalPart(valueWithFullDecimals, decimalSeparator);
|
|
262
|
+
// show only the characters that are not already displayed by the input
|
|
263
|
+
return fullDecimalPart?.slice(decimalPart?.length);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// in unfocused state, always show the full decimal part unless the user has already entered decimals
|
|
267
|
+
if (!focus && !decimalMode) {
|
|
268
|
+
const [_, decimalPlaceholder] = placeholder.split(decimalSeparator);
|
|
269
|
+
return decimalSeparator + decimalPlaceholder;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
return null;
|
|
273
|
+
}, [
|
|
274
|
+
decimalMode,
|
|
275
|
+
decimalPart?.length,
|
|
276
|
+
decimalSeparator,
|
|
277
|
+
focus,
|
|
278
|
+
placeholder,
|
|
279
|
+
value,
|
|
280
|
+
valueWithFullDecimals,
|
|
281
|
+
]);
|
|
282
|
+
|
|
283
|
+
const style = useInputStyle({
|
|
284
|
+
// whenever decimals are shown, we need to account for the full decimal part for the font size calculation
|
|
285
|
+
value: addonContent ? valueWithFullDecimals : value,
|
|
286
|
+
focus: visualFocus,
|
|
287
|
+
inputElement: ref.current,
|
|
288
|
+
loading,
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
return (
|
|
292
|
+
<div className="wds-amount-input-container">
|
|
293
|
+
<div
|
|
294
|
+
className={clsx('wds-amount-input-input-container', 'np-text-display-large')}
|
|
295
|
+
style={style}
|
|
296
|
+
>
|
|
297
|
+
<input
|
|
298
|
+
ref={ref}
|
|
299
|
+
className="wds-amount-input-input"
|
|
300
|
+
id={id}
|
|
301
|
+
autoComplete="off"
|
|
302
|
+
inputMode="decimal"
|
|
303
|
+
value={value}
|
|
304
|
+
type="text"
|
|
305
|
+
placeholder={placeholder}
|
|
306
|
+
aria-describedby={describedById}
|
|
307
|
+
/* without this, the input tries to keep an aspect ratio and doesn't respect CSS width rules */
|
|
308
|
+
size={1}
|
|
309
|
+
onChange={(e) => {
|
|
310
|
+
if (isAllowedInput(e)) {
|
|
311
|
+
handleChange(e.target.value);
|
|
312
|
+
}
|
|
313
|
+
}}
|
|
314
|
+
onBlurCapture={() => handleBlur()}
|
|
315
|
+
onPaste={(e) => handlePaste(e)}
|
|
316
|
+
onFocus={() => {
|
|
317
|
+
setFocus(true);
|
|
318
|
+
}}
|
|
319
|
+
onBlur={() => {
|
|
320
|
+
setTimeout(() => setVisualFocus(false), 30);
|
|
321
|
+
}}
|
|
322
|
+
onKeyDown={(e) => handleKeyDown(e)}
|
|
323
|
+
/>
|
|
324
|
+
<AnimatePresence initial={false}>
|
|
325
|
+
{addonContent !== null && (
|
|
326
|
+
<AnimatedNumber
|
|
327
|
+
className={clsx(
|
|
328
|
+
'wds-amount-input-placeholder',
|
|
329
|
+
visualFocus && 'wds-amount-input-placeholder-focus',
|
|
330
|
+
)}
|
|
331
|
+
onClick={() => ref.current?.focus()}
|
|
332
|
+
>
|
|
333
|
+
{addonContent}
|
|
334
|
+
</AnimatedNumber>
|
|
335
|
+
)}
|
|
336
|
+
</AnimatePresence>
|
|
337
|
+
</div>
|
|
338
|
+
</div>
|
|
339
|
+
);
|
|
340
|
+
};
|
|
341
|
+
|
|
342
|
+
const getPlaceholder = (currency: string, locale: string) => {
|
|
343
|
+
return formatAmount(0, currency, locale, { alwaysShowDecimals: true });
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
const getDecimalPart = (value: string, decimalSeparator: string | null) => {
|
|
347
|
+
if (!value || !decimalSeparator) {
|
|
348
|
+
return undefined;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
const [_, decimalPart] = value.split(decimalSeparator);
|
|
352
|
+
return decimalPart ?? undefined;
|
|
353
|
+
};
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getDecimalSeparator,
|
|
3
|
+
getFormattedString,
|
|
4
|
+
getGroupSeparator,
|
|
5
|
+
getUnformattedNumber,
|
|
6
|
+
} from './utils';
|
|
7
|
+
|
|
8
|
+
describe('utils', () => {
|
|
9
|
+
describe('getDecimalSeparator', () => {
|
|
10
|
+
it('gets decimal separator for Spanish', () => {
|
|
11
|
+
expect(getDecimalSeparator('EUR', 'es')).toBe(',');
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it('gets decimal separator for English', () => {
|
|
15
|
+
expect(getDecimalSeparator('EUR', 'en')).toBe('.');
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('gets decimal separator for Spanish in HUF', () => {
|
|
19
|
+
expect(getDecimalSeparator('HUF', 'es')).toBe('');
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('gets decimal separator for English in HUF', () => {
|
|
23
|
+
expect(getDecimalSeparator('HUF', 'en')).toBe('');
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
describe('getGroupSeparator', () => {
|
|
28
|
+
it('gets group separator for Spanish', () => {
|
|
29
|
+
expect(getGroupSeparator('EUR', 'es')).toBe('.');
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('gets group separator for English', () => {
|
|
33
|
+
expect(getGroupSeparator('EUR', 'en')).toBe(',');
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('gets group separator for French', () => {
|
|
37
|
+
expect(getGroupSeparator('EUR', 'fr')).toBe(' ');
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
describe('getUnformattedNumber', () => {
|
|
42
|
+
it('can turn a Spanish string into a number', () => {
|
|
43
|
+
expect(
|
|
44
|
+
getUnformattedNumber({
|
|
45
|
+
value: '123.456,00',
|
|
46
|
+
currency: 'EUR',
|
|
47
|
+
locale: 'es',
|
|
48
|
+
}),
|
|
49
|
+
).toBe(123456);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('can turn a French string into a number', () => {
|
|
53
|
+
expect(
|
|
54
|
+
getUnformattedNumber({
|
|
55
|
+
value: '1 234 567,45',
|
|
56
|
+
currency: 'EUR',
|
|
57
|
+
locale: 'fr',
|
|
58
|
+
}),
|
|
59
|
+
).toBe(1234567.45);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('can turn a English string into a number', () => {
|
|
63
|
+
expect(
|
|
64
|
+
getUnformattedNumber({
|
|
65
|
+
value: '123,456.78',
|
|
66
|
+
currency: 'EUR',
|
|
67
|
+
locale: 'en',
|
|
68
|
+
}),
|
|
69
|
+
).toBe(123456.78);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('can turn a Magyar string into a number', () => {
|
|
73
|
+
expect(
|
|
74
|
+
getUnformattedNumber({
|
|
75
|
+
value: '11 000 000',
|
|
76
|
+
currency: 'HUF',
|
|
77
|
+
locale: 'hu',
|
|
78
|
+
}),
|
|
79
|
+
).toBe(11000000);
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
describe('getFormattedString', () => {
|
|
84
|
+
it('can turn a number into a Spanish string', () => {
|
|
85
|
+
expect(
|
|
86
|
+
getFormattedString({
|
|
87
|
+
value: 123456.45,
|
|
88
|
+
currency: 'EUR',
|
|
89
|
+
locale: 'es',
|
|
90
|
+
}),
|
|
91
|
+
).toBe('123.456,45');
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it('can turn a number into a French string', () => {
|
|
95
|
+
expect(
|
|
96
|
+
getFormattedString({
|
|
97
|
+
value: 1234567.45,
|
|
98
|
+
currency: 'EUR',
|
|
99
|
+
locale: 'fr',
|
|
100
|
+
}),
|
|
101
|
+
).toBe('1 234 567,45');
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('can turn a number into a English string', () => {
|
|
105
|
+
expect(
|
|
106
|
+
getFormattedString({
|
|
107
|
+
value: 123456.78,
|
|
108
|
+
currency: 'EUR',
|
|
109
|
+
locale: 'en',
|
|
110
|
+
}),
|
|
111
|
+
).toBe('123,456.78');
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
});
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { formatAmount } from '@transferwise/formatting';
|
|
2
|
+
import type { KeyboardEvent } from 'react';
|
|
3
|
+
|
|
4
|
+
export const getDecimalSeparator = (currency: string, locale: string): string | null => {
|
|
5
|
+
return formatAmount(1.1, currency, locale).replace(/\p{Number}/gu, '');
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export const getGroupSeparator = (currency: string, locale: string): string | null => {
|
|
9
|
+
return formatAmount(10000000, currency, locale).replace(/\p{Number}/gu, '')[0];
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export const getDecimalCount = (currency: string, locale: string): number => {
|
|
13
|
+
const decimalSeparator = getDecimalSeparator(currency, locale);
|
|
14
|
+
if (!decimalSeparator) {
|
|
15
|
+
return 0;
|
|
16
|
+
}
|
|
17
|
+
const parts = formatAmount(1.1, currency, locale).split(decimalSeparator);
|
|
18
|
+
return parts.length === 2 ? parts[1].length : 0;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export const getEnteredDecimalsCount = (value: string, decimalSeparator: string) => {
|
|
22
|
+
return value.split(decimalSeparator)[1]?.length ?? 0;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export const getUnformattedNumber = ({
|
|
26
|
+
value: formattedValue,
|
|
27
|
+
currency,
|
|
28
|
+
locale,
|
|
29
|
+
}: {
|
|
30
|
+
value: string;
|
|
31
|
+
currency: string;
|
|
32
|
+
locale: string;
|
|
33
|
+
}): number | null => {
|
|
34
|
+
const groupSeparator = getGroupSeparator(currency, locale);
|
|
35
|
+
const decimalSeparator = getDecimalSeparator(currency, locale);
|
|
36
|
+
if (!formattedValue) {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// parseFloat can't handle thousands separators
|
|
41
|
+
const withoutGroupSeparator = groupSeparator
|
|
42
|
+
? formattedValue.replace(/ /gu, '').replace(new RegExp(`\\${groupSeparator}`, 'g'), '')
|
|
43
|
+
: formattedValue;
|
|
44
|
+
|
|
45
|
+
// parseFloat can only handle . as decimal separator
|
|
46
|
+
const withNormalisedDecimalSeparator = decimalSeparator
|
|
47
|
+
? withoutGroupSeparator.replace(decimalSeparator, '.')
|
|
48
|
+
: withoutGroupSeparator;
|
|
49
|
+
|
|
50
|
+
const parsedValue = Number.parseFloat(withNormalisedDecimalSeparator);
|
|
51
|
+
return parsedValue;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export const getFormattedString = ({
|
|
55
|
+
value: unformattedValue,
|
|
56
|
+
currency,
|
|
57
|
+
locale,
|
|
58
|
+
alwaysShowDecimals = false,
|
|
59
|
+
}: {
|
|
60
|
+
value: number;
|
|
61
|
+
currency: string;
|
|
62
|
+
locale: string;
|
|
63
|
+
alwaysShowDecimals?: boolean;
|
|
64
|
+
}): string => {
|
|
65
|
+
const decimalSeparator = getDecimalSeparator(currency, locale);
|
|
66
|
+
// formatAmount rounds extra decimals, so 1.999 will become 2. Instead we will manually strip extra decimals so that it becomes 1.99 after formatting
|
|
67
|
+
const decimalCount = getDecimalCount(currency, locale);
|
|
68
|
+
const unformattedString = unformattedValue.toString();
|
|
69
|
+
|
|
70
|
+
const [integerPart, decimalPart] = decimalSeparator
|
|
71
|
+
? unformattedString.split(decimalSeparator)
|
|
72
|
+
: [unformattedString, undefined];
|
|
73
|
+
|
|
74
|
+
const formattedDecimalPart = decimalPart ? decimalPart.slice(0, decimalCount) : '';
|
|
75
|
+
|
|
76
|
+
const sanitisedUnformattedValue = Number.parseFloat(`${integerPart}.${formattedDecimalPart}`);
|
|
77
|
+
|
|
78
|
+
return formatAmount(sanitisedUnformattedValue, currency, locale, {
|
|
79
|
+
alwaysShowDecimals,
|
|
80
|
+
});
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
export const isInputPossiblyOverflowing = ({
|
|
84
|
+
ref,
|
|
85
|
+
value,
|
|
86
|
+
}: {
|
|
87
|
+
ref: React.RefObject<HTMLInputElement>;
|
|
88
|
+
value: string;
|
|
89
|
+
}) => {
|
|
90
|
+
const textLength = value.length;
|
|
91
|
+
const inputWidth = ref.current?.clientWidth;
|
|
92
|
+
if (!inputWidth || !textLength) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const maxCharactersWithoutOverflow = Math.floor(inputWidth / 19);
|
|
97
|
+
return textLength > maxCharactersWithoutOverflow;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const allowedInputKeys = new Set([
|
|
101
|
+
'Backspace',
|
|
102
|
+
'Delete',
|
|
103
|
+
',',
|
|
104
|
+
'.',
|
|
105
|
+
'ArrowLeft',
|
|
106
|
+
'ArrowRight',
|
|
107
|
+
'Enter',
|
|
108
|
+
'Tab',
|
|
109
|
+
]);
|
|
110
|
+
|
|
111
|
+
export const isAllowedInputKey = (e: KeyboardEvent<HTMLInputElement>) => {
|
|
112
|
+
const { metaKey, key, ctrlKey } = e;
|
|
113
|
+
const isNumberKey = !Number.isNaN(Number.parseInt(key, 10));
|
|
114
|
+
|
|
115
|
+
return isNumberKey || metaKey || ctrlKey || allowedInputKeys.has(key);
|
|
116
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { motion, useReducedMotion } from 'framer-motion';
|
|
2
|
+
import type { ReactNode } from 'react';
|
|
3
|
+
|
|
4
|
+
interface Props {
|
|
5
|
+
children: ReactNode;
|
|
6
|
+
onClick?: () => void;
|
|
7
|
+
className?: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const AnimatedNumber = ({ children, onClick, className }: Props) => {
|
|
11
|
+
const reducedMotion = useReducedMotion();
|
|
12
|
+
|
|
13
|
+
return (
|
|
14
|
+
<motion.span
|
|
15
|
+
className={className}
|
|
16
|
+
aria-hidden
|
|
17
|
+
initial={{ zoom: 0.01 }}
|
|
18
|
+
animate={{ zoom: 1 }}
|
|
19
|
+
exit={{ zoom: 0.01 }}
|
|
20
|
+
transition={{
|
|
21
|
+
duration: reducedMotion ? 0 : 0.3,
|
|
22
|
+
type: 'tween',
|
|
23
|
+
ease: [0.3, 0, 0.1, 1],
|
|
24
|
+
}}
|
|
25
|
+
onClick={() => onClick?.()}
|
|
26
|
+
>
|
|
27
|
+
<motion.span
|
|
28
|
+
initial={{ opacity: 0 }}
|
|
29
|
+
animate={{ opacity: [0, 0, 1] }}
|
|
30
|
+
exit={{ opacity: [1, 0, 0] }}
|
|
31
|
+
transition={{
|
|
32
|
+
duration: reducedMotion ? 0 : 0.3,
|
|
33
|
+
times: [0, 0.5, 1],
|
|
34
|
+
}}
|
|
35
|
+
>
|
|
36
|
+
{children}
|
|
37
|
+
</motion.span>
|
|
38
|
+
</motion.span>
|
|
39
|
+
);
|
|
40
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { ChevronLeft } from '@transferwise/icons';
|
|
2
|
+
import { clsx } from 'clsx';
|
|
3
|
+
import { motion } from 'framer-motion';
|
|
4
|
+
|
|
5
|
+
interface Props {
|
|
6
|
+
shouldShow: boolean;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export const Chevron = ({ shouldShow = true }: Props) => {
|
|
10
|
+
return (
|
|
11
|
+
<div
|
|
12
|
+
className={clsx(
|
|
13
|
+
'd-flex align-items-center',
|
|
14
|
+
'wds-chevron-container',
|
|
15
|
+
!shouldShow && 'wds-chevron-hidden',
|
|
16
|
+
)}
|
|
17
|
+
>
|
|
18
|
+
<motion.div
|
|
19
|
+
animate={{
|
|
20
|
+
x: [12, 0, 0, -12],
|
|
21
|
+
opacity: [0, 1, 1, 0],
|
|
22
|
+
}}
|
|
23
|
+
transition={{
|
|
24
|
+
duration: 3,
|
|
25
|
+
ease: [[0.3, 0, 0.1, 1], 'linear', [0.3, 0, 0.1, 1]],
|
|
26
|
+
times: [0, 0.1, 0.9, 1],
|
|
27
|
+
repeat: Infinity,
|
|
28
|
+
repeatType: 'loop',
|
|
29
|
+
}}
|
|
30
|
+
>
|
|
31
|
+
<ChevronLeft />
|
|
32
|
+
</motion.div>
|
|
33
|
+
</div>
|
|
34
|
+
);
|
|
35
|
+
};
|