@commercetools-uikit/money-input 20.4.0 → 20.6.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.
@@ -22,7 +22,6 @@ var _parseInt = require('@babel/runtime-corejs3/core-js-stable/parse-int');
22
22
  var _trimInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/trim');
23
23
  var _mapInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/map');
24
24
  var _findInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/find');
25
- var _concatInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/concat');
26
25
  var react$1 = require('react');
27
26
  var ReactDOM = require('react-dom');
28
27
  require('lodash/has');
@@ -58,7 +57,6 @@ var _parseInt__default = /*#__PURE__*/_interopDefault(_parseInt);
58
57
  var _trimInstanceProperty__default = /*#__PURE__*/_interopDefault(_trimInstanceProperty);
59
58
  var _mapInstanceProperty__default = /*#__PURE__*/_interopDefault(_mapInstanceProperty);
60
59
  var _findInstanceProperty__default = /*#__PURE__*/_interopDefault(_findInstanceProperty);
61
- var _concatInstanceProperty__default = /*#__PURE__*/_interopDefault(_concatInstanceProperty);
62
60
  var ReactDOM__default = /*#__PURE__*/_interopDefault(ReactDOM);
63
61
  var Select__default = /*#__PURE__*/_interopDefault(Select);
64
62
  var Tooltip__default = /*#__PURE__*/_interopDefault(Tooltip);
@@ -727,6 +725,25 @@ const currencies = {
727
725
  },
728
726
  ZWR: {
729
727
  fractionDigits: 2
728
+ },
729
+ // Zero-fraction variants (non-ISO): base currency with 0 decimal places.
730
+ CZK0: {
731
+ fractionDigits: 0
732
+ },
733
+ HUF0: {
734
+ fractionDigits: 0
735
+ },
736
+ ILS0: {
737
+ fractionDigits: 0
738
+ },
739
+ KZT0: {
740
+ fractionDigits: 0
741
+ },
742
+ TRY0: {
743
+ fractionDigits: 0
744
+ },
745
+ TWD0: {
746
+ fractionDigits: 0
730
747
  }
731
748
  };
732
749
  var allCurrencies = currencies;
@@ -760,7 +777,7 @@ var messages = reactIntl.defineMessages({
760
777
  const _excluded = ["id"],
761
778
  _excluded2 = ["currencies", "horizontalConstraint", "menuPortalZIndex"];
762
779
  function ownKeys(e, r) { var t = _Object$keys__default["default"](e); if (_Object$getOwnPropertySymbols__default["default"]) { var o = _Object$getOwnPropertySymbols__default["default"](e); r && (o = _filterInstanceProperty__default["default"](o).call(o, function (r) { return _Object$getOwnPropertyDescriptor__default["default"](e, r).enumerable; })), t.push.apply(t, o); } return t; }
763
- function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var _context11, _context12; var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? _forEachInstanceProperty__default["default"](_context11 = ownKeys(Object(t), !0)).call(_context11, function (r) { _defineProperty(e, r, t[r]); }) : _Object$getOwnPropertyDescriptors__default["default"] ? _Object$defineProperties__default["default"](e, _Object$getOwnPropertyDescriptors__default["default"](t)) : _forEachInstanceProperty__default["default"](_context12 = ownKeys(Object(t))).call(_context12, function (r) { _Object$defineProperty__default["default"](e, r, _Object$getOwnPropertyDescriptor__default["default"](t, r)); }); } return e; }
780
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var _context1, _context10; var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? _forEachInstanceProperty__default["default"](_context1 = ownKeys(Object(t), !0)).call(_context1, function (r) { _defineProperty(e, r, t[r]); }) : _Object$getOwnPropertyDescriptors__default["default"] ? _Object$defineProperties__default["default"](e, _Object$getOwnPropertyDescriptors__default["default"](t)) : _forEachInstanceProperty__default["default"](_context10 = ownKeys(Object(t))).call(_context10, function (r) { _Object$defineProperty__default["default"](e, r, _Object$getOwnPropertyDescriptor__default["default"](t, r)); }); } return e; }
764
781
  const TooltipWrapper = /*#__PURE__*/_styled__default["default"]("div", {
765
782
  target: "e1u90zjc0"
766
783
  } )({
@@ -768,8 +785,8 @@ const TooltipWrapper = /*#__PURE__*/_styled__default["default"]("div", {
768
785
  styles: "display:flex"
769
786
  } );
770
787
  const moneyInputSequentialId = utils.createSequentialId('money-input-');
771
- const getPortalId = id => "portal-".concat(id);
772
- const getPortalNode = id => document.querySelector("#".concat(getPortalId(id)));
788
+ const getPortalId = id => `portal-${id}`;
789
+ const getPortalNode = id => document.querySelector(`#${getPortalId(id)}`);
773
790
  const Portal = props => {
774
791
  const domNode = getPortalNode(props.id);
775
792
  if (domNode) {
@@ -813,15 +830,15 @@ const createCurrencySelectStyles = _ref4 => {
813
830
  });
814
831
  return _objectSpread(_objectSpread({}, selectStyles), {}, {
815
832
  control: (base, state) => _objectSpread(_objectSpread({}, selectStyles.control(base, state)), {}, {
816
- padding: "0 ".concat(designSystem.designTokens.spacing25),
833
+ padding: `0 ${designSystem.designTokens.spacing25}`,
817
834
  borderTopRightRadius: '0',
818
835
  borderBottomRightRadius: '0',
819
836
  borderRight: '0',
820
837
  minWidth: '80px',
821
838
  height: '100%',
822
839
  borderColor: (() => {
823
- if (isDisabled) return "".concat(designSystem.designTokens.borderColorForInputWhenDisabled, " !important");
824
- if (isReadOnly) return "".concat(designSystem.designTokens.borderColorForInputWhenReadonly, " !important");
840
+ if (isDisabled) return `${designSystem.designTokens.borderColorForInputWhenDisabled} !important`;
841
+ if (isReadOnly) return `${designSystem.designTokens.borderColorForInputWhenReadonly} !important`;
825
842
  if (hasError) return designSystem.designTokens.borderColorForInputWhenError;
826
843
  if (hasWarning) return designSystem.designTokens.borderColorForInputWhenWarning;
827
844
  if (currencyHasFocus) {
@@ -831,7 +848,7 @@ const createCurrencySelectStyles = _ref4 => {
831
848
  })(),
832
849
  cursor: (() => {
833
850
  if (isDisabled) return 'not-allowed';
834
- if (isReadOnly) return "default";
851
+ if (isReadOnly) return `default`;
835
852
  return 'pointer';
836
853
  })(),
837
854
  backgroundColor: (() => {
@@ -944,7 +961,7 @@ const parseRawAmountToNumber = (rawAmount, locale) => {
944
961
  }
945
962
  fractionsSeparator = fractionsSeparator === '.' ? '\\.' : fractionsSeparator; // here we escape the '.' to use it as regex
946
963
  // The raw amount with only one sparator
947
- const normalizedAmount = String(rawAmount).replace(new RegExp("[^-0-9".concat(fractionsSeparator, "]"), 'g'), '') // we just keep the numbers and the fraction symbol
964
+ const normalizedAmount = String(rawAmount).replace(new RegExp(`[^-0-9${fractionsSeparator}]`, 'g'), '') // we just keep the numbers and the fraction symbol
948
965
  .replace(fractionsSeparator, '.'); // then we change whatever `fractionsSeparator` was to `.` so we can parse it as float
949
966
 
950
967
  return _parseFloat__default["default"](normalizedAmount);
@@ -1021,7 +1038,7 @@ const createEmptyMoneyValue = currencyCode => ({
1021
1038
  type: 'centPrecision',
1022
1039
  currencyCode,
1023
1040
  centAmount: NaN,
1024
- fractionDigits: 2
1041
+ fractionDigits: allCurrencies[currencyCode].fractionDigits ?? 2
1025
1042
  });
1026
1043
  const getAmountAsNumberFromMoneyValue = moneyValue => moneyValue.type === 'highPrecision' ? moneyValue.preciseAmount / 10 ** moneyValue.fractionDigits : moneyValue.centAmount / 10 ** allCurrencies[moneyValue.currencyCode].fractionDigits;
1027
1044
 
@@ -1035,8 +1052,8 @@ const formatAmount = (rawAmount, locale, currencyCode) => {
1035
1052
  minimumFractionDigits: fractionDigits
1036
1053
  });
1037
1054
  };
1038
- const getAmountInputName = name => name ? "".concat(name, ".amount") : undefined;
1039
- const getCurrencyDropdownName = name => name ? "".concat(name, ".currencyCode") : undefined;
1055
+ const getAmountInputName = name => name ? `${name}.amount` : undefined;
1056
+ const getCurrencyDropdownName = name => name ? `${name}.currencyCode` : undefined;
1040
1057
  var _ref = {
1041
1058
  name: "pw7jst",
1042
1059
  styles: "position:relative;width:100%"
@@ -1046,7 +1063,6 @@ var _ref2 = {
1046
1063
  styles: "font-family:inherit;width:100%;position:relative;display:flex"
1047
1064
  } ;
1048
1065
  const MoneyInput = _ref5 => {
1049
- var _context8, _context9;
1050
1066
  let _ref5$currencies = _ref5.currencies,
1051
1067
  currencies = _ref5$currencies === void 0 ? [] : _ref5$currencies,
1052
1068
  _ref5$horizontalConst = _ref5.horizontalConstraint,
@@ -1104,20 +1120,18 @@ const MoneyInput = _ref5 => {
1104
1120
  value: formattedAmount
1105
1121
  }
1106
1122
  };
1107
- onChange === null || onChange === void 0 || onChange(fakeEvent);
1123
+ onChange?.(fakeEvent);
1108
1124
  }
1109
1125
  }
1110
1126
  }, [intl.locale, onChange, moneyInputId, props.name, props.value.amount, props.value.currencyCode, toggleAmountHasFocus]);
1111
1127
  const handleAmountChange = react$1.useCallback(event => {
1112
- var _event$target;
1113
- if (utils.isNumberish((_event$target = event.target) === null || _event$target === void 0 ? void 0 : _event$target.value)) {
1114
- var _event$target2;
1115
- onChange === null || onChange === void 0 || onChange({
1128
+ if (utils.isNumberish(event.target?.value)) {
1129
+ onChange?.({
1116
1130
  persist: () => {},
1117
1131
  target: {
1118
1132
  id: MoneyInput.getAmountInputId(moneyInputId),
1119
1133
  name: getAmountInputName(props.name),
1120
- value: (_event$target2 = event.target) === null || _event$target2 === void 0 ? void 0 : _event$target2.value
1134
+ value: event.target?.value
1121
1135
  }
1122
1136
  });
1123
1137
  }
@@ -1125,7 +1139,7 @@ const MoneyInput = _ref5 => {
1125
1139
  const handleCurrencyChange = react$1.useCallback(option => {
1126
1140
  const currencyCode = option.value;
1127
1141
  if (props.value.currencyCode !== currencyCode) {
1128
- var _context6, _amountInputRef$curre;
1142
+ var _context6;
1129
1143
  // When the user changes from a currency with 3 fraction digits to
1130
1144
  // a currency with 2 fraction digits, and when the input value was
1131
1145
  // "9.000" (9), then it should change to "9.00" to reflect the new
@@ -1147,11 +1161,11 @@ const MoneyInput = _ref5 => {
1147
1161
  value: currencyCode || ''
1148
1162
  }
1149
1163
  };
1150
- onChange === null || onChange === void 0 || onChange(fakeCurrencyEvent);
1164
+ onChange?.(fakeCurrencyEvent);
1151
1165
 
1152
1166
  // change amount if necessary
1153
1167
  if (props.value.amount !== nextAmount) {
1154
- onChange === null || onChange === void 0 || onChange({
1168
+ onChange?.({
1155
1169
  persist: () => {},
1156
1170
  target: {
1157
1171
  id: MoneyInput.getAmountInputId(moneyInputId),
@@ -1160,7 +1174,7 @@ const MoneyInput = _ref5 => {
1160
1174
  }
1161
1175
  });
1162
1176
  }
1163
- (_amountInputRef$curre = amountInputRef.current) === null || _amountInputRef$curre === void 0 || _amountInputRef$curre.focus();
1177
+ amountInputRef.current?.focus();
1164
1178
  }
1165
1179
  }, [intl.locale, onChange, moneyInputId, props.name, props.value.amount, props.value.currencyCode]);
1166
1180
  const handleCurrencyFocus = react$1.useCallback(() => {
@@ -1204,10 +1218,9 @@ const MoneyInput = _ref5 => {
1204
1218
  const isHighPrecision = !MoneyInput.isEmpty(props.value) && MoneyInput.isHighPrecision(props.value, intl.locale);
1205
1219
  const onBlur = props.onBlur;
1206
1220
  const handleContainerBlur = react$1.useCallback(event => {
1207
- var _containerRef$current;
1208
1221
  // ensures that both fields are marked as touched when one of them
1209
1222
  // is blurred
1210
- if (typeof onBlur === 'function' && !((_containerRef$current = containerRef.current) !== null && _containerRef$current !== void 0 && _containerRef$current.contains(event.relatedTarget))) {
1223
+ if (typeof onBlur === 'function' && !containerRef.current?.contains(event.relatedTarget)) {
1211
1224
  onBlur({
1212
1225
  target: {
1213
1226
  id: MoneyInput.getCurrencyDropdownId(moneyInputId),
@@ -1307,7 +1320,7 @@ const MoneyInput = _ref5 => {
1307
1320
  ,
1308
1321
  styles: {
1309
1322
  body: {
1310
- margin: _concatInstanceProperty__default["default"](_context8 = _concatInstanceProperty__default["default"](_context9 = "".concat(designSystem.designTokens.spacing20, " -")).call(_context9, designSystem.designTokens.spacing10, " ")).call(_context8, designSystem.designTokens.spacing20, " 0")
1323
+ margin: `${designSystem.designTokens.spacing20} -${designSystem.designTokens.spacing10} ${designSystem.designTokens.spacing20} 0`
1311
1324
  }
1312
1325
  },
1313
1326
  title: intl.formatMessage(messages.highPrecision),
@@ -1329,8 +1342,8 @@ MoneyInput.displayName = 'MoneyInput';
1329
1342
  MoneyInput.getAmountInputId = getAmountInputName;
1330
1343
  MoneyInput.getCurrencyDropdownId = getCurrencyDropdownName;
1331
1344
  MoneyInput.convertToMoneyValue = (value, locale) => {
1332
- var _context0;
1333
- return createMoneyValue(typeof value.amount === 'string' ? _trimInstanceProperty__default["default"](_context0 = value.amount).call(_context0) : '', locale, value.currencyCode);
1345
+ var _context8;
1346
+ return createMoneyValue(typeof value.amount === 'string' ? _trimInstanceProperty__default["default"](_context8 = value.amount).call(_context8) : '', locale, value.currencyCode);
1334
1347
  };
1335
1348
  MoneyInput.parseMoneyValue = (moneyValue, locale) => {
1336
1349
  if (!moneyValue) return {
@@ -1346,18 +1359,18 @@ MoneyInput.parseMoneyValue = (moneyValue, locale) => {
1346
1359
  };
1347
1360
  };
1348
1361
  MoneyInput.isEmpty = formValue => {
1349
- var _context1, _context10;
1350
- return !formValue || _trimInstanceProperty__default["default"](_context1 = formValue.amount).call(_context1) === '' || _trimInstanceProperty__default["default"](_context10 = formValue.currencyCode).call(_context10) === '';
1362
+ var _context9, _context0;
1363
+ return !formValue || _trimInstanceProperty__default["default"](_context9 = formValue.amount).call(_context9) === '' || _trimInstanceProperty__default["default"](_context0 = formValue.currencyCode).call(_context0) === '';
1351
1364
  };
1352
1365
  MoneyInput.isHighPrecision = (formValue, locale) => {
1353
1366
  const moneyValue = MoneyInput.convertToMoneyValue(formValue, locale);
1354
- return (moneyValue === null || moneyValue === void 0 ? void 0 : moneyValue.type) === 'highPrecision';
1367
+ return moneyValue?.type === 'highPrecision';
1355
1368
  };
1356
1369
  MoneyInput.isTouched = touched => Boolean(touched && touched.currencyCode && touched.amount);
1357
1370
  var MoneyInput$1 = MoneyInput;
1358
1371
 
1359
1372
  // NOTE: This string will be replaced on build time with the package version.
1360
- var version = "20.4.0";
1373
+ var version = "20.6.0";
1361
1374
 
1362
1375
  exports["default"] = MoneyInput$1;
1363
1376
  exports.version = version;