@commercetools-uikit/money-input 13.0.3 → 13.0.4

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.
@@ -873,35 +873,39 @@ var createCurrencySelectStyles = function createCurrencySelectStyles(_ref4, them
873
873
  };
874
874
  }
875
875
  });
876
- }; // The MoneyInput component always operates on a value consisting of:
877
- // { amount: String, currencyCode: String }
876
+ };
877
+
878
+ // The MoneyInput component always operates on a value consisting of:
879
+ // ```
880
+ // { amount: String, currencyCode: String }
881
+ // ```
878
882
  //
879
883
  // The amount may only use a dot as the decimal separator.
880
- // The currencyCode must be supported by the API.
884
+ // The `currencyCode` must be supported by the API.
881
885
  //
882
- // The MoneyInput does not do any validation on its own. It only serves as a way
883
- // to get the amount and currencyCode input from the user. Validation is always
886
+ // The `MoneyInput` does not do any validation on its own. It only serves as a way
887
+ // to get the amount and `currencyCode` input from the user. Validation is always
884
888
  // up to the parent.
885
889
  //
886
- // The CTP API supports prices two types of prices: centPrecision and
887
- // highPrecision. The MoneyInput itself does not know about these. However,
888
- // it has two static methods defined (convertToMoneyValue and parseMoneyValue),
889
- // which can be used to convert between MoneyInput value and the MoneyValue
890
+ // The CTP API supports two types of prices: `centPrecision` and `highPrecision`.
891
+ // The `MoneyInput` itself does not know about these. However,
892
+ // it has two static methods defined (`convertToMoneyValue` and `parseMoneyValue`),
893
+ // which can be used to convert between `MoneyInput` value and the `MoneyValue`
890
894
  // supported by the API.
891
- // Some places in the API do not support highPrecision prices, but the
892
- // convertToMoneyValue will always return either a centPrecision or a
893
- // highPrecision price. It's up the MoneyInput's parent to show a validation
894
- // error in case a highPrecision price is used.
895
+ // Some places in the API do not support `highPrecision` prices, but the
896
+ // `convertToMoneyValue `will always return either a `centPrecision `or a
897
+ // `highPrecision` price. It's up the `MoneyInput`'s parent to show a validation
898
+ // error in case a `highPrecision` price is used.
895
899
  //
896
- // A value is considered as to have highPrecision when the number of supplied
900
+ // A value is considered as to have `highPrecision` when the number of supplied
897
901
  // fraction digits exceed the number of fraction digits the currency uses. For
898
- // example, 42.00 is always a centPrecision price, while 42.001 is always a
899
- // highPrecision price. It is not possible to hae 42.00 as a highPrecision
902
+ // example, `EUR 42.00` is always a `centPrecision` price, while `EUR 42.001` is always a
903
+ // `highPrecision` price. It is not possible to have `EUR 42.00` as a `highPrecision`
900
904
  // price.
901
905
  //
902
- // The first time the component renders, we want to try to show the centAmount
903
- // as a formatted number. To achieve this, the parseMoneyValue function can
904
- // be used to turn the API value into a value the MoneyInput understands.
906
+ // The first time the component renders, we want to try to show the `centAmount`
907
+ // as a formatted number. To achieve this, the `parseMoneyValue` function can
908
+ // be used to turn the API value into a value the `MoneyInput` understands.
905
909
  // During this transformation, the money value will get formatted into "amount".
906
910
  //
907
911
  // When the user changes the value, we don't want to format again. We only format
@@ -909,16 +913,19 @@ var createCurrencySelectStyles = function createCurrencySelectStyles(_ref4, them
909
913
  // formatting would mess with the user's input.
910
914
  //
911
915
  //
912
- // A full example of an MoneyValue with centPrecision would be
916
+ // A full example of an `MoneyValue` with `centPrecision` would be
917
+ // ```
913
918
  // {
914
919
  // "type": "centPrecision",
915
920
  // "currencyCode": "EUR",
916
921
  // "centAmount": 4200,
917
922
  // "fractionDigits": 2
918
923
  // }
919
- // which equals 42.00 €
924
+ // ```
925
+ // which equals `EUR 42.00`.
920
926
  //
921
- // A full example of an MoneyValue with highPrecision would be
927
+ // A full example of an `MoneyValue` with `highPrecision` would be
928
+ // ```
922
929
  // {
923
930
  // "type": "highPrecision",
924
931
  // "currencyCode": "EUR",
@@ -926,7 +933,8 @@ var createCurrencySelectStyles = function createCurrencySelectStyles(_ref4, them
926
933
  // "preciseAmount": 123456,
927
934
  // "fractionDigits": 7
928
935
  // }
929
- // which equals 0.0123456 €
936
+ // ```
937
+ // which equals `EUR 0.0123456`
930
938
  // Parsing
931
939
  // Since most users are not careful about how they enter values, we will parse
932
940
  // both `.` and `,` as decimal separators.
@@ -934,10 +942,7 @@ var createCurrencySelectStyles = function createCurrencySelectStyles(_ref4, them
934
942
  // When a value is `1,000.00` we also parse it as `1000`.
935
943
  //
936
944
  // This means the highest amount always wins. We do this by comparing the last
937
- // position of `.` and `,`. Whatever occurs later is used as the decimal
938
- // separator.
939
-
940
-
945
+ // position of `.` and `,`. Whatever occurs later is used as the decimal separator.
941
946
  var parseRawAmountToNumber = function parseRawAmountToNumber(rawAmount, locale) {
942
947
  var fractionsSeparator;
943
948
 
@@ -974,7 +979,7 @@ var parseRawAmountToNumber = function parseRawAmountToNumber(rawAmount, locale)
974
979
  //
975
980
  // This function expects the "amount" to be a trimmed value.
976
981
 
977
- var createMoneyValue = function createMoneyValue(currencyCode, rawAmount, locale) {
982
+ var createMoneyValue = function createMoneyValue(rawAmount, locale, currencyCode) {
978
983
  var _context3, _context4;
979
984
 
980
985
  if (!currencyCode) return null;
@@ -1030,17 +1035,23 @@ var createMoneyValue = function createMoneyValue(currencyCode, rawAmount, locale
1030
1035
  };
1031
1036
  };
1032
1037
 
1038
+ var createEmptyMoneyValue = function createEmptyMoneyValue(currencyCode) {
1039
+ return {
1040
+ type: 'centPrecision',
1041
+ currencyCode: currencyCode,
1042
+ centAmount: NaN,
1043
+ fractionDigits: 2
1044
+ };
1045
+ };
1046
+
1033
1047
  var getAmountAsNumberFromMoneyValue = function getAmountAsNumberFromMoneyValue(moneyValue) {
1034
1048
  return moneyValue.type === 'highPrecision' ? moneyValue.preciseAmount / Math.pow(10, moneyValue.fractionDigits) : moneyValue.centAmount / Math.pow(10, currencies[moneyValue.currencyCode].fractionDigits);
1035
1049
  }; // gets called with a string and should return a formatted string
1036
1050
 
1037
1051
 
1038
- var formatAmount = function formatAmount(rawAmount, currencyCode, locale) {
1052
+ var formatAmount = function formatAmount(rawAmount, locale, currencyCode) {
1039
1053
  // fallback in case the user didn't enter an amount yet (or it's invalid)
1040
- var moneyValue = createMoneyValue(currencyCode, rawAmount, locale) || {
1041
- currencyCode: currencyCode,
1042
- centAmount: NaN
1043
- };
1054
+ var moneyValue = createMoneyValue(rawAmount, locale, currencyCode) || createEmptyMoneyValue(currencyCode);
1044
1055
  var amount = getAmountAsNumberFromMoneyValue(moneyValue);
1045
1056
  var fractionDigits = moneyValue.preciseAmount ? moneyValue.fractionDigits : currencies[moneyValue.currencyCode].fractionDigits;
1046
1057
  return isNaN(amount) ? '' : amount.toLocaleString(locale, {
@@ -1112,8 +1123,8 @@ var MoneyInput = function MoneyInput(props) {
1112
1123
  toggleAmountHasFocus(false); // Skip formatting for empty value or when the input is used with an
1113
1124
  // unknown currency.
1114
1125
 
1115
- if (amount.length > 0 && currencies[props.value.currencyCode]) {
1116
- var formattedAmount = formatAmount(amount, props.value.currencyCode, intl.locale); // When the user entered a value with centPrecision, we can format
1126
+ if (amount.length > 0 && props.value.currencyCode && currencies[props.value.currencyCode]) {
1127
+ var formattedAmount = formatAmount(amount, intl.locale, props.value.currencyCode); // When the user entered a value with centPrecision, we can format
1117
1128
  // the resulting value to that currency, e.g. 20.1 to 20.10
1118
1129
 
1119
1130
  if (String(formattedAmount) !== amount) {
@@ -1154,7 +1165,7 @@ var MoneyInput = function MoneyInput(props) {
1154
1165
  // currency's number of fraction digits.
1155
1166
  // When the currency was a high-precision price, then no digits should
1156
1167
  // be lost
1157
- var formattedAmount = formatAmount(_trimInstanceProperty__default["default"](_context6 = props.value.amount).call(_context6), currencyCode, intl.locale); // The user could be changing the currency before entering any amount,
1168
+ var formattedAmount = formatAmount(_trimInstanceProperty__default["default"](_context6 = props.value.amount).call(_context6), intl.locale, currencyCode); // The user could be changing the currency before entering any amount,
1158
1169
  // or while the amount is invalid. In these cases, we don't attempt to
1159
1170
  // format the amount.
1160
1171
 
@@ -1301,7 +1312,7 @@ var MoneyInput = function MoneyInput(props) {
1301
1312
  "data-testid": "currency-dropdown"
1302
1313
  }), jsxRuntime.jsxs("div", {
1303
1314
  css: _ref,
1304
- children: [jsxRuntime.jsx("input", _objectSpread({
1315
+ children: [jsxRuntime.jsx("input", _objectSpread(_objectSpread({
1305
1316
  ref: amountInputRef,
1306
1317
  id: MoneyInput.getAmountInputId(moneyInputId),
1307
1318
  autoComplete: props.autoComplete,
@@ -1319,7 +1330,11 @@ var MoneyInput = function MoneyInput(props) {
1319
1330
  disabled: props.isDisabled,
1320
1331
  readOnly: props.isReadOnly,
1321
1332
  autoFocus: props.isAutofocussed
1322
- }, utils.filterDataAttributes(props))), props.hasHighPrecisionBadge && isHighPrecision && jsxRuntime.jsxs(jsxRuntime.Fragment, {
1333
+ }, utils.filterDataAttributes(props)), {}, {
1334
+ /* ARIA */
1335
+ "aria-invalid": props['aria-invalid'],
1336
+ "aria-errormessage": props['aria-errormessage']
1337
+ })), props.hasHighPrecisionBadge && isHighPrecision && jsxRuntime.jsxs(jsxRuntime.Fragment, {
1323
1338
  children: [!props.isDisabled && jsxRuntime.jsx("div", {
1324
1339
  id: getPortalId(props.id)
1325
1340
  }), jsxRuntime.jsx("div", {
@@ -1362,7 +1377,7 @@ MoneyInput.getCurrencyDropdownId = getCurrencyDropdownName;
1362
1377
  MoneyInput.convertToMoneyValue = function (value, locale) {
1363
1378
  var _context11;
1364
1379
 
1365
- return createMoneyValue(value.currencyCode, typeof value.amount === 'string' ? _trimInstanceProperty__default["default"](_context11 = value.amount).call(_context11) : '', locale);
1380
+ return createMoneyValue(typeof value.amount === 'string' ? _trimInstanceProperty__default["default"](_context11 = value.amount).call(_context11) : '', locale, value.currencyCode);
1366
1381
  };
1367
1382
 
1368
1383
  MoneyInput.parseMoneyValue = function (moneyValue, locale) {
@@ -1372,7 +1387,7 @@ MoneyInput.parseMoneyValue = function (moneyValue, locale) {
1372
1387
  };
1373
1388
  var amount = formatAmount(getAmountAsNumberFromMoneyValue(moneyValue).toLocaleString(locale, {
1374
1389
  minimumFractionDigits: moneyValue.fractionDigits
1375
- }), moneyValue.currencyCode, locale);
1390
+ }), locale, moneyValue.currencyCode);
1376
1391
  return {
1377
1392
  amount: amount,
1378
1393
  currencyCode: moneyValue.currencyCode
@@ -1387,7 +1402,7 @@ MoneyInput.isEmpty = function (formValue) {
1387
1402
 
1388
1403
  MoneyInput.isHighPrecision = function (formValue, locale) {
1389
1404
  var moneyValue = MoneyInput.convertToMoneyValue(formValue, locale);
1390
- return moneyValue && moneyValue.type === 'highPrecision';
1405
+ return (moneyValue === null || moneyValue === void 0 ? void 0 : moneyValue.type) === 'highPrecision';
1391
1406
  };
1392
1407
 
1393
1408
  MoneyInput.isTouched = function (touched) {
@@ -1398,7 +1413,7 @@ MoneyInput.defaultProps = defaultProps;
1398
1413
  var MoneyInput$1 = MoneyInput;
1399
1414
 
1400
1415
  // NOTE: This string will be replaced on build time with the package version.
1401
- var version = "13.0.3";
1416
+ var version = "13.0.4";
1402
1417
 
1403
1418
  exports["default"] = MoneyInput$1;
1404
1419
  exports.version = version;