@transferwise/components 46.112.0 → 46.113.0
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/listItem/useListItemControl.js +1 -1
- package/build/listItem/useListItemControl.js.map +1 -1
- package/build/listItem/useListItemControl.mjs +2 -2
- package/build/listItem/useListItemControl.mjs.map +1 -1
- package/build/listItem/useListItemMedia.js +1 -1
- package/build/listItem/useListItemMedia.js.map +1 -1
- package/build/listItem/useListItemMedia.mjs +2 -2
- package/build/listItem/useListItemMedia.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/build/types/withDisplayFormat/WithDisplayFormat.d.ts.map +1 -1
- package/build/withDisplayFormat/WithDisplayFormat.js +0 -1
- package/build/withDisplayFormat/WithDisplayFormat.js.map +1 -1
- package/build/withDisplayFormat/WithDisplayFormat.mjs +0 -1
- package/build/withDisplayFormat/WithDisplayFormat.mjs.map +1 -1
- package/package.json +11 -4
- 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/listItem/useListItemControl.tsx +2 -2
- package/src/listItem/useListItemMedia.tsx +2 -2
- 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
- package/src/withDisplayFormat/WithDisplayFormat.spec.js +28 -1
- package/src/withDisplayFormat/WithDisplayFormat.tsx +0 -1
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import Body from '../body';
|
|
2
|
+
import { Label } from '../label/Label';
|
|
3
|
+
import { clsx } from 'clsx';
|
|
4
|
+
import { AnimatePresence, motion } from 'framer-motion';
|
|
5
|
+
import { useId, type ReactNode } from 'react';
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
type Props as CurrencySelectorProps,
|
|
9
|
+
CurrencySelector,
|
|
10
|
+
} from './currencySelector/CurrencySelector';
|
|
11
|
+
import { CommonProps } from '../common';
|
|
12
|
+
import { AmountInput } from './amountInput/AmountInput';
|
|
13
|
+
import { Chevron } from './chevron/Chevron';
|
|
14
|
+
import { InlinePrompt, type InlinePromptProps } from '../prompt/InlinePrompt';
|
|
15
|
+
|
|
16
|
+
type AmountType = number | null;
|
|
17
|
+
export type CurrencyType = string;
|
|
18
|
+
|
|
19
|
+
type DefaultCurrencySelectorInstanceType = Pick<
|
|
20
|
+
CurrencySelectorProps,
|
|
21
|
+
'addons' | 'options' | 'onChange' | 'onOpen' | 'onSearchChange'
|
|
22
|
+
>;
|
|
23
|
+
type CustomCurrencySelectorInstanceType = {
|
|
24
|
+
customRender?: (props: { id: string; labelId: string }) => ReactNode;
|
|
25
|
+
};
|
|
26
|
+
type CurrencySelectorType = DefaultCurrencySelectorInstanceType &
|
|
27
|
+
CustomCurrencySelectorInstanceType;
|
|
28
|
+
|
|
29
|
+
export type Props = {
|
|
30
|
+
label?: ReactNode;
|
|
31
|
+
currencySelector?: CurrencySelectorType;
|
|
32
|
+
amount?: AmountType;
|
|
33
|
+
/**
|
|
34
|
+
* The currency code, e.g. `USD`, `EUR`, `GBP`, etc. Governs the flag rendered in the currency selector.
|
|
35
|
+
*/
|
|
36
|
+
currency: CurrencyType;
|
|
37
|
+
inlinePrompt?: {
|
|
38
|
+
sentiment?: InlinePromptProps['sentiment'];
|
|
39
|
+
message: InlinePromptProps['children'];
|
|
40
|
+
media?: InlinePromptProps['media'];
|
|
41
|
+
};
|
|
42
|
+
showChevron?: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* If set, it auto-focuses the amount input upon component mount.<br />
|
|
45
|
+
* ⚠️ **Use with caution**, as it may impact user experience and
|
|
46
|
+
* fail [WCAG 2.4.3 requirements](https://www.w3.org/WAI/WCAG21/Understanding/focus-order.html)
|
|
47
|
+
*/
|
|
48
|
+
autoFocus?: boolean;
|
|
49
|
+
/**
|
|
50
|
+
* Dims the input to indicate a loading state. Does not disable it.
|
|
51
|
+
*/
|
|
52
|
+
loading?: boolean;
|
|
53
|
+
onAmountChange: (amount: AmountType) => void;
|
|
54
|
+
onFocusChange?: (focused: boolean) => void;
|
|
55
|
+
} & CommonProps;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* This component has been kindly contributed by our friends at the `Send` team 🎉.
|
|
59
|
+
*
|
|
60
|
+
* Some patterns and implementation details used in this component may differ
|
|
61
|
+
* from what's commonly used in the Design System. The overall build and QA was managed
|
|
62
|
+
* by the contributing team directly, and such did not follow a typical DS lifecycle.
|
|
63
|
+
*
|
|
64
|
+
* While we house this component and will help facilitate its future iterations,
|
|
65
|
+
* direct contributions by consuming product teams are highly encouraged.
|
|
66
|
+
*
|
|
67
|
+
* > ⚠️ **Prerequisite:** <br />The component depends on the
|
|
68
|
+
* [framer-motion](https://www.npmjs.com/package/framer-motion) package, which has been
|
|
69
|
+
* deliberately excluded from the Design System bundle and kept as an opt-in only.
|
|
70
|
+
* **Make sure to add it to your project dependencies.**
|
|
71
|
+
*/
|
|
72
|
+
export default function ExpressiveMoneyInput({
|
|
73
|
+
label,
|
|
74
|
+
currency,
|
|
75
|
+
currencySelector = { options: [] } as DefaultCurrencySelectorInstanceType,
|
|
76
|
+
amount,
|
|
77
|
+
onAmountChange,
|
|
78
|
+
className,
|
|
79
|
+
inlinePrompt,
|
|
80
|
+
showChevron,
|
|
81
|
+
autoFocus,
|
|
82
|
+
loading,
|
|
83
|
+
onFocusChange,
|
|
84
|
+
}: Props) {
|
|
85
|
+
const inputId = useId();
|
|
86
|
+
const labelId = useId();
|
|
87
|
+
const customAlertId = useId();
|
|
88
|
+
const currencyId = useId();
|
|
89
|
+
|
|
90
|
+
return (
|
|
91
|
+
<div className={clsx('wds-expressive-money-input', className)}>
|
|
92
|
+
<Label id={labelId} htmlFor={inputId} className={clsx('m-b-1', 'font-weight-normal')}>
|
|
93
|
+
{label}
|
|
94
|
+
</Label>
|
|
95
|
+
<div
|
|
96
|
+
className={clsx('d-flex')}
|
|
97
|
+
role="group"
|
|
98
|
+
aria-labelledby={labelId}
|
|
99
|
+
{...(inlinePrompt ? { 'aria-describedby': customAlertId } : {})}
|
|
100
|
+
>
|
|
101
|
+
<div className="wds-expressive-money-input-currency-selector">
|
|
102
|
+
{currencySelector.customRender?.({ id: currencyId, labelId }) ?? (
|
|
103
|
+
<CurrencySelector
|
|
104
|
+
id={currencyId}
|
|
105
|
+
labelId={labelId}
|
|
106
|
+
currency={currency}
|
|
107
|
+
{...currencySelector}
|
|
108
|
+
/>
|
|
109
|
+
)}
|
|
110
|
+
</div>
|
|
111
|
+
<AmountInput
|
|
112
|
+
id={inputId}
|
|
113
|
+
describedById={currencyId}
|
|
114
|
+
amount={amount}
|
|
115
|
+
currency={currency}
|
|
116
|
+
// eslint-disable-next-line jsx-a11y/no-autofocus
|
|
117
|
+
autoFocus={autoFocus}
|
|
118
|
+
loading={loading}
|
|
119
|
+
onChange={onAmountChange}
|
|
120
|
+
onFocusChange={onFocusChange}
|
|
121
|
+
/>
|
|
122
|
+
<div className={clsx('d-flex align-items-center', 'wds-expressive-money-input-chevron')}>
|
|
123
|
+
<Chevron shouldShow={Boolean(showChevron)} />
|
|
124
|
+
</div>
|
|
125
|
+
</div>
|
|
126
|
+
<AnimatePresence initial={false}>
|
|
127
|
+
{inlinePrompt && (
|
|
128
|
+
<div className={clsx('d-flex justify-content-end', inlinePrompt && 'm-t-1')}>
|
|
129
|
+
<motion.div
|
|
130
|
+
key={customAlertId}
|
|
131
|
+
initial={{ opacity: 0, height: 0 }}
|
|
132
|
+
animate={{
|
|
133
|
+
opacity: 1,
|
|
134
|
+
height: 'auto',
|
|
135
|
+
transition: { delay: 0.75, duration: 0.3 },
|
|
136
|
+
}}
|
|
137
|
+
exit={{ opacity: 0, height: 0 }}
|
|
138
|
+
>
|
|
139
|
+
{inlinePrompt.sentiment && Object.keys(inlinePrompt.sentiment).length > 0 ? (
|
|
140
|
+
<InlinePrompt
|
|
141
|
+
id={customAlertId}
|
|
142
|
+
media={inlinePrompt.media}
|
|
143
|
+
sentiment={inlinePrompt.sentiment}
|
|
144
|
+
>
|
|
145
|
+
{inlinePrompt.message}
|
|
146
|
+
</InlinePrompt>
|
|
147
|
+
) : (
|
|
148
|
+
<Body>{inlinePrompt.message}</Body>
|
|
149
|
+
)}
|
|
150
|
+
</motion.div>
|
|
151
|
+
</div>
|
|
152
|
+
)}
|
|
153
|
+
</AnimatePresence>
|
|
154
|
+
</div>
|
|
155
|
+
);
|
|
156
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
.wds-amount-input-container {
|
|
2
|
+
width: 100%;
|
|
3
|
+
}
|
|
4
|
+
.wds-amount-input-input-container {
|
|
5
|
+
display: flex;
|
|
6
|
+
justify-content: right;
|
|
7
|
+
width: 100%;
|
|
8
|
+
transition: font-size 0.4s cubic-bezier(0.3, 0, 0.1, 1), height 0.4s cubic-bezier(0.3, 0, 0.1, 1), margin-top 0.4s cubic-bezier(0.3, 0, 0.1, 1), color 0.4s ease;
|
|
9
|
+
color: var(--color-interactive-primary);
|
|
10
|
+
overflow: hidden;
|
|
11
|
+
margin-bottom: 0 !important;
|
|
12
|
+
}
|
|
13
|
+
@media (prefers-reduced-motion: reduce) {
|
|
14
|
+
.wds-amount-input-input-container {
|
|
15
|
+
transition: none;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
.wds-amount-input-input {
|
|
19
|
+
border: none;
|
|
20
|
+
outline: none;
|
|
21
|
+
flex-grow: 1;
|
|
22
|
+
text-align: right;
|
|
23
|
+
background-color: transparent;
|
|
24
|
+
}
|
|
25
|
+
.wds-amount-input-input:focus-visible {
|
|
26
|
+
outline: none;
|
|
27
|
+
}
|
|
28
|
+
.wds-amount-input-placeholder {
|
|
29
|
+
flex-grow: 0;
|
|
30
|
+
display: flex;
|
|
31
|
+
align-items: center;
|
|
32
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
.wds-amount-input {
|
|
2
|
+
&-container {
|
|
3
|
+
width: 100%;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
&-input-container {
|
|
7
|
+
display: flex;
|
|
8
|
+
justify-content: right;
|
|
9
|
+
width: 100%;
|
|
10
|
+
transition:
|
|
11
|
+
font-size 0.4s cubic-bezier(0.3, 0, 0.1, 1),
|
|
12
|
+
height 0.4s cubic-bezier(0.3, 0, 0.1, 1),
|
|
13
|
+
margin-top 0.4s cubic-bezier(0.3, 0, 0.1, 1),
|
|
14
|
+
color 0.4s ease;
|
|
15
|
+
color: var(--color-interactive-primary);
|
|
16
|
+
overflow: hidden;
|
|
17
|
+
margin-bottom: 0 !important;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
@media (prefers-reduced-motion: reduce) {
|
|
21
|
+
&-input-container {
|
|
22
|
+
transition: none;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
&-input {
|
|
27
|
+
border: none;
|
|
28
|
+
outline: none;
|
|
29
|
+
flex-grow: 1;
|
|
30
|
+
text-align: right;
|
|
31
|
+
background-color: transparent;
|
|
32
|
+
|
|
33
|
+
&:focus-visible {
|
|
34
|
+
outline: none;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
&-placeholder {
|
|
39
|
+
flex-grow: 0;
|
|
40
|
+
display: flex;
|
|
41
|
+
align-items: center;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -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
|
+
});
|