@transferwise/components 0.0.0-experimental-9442a3d → 0.0.0-experimental-fe42ce4

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.
Files changed (120) hide show
  1. package/build/dateInput/DateInput.js +2 -2
  2. package/build/dateInput/DateInput.js.map +1 -1
  3. package/build/dateInput/DateInput.mjs +2 -2
  4. package/build/dateInput/DateInput.mjs.map +1 -1
  5. package/build/i18n/en.json +2 -0
  6. package/build/i18n/en.json.js +2 -0
  7. package/build/i18n/en.json.js.map +1 -1
  8. package/build/i18n/en.json.mjs +2 -0
  9. package/build/i18n/en.json.mjs.map +1 -1
  10. package/build/index.js +2 -0
  11. package/build/index.js.map +1 -1
  12. package/build/index.mjs +1 -0
  13. package/build/index.mjs.map +1 -1
  14. package/build/main.css +66 -0
  15. package/build/moneyInputField/AmountInput.js +284 -0
  16. package/build/moneyInputField/AmountInput.js.map +1 -0
  17. package/build/moneyInputField/AmountInput.mjs +282 -0
  18. package/build/moneyInputField/AmountInput.mjs.map +1 -0
  19. package/build/moneyInputField/AnimatedNumber.js +50 -0
  20. package/build/moneyInputField/AnimatedNumber.js.map +1 -0
  21. package/build/moneyInputField/AnimatedNumber.mjs +48 -0
  22. package/build/moneyInputField/AnimatedNumber.mjs.map +1 -0
  23. package/build/moneyInputField/Chevron.js +33 -0
  24. package/build/moneyInputField/Chevron.js.map +1 -0
  25. package/build/moneyInputField/Chevron.mjs +31 -0
  26. package/build/moneyInputField/Chevron.mjs.map +1 -0
  27. package/build/moneyInputField/CurrencySelector.js +160 -0
  28. package/build/moneyInputField/CurrencySelector.js.map +1 -0
  29. package/build/moneyInputField/CurrencySelector.mjs +157 -0
  30. package/build/moneyInputField/CurrencySelector.mjs.map +1 -0
  31. package/build/moneyInputField/MoneyInputField.js +112 -0
  32. package/build/moneyInputField/MoneyInputField.js.map +1 -0
  33. package/build/moneyInputField/MoneyInputField.messages.js +17 -0
  34. package/build/moneyInputField/MoneyInputField.messages.js.map +1 -0
  35. package/build/moneyInputField/MoneyInputField.messages.mjs +13 -0
  36. package/build/moneyInputField/MoneyInputField.messages.mjs.map +1 -0
  37. package/build/moneyInputField/MoneyInputField.mjs +108 -0
  38. package/build/moneyInputField/MoneyInputField.mjs.map +1 -0
  39. package/build/moneyInputField/useFocus.js +37 -0
  40. package/build/moneyInputField/useFocus.js.map +1 -0
  41. package/build/moneyInputField/useFocus.mjs +35 -0
  42. package/build/moneyInputField/useFocus.mjs.map +1 -0
  43. package/build/moneyInputField/useInputStyle.js +71 -0
  44. package/build/moneyInputField/useInputStyle.js.map +1 -0
  45. package/build/moneyInputField/useInputStyle.mjs +69 -0
  46. package/build/moneyInputField/useInputStyle.mjs.map +1 -0
  47. package/build/moneyInputField/utils.js +87 -0
  48. package/build/moneyInputField/utils.js.map +1 -0
  49. package/build/moneyInputField/utils.mjs +78 -0
  50. package/build/moneyInputField/utils.mjs.map +1 -0
  51. package/build/phoneNumberInput/PhoneNumberInput.js +1 -1
  52. package/build/phoneNumberInput/PhoneNumberInput.js.map +1 -1
  53. package/build/phoneNumberInput/PhoneNumberInput.mjs +1 -1
  54. package/build/phoneNumberInput/PhoneNumberInput.mjs.map +1 -1
  55. package/build/styles/inputs/Input.css +5 -0
  56. package/build/styles/inputs/TextArea.css +5 -0
  57. package/build/styles/main.css +66 -0
  58. package/build/styles/moneyInputField/AmountInput.css +32 -0
  59. package/build/styles/moneyInputField/Chevron.css +12 -0
  60. package/build/styles/moneyInputField/CurrencySelector.css +6 -0
  61. package/build/styles/moneyInputField/MoneyInputField.css +61 -0
  62. package/build/types/index.d.ts +2 -0
  63. package/build/types/index.d.ts.map +1 -1
  64. package/build/types/moneyInputField/AmountInput.d.ts +13 -0
  65. package/build/types/moneyInputField/AmountInput.d.ts.map +1 -0
  66. package/build/types/moneyInputField/AnimatedNumber.d.ts +9 -0
  67. package/build/types/moneyInputField/AnimatedNumber.d.ts.map +1 -0
  68. package/build/types/moneyInputField/Chevron.d.ts +6 -0
  69. package/build/types/moneyInputField/Chevron.d.ts.map +1 -0
  70. package/build/types/moneyInputField/CurrencySelector.d.ts +30 -0
  71. package/build/types/moneyInputField/CurrencySelector.d.ts.map +1 -0
  72. package/build/types/moneyInputField/MoneyInputField.d.ts +22 -0
  73. package/build/types/moneyInputField/MoneyInputField.d.ts.map +1 -0
  74. package/build/types/moneyInputField/MoneyInputField.messages.d.ts +12 -0
  75. package/build/types/moneyInputField/MoneyInputField.messages.d.ts.map +1 -0
  76. package/build/types/moneyInputField/index.d.ts +3 -0
  77. package/build/types/moneyInputField/index.d.ts.map +1 -0
  78. package/build/types/moneyInputField/useFocus.d.ts +7 -0
  79. package/build/types/moneyInputField/useFocus.d.ts.map +1 -0
  80. package/build/types/moneyInputField/useInputStyle.d.ts +10 -0
  81. package/build/types/moneyInputField/useInputStyle.d.ts.map +1 -0
  82. package/build/types/moneyInputField/useSelectionRange.d.ts +10 -0
  83. package/build/types/moneyInputField/useSelectionRange.d.ts.map +1 -0
  84. package/build/types/moneyInputField/utils.d.ts +22 -0
  85. package/build/types/moneyInputField/utils.d.ts.map +1 -0
  86. package/build/types/test-utils/index.d.ts +4 -0
  87. package/build/types/test-utils/index.d.ts.map +1 -1
  88. package/package.json +3 -2
  89. package/src/DisabledComponents.story.tsx +156 -0
  90. package/src/dateInput/DateInput.tsx +2 -2
  91. package/src/i18n/en.json +2 -0
  92. package/src/index.ts +2 -0
  93. package/src/inputs/Input.css +5 -0
  94. package/src/inputs/TextArea.css +5 -0
  95. package/src/inputs/_common.less +5 -0
  96. package/src/main.css +66 -0
  97. package/src/main.less +1 -0
  98. package/src/moneyInputField/AmountInput.css +32 -0
  99. package/src/moneyInputField/AmountInput.less +43 -0
  100. package/src/moneyInputField/AmountInput.tsx +355 -0
  101. package/src/moneyInputField/AnimatedNumber.tsx +40 -0
  102. package/src/moneyInputField/Chevron.css +12 -0
  103. package/src/moneyInputField/Chevron.less +13 -0
  104. package/src/moneyInputField/Chevron.tsx +35 -0
  105. package/src/moneyInputField/CurrencySelector.css +6 -0
  106. package/src/moneyInputField/CurrencySelector.less +7 -0
  107. package/src/moneyInputField/CurrencySelector.tsx +219 -0
  108. package/src/moneyInputField/MoneyInputField.css +61 -0
  109. package/src/moneyInputField/MoneyInputField.less +16 -0
  110. package/src/moneyInputField/MoneyInputField.messages.ts +13 -0
  111. package/src/moneyInputField/MoneyInputField.story.tsx +193 -0
  112. package/src/moneyInputField/MoneyInputField.tsx +118 -0
  113. package/src/moneyInputField/index.ts +2 -0
  114. package/src/moneyInputField/useFocus.ts +35 -0
  115. package/src/moneyInputField/useInputStyle.ts +85 -0
  116. package/src/moneyInputField/useSelectionRange.ts +23 -0
  117. package/src/moneyInputField/utils.spec.ts +114 -0
  118. package/src/moneyInputField/utils.ts +116 -0
  119. package/src/phoneNumberInput/PhoneNumberInput.tsx +1 -1
  120. package/src/ssr.spec.tsx +1 -0
@@ -0,0 +1,61 @@
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
+ }
33
+ .wds-currency-selector:disabled {
34
+ opacity: 1 !important;
35
+ cursor: auto !important;
36
+ cursor: initial !important;
37
+ mix-blend-mode: initial !important;
38
+ }
39
+ .wds-chevron-container {
40
+ width: 32px;
41
+ width: var(--size-32);
42
+ overflow: hidden;
43
+ color: var(--color-interactive-primary);
44
+ margin-left: 8px;
45
+ margin-left: var(--size-8);
46
+ transition: width 0.3s ease;
47
+ }
48
+ .wds-chevron-hidden {
49
+ width: 0;
50
+ }
51
+ .wds-money-input-field {
52
+ display: flex;
53
+ }
54
+ .wds-money-input-field-currency-selector {
55
+ flex-shrink: 0;
56
+ margin-right: 24px;
57
+ margin-right: var(--size-24);
58
+ }
59
+ .wds-money-input-field-chevron {
60
+ transform: translateY(-5%);
61
+ }
@@ -0,0 +1,16 @@
1
+ @import "./AmountInput.less";
2
+ @import "./CurrencySelector.less";
3
+ @import "./Chevron.less";
4
+
5
+ .wds-money-input-field {
6
+ display: flex;
7
+
8
+ &-currency-selector {
9
+ flex-shrink: 0;
10
+ margin-right: var(--size-24);
11
+ }
12
+
13
+ &-chevron {
14
+ transform: translateY(-5%);
15
+ }
16
+ }
@@ -0,0 +1,13 @@
1
+ import { defineMessages } from 'react-intl';
2
+
3
+ export default defineMessages({
4
+ currencySelectorSearchPlaceholder: {
5
+ id: 'neptune.MoneyInputField.currency.search.placeholder',
6
+ defaultMessage: 'Type a currency / country',
7
+ },
8
+
9
+ currencySelectorSelectCurrency: {
10
+ id: 'neptune.MoneyInputField.currency.select.currency',
11
+ defaultMessage: 'Select currency',
12
+ },
13
+ });
@@ -0,0 +1,193 @@
1
+ /* eslint-disable jsx-a11y/anchor-is-valid, no-console, jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions */
2
+ import { Meta } from '@storybook/react-webpack5';
3
+ import MoneyInputField, { Props as MoneyInputFieldProps } from './MoneyInputField';
4
+ import { lorem10 } from '../test-utils';
5
+ import { Sentiment } from '../common';
6
+ import Image from '../image';
7
+ import { Field } from '../field/Field';
8
+ import Link from '../link';
9
+ import Money from '../money';
10
+ import React from 'react';
11
+
12
+ export default {
13
+ title: 'Forms/MoneyInputField',
14
+ component: MoneyInputField,
15
+ } as Meta;
16
+
17
+ const props: MoneyInputFieldProps = {
18
+ currency: 'GBP',
19
+ amount: 1234.56,
20
+ onAmountChange: (amount) => {
21
+ console.log('Amount changed', amount);
22
+ },
23
+ currencySelector: {
24
+ addons: [
25
+ { asset: <Image src="../avatar-square-dude.webp" alt="" /> },
26
+ { profileName: 'Test Test' },
27
+ ],
28
+ options: [
29
+ {
30
+ title: 'Popular',
31
+ currencies: [
32
+ { code: 'USD', label: 'US Dollar', keywords: ['dollar', 'us'] },
33
+ { code: 'AUD', label: 'Australia Dollar', keywords: ['dollar', 'us'] },
34
+ ],
35
+ },
36
+ {
37
+ title: 'Others',
38
+ currencies: [
39
+ { code: 'GBP', label: 'Pound', keywords: ['british'] },
40
+ { code: 'EUR', label: 'Euro', keywords: ['euro'] },
41
+ ],
42
+ },
43
+ ],
44
+ onOpen: () => {
45
+ console.log('Currency selector opened');
46
+ },
47
+ onSearchChange: (payload) => {
48
+ console.log('Search changed', payload);
49
+ },
50
+ onChange: (currency) => {
51
+ console.log('Currency changed', currency);
52
+ },
53
+ },
54
+ loading: false,
55
+ inlinePrompt: { message: lorem10, sentiment: Sentiment.POSITIVE },
56
+ showChevron: true,
57
+ autoFocus: true,
58
+ };
59
+
60
+ export const NullAmount = {
61
+ args: props,
62
+ render: (args: MoneyInputFieldProps) => (
63
+ <Field label="You send" description="Amount is 'null' no dropdown and no addons">
64
+ <MoneyInputField amount={null} currency="EUR" onAmountChange={args.onAmountChange} />
65
+ </Field>
66
+ ),
67
+ };
68
+
69
+ export const WithInitAmount = {
70
+ args: props,
71
+ render: (args: MoneyInputFieldProps) => {
72
+ const availableBalance = 1500;
73
+ return (
74
+ <Field label="You send" description="With init amount">
75
+ <MoneyInputField
76
+ amount={availableBalance}
77
+ currency="EUR"
78
+ onAmountChange={args.onAmountChange}
79
+ />
80
+ </Field>
81
+ );
82
+ },
83
+ };
84
+
85
+ export const WithLoading = {
86
+ args: props,
87
+ render: (args: MoneyInputFieldProps) => {
88
+ const availableBalance = 1500;
89
+ return (
90
+ <Field label="You send" description="With init amount and loading=true">
91
+ <MoneyInputField
92
+ amount={availableBalance}
93
+ currency="EUR"
94
+ loading
95
+ onAmountChange={args.onAmountChange}
96
+ />
97
+ </Field>
98
+ );
99
+ },
100
+ };
101
+
102
+ export const WithoutChevron = {
103
+ args: props,
104
+ render: (args: MoneyInputFieldProps) => {
105
+ const availableBalance = 1500;
106
+ return (
107
+ <Field label="You send" description="With init amount and showChevron=false">
108
+ <MoneyInputField
109
+ amount={availableBalance}
110
+ currency="EUR"
111
+ showChevron={false}
112
+ onAmountChange={args.onAmountChange}
113
+ />
114
+ </Field>
115
+ );
116
+ },
117
+ };
118
+
119
+ export const WithCurrencySelector = {
120
+ args: props,
121
+ render: (args: MoneyInputFieldProps) => {
122
+ const availableBalance = 1500;
123
+ return (
124
+ <Field label="You send" description="With init amount, currencySelector setup">
125
+ <MoneyInputField
126
+ amount={availableBalance}
127
+ currency="EUR"
128
+ currencySelector={args.currencySelector}
129
+ onAmountChange={args.onAmountChange}
130
+ />
131
+ </Field>
132
+ );
133
+ },
134
+ };
135
+
136
+ export const WithInlinePromptNoSentiment = {
137
+ args: props,
138
+ render: (args: MoneyInputFieldProps) => {
139
+ const availableBalance = 1500;
140
+ const [sourceAmount, setSourceAmount] = React.useState(args.amount);
141
+ return (
142
+ <Field label="You send" description="No sentiment inline prompt">
143
+ <MoneyInputField
144
+ amount={sourceAmount}
145
+ currency="EUR"
146
+ currencySelector={args.currencySelector}
147
+ inlinePrompt={{
148
+ message: (
149
+ <>
150
+ {`Available balance `}
151
+ <Link
152
+ onClick={() => {
153
+ setSourceAmount(availableBalance);
154
+ }}
155
+ >
156
+ <Money amount={availableBalance} currency={args.currency} />
157
+ </Link>
158
+ </>
159
+ ),
160
+ }}
161
+ onAmountChange={args.onAmountChange}
162
+ />
163
+ </Field>
164
+ );
165
+ },
166
+ };
167
+
168
+ export const WithInlinePromptSentiment = {
169
+ args: props,
170
+ render: (args: MoneyInputFieldProps) => {
171
+ const availableBalance = 1500;
172
+ return (
173
+ <Field label="You send" description="Sentiment inline prompt">
174
+ <MoneyInputField
175
+ amount={availableBalance}
176
+ currency="EUR"
177
+ currencySelector={args.currencySelector}
178
+ inlinePrompt={args.inlinePrompt}
179
+ onAmountChange={args.onAmountChange}
180
+ />
181
+ </Field>
182
+ );
183
+ },
184
+ };
185
+
186
+ export const Autofocus = {
187
+ args: props,
188
+ render: (args: MoneyInputFieldProps) => (
189
+ <Field label="You send" description="Set autofocus=true and showChevron=true">
190
+ <MoneyInputField {...args} currency="MXN" inlinePrompt={undefined} />
191
+ </Field>
192
+ ),
193
+ };
@@ -0,0 +1,118 @@
1
+ import { Body } from '..';
2
+ import { clsx } from 'clsx';
3
+ import { AnimatePresence, motion } from 'framer-motion';
4
+ import { useId } from 'react';
5
+
6
+ import { type Props as CurrencySelectorProps, CurrencySelector } from './CurrencySelector';
7
+ import { CommonProps } from '../common';
8
+ import { AmountInput } from './AmountInput';
9
+ import { Chevron } from './Chevron';
10
+ import { useInputAttributes } from '../inputs/contexts';
11
+ import { InlinePrompt, type InlinePromptProps } from '../prompt/InlinePrompt';
12
+
13
+ type AmountType = number | null;
14
+ export type CurrencyType = string;
15
+
16
+ export type Props = {
17
+ currencySelector?: Pick<
18
+ CurrencySelectorProps,
19
+ 'addons' | 'options' | 'onChange' | 'onOpen' | 'onSearchChange'
20
+ >;
21
+ amount?: AmountType;
22
+ currency: CurrencyType;
23
+ inlinePrompt?: {
24
+ sentiment?: InlinePromptProps['sentiment'];
25
+ message: InlinePromptProps['children'];
26
+ };
27
+ showChevron?: boolean;
28
+ autoFocus?: boolean;
29
+ loading?: boolean;
30
+ onAmountChange: (amount: AmountType) => void;
31
+ onFocusChange?: (focused: boolean) => void;
32
+ } & CommonProps;
33
+
34
+ export default function MoneyInputField({
35
+ currency,
36
+ currencySelector = {},
37
+ amount,
38
+ onAmountChange,
39
+ className,
40
+ inlinePrompt,
41
+ showChevron,
42
+ autoFocus,
43
+ loading,
44
+ onFocusChange,
45
+ }: Props) {
46
+ // get input and label ids from Field component
47
+ const { id: fieldInputId, 'aria-labelledby': fieldLabelId } = useInputAttributes();
48
+ // if Field wasn't used fall back to generating ids locally
49
+ const fieldInputIdFallback = useId();
50
+ const fieldLabelIdFallback = useId();
51
+ const inputId = fieldInputId ?? fieldInputIdFallback;
52
+ const labelId = fieldLabelId ?? fieldLabelIdFallback;
53
+
54
+ const customAlertId = useId();
55
+ const currencyId = useId();
56
+
57
+ return (
58
+ <>
59
+ <div
60
+ className={clsx('wds-money-input-field', className)}
61
+ role="group"
62
+ aria-labelledby={labelId}
63
+ {...(inlinePrompt ? { 'aria-describedby': customAlertId } : {})}
64
+ >
65
+ <div className="wds-money-input-field-currency-selector">
66
+ <CurrencySelector
67
+ id={currencyId}
68
+ currency={currency}
69
+ options={currencySelector.options ?? []}
70
+ labelId={labelId}
71
+ addons={currencySelector.addons}
72
+ onChange={currencySelector.onChange}
73
+ onOpen={currencySelector.onOpen}
74
+ onSearchChange={currencySelector.onSearchChange}
75
+ />
76
+ </div>
77
+ <AmountInput
78
+ id={inputId}
79
+ describedById={currencyId}
80
+ amount={amount}
81
+ currency={currency}
82
+ // eslint-disable-next-line jsx-a11y/no-autofocus
83
+ autoFocus={autoFocus}
84
+ loading={loading}
85
+ onChange={onAmountChange}
86
+ onFocusChange={onFocusChange}
87
+ />
88
+ <div className={clsx('d-flex align-items-center', 'wds-money-input-field-chevron')}>
89
+ <Chevron shouldShow={Boolean(showChevron)} />
90
+ </div>
91
+ </div>
92
+ <AnimatePresence initial={false}>
93
+ {inlinePrompt && (
94
+ <div className={clsx('d-flex justify-content-end', inlinePrompt && 'm-t-1')}>
95
+ <motion.div
96
+ key={customAlertId}
97
+ initial={{ opacity: 0, height: 0 }}
98
+ animate={{
99
+ opacity: 1,
100
+ height: 'auto',
101
+ transition: { delay: 0.75, duration: 0.3 },
102
+ }}
103
+ exit={{ opacity: 0, height: 0 }}
104
+ >
105
+ {inlinePrompt.sentiment ? (
106
+ <InlinePrompt id={customAlertId} sentiment={inlinePrompt.sentiment}>
107
+ {inlinePrompt.message}
108
+ </InlinePrompt>
109
+ ) : (
110
+ <Body>{inlinePrompt.message}</Body>
111
+ )}
112
+ </motion.div>
113
+ </div>
114
+ )}
115
+ </AnimatePresence>
116
+ </>
117
+ );
118
+ }
@@ -0,0 +1,2 @@
1
+ export type { Props as MoneyInputFieldProps } from './MoneyInputField';
2
+ export { default } from './MoneyInputField';
@@ -0,0 +1,35 @@
1
+ import { useEffect, useState } from 'react';
2
+
3
+ const focusTimeout = 5 * 1000;
4
+
5
+ export const useFocus = () => {
6
+ const [focus, setFocus] = useState(false);
7
+ const [visualFocus, setVisualFocus] = useState(false);
8
+ const [counter, setCounter] = useState(0);
9
+
10
+ useEffect(() => {
11
+ if (focus) {
12
+ const timeout = setTimeout(() => {
13
+ setVisualFocus(false);
14
+ }, focusTimeout);
15
+ return () => clearTimeout(timeout);
16
+ }
17
+ }, [focus, counter]);
18
+
19
+ return {
20
+ focus,
21
+ setFocus: (value: boolean) => {
22
+ setFocus(value);
23
+ if (value) {
24
+ setVisualFocus(true);
25
+ }
26
+ // any call to setFocus should reset the timeout, even if the input was already in focus
27
+ // updating the counter will trigger the useEffect to reset the timeout
28
+ setCounter((prev) => prev + 1);
29
+ },
30
+ visualFocus,
31
+ setVisualFocus: (value: boolean) => {
32
+ setVisualFocus(value);
33
+ },
34
+ };
35
+ };
@@ -0,0 +1,85 @@
1
+ import { type CSSProperties, useEffect, useLayoutEffect, useState } from 'react';
2
+ import { Props as MoneyInputFieldProps } from './MoneyInputField';
3
+
4
+ type InputStyleObject = {
5
+ value: string;
6
+ focus: boolean;
7
+ inputElement: HTMLInputElement | null;
8
+ } & Pick<MoneyInputFieldProps, 'loading'>;
9
+
10
+ export const useInputStyle = ({ value, focus, inputElement, loading }: InputStyleObject) => {
11
+ const initialRender = !useTimeout(300);
12
+ const inputWidth = useFirstDefinedValue(inputElement?.clientWidth, [inputElement, value]);
13
+
14
+ const getStyle = (): CSSProperties => {
15
+ const fontSize = getFontSize(value, focus, inputWidth);
16
+
17
+ return {
18
+ fontSize,
19
+ height: fontSize,
20
+ // aligns the top of the digit with the currency button
21
+ marginTop: fontSize * -0.19,
22
+ // if the input loads with a pre-filled value, we don't want to animate the font-size immediately
23
+ transition: initialRender ? 'none' : undefined,
24
+ color: loading ? 'var(--color-interactive-secondary)' : undefined,
25
+ };
26
+ };
27
+
28
+ const [style, setStyle] = useState(getStyle());
29
+
30
+ useLayoutEffect(() => {
31
+ setStyle(getStyle());
32
+ }, [value, focus, loading, inputWidth]);
33
+
34
+ return style;
35
+ };
36
+
37
+ function getFontSize(inputValue: string, isFocused: boolean, inputWidth: number | undefined) {
38
+ const defaultFontSize = 52;
39
+ const focusFontSize = 90;
40
+ const minimumFontSize = 34;
41
+
42
+ let fontSize = isFocused ? focusFontSize : defaultFontSize;
43
+
44
+ if (typeof inputWidth === 'undefined') {
45
+ return fontSize;
46
+ }
47
+ const textLength = inputValue.length;
48
+ const maxCharactersWithoutShrinking = Math.floor(inputWidth / 40);
49
+
50
+ if (textLength > maxCharactersWithoutShrinking) {
51
+ const adjustedSize = Math.round((inputWidth / textLength) * 1.9);
52
+ fontSize = Math.min(fontSize, adjustedSize);
53
+ }
54
+
55
+ return Math.max(fontSize, minimumFontSize);
56
+ }
57
+
58
+ const useFirstDefinedValue = (newValue: number | undefined, dependencies: unknown[]) => {
59
+ const [value, setValue] = useState<number | undefined>(newValue);
60
+
61
+ useLayoutEffect(() => {
62
+ if (typeof newValue !== 'undefined' && typeof value === 'undefined') {
63
+ setValue(newValue);
64
+ }
65
+ // eslint-disable-next-line react-hooks/exhaustive-deps
66
+ }, [...dependencies, value]);
67
+
68
+ return value;
69
+ };
70
+
71
+ const useTimeout = (delay: number) => {
72
+ const [ready, setReady] = useState(false);
73
+
74
+ useEffect(() => {
75
+ const timeout = setTimeout(() => {
76
+ setReady(true);
77
+ }, delay);
78
+
79
+ return () => {
80
+ clearTimeout(timeout);
81
+ };
82
+ }, [delay]);
83
+
84
+ return ready;
85
+ };
@@ -0,0 +1,23 @@
1
+ import { type SyntheticEvent, useRef } from 'react';
2
+
3
+ export const useSelectionRange = () => {
4
+ const selection = useRef<{
5
+ selectionStart: number | null;
6
+ selectionEnd: number | null;
7
+ }>();
8
+
9
+ const handleSelect = (e: SyntheticEvent<HTMLInputElement>) => {
10
+ const input = e.target as HTMLInputElement;
11
+ const { selectionStart, selectionEnd } = input;
12
+ selection.current = { selectionStart, selectionEnd };
13
+ };
14
+ const handleSelectionBlur = () => {
15
+ selection.current = undefined;
16
+ };
17
+
18
+ return {
19
+ selection: selection.current,
20
+ handleSelect,
21
+ handleSelectionBlur,
22
+ };
23
+ };
@@ -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
+ });