@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.
Files changed (120) hide show
  1. package/build/expressiveMoneyInput/ExpressiveMoneyInput.js +113 -0
  2. package/build/expressiveMoneyInput/ExpressiveMoneyInput.js.map +1 -0
  3. package/build/expressiveMoneyInput/ExpressiveMoneyInput.messages.js +17 -0
  4. package/build/expressiveMoneyInput/ExpressiveMoneyInput.messages.js.map +1 -0
  5. package/build/expressiveMoneyInput/ExpressiveMoneyInput.messages.mjs +13 -0
  6. package/build/expressiveMoneyInput/ExpressiveMoneyInput.messages.mjs.map +1 -0
  7. package/build/expressiveMoneyInput/ExpressiveMoneyInput.mjs +109 -0
  8. package/build/expressiveMoneyInput/ExpressiveMoneyInput.mjs.map +1 -0
  9. package/build/expressiveMoneyInput/amountInput/AmountInput.js +281 -0
  10. package/build/expressiveMoneyInput/amountInput/AmountInput.js.map +1 -0
  11. package/build/expressiveMoneyInput/amountInput/AmountInput.mjs +279 -0
  12. package/build/expressiveMoneyInput/amountInput/AmountInput.mjs.map +1 -0
  13. package/build/expressiveMoneyInput/amountInput/utils.js +87 -0
  14. package/build/expressiveMoneyInput/amountInput/utils.js.map +1 -0
  15. package/build/expressiveMoneyInput/amountInput/utils.mjs +78 -0
  16. package/build/expressiveMoneyInput/amountInput/utils.mjs.map +1 -0
  17. package/build/expressiveMoneyInput/animatedNumber/AnimatedNumber.js +50 -0
  18. package/build/expressiveMoneyInput/animatedNumber/AnimatedNumber.js.map +1 -0
  19. package/build/expressiveMoneyInput/animatedNumber/AnimatedNumber.mjs +48 -0
  20. package/build/expressiveMoneyInput/animatedNumber/AnimatedNumber.mjs.map +1 -0
  21. package/build/expressiveMoneyInput/chevron/Chevron.js +31 -0
  22. package/build/expressiveMoneyInput/chevron/Chevron.js.map +1 -0
  23. package/build/expressiveMoneyInput/chevron/Chevron.mjs +29 -0
  24. package/build/expressiveMoneyInput/chevron/Chevron.mjs.map +1 -0
  25. package/build/expressiveMoneyInput/currencySelector/CurrencySelector.js +160 -0
  26. package/build/expressiveMoneyInput/currencySelector/CurrencySelector.js.map +1 -0
  27. package/build/expressiveMoneyInput/currencySelector/CurrencySelector.mjs +157 -0
  28. package/build/expressiveMoneyInput/currencySelector/CurrencySelector.mjs.map +1 -0
  29. package/build/expressiveMoneyInput/hooks/useFocus.js +37 -0
  30. package/build/expressiveMoneyInput/hooks/useFocus.js.map +1 -0
  31. package/build/expressiveMoneyInput/hooks/useFocus.mjs +35 -0
  32. package/build/expressiveMoneyInput/hooks/useFocus.mjs.map +1 -0
  33. package/build/expressiveMoneyInput/hooks/useInputStyle.js +71 -0
  34. package/build/expressiveMoneyInput/hooks/useInputStyle.js.map +1 -0
  35. package/build/expressiveMoneyInput/hooks/useInputStyle.mjs +69 -0
  36. package/build/expressiveMoneyInput/hooks/useInputStyle.mjs.map +1 -0
  37. package/build/i18n/en.json +2 -0
  38. package/build/i18n/en.json.js +2 -0
  39. package/build/i18n/en.json.js.map +1 -1
  40. package/build/i18n/en.json.mjs +2 -0
  41. package/build/i18n/en.json.mjs.map +1 -1
  42. package/build/index.js +2 -0
  43. package/build/index.js.map +1 -1
  44. package/build/index.mjs +1 -0
  45. package/build/index.mjs.map +1 -1
  46. package/build/main.css +73 -7
  47. package/build/prompt/InlinePrompt/InlinePrompt.js +7 -0
  48. package/build/prompt/InlinePrompt/InlinePrompt.js.map +1 -1
  49. package/build/prompt/InlinePrompt/InlinePrompt.mjs +8 -1
  50. package/build/prompt/InlinePrompt/InlinePrompt.mjs.map +1 -1
  51. package/build/styles/expressiveMoneyInput/ExpressiveMoneyInput.css +58 -0
  52. package/build/styles/expressiveMoneyInput/amountInput/AmountInput.css +32 -0
  53. package/build/styles/expressiveMoneyInput/chevron/Chevron.css +12 -0
  54. package/build/styles/expressiveMoneyInput/currencySelector/CurrencySelector.css +6 -0
  55. package/build/styles/main.css +73 -7
  56. package/build/styles/moneyInput/MoneyInput.css +8 -0
  57. package/build/styles/prompt/InlinePrompt/InlinePrompt.css +7 -7
  58. package/build/types/expressiveMoneyInput/ExpressiveMoneyInput.d.ts +59 -0
  59. package/build/types/expressiveMoneyInput/ExpressiveMoneyInput.d.ts.map +1 -0
  60. package/build/types/expressiveMoneyInput/ExpressiveMoneyInput.messages.d.ts +12 -0
  61. package/build/types/expressiveMoneyInput/ExpressiveMoneyInput.messages.d.ts.map +1 -0
  62. package/build/types/expressiveMoneyInput/amountInput/AmountInput.d.ts +13 -0
  63. package/build/types/expressiveMoneyInput/amountInput/AmountInput.d.ts.map +1 -0
  64. package/build/types/expressiveMoneyInput/amountInput/utils.d.ts +22 -0
  65. package/build/types/expressiveMoneyInput/amountInput/utils.d.ts.map +1 -0
  66. package/build/types/expressiveMoneyInput/animatedNumber/AnimatedNumber.d.ts +9 -0
  67. package/build/types/expressiveMoneyInput/animatedNumber/AnimatedNumber.d.ts.map +1 -0
  68. package/build/types/expressiveMoneyInput/chevron/Chevron.d.ts +6 -0
  69. package/build/types/expressiveMoneyInput/chevron/Chevron.d.ts.map +1 -0
  70. package/build/types/expressiveMoneyInput/currencySelector/CurrencySelector.d.ts +30 -0
  71. package/build/types/expressiveMoneyInput/currencySelector/CurrencySelector.d.ts.map +1 -0
  72. package/build/types/expressiveMoneyInput/hooks/useFocus.d.ts +7 -0
  73. package/build/types/expressiveMoneyInput/hooks/useFocus.d.ts.map +1 -0
  74. package/build/types/expressiveMoneyInput/hooks/useInputStyle.d.ts +10 -0
  75. package/build/types/expressiveMoneyInput/hooks/useInputStyle.d.ts.map +1 -0
  76. package/build/types/expressiveMoneyInput/hooks/useSelectionRange.d.ts +10 -0
  77. package/build/types/expressiveMoneyInput/hooks/useSelectionRange.d.ts.map +1 -0
  78. package/build/types/expressiveMoneyInput/index.d.ts +3 -0
  79. package/build/types/expressiveMoneyInput/index.d.ts.map +1 -0
  80. package/build/types/index.d.ts +2 -0
  81. package/build/types/index.d.ts.map +1 -1
  82. package/build/types/prompt/InlinePrompt/InlinePrompt.d.ts +3 -2
  83. package/build/types/prompt/InlinePrompt/InlinePrompt.d.ts.map +1 -1
  84. package/build/types/test-utils/index.d.ts +4 -0
  85. package/build/types/test-utils/index.d.ts.map +1 -1
  86. package/package.json +6 -6
  87. package/src/expressiveMoneyInput/ExpressiveMoneyInput.autofocus.docs.mdx +12 -0
  88. package/src/expressiveMoneyInput/ExpressiveMoneyInput.css +58 -0
  89. package/src/expressiveMoneyInput/ExpressiveMoneyInput.less +13 -0
  90. package/src/expressiveMoneyInput/ExpressiveMoneyInput.messages.ts +13 -0
  91. package/src/expressiveMoneyInput/ExpressiveMoneyInput.story.tsx +232 -0
  92. package/src/expressiveMoneyInput/ExpressiveMoneyInput.tsx +156 -0
  93. package/src/expressiveMoneyInput/amountInput/AmountInput.css +32 -0
  94. package/src/expressiveMoneyInput/amountInput/AmountInput.less +43 -0
  95. package/src/expressiveMoneyInput/amountInput/AmountInput.tsx +353 -0
  96. package/src/expressiveMoneyInput/amountInput/utils.spec.ts +114 -0
  97. package/src/expressiveMoneyInput/amountInput/utils.ts +116 -0
  98. package/src/expressiveMoneyInput/animatedNumber/AnimatedNumber.tsx +40 -0
  99. package/src/expressiveMoneyInput/chevron/Chevron.css +12 -0
  100. package/src/expressiveMoneyInput/chevron/Chevron.less +13 -0
  101. package/src/expressiveMoneyInput/chevron/Chevron.tsx +35 -0
  102. package/src/expressiveMoneyInput/currencySelector/CurrencySelector.css +6 -0
  103. package/src/expressiveMoneyInput/currencySelector/CurrencySelector.less +7 -0
  104. package/src/expressiveMoneyInput/currencySelector/CurrencySelector.tsx +220 -0
  105. package/src/expressiveMoneyInput/hooks/useFocus.ts +35 -0
  106. package/src/expressiveMoneyInput/hooks/useInputStyle.ts +85 -0
  107. package/src/expressiveMoneyInput/hooks/useSelectionRange.ts +23 -0
  108. package/src/expressiveMoneyInput/index.ts +2 -0
  109. package/src/i18n/en.json +2 -0
  110. package/src/index.ts +2 -0
  111. package/src/main.css +73 -7
  112. package/src/main.less +1 -0
  113. package/src/moneyInput/MoneyInput.css +8 -0
  114. package/src/moneyInput/MoneyInput.less +5 -0
  115. package/src/prompt/InlinePrompt/InlinePrompt.css +7 -7
  116. package/src/prompt/InlinePrompt/InlinePrompt.less +7 -7
  117. package/src/prompt/InlinePrompt/InlinePrompt.spec.tsx +6 -0
  118. package/src/prompt/InlinePrompt/InlinePrompt.story.tsx +39 -0
  119. package/src/prompt/InlinePrompt/InlinePrompt.tsx +12 -2
  120. package/src/ssr.spec.tsx +1 -0
@@ -0,0 +1,31 @@
1
+ 'use strict';
2
+
3
+ var icons = require('@transferwise/icons');
4
+ var clsx = require('clsx');
5
+ var framerMotion = require('framer-motion');
6
+ var jsxRuntime = require('react/jsx-runtime');
7
+
8
+ const Chevron = ({
9
+ shouldShow = true
10
+ }) => {
11
+ return /*#__PURE__*/jsxRuntime.jsx("div", {
12
+ className: clsx.clsx('d-flex align-items-center', 'wds-chevron-container', !shouldShow && 'wds-chevron-hidden'),
13
+ children: /*#__PURE__*/jsxRuntime.jsx(framerMotion.motion.div, {
14
+ animate: {
15
+ x: [12, 0, 0, -12],
16
+ opacity: [0, 1, 1, 0]
17
+ },
18
+ transition: {
19
+ duration: 3,
20
+ ease: [[0.3, 0, 0.1, 1], 'linear', [0.3, 0, 0.1, 1]],
21
+ times: [0, 0.1, 0.9, 1],
22
+ repeat: Infinity,
23
+ repeatType: 'loop'
24
+ },
25
+ children: /*#__PURE__*/jsxRuntime.jsx(icons.ChevronLeft, {})
26
+ })
27
+ });
28
+ };
29
+
30
+ exports.Chevron = Chevron;
31
+ //# sourceMappingURL=Chevron.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Chevron.js","sources":["../../../src/expressiveMoneyInput/chevron/Chevron.tsx"],"sourcesContent":["import { ChevronLeft } from '@transferwise/icons';\nimport { clsx } from 'clsx';\nimport { motion } from 'framer-motion';\n\ninterface Props {\n shouldShow: boolean;\n}\n\nexport const Chevron = ({ shouldShow = true }: Props) => {\n return (\n <div\n className={clsx(\n 'd-flex align-items-center',\n 'wds-chevron-container',\n !shouldShow && 'wds-chevron-hidden',\n )}\n >\n <motion.div\n animate={{\n x: [12, 0, 0, -12],\n opacity: [0, 1, 1, 0],\n }}\n transition={{\n duration: 3,\n ease: [[0.3, 0, 0.1, 1], 'linear', [0.3, 0, 0.1, 1]],\n times: [0, 0.1, 0.9, 1],\n repeat: Infinity,\n repeatType: 'loop',\n }}\n >\n <ChevronLeft />\n </motion.div>\n </div>\n );\n};\n"],"names":["Chevron","shouldShow","_jsx","className","clsx","children","motion","div","animate","x","opacity","transition","duration","ease","times","repeat","Infinity","repeatType","ChevronLeft"],"mappings":";;;;;;;AAQO,MAAMA,OAAO,GAAGA,CAAC;AAAEC,EAAAA,UAAU,GAAG;AAAI,CAAS,KAAI;AACtD,EAAA,oBACEC,cAAA,CAAA,KAAA,EAAA;IACEC,SAAS,EAAEC,SAAI,CACb,2BAA2B,EAC3B,uBAAuB,EACvB,CAACH,UAAU,IAAI,oBAAoB,CACnC;AAAAI,IAAAA,QAAA,eAEFH,cAAA,CAACI,mBAAM,CAACC,GAAG,EAAA;AACTC,MAAAA,OAAO,EAAE;QACPC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,GAAG,CAAC;QAClBC,OAAO,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC;OACpB;AACFC,MAAAA,UAAU,EAAE;AACVC,QAAAA,QAAQ,EAAE,CAAC;QACXC,IAAI,EAAE,CAAC,CAAC,GAAG,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,GAAG,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC;QACpDC,KAAK,EAAE,CAAC,CAAC,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC;AACvBC,QAAAA,MAAM,EAAEC,QAAQ;AAChBC,QAAAA,UAAU,EAAE;OACZ;AAAAZ,MAAAA,QAAA,eAEFH,cAAA,CAACgB,iBAAW,EAAA,EAAA;KACF;AACd,GAAK,CAAC;AAEV;;;;"}
@@ -0,0 +1,29 @@
1
+ import { ChevronLeft } from '@transferwise/icons';
2
+ import { clsx } from 'clsx';
3
+ import { motion } from 'framer-motion';
4
+ import { jsx } from 'react/jsx-runtime';
5
+
6
+ const Chevron = ({
7
+ shouldShow = true
8
+ }) => {
9
+ return /*#__PURE__*/jsx("div", {
10
+ className: clsx('d-flex align-items-center', 'wds-chevron-container', !shouldShow && 'wds-chevron-hidden'),
11
+ children: /*#__PURE__*/jsx(motion.div, {
12
+ animate: {
13
+ x: [12, 0, 0, -12],
14
+ opacity: [0, 1, 1, 0]
15
+ },
16
+ transition: {
17
+ duration: 3,
18
+ ease: [[0.3, 0, 0.1, 1], 'linear', [0.3, 0, 0.1, 1]],
19
+ times: [0, 0.1, 0.9, 1],
20
+ repeat: Infinity,
21
+ repeatType: 'loop'
22
+ },
23
+ children: /*#__PURE__*/jsx(ChevronLeft, {})
24
+ })
25
+ });
26
+ };
27
+
28
+ export { Chevron };
29
+ //# sourceMappingURL=Chevron.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Chevron.mjs","sources":["../../../src/expressiveMoneyInput/chevron/Chevron.tsx"],"sourcesContent":["import { ChevronLeft } from '@transferwise/icons';\nimport { clsx } from 'clsx';\nimport { motion } from 'framer-motion';\n\ninterface Props {\n shouldShow: boolean;\n}\n\nexport const Chevron = ({ shouldShow = true }: Props) => {\n return (\n <div\n className={clsx(\n 'd-flex align-items-center',\n 'wds-chevron-container',\n !shouldShow && 'wds-chevron-hidden',\n )}\n >\n <motion.div\n animate={{\n x: [12, 0, 0, -12],\n opacity: [0, 1, 1, 0],\n }}\n transition={{\n duration: 3,\n ease: [[0.3, 0, 0.1, 1], 'linear', [0.3, 0, 0.1, 1]],\n times: [0, 0.1, 0.9, 1],\n repeat: Infinity,\n repeatType: 'loop',\n }}\n >\n <ChevronLeft />\n </motion.div>\n </div>\n );\n};\n"],"names":["Chevron","shouldShow","_jsx","className","clsx","children","motion","div","animate","x","opacity","transition","duration","ease","times","repeat","Infinity","repeatType","ChevronLeft"],"mappings":";;;;;AAQO,MAAMA,OAAO,GAAGA,CAAC;AAAEC,EAAAA,UAAU,GAAG;AAAI,CAAS,KAAI;AACtD,EAAA,oBACEC,GAAA,CAAA,KAAA,EAAA;IACEC,SAAS,EAAEC,IAAI,CACb,2BAA2B,EAC3B,uBAAuB,EACvB,CAACH,UAAU,IAAI,oBAAoB,CACnC;AAAAI,IAAAA,QAAA,eAEFH,GAAA,CAACI,MAAM,CAACC,GAAG,EAAA;AACTC,MAAAA,OAAO,EAAE;QACPC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,GAAG,CAAC;QAClBC,OAAO,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC;OACpB;AACFC,MAAAA,UAAU,EAAE;AACVC,QAAAA,QAAQ,EAAE,CAAC;QACXC,IAAI,EAAE,CAAC,CAAC,GAAG,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,GAAG,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC;QACpDC,KAAK,EAAE,CAAC,CAAC,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC;AACvBC,QAAAA,MAAM,EAAEC,QAAQ;AAChBC,QAAAA,UAAU,EAAE;OACZ;AAAAZ,MAAAA,QAAA,eAEFH,GAAA,CAACgB,WAAW,EAAA,EAAA;KACF;AACd,GAAK,CAAC;AAEV;;;;"}
@@ -0,0 +1,160 @@
1
+ 'use strict';
2
+
3
+ var Button_resolver = require('../../button/Button.resolver.js');
4
+ var SelectInput = require('../../inputs/SelectInput.js');
5
+ var icons = require('@transferwise/icons');
6
+ var art = require('@wise/art');
7
+ var React = require('react');
8
+ var reactIntl = require('react-intl');
9
+ var ExpressiveMoneyInput_messages = require('../ExpressiveMoneyInput.messages.js');
10
+ var jsxRuntime = require('react/jsx-runtime');
11
+
12
+ const CurrencySelector = ({
13
+ id,
14
+ currency,
15
+ options = [],
16
+ labelId,
17
+ onChange,
18
+ addons,
19
+ onOpen,
20
+ onSearchChange
21
+ }) => {
22
+ const intl = reactIntl.useIntl();
23
+ const allCurrencyOptions = React.useMemo(() => getUniqueCurrencies(options), [options]);
24
+ const activeCurrencyOption = React.useMemo(() => {
25
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
26
+ return allCurrencyOptions.find(option => option.code === currency);
27
+ }, [currency, allCurrencyOptions]);
28
+ const disabled = !onChange || options.length === 0 || options.length === 1 && options[0].currencies.length <= 1;
29
+ const [searchQuery, setSearchQuery] = React.useState('');
30
+ const handleTriggerClick = event => {
31
+ const triggerEl = event.currentTarget;
32
+ if (triggerEl?.getAttribute('aria-expanded') === 'false') {
33
+ onOpen?.();
34
+ }
35
+ };
36
+ const items = searchQuery ? filterAndSortCurrenciesForQuery(allCurrencyOptions, searchQuery).map(getCurrencySelectOption) : options.map(getCurrencyGroup);
37
+ return /*#__PURE__*/jsxRuntime.jsx(SelectInput.SelectInput, {
38
+ compareValues: "code",
39
+ disabled: disabled,
40
+ id: id,
41
+ value: activeCurrencyOption,
42
+ filterable: true,
43
+ filterPlaceholder: intl.formatMessage(ExpressiveMoneyInput_messages.default.currencySelectorSearchPlaceholder),
44
+ UNSAFE_triggerButtonProps: {
45
+ id: undefined,
46
+ 'aria-labelledby': undefined,
47
+ 'aria-describedby': labelId,
48
+ 'aria-invalid': undefined,
49
+ 'aria-label': intl.formatMessage(ExpressiveMoneyInput_messages.default.currencySelectorSelectCurrency)
50
+ },
51
+ items: items,
52
+ renderValue: ({
53
+ code,
54
+ label
55
+ }) => {
56
+ return /*#__PURE__*/jsxRuntime.jsx(SelectInput.SelectInputOptionContent, {
57
+ title: code,
58
+ note: label,
59
+ icon: /*#__PURE__*/jsxRuntime.jsx(art.Flag, {
60
+ code: code,
61
+ intrinsicSize: 24
62
+ })
63
+ });
64
+ },
65
+ renderTrigger: () => /*#__PURE__*/jsxRuntime.jsx(SelectInput.SelectInputTriggerButton, {
66
+ as: ButtonInput
67
+ // @ts-expect-error new (v2) ButtonProps
68
+ ,
69
+ addonStart: {
70
+ type: 'avatar',
71
+ value: [addons ? addons[0] : null, {
72
+ ...(addons && addons.length > 1 ? {
73
+ ...addons[1]
74
+ } : {
75
+ asset: /*#__PURE__*/jsxRuntime.jsx(art.Flag, {
76
+ code: currency
77
+ })
78
+ })
79
+ }].filter(Boolean).filter(avatar => !(avatar && Object.keys(avatar).length === 0))
80
+ },
81
+ addonEnd: disabled ? undefined : {
82
+ type: 'icon',
83
+ value: /*#__PURE__*/jsxRuntime.jsx(icons.ChevronDown, {})
84
+ },
85
+ onClick: event => handleTriggerClick(event),
86
+ children: currency
87
+ }),
88
+ onChange: newValue => {
89
+ onChange?.(newValue.code);
90
+ },
91
+ onFilterChange: ({
92
+ queryNormalized
93
+ }) => {
94
+ setSearchQuery(queryNormalized ?? '');
95
+ if (queryNormalized) {
96
+ onSearchChange?.({
97
+ query: queryNormalized,
98
+ resultCount: filterAndSortCurrenciesForQuery(allCurrencyOptions, queryNormalized).length
99
+ });
100
+ }
101
+ }
102
+ });
103
+ };
104
+ const ButtonInput = /*#__PURE__*/React.forwardRef(function ButtonInput({
105
+ children,
106
+ ...rest
107
+ }, ref) {
108
+ return /*#__PURE__*/jsxRuntime.jsx(Button_resolver.default, {
109
+ ref: ref,
110
+ size: "md",
111
+ v2: true,
112
+ className: "wds-currency-selector",
113
+ priority: "secondary-neutral",
114
+ ...rest,
115
+ children: children
116
+ });
117
+ });
118
+ const getCurrencySelectOption = currency => {
119
+ return {
120
+ type: 'option',
121
+ value: currency,
122
+ filterMatchers: currency.keywords
123
+ };
124
+ };
125
+ const getCurrencyGroup = section => {
126
+ return {
127
+ type: 'group',
128
+ label: section.title,
129
+ options: section.currencies.map(getCurrencySelectOption)
130
+ };
131
+ };
132
+ const getUniqueCurrencies = options => {
133
+ const allCurrencyOptions = options.flatMap(section => section.currencies);
134
+ const uniqueCurrencies = new Map();
135
+ allCurrencyOptions.forEach(currencyObj => {
136
+ uniqueCurrencies.set(currencyObj.code, currencyObj);
137
+ });
138
+ return Array.from(uniqueCurrencies.values());
139
+ };
140
+ const filterAndSortCurrenciesForQuery = (currencies, query) => {
141
+ return currencies.filter(currency => {
142
+ return currency.code.toLowerCase().includes(query) || (currency.label ?? '').toLowerCase().includes(query) || currency.keywords?.some(keyword => keyword.toLowerCase().includes(query));
143
+ })
144
+ // prefer exact matches, then sort alphabetically by code
145
+ .sort((a, b) => {
146
+ const aCode = a.code.toLowerCase();
147
+ const bCode = b.code.toLowerCase();
148
+ if (aCode === query) {
149
+ return -1;
150
+ }
151
+ if (bCode === query) {
152
+ return 1;
153
+ }
154
+ return aCode.localeCompare(bCode);
155
+ });
156
+ };
157
+
158
+ exports.ButtonInput = ButtonInput;
159
+ exports.CurrencySelector = CurrencySelector;
160
+ //# sourceMappingURL=CurrencySelector.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CurrencySelector.js","sources":["../../../src/expressiveMoneyInput/currencySelector/CurrencySelector.tsx"],"sourcesContent":["import type { AvatarLayoutProps } from '../../avatarLayout';\nimport Button from '../../button';\nimport {\n SelectInput,\n SelectInputOptionContent,\n SelectInputTriggerButton,\n} from '../../inputs/SelectInput';\nimport { CurrencyType, Props as ExpressiveMoneyInputProps } from '../ExpressiveMoneyInput';\nimport { ChevronDown } from '@transferwise/icons';\nimport { Flag } from '@wise/art';\nimport {\n type ButtonHTMLAttributes,\n forwardRef,\n type MouseEventHandler,\n useMemo,\n useState,\n} from 'react';\nimport { useIntl } from 'react-intl';\n\nimport messages from '../ExpressiveMoneyInput.messages';\n\nexport interface CurrencyOption {\n label?: string;\n code: string;\n keywords: string[] | undefined;\n}\n\nexport interface CurrencySection {\n title: string;\n currencies: CurrencyOption[];\n}\n\nexport type CurrencyOptions = CurrencySection[];\n\nexport type Props = {\n id: string;\n labelId: string;\n options?: CurrencyOptions;\n onChange?: (currency: CurrencyType) => void;\n onOpen?: () => void;\n addons?: AvatarLayoutProps['avatars'];\n onSearchChange?: (payload: { query: string; resultCount: number }) => void;\n} & Pick<ExpressiveMoneyInputProps, 'currency'>;\n\nexport const CurrencySelector = ({\n id,\n currency,\n options = [],\n labelId,\n onChange,\n addons,\n onOpen,\n onSearchChange,\n}: Props) => {\n const intl = useIntl();\n\n const allCurrencyOptions = useMemo(() => getUniqueCurrencies(options), [options]);\n\n const activeCurrencyOption = useMemo(() => {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n return allCurrencyOptions.find((option) => option.code === currency)!;\n }, [currency, allCurrencyOptions]);\n\n const disabled =\n !onChange ||\n options.length === 0 ||\n (options.length === 1 && options[0].currencies.length <= 1);\n\n const [searchQuery, setSearchQuery] = useState<string>('');\n\n const handleTriggerClick: MouseEventHandler = (event) => {\n const triggerEl = event.currentTarget;\n if (triggerEl?.getAttribute('aria-expanded') === 'false') {\n onOpen?.();\n }\n };\n\n const items = searchQuery\n ? filterAndSortCurrenciesForQuery(allCurrencyOptions, searchQuery).map(getCurrencySelectOption)\n : options.map(getCurrencyGroup);\n\n return (\n <SelectInput\n compareValues=\"code\"\n disabled={disabled}\n id={id}\n value={activeCurrencyOption}\n filterable\n filterPlaceholder={intl.formatMessage(messages.currencySelectorSearchPlaceholder)}\n UNSAFE_triggerButtonProps={{\n id: undefined,\n 'aria-labelledby': undefined,\n 'aria-describedby': labelId,\n 'aria-invalid': undefined,\n 'aria-label': intl.formatMessage(messages.currencySelectorSelectCurrency),\n }}\n items={items}\n renderValue={({ code, label }) => {\n return (\n <SelectInputOptionContent\n title={code}\n note={label}\n icon={<Flag code={code} intrinsicSize={24} />}\n />\n );\n }}\n renderTrigger={() => (\n <SelectInputTriggerButton\n as={ButtonInput}\n // @ts-expect-error new (v2) ButtonProps\n addonStart={{\n type: 'avatar',\n value: [\n addons ? addons[0] : null,\n {\n ...(addons && addons.length > 1\n ? { ...addons[1] }\n : {\n asset: <Flag code={currency} />,\n }),\n },\n ]\n .filter(Boolean)\n .filter((avatar) => !(avatar && Object.keys(avatar).length === 0)),\n }}\n addonEnd={disabled ? undefined : { type: 'icon', value: <ChevronDown /> }}\n onClick={(event) => handleTriggerClick(event)}\n >\n {currency}\n </SelectInputTriggerButton>\n )}\n onChange={(newValue) => {\n onChange?.(newValue.code);\n }}\n onFilterChange={({ queryNormalized }) => {\n setSearchQuery(queryNormalized ?? '');\n if (queryNormalized) {\n onSearchChange?.({\n query: queryNormalized,\n resultCount: filterAndSortCurrenciesForQuery(allCurrencyOptions, queryNormalized)\n .length,\n });\n }\n }}\n />\n );\n};\n\nexport const ButtonInput = forwardRef(function ButtonInput(\n { children, ...rest }: React.PropsWithChildren<ButtonHTMLAttributes<HTMLButtonElement>>,\n ref: React.ForwardedRef<HTMLButtonElement | null>,\n) {\n return (\n <Button\n ref={ref}\n size=\"md\"\n v2\n className=\"wds-currency-selector\"\n priority=\"secondary-neutral\"\n {...rest}\n >\n {children}\n </Button>\n );\n});\n\nconst getCurrencySelectOption = (currency: CurrencyOption) => {\n return {\n type: 'option' as const,\n value: currency,\n filterMatchers: currency.keywords,\n };\n};\n\nconst getCurrencyGroup = (section: CurrencySection) => {\n return {\n type: 'group' as const,\n label: section.title,\n options: section.currencies.map(getCurrencySelectOption),\n };\n};\n\nconst getUniqueCurrencies = (options: CurrencyOptions) => {\n const allCurrencyOptions = options.flatMap((section) => section.currencies);\n const uniqueCurrencies = new Map<string, CurrencyOption>();\n\n allCurrencyOptions.forEach((currencyObj) => {\n uniqueCurrencies.set(currencyObj.code, currencyObj);\n });\n\n return Array.from(uniqueCurrencies.values());\n};\n\nconst filterAndSortCurrenciesForQuery = (\n currencies: CurrencyOption[],\n query: string,\n): CurrencyOption[] => {\n return (\n currencies\n .filter((currency) => {\n return (\n currency.code.toLowerCase().includes(query) ||\n (currency.label ?? '').toLowerCase().includes(query) ||\n currency.keywords?.some((keyword) => keyword.toLowerCase().includes(query))\n );\n })\n // prefer exact matches, then sort alphabetically by code\n .sort((a, b) => {\n const aCode = a.code.toLowerCase();\n const bCode = b.code.toLowerCase();\n if (aCode === query) {\n return -1;\n }\n if (bCode === query) {\n return 1;\n }\n return aCode.localeCompare(bCode);\n })\n );\n};\n"],"names":["CurrencySelector","id","currency","options","labelId","onChange","addons","onOpen","onSearchChange","intl","useIntl","allCurrencyOptions","useMemo","getUniqueCurrencies","activeCurrencyOption","find","option","code","disabled","length","currencies","searchQuery","setSearchQuery","useState","handleTriggerClick","event","triggerEl","currentTarget","getAttribute","items","filterAndSortCurrenciesForQuery","map","getCurrencySelectOption","getCurrencyGroup","_jsx","SelectInput","compareValues","value","filterable","filterPlaceholder","formatMessage","messages","currencySelectorSearchPlaceholder","UNSAFE_triggerButtonProps","undefined","currencySelectorSelectCurrency","renderValue","label","SelectInputOptionContent","title","note","icon","Flag","intrinsicSize","renderTrigger","SelectInputTriggerButton","as","ButtonInput","addonStart","type","asset","filter","Boolean","avatar","Object","keys","addonEnd","ChevronDown","onClick","children","newValue","onFilterChange","queryNormalized","query","resultCount","forwardRef","rest","ref","Button","size","v2","className","priority","filterMatchers","keywords","section","flatMap","uniqueCurrencies","Map","forEach","currencyObj","set","Array","from","values","toLowerCase","includes","some","keyword","sort","a","b","aCode","bCode","localeCompare"],"mappings":";;;;;;;;;;;AA4CO,MAAMA,gBAAgB,GAAGA,CAAC;EAC/BC,EAAE;EACFC,QAAQ;AACRC,EAAAA,OAAO,GAAG,EAAE;EACZC,OAAO;EACPC,QAAQ;EACRC,MAAM;EACNC,MAAM;AACNC,EAAAA;AAAc,CACR,KAAI;AACV,EAAA,MAAMC,IAAI,GAAGC,iBAAO,EAAE;AAEtB,EAAA,MAAMC,kBAAkB,GAAGC,aAAO,CAAC,MAAMC,mBAAmB,CAACV,OAAO,CAAC,EAAE,CAACA,OAAO,CAAC,CAAC;AAEjF,EAAA,MAAMW,oBAAo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CAACpF,IAAI,CAAC8E,WAAW,EAAE;IAClC,IAAIO,KAAK,KAAK7B,KAAK,EAAE;AACnB,MAAA,OAAO,EAAE;AACX,IAAA;IACA,IAAI8B,KAAK,KAAK9B,KAAK,EAAE;AACnB,MAAA,OAAO,CAAC;AACV,IAAA;AACA,IAAA,OAAO6B,KAAK,CAACE,aAAa,CAACD,KAAK,CAAC;AACnC,EAAA,CAAC,CAAC;AAER,CAAC;;;;;"}
@@ -0,0 +1,157 @@
1
+ import Button from '../../button/Button.resolver.mjs';
2
+ import { SelectInput, SelectInputTriggerButton, SelectInputOptionContent } from '../../inputs/SelectInput.mjs';
3
+ import { ChevronDown } from '@transferwise/icons';
4
+ import { Flag } from '@wise/art';
5
+ import { useMemo, useState, forwardRef } from 'react';
6
+ import { useIntl } from 'react-intl';
7
+ import messages from '../ExpressiveMoneyInput.messages.mjs';
8
+ import { jsx } from 'react/jsx-runtime';
9
+
10
+ const CurrencySelector = ({
11
+ id,
12
+ currency,
13
+ options = [],
14
+ labelId,
15
+ onChange,
16
+ addons,
17
+ onOpen,
18
+ onSearchChange
19
+ }) => {
20
+ const intl = useIntl();
21
+ const allCurrencyOptions = useMemo(() => getUniqueCurrencies(options), [options]);
22
+ const activeCurrencyOption = useMemo(() => {
23
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
24
+ return allCurrencyOptions.find(option => option.code === currency);
25
+ }, [currency, allCurrencyOptions]);
26
+ const disabled = !onChange || options.length === 0 || options.length === 1 && options[0].currencies.length <= 1;
27
+ const [searchQuery, setSearchQuery] = useState('');
28
+ const handleTriggerClick = event => {
29
+ const triggerEl = event.currentTarget;
30
+ if (triggerEl?.getAttribute('aria-expanded') === 'false') {
31
+ onOpen?.();
32
+ }
33
+ };
34
+ const items = searchQuery ? filterAndSortCurrenciesForQuery(allCurrencyOptions, searchQuery).map(getCurrencySelectOption) : options.map(getCurrencyGroup);
35
+ return /*#__PURE__*/jsx(SelectInput, {
36
+ compareValues: "code",
37
+ disabled: disabled,
38
+ id: id,
39
+ value: activeCurrencyOption,
40
+ filterable: true,
41
+ filterPlaceholder: intl.formatMessage(messages.currencySelectorSearchPlaceholder),
42
+ UNSAFE_triggerButtonProps: {
43
+ id: undefined,
44
+ 'aria-labelledby': undefined,
45
+ 'aria-describedby': labelId,
46
+ 'aria-invalid': undefined,
47
+ 'aria-label': intl.formatMessage(messages.currencySelectorSelectCurrency)
48
+ },
49
+ items: items,
50
+ renderValue: ({
51
+ code,
52
+ label
53
+ }) => {
54
+ return /*#__PURE__*/jsx(SelectInputOptionContent, {
55
+ title: code,
56
+ note: label,
57
+ icon: /*#__PURE__*/jsx(Flag, {
58
+ code: code,
59
+ intrinsicSize: 24
60
+ })
61
+ });
62
+ },
63
+ renderTrigger: () => /*#__PURE__*/jsx(SelectInputTriggerButton, {
64
+ as: ButtonInput
65
+ // @ts-expect-error new (v2) ButtonProps
66
+ ,
67
+ addonStart: {
68
+ type: 'avatar',
69
+ value: [addons ? addons[0] : null, {
70
+ ...(addons && addons.length > 1 ? {
71
+ ...addons[1]
72
+ } : {
73
+ asset: /*#__PURE__*/jsx(Flag, {
74
+ code: currency
75
+ })
76
+ })
77
+ }].filter(Boolean).filter(avatar => !(avatar && Object.keys(avatar).length === 0))
78
+ },
79
+ addonEnd: disabled ? undefined : {
80
+ type: 'icon',
81
+ value: /*#__PURE__*/jsx(ChevronDown, {})
82
+ },
83
+ onClick: event => handleTriggerClick(event),
84
+ children: currency
85
+ }),
86
+ onChange: newValue => {
87
+ onChange?.(newValue.code);
88
+ },
89
+ onFilterChange: ({
90
+ queryNormalized
91
+ }) => {
92
+ setSearchQuery(queryNormalized ?? '');
93
+ if (queryNormalized) {
94
+ onSearchChange?.({
95
+ query: queryNormalized,
96
+ resultCount: filterAndSortCurrenciesForQuery(allCurrencyOptions, queryNormalized).length
97
+ });
98
+ }
99
+ }
100
+ });
101
+ };
102
+ const ButtonInput = /*#__PURE__*/forwardRef(function ButtonInput({
103
+ children,
104
+ ...rest
105
+ }, ref) {
106
+ return /*#__PURE__*/jsx(Button, {
107
+ ref: ref,
108
+ size: "md",
109
+ v2: true,
110
+ className: "wds-currency-selector",
111
+ priority: "secondary-neutral",
112
+ ...rest,
113
+ children: children
114
+ });
115
+ });
116
+ const getCurrencySelectOption = currency => {
117
+ return {
118
+ type: 'option',
119
+ value: currency,
120
+ filterMatchers: currency.keywords
121
+ };
122
+ };
123
+ const getCurrencyGroup = section => {
124
+ return {
125
+ type: 'group',
126
+ label: section.title,
127
+ options: section.currencies.map(getCurrencySelectOption)
128
+ };
129
+ };
130
+ const getUniqueCurrencies = options => {
131
+ const allCurrencyOptions = options.flatMap(section => section.currencies);
132
+ const uniqueCurrencies = new Map();
133
+ allCurrencyOptions.forEach(currencyObj => {
134
+ uniqueCurrencies.set(currencyObj.code, currencyObj);
135
+ });
136
+ return Array.from(uniqueCurrencies.values());
137
+ };
138
+ const filterAndSortCurrenciesForQuery = (currencies, query) => {
139
+ return currencies.filter(currency => {
140
+ return currency.code.toLowerCase().includes(query) || (currency.label ?? '').toLowerCase().includes(query) || currency.keywords?.some(keyword => keyword.toLowerCase().includes(query));
141
+ })
142
+ // prefer exact matches, then sort alphabetically by code
143
+ .sort((a, b) => {
144
+ const aCode = a.code.toLowerCase();
145
+ const bCode = b.code.toLowerCase();
146
+ if (aCode === query) {
147
+ return -1;
148
+ }
149
+ if (bCode === query) {
150
+ return 1;
151
+ }
152
+ return aCode.localeCompare(bCode);
153
+ });
154
+ };
155
+
156
+ export { ButtonInput, CurrencySelector };
157
+ //# sourceMappingURL=CurrencySelector.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CurrencySelector.mjs","sources":["../../../src/expressiveMoneyInput/currencySelector/CurrencySelector.tsx"],"sourcesContent":["import type { AvatarLayoutProps } from '../../avatarLayout';\nimport Button from '../../button';\nimport {\n SelectInput,\n SelectInputOptionContent,\n SelectInputTriggerButton,\n} from '../../inputs/SelectInput';\nimport { CurrencyType, Props as ExpressiveMoneyInputProps } from '../ExpressiveMoneyInput';\nimport { ChevronDown } from '@transferwise/icons';\nimport { Flag } from '@wise/art';\nimport {\n type ButtonHTMLAttributes,\n forwardRef,\n type MouseEventHandler,\n useMemo,\n useState,\n} from 'react';\nimport { useIntl } from 'react-intl';\n\nimport messages from '../ExpressiveMoneyInput.messages';\n\nexport interface CurrencyOption {\n label?: string;\n code: string;\n keywords: string[] | undefined;\n}\n\nexport interface CurrencySection {\n title: string;\n currencies: CurrencyOption[];\n}\n\nexport type CurrencyOptions = CurrencySection[];\n\nexport type Props = {\n id: string;\n labelId: string;\n options?: CurrencyOptions;\n onChange?: (currency: CurrencyType) => void;\n onOpen?: () => void;\n addons?: AvatarLayoutProps['avatars'];\n onSearchChange?: (payload: { query: string; resultCount: number }) => void;\n} & Pick<ExpressiveMoneyInputProps, 'currency'>;\n\nexport const CurrencySelector = ({\n id,\n currency,\n options = [],\n labelId,\n onChange,\n addons,\n onOpen,\n onSearchChange,\n}: Props) => {\n const intl = useIntl();\n\n const allCurrencyOptions = useMemo(() => getUniqueCurrencies(options), [options]);\n\n const activeCurrencyOption = useMemo(() => {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n return allCurrencyOptions.find((option) => option.code === currency)!;\n }, [currency, allCurrencyOptions]);\n\n const disabled =\n !onChange ||\n options.length === 0 ||\n (options.length === 1 && options[0].currencies.length <= 1);\n\n const [searchQuery, setSearchQuery] = useState<string>('');\n\n const handleTriggerClick: MouseEventHandler = (event) => {\n const triggerEl = event.currentTarget;\n if (triggerEl?.getAttribute('aria-expanded') === 'false') {\n onOpen?.();\n }\n };\n\n const items = searchQuery\n ? filterAndSortCurrenciesForQuery(allCurrencyOptions, searchQuery).map(getCurrencySelectOption)\n : options.map(getCurrencyGroup);\n\n return (\n <SelectInput\n compareValues=\"code\"\n disabled={disabled}\n id={id}\n value={activeCurrencyOption}\n filterable\n filterPlaceholder={intl.formatMessage(messages.currencySelectorSearchPlaceholder)}\n UNSAFE_triggerButtonProps={{\n id: undefined,\n 'aria-labelledby': undefined,\n 'aria-describedby': labelId,\n 'aria-invalid': undefined,\n 'aria-label': intl.formatMessage(messages.currencySelectorSelectCurrency),\n }}\n items={items}\n renderValue={({ code, label }) => {\n return (\n <SelectInputOptionContent\n title={code}\n note={label}\n icon={<Flag code={code} intrinsicSize={24} />}\n />\n );\n }}\n renderTrigger={() => (\n <SelectInputTriggerButton\n as={ButtonInput}\n // @ts-expect-error new (v2) ButtonProps\n addonStart={{\n type: 'avatar',\n value: [\n addons ? addons[0] : null,\n {\n ...(addons && addons.length > 1\n ? { ...addons[1] }\n : {\n asset: <Flag code={currency} />,\n }),\n },\n ]\n .filter(Boolean)\n .filter((avatar) => !(avatar && Object.keys(avatar).length === 0)),\n }}\n addonEnd={disabled ? undefined : { type: 'icon', value: <ChevronDown /> }}\n onClick={(event) => handleTriggerClick(event)}\n >\n {currency}\n </SelectInputTriggerButton>\n )}\n onChange={(newValue) => {\n onChange?.(newValue.code);\n }}\n onFilterChange={({ queryNormalized }) => {\n setSearchQuery(queryNormalized ?? '');\n if (queryNormalized) {\n onSearchChange?.({\n query: queryNormalized,\n resultCount: filterAndSortCurrenciesForQuery(allCurrencyOptions, queryNormalized)\n .length,\n });\n }\n }}\n />\n );\n};\n\nexport const ButtonInput = forwardRef(function ButtonInput(\n { children, ...rest }: React.PropsWithChildren<ButtonHTMLAttributes<HTMLButtonElement>>,\n ref: React.ForwardedRef<HTMLButtonElement | null>,\n) {\n return (\n <Button\n ref={ref}\n size=\"md\"\n v2\n className=\"wds-currency-selector\"\n priority=\"secondary-neutral\"\n {...rest}\n >\n {children}\n </Button>\n );\n});\n\nconst getCurrencySelectOption = (currency: CurrencyOption) => {\n return {\n type: 'option' as const,\n value: currency,\n filterMatchers: currency.keywords,\n };\n};\n\nconst getCurrencyGroup = (section: CurrencySection) => {\n return {\n type: 'group' as const,\n label: section.title,\n options: section.currencies.map(getCurrencySelectOption),\n };\n};\n\nconst getUniqueCurrencies = (options: CurrencyOptions) => {\n const allCurrencyOptions = options.flatMap((section) => section.currencies);\n const uniqueCurrencies = new Map<string, CurrencyOption>();\n\n allCurrencyOptions.forEach((currencyObj) => {\n uniqueCurrencies.set(currencyObj.code, currencyObj);\n });\n\n return Array.from(uniqueCurrencies.values());\n};\n\nconst filterAndSortCurrenciesForQuery = (\n currencies: CurrencyOption[],\n query: string,\n): CurrencyOption[] => {\n return (\n currencies\n .filter((currency) => {\n return (\n currency.code.toLowerCase().includes(query) ||\n (currency.label ?? '').toLowerCase().includes(query) ||\n currency.keywords?.some((keyword) => keyword.toLowerCase().includes(query))\n );\n })\n // prefer exact matches, then sort alphabetically by code\n .sort((a, b) => {\n const aCode = a.code.toLowerCase();\n const bCode = b.code.toLowerCase();\n if (aCode === query) {\n return -1;\n }\n if (bCode === query) {\n return 1;\n }\n return aCode.localeCompare(bCode);\n })\n );\n};\n"],"names":["CurrencySelector","id","currency","options","labelId","onChange","addons","onOpen","onSearchChange","intl","useIntl","allCurrencyOptions","useMemo","getUniqueCurrencies","activeCurrencyOption","find","option","code","disabled","length","currencies","searchQuery","setSearchQuery","useState","handleTriggerClick","event","triggerEl","currentTarget","getAttribute","items","filterAndSortCurrenciesForQuery","map","getCurrencySelectOption","getCurrencyGroup","_jsx","SelectInput","compareValues","value","filterable","filterPlaceholder","formatMessage","messages","currencySelectorSearchPlaceholder","UNSAFE_triggerButtonProps","undefined","currencySelectorSelectCurrency","renderValue","label","SelectInputOptionContent","title","note","icon","Flag","intrinsicSize","renderTrigger","SelectInputTriggerButton","as","ButtonInput","addonStart","type","asset","filter","Boolean","avatar","Object","keys","addonEnd","ChevronDown","onClick","children","newValue","onFilterChange","queryNormalized","query","resultCount","forwardRef","rest","ref","Button","size","v2","className","priority","filterMatchers","keywords","section","flatMap","uniqueCurrencies","Map","forEach","currencyObj","set","Array","from","values","toLowerCase","includes","some","keyword","sort","a","b","aCode","bCode","localeCompare"],"mappings":";;;;;;;;;AA4CO,MAAMA,gBAAgB,GAAGA,CAAC;EAC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AK,CAAC,IACpDvE,QAAQ,CAACkF,QAAQ,EAAEa,IAAI,CAAEC,OAAO,IAAKA,OAAO,CAACH,WAAW,EAAE,CAACC,QAAQ,CAACvB,KAAK,CAAC,CAAC;EAE/E,CAAC;AACD;AAAA,GACC0B,IAAI,CAAC,CAACC,CAAC,EAAEC,CAAC,KAAI;IACb,MAAMC,KAAK,GAAGF,CAAC,CAACnF,IAAI,CAAC8E,WAAW,EAAE;IAClC,MAAMQ,KAAK,GAAGF,CAAC,CAACpF,IAAI,CAAC8E,WAAW,EAAE;IAClC,IAAIO,KAAK,KAAK7B,KAAK,EAAE;AACnB,MAAA,OAAO,EAAE;AACX,IAAA;IACA,IAAI8B,KAAK,KAAK9B,KAAK,EAAE;AACnB,MAAA,OAAO,CAAC;AACV,IAAA;AACA,IAAA,OAAO6B,KAAK,CAACE,aAAa,CAACD,KAAK,CAAC;AACnC,EAAA,CAAC,CAAC;AAER,CAAC;;;;"}
@@ -0,0 +1,37 @@
1
+ 'use strict';
2
+
3
+ var React = require('react');
4
+
5
+ const focusTimeout = 5 * 1000;
6
+ const useFocus = () => {
7
+ const [focus, setFocus] = React.useState(false);
8
+ const [visualFocus, setVisualFocus] = React.useState(false);
9
+ const [counter, setCounter] = React.useState(0);
10
+ React.useEffect(() => {
11
+ if (focus) {
12
+ const timeout = setTimeout(() => {
13
+ setVisualFocus(false);
14
+ }, focusTimeout);
15
+ return () => clearTimeout(timeout);
16
+ }
17
+ }, [focus, counter]);
18
+ return {
19
+ focus,
20
+ setFocus: value => {
21
+ setFocus(value);
22
+ if (value) {
23
+ setVisualFocus(true);
24
+ }
25
+ // any call to setFocus should reset the timeout, even if the input was already in focus
26
+ // updating the counter will trigger the useEffect to reset the timeout
27
+ setCounter(prev => prev + 1);
28
+ },
29
+ visualFocus,
30
+ setVisualFocus: value => {
31
+ setVisualFocus(value);
32
+ }
33
+ };
34
+ };
35
+
36
+ exports.useFocus = useFocus;
37
+ //# sourceMappingURL=useFocus.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useFocus.js","sources":["../../../src/expressiveMoneyInput/hooks/useFocus.ts"],"sourcesContent":["import { useEffect, useState } from 'react';\n\nconst focusTimeout = 5 * 1000;\n\nexport const useFocus = () => {\n const [focus, setFocus] = useState(false);\n const [visualFocus, setVisualFocus] = useState(false);\n const [counter, setCounter] = useState(0);\n\n useEffect(() => {\n if (focus) {\n const timeout = setTimeout(() => {\n setVisualFocus(false);\n }, focusTimeout);\n return () => clearTimeout(timeout);\n }\n }, [focus, counter]);\n\n return {\n focus,\n setFocus: (value: boolean) => {\n setFocus(value);\n if (value) {\n setVisualFocus(true);\n }\n // any call to setFocus should reset the timeout, even if the input was already in focus\n // updating the counter will trigger the useEffect to reset the timeout\n setCounter((prev) => prev + 1);\n },\n visualFocus,\n setVisualFocus: (value: boolean) => {\n setVisualFocus(value);\n },\n };\n};\n"],"names":["focusTimeout","useFocus","focus","setFocus","useState","visualFocus","setVisualFocus","counter","setCounter","useEffect","timeout","setTimeout","clearTimeout","value","prev"],"mappings":";;;;AAEA,MAAMA,YAAY,GAAG,CAAC,GAAG,IAAI;AAEtB,MAAMC,QAAQ,GAAGA,MAAK;EAC3B,MAAM,CAACC,KAAK,EAAEC,QAAQ,CAAC,GAAGC,cAAQ,CAAC,KAAK,CAAC;EACzC,MAAM,CAACC,WAAW,EAAEC,cAAc,CAAC,GAAGF,cAAQ,CAAC,KAAK,CAAC;EACrD,MAAM,CAACG,OAAO,EAAEC,UAAU,CAAC,GAAGJ,cAAQ,CAAC,CAAC,CAAC;AAEzCK,EAAAA,eAAS,CAAC,MAAK;AACb,IAAA,IAAIP,KAAK,EAAE;AACT,MAAA,MAAMQ,OAAO,GAAGC,UAAU,CAAC,MAAK;QAC9BL,cAAc,CAAC,KAAK,CAAC;MACvB,CAAC,EAAEN,YAAY,CAAC;AAChB,MAAA,OAAO,MAAMY,YAAY,CAACF,OAAO,CAAC;AACpC,IAAA;AACF,EAAA,CAAC,EAAE,CAACR,KAAK,EAAEK,OAAO,CAAC,CAAC;EAEpB,OAAO;IACLL,KAAK;IACLC,QAAQ,EAAGU,KAAc,IAAI;MAC3BV,QAAQ,CAACU,KAAK,CAAC;AACf,MAAA,IAAIA,KAAK,EAAE;QACTP,cAAc,CAAC,IAAI,CAAC;AACtB,MAAA;AACA;AACA;AACAE,MAAAA,UAAU,CAAEM,IAAI,IAAKA,IAAI,GAAG,CAAC,CAAC;IAChC,CAAC;IACDT,WAAW;IACXC,cAAc,EAAGO,KAAc,IAAI;MACjCP,cAAc,CAACO,KAAK,CAAC;AACvB,IAAA;GACD;AACH;;;;"}
@@ -0,0 +1,35 @@
1
+ import { useState, useEffect } from 'react';
2
+
3
+ const focusTimeout = 5 * 1000;
4
+ const useFocus = () => {
5
+ const [focus, setFocus] = useState(false);
6
+ const [visualFocus, setVisualFocus] = useState(false);
7
+ const [counter, setCounter] = useState(0);
8
+ useEffect(() => {
9
+ if (focus) {
10
+ const timeout = setTimeout(() => {
11
+ setVisualFocus(false);
12
+ }, focusTimeout);
13
+ return () => clearTimeout(timeout);
14
+ }
15
+ }, [focus, counter]);
16
+ return {
17
+ focus,
18
+ setFocus: value => {
19
+ setFocus(value);
20
+ if (value) {
21
+ setVisualFocus(true);
22
+ }
23
+ // any call to setFocus should reset the timeout, even if the input was already in focus
24
+ // updating the counter will trigger the useEffect to reset the timeout
25
+ setCounter(prev => prev + 1);
26
+ },
27
+ visualFocus,
28
+ setVisualFocus: value => {
29
+ setVisualFocus(value);
30
+ }
31
+ };
32
+ };
33
+
34
+ export { useFocus };
35
+ //# sourceMappingURL=useFocus.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useFocus.mjs","sources":["../../../src/expressiveMoneyInput/hooks/useFocus.ts"],"sourcesContent":["import { useEffect, useState } from 'react';\n\nconst focusTimeout = 5 * 1000;\n\nexport const useFocus = () => {\n const [focus, setFocus] = useState(false);\n const [visualFocus, setVisualFocus] = useState(false);\n const [counter, setCounter] = useState(0);\n\n useEffect(() => {\n if (focus) {\n const timeout = setTimeout(() => {\n setVisualFocus(false);\n }, focusTimeout);\n return () => clearTimeout(timeout);\n }\n }, [focus, counter]);\n\n return {\n focus,\n setFocus: (value: boolean) => {\n setFocus(value);\n if (value) {\n setVisualFocus(true);\n }\n // any call to setFocus should reset the timeout, even if the input was already in focus\n // updating the counter will trigger the useEffect to reset the timeout\n setCounter((prev) => prev + 1);\n },\n visualFocus,\n setVisualFocus: (value: boolean) => {\n setVisualFocus(value);\n },\n };\n};\n"],"names":["focusTimeout","useFocus","focus","setFocus","useState","visualFocus","setVisualFocus","counter","setCounter","useEffect","timeout","setTimeout","clearTimeout","value","prev"],"mappings":";;AAEA,MAAMA,YAAY,GAAG,CAAC,GAAG,IAAI;AAEtB,MAAMC,QAAQ,GAAGA,MAAK;EAC3B,MAAM,CAACC,KAAK,EAAEC,QAAQ,CAAC,GAAGC,QAAQ,CAAC,KAAK,CAAC;EACzC,MAAM,CAACC,WAAW,EAAEC,cAAc,CAAC,GAAGF,QAAQ,CAAC,KAAK,CAAC;EACrD,MAAM,CAACG,OAAO,EAAEC,UAAU,CAAC,GAAGJ,QAAQ,CAAC,CAAC,CAAC;AAEzCK,EAAAA,SAAS,CAAC,MAAK;AACb,IAAA,IAAIP,KAAK,EAAE;AACT,MAAA,MAAMQ,OAAO,GAAGC,UAAU,CAAC,MAAK;QAC9BL,cAAc,CAAC,KAAK,CAAC;MACvB,CAAC,EAAEN,YAAY,CAAC;AAChB,MAAA,OAAO,MAAMY,YAAY,CAACF,OAAO,CAAC;AACpC,IAAA;AACF,EAAA,CAAC,EAAE,CAACR,KAAK,EAAEK,OAAO,CAAC,CAAC;EAEpB,OAAO;IACLL,KAAK;IACLC,QAAQ,EAAGU,KAAc,IAAI;MAC3BV,QAAQ,CAACU,KAAK,CAAC;AACf,MAAA,IAAIA,KAAK,EAAE;QACTP,cAAc,CAAC,IAAI,CAAC;AACtB,MAAA;AACA;AACA;AACAE,MAAAA,UAAU,CAAEM,IAAI,IAAKA,IAAI,GAAG,CAAC,CAAC;IAChC,CAAC;IACDT,WAAW;IACXC,cAAc,EAAGO,KAAc,IAAI;MACjCP,cAAc,CAACO,KAAK,CAAC;AACvB,IAAA;GACD;AACH;;;;"}
@@ -0,0 +1,71 @@
1
+ 'use strict';
2
+
3
+ var React = require('react');
4
+
5
+ const useInputStyle = ({
6
+ value,
7
+ focus,
8
+ inputElement,
9
+ loading
10
+ }) => {
11
+ const initialRender = !useTimeout(300);
12
+ const inputWidth = useFirstDefinedValue(inputElement?.clientWidth, [inputElement, value]);
13
+ const getStyle = () => {
14
+ const fontSize = getFontSize(value, focus, inputWidth);
15
+ return {
16
+ fontSize,
17
+ height: fontSize,
18
+ // aligns the top of the digit with the currency button
19
+ marginTop: fontSize * -0.19,
20
+ // if the input loads with a pre-filled value, we don't want to animate the font-size immediately
21
+ transition: initialRender ? 'none' : undefined,
22
+ color: loading ? 'var(--color-interactive-secondary)' : undefined
23
+ };
24
+ };
25
+ const [style, setStyle] = React.useState(getStyle());
26
+ React.useLayoutEffect(() => {
27
+ setStyle(getStyle());
28
+ }, [value, focus, loading, inputWidth]);
29
+ return style;
30
+ };
31
+ function getFontSize(inputValue, isFocused, inputWidth) {
32
+ const defaultFontSize = 52;
33
+ const focusFontSize = 90;
34
+ const minimumFontSize = 34;
35
+ let fontSize = isFocused ? focusFontSize : defaultFontSize;
36
+ if (typeof inputWidth === 'undefined') {
37
+ return fontSize;
38
+ }
39
+ const textLength = inputValue.length;
40
+ const maxCharactersWithoutShrinking = Math.floor(inputWidth / 40);
41
+ if (textLength > maxCharactersWithoutShrinking) {
42
+ const adjustedSize = Math.round(inputWidth / textLength * 1.9);
43
+ fontSize = Math.min(fontSize, adjustedSize);
44
+ }
45
+ return Math.max(fontSize, minimumFontSize);
46
+ }
47
+ const useFirstDefinedValue = (newValue, dependencies) => {
48
+ const [value, setValue] = React.useState(newValue);
49
+ React.useLayoutEffect(() => {
50
+ if (typeof newValue !== 'undefined' && typeof value === 'undefined') {
51
+ setValue(newValue);
52
+ }
53
+ // eslint-disable-next-line react-hooks/exhaustive-deps
54
+ }, [...dependencies, value]);
55
+ return value;
56
+ };
57
+ const useTimeout = delay => {
58
+ const [ready, setReady] = React.useState(false);
59
+ React.useEffect(() => {
60
+ const timeout = setTimeout(() => {
61
+ setReady(true);
62
+ }, delay);
63
+ return () => {
64
+ clearTimeout(timeout);
65
+ };
66
+ }, [delay]);
67
+ return ready;
68
+ };
69
+
70
+ exports.useInputStyle = useInputStyle;
71
+ //# sourceMappingURL=useInputStyle.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useInputStyle.js","sources":["../../../src/expressiveMoneyInput/hooks/useInputStyle.ts"],"sourcesContent":["import { type CSSProperties, useEffect, useLayoutEffect, useState } from 'react';\nimport { Props as ExpressiveMoneyInputProps } from '../ExpressiveMoneyInput';\n\ntype InputStyleObject = {\n value: string;\n focus: boolean;\n inputElement: HTMLInputElement | null;\n} & Pick<ExpressiveMoneyInputProps, 'loading'>;\n\nexport const useInputStyle = ({ value, focus, inputElement, loading }: InputStyleObject) => {\n const initialRender = !useTimeout(300);\n const inputWidth = useFirstDefinedValue(inputElement?.clientWidth, [inputElement, value]);\n\n const getStyle = (): CSSProperties => {\n const fontSize = getFontSize(value, focus, inputWidth);\n\n return {\n fontSize,\n height: fontSize,\n // aligns the top of the digit with the currency button\n marginTop: fontSize * -0.19,\n // if the input loads with a pre-filled value, we don't want to animate the font-size immediately\n transition: initialRender ? 'none' : undefined,\n color: loading ? 'var(--color-interactive-secondary)' : undefined,\n };\n };\n\n const [style, setStyle] = useState(getStyle());\n\n useLayoutEffect(() => {\n setStyle(getStyle());\n }, [value, focus, loading, inputWidth]);\n\n return style;\n};\n\nfunction getFontSize(inputValue: string, isFocused: boolean, inputWidth: number | undefined) {\n const defaultFontSize = 52;\n const focusFontSize = 90;\n const minimumFontSize = 34;\n\n let fontSize = isFocused ? focusFontSize : defaultFontSize;\n\n if (typeof inputWidth === 'undefined') {\n return fontSize;\n }\n const textLength = inputValue.length;\n const maxCharactersWithoutShrinking = Math.floor(inputWidth / 40);\n\n if (textLength > maxCharactersWithoutShrinking) {\n const adjustedSize = Math.round((inputWidth / textLength) * 1.9);\n fontSize = Math.min(fontSize, adjustedSize);\n }\n\n return Math.max(fontSize, minimumFontSize);\n}\n\nconst useFirstDefinedValue = (newValue: number | undefined, dependencies: unknown[]) => {\n const [value, setValue] = useState<number | undefined>(newValue);\n\n useLayoutEffect(() => {\n if (typeof newValue !== 'undefined' && typeof value === 'undefined') {\n setValue(newValue);\n }\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [...dependencies, value]);\n\n return value;\n};\n\nconst useTimeout = (delay: number) => {\n const [ready, setReady] = useState(false);\n\n useEffect(() => {\n const timeout = setTimeout(() => {\n setReady(true);\n }, delay);\n\n return () => {\n clearTimeout(timeout);\n };\n }, [delay]);\n\n return ready;\n};\n"],"names":["useInputStyle","value","focus","inputElement","loading","initialRender","useTimeout","inputWidth","useFirstDefinedValue","clientWidth","getStyle","fontSize","getFontSize","height","marginTop","transition","undefined","color","style","setStyle","useState","useLayoutEffect","inputValue","isFocused","defaultFontSize","focusFontSize","minimumFontSize","textLength","length","maxCharactersWithoutShrinking","Math","floor","adjustedSize","round","min","max","newValue","dependencies","setValue","delay","ready","setReady","useEffect","timeout","setTimeout","clearTimeout"],"mappings":";;;;AASO,MAAMA,aAAa,GAAGA,CAAC;EAAEC,KAAK;EAAEC,KAAK;EAAEC,YAAY;AAAEC,EAAAA;AAAO,CAAoB,KAAI;AACzF,EAAA,MAAMC,aAAa,GAAG,CAACC,UAAU,CAAC,GAAG,CAAC;AACtC,EAAA,MAAMC,UAAU,GAAGC,oBAAoB,CAACL,YAAY,EAAEM,WAAW,EAAE,CAACN,YAAY,EAAEF,KAAK,CAAC,CAAC;EAEzF,MAAMS,QAAQ,GAAGA,MAAoB;IACnC,MAAMC,QAAQ,GAAGC,WAAW,CAACX,KAAK,EAAEC,KAAK,EAAEK,UAAU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