@commercetools-uikit/money-input 12.2.9 → 13.0.3

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.
@@ -10,11 +10,11 @@ var _forEachInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/in
10
10
  var _Object$getOwnPropertyDescriptors = require('@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors');
11
11
  var _Object$defineProperties = require('@babel/runtime-corejs3/core-js-stable/object/define-properties');
12
12
  var _Object$defineProperty = require('@babel/runtime-corejs3/core-js-stable/object/define-property');
13
- require('@babel/runtime-corejs3/helpers/typeof');
14
13
  var _slicedToArray = require('@babel/runtime-corejs3/helpers/slicedToArray');
15
14
  var _defineProperty = require('@babel/runtime-corejs3/helpers/defineProperty');
16
15
  var _objectWithoutProperties = require('@babel/runtime-corejs3/helpers/objectWithoutProperties');
17
16
  var _styled = require('@emotion/styled/base');
17
+ var _pt = require('prop-types');
18
18
  var _lastIndexOfInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/last-index-of');
19
19
  var _parseFloat = require('@babel/runtime-corejs3/core-js-stable/parse-float');
20
20
  var _Math$trunc = require('@babel/runtime-corejs3/core-js-stable/math/trunc');
@@ -26,9 +26,7 @@ var _findInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/insta
26
26
  var _concatInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/concat');
27
27
  var react$1 = require('react');
28
28
  var ReactDOM = require('react-dom');
29
- require('prop-types');
30
29
  require('lodash/has');
31
- require('react-required-if');
32
30
  var Select = require('react-select');
33
31
  var reactIntl = require('react-intl');
34
32
  var react = require('@emotion/react');
@@ -53,6 +51,7 @@ var _Object$getOwnPropertyDescriptors__default = /*#__PURE__*/_interopDefault(_O
53
51
  var _Object$defineProperties__default = /*#__PURE__*/_interopDefault(_Object$defineProperties);
54
52
  var _Object$defineProperty__default = /*#__PURE__*/_interopDefault(_Object$defineProperty);
55
53
  var _styled__default = /*#__PURE__*/_interopDefault(_styled);
54
+ var _pt__default = /*#__PURE__*/_interopDefault(_pt);
56
55
  var _lastIndexOfInstanceProperty__default = /*#__PURE__*/_interopDefault(_lastIndexOfInstanceProperty);
57
56
  var _parseFloat__default = /*#__PURE__*/_interopDefault(_parseFloat);
58
57
  var _Math$trunc__default = /*#__PURE__*/_interopDefault(_Math$trunc);
@@ -753,17 +752,19 @@ var messages = reactIntl.defineMessages({
753
752
 
754
753
  var _excluded = ["id"];
755
754
 
756
- function ownKeys(object, enumerableOnly) { var keys = _Object$keys__default["default"](object); if (_Object$getOwnPropertySymbols__default["default"]) { var symbols = _Object$getOwnPropertySymbols__default["default"](object); if (enumerableOnly) { symbols = _filterInstanceProperty__default["default"](symbols).call(symbols, function (sym) { return _Object$getOwnPropertyDescriptor__default["default"](object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
755
+ function ownKeys(object, enumerableOnly) { var keys = _Object$keys__default["default"](object); if (_Object$getOwnPropertySymbols__default["default"]) { var symbols = _Object$getOwnPropertySymbols__default["default"](object); enumerableOnly && (symbols = _filterInstanceProperty__default["default"](symbols).call(symbols, function (sym) { return _Object$getOwnPropertyDescriptor__default["default"](object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
757
756
 
758
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { var _context14; _forEachInstanceProperty__default["default"](_context14 = ownKeys(Object(source), true)).call(_context14, function (key) { _defineProperty(target, key, source[key]); }); } else if (_Object$getOwnPropertyDescriptors__default["default"]) { _Object$defineProperties__default["default"](target, _Object$getOwnPropertyDescriptors__default["default"](source)); } else { var _context15; _forEachInstanceProperty__default["default"](_context15 = ownKeys(Object(source))).call(_context15, function (key) { _Object$defineProperty__default["default"](target, key, _Object$getOwnPropertyDescriptor__default["default"](source, key)); }); } } return target; }
757
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var _context14, _context15; var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? _forEachInstanceProperty__default["default"](_context14 = ownKeys(Object(source), !0)).call(_context14, function (key) { _defineProperty(target, key, source[key]); }) : _Object$getOwnPropertyDescriptors__default["default"] ? _Object$defineProperties__default["default"](target, _Object$getOwnPropertyDescriptors__default["default"](source)) : _forEachInstanceProperty__default["default"](_context15 = ownKeys(Object(source))).call(_context15, function (key) { _Object$defineProperty__default["default"](target, key, _Object$getOwnPropertyDescriptor__default["default"](source, key)); }); } return target; }
759
758
 
760
759
  var TooltipWrapper = _styled__default["default"]("div", {
761
- target: "elvjg270"
760
+ target: "e1u90zjc0"
762
761
  } )({
763
762
  name: "zjik7",
764
763
  styles: "display:flex"
765
764
  } );
766
765
 
766
+ var moneyInputSequentialId = utils.createSequentialId('money-input-');
767
+
767
768
  var getPortalId = function getPortalId(id) {
768
769
  return "portal-".concat(id);
769
770
  };
@@ -774,7 +775,18 @@ var getPortalNode = function getPortalNode(id) {
774
775
 
775
776
  var Portal = function Portal(props) {
776
777
  var domNode = getPortalNode(props.id);
777
- return /*#__PURE__*/ReactDOM__default["default"].createPortal(props.children, domNode);
778
+
779
+ if (domNode) {
780
+ return /*#__PURE__*/ReactDOM__default["default"].createPortal(props.children, domNode);
781
+ }
782
+
783
+ return null;
784
+ };
785
+
786
+ Portal.propTypes = {
787
+ id: _pt__default["default"].string.isRequired,
788
+ children: _pt__default["default"].node,
789
+ isDisabled: _pt__default["default"].bool
778
790
  };
779
791
 
780
792
  var CurrencyLabel = function CurrencyLabel(props) {
@@ -785,8 +797,8 @@ var CurrencyLabel = function CurrencyLabel(props) {
785
797
  });
786
798
  };
787
799
 
788
- CurrencyLabel.displayName = 'CurrencyLabel';
789
800
  CurrencyLabel.propTypes = {};
801
+ CurrencyLabel.displayName = 'CurrencyLabel';
790
802
 
791
803
  var _SingleValue = function SingleValue(_ref3) {
792
804
  var id = _ref3.id,
@@ -800,9 +812,10 @@ var _SingleValue = function SingleValue(_ref3) {
800
812
  }));
801
813
  };
802
814
 
815
+ _SingleValue.propTypes = {};
803
816
  _SingleValue.displayName = 'SingleValue';
804
- _SingleValue.propTypes = {}; // overwrite styles of createSelectStyles
805
817
 
818
+ // overwrite styles of createSelectStyles
806
819
  var createCurrencySelectStyles = function createCurrencySelectStyles(_ref4, theme) {
807
820
  var hasWarning = _ref4.hasWarning,
808
821
  hasError = _ref4.hasError,
@@ -948,7 +961,7 @@ var parseRawAmountToNumber = function parseRawAmountToNumber(rawAmount, locale)
948
961
  var normalizedAmount = String(rawAmount).replace(new RegExp("[^0-9".concat(fractionsSeparator, "]"), 'g'), '') // we just keep the numbers and the fraction symbol
949
962
  .replace(fractionsSeparator, '.'); // then we change whatever `fractionsSeparator` was to `.` so we can parse it as float
950
963
 
951
- return _parseFloat__default["default"](normalizedAmount, 10);
964
+ return _parseFloat__default["default"](normalizedAmount);
952
965
  }; // Turns the user input into a value the MoneyInput can pass up through onChange
953
966
  // In case the number of fraction digits contained in "amount" exceeds the
954
967
  // number of fraction digits the currency uses, it will emit a price of
@@ -1043,6 +1056,12 @@ var getCurrencyDropdownName = function getCurrencyDropdownName(name) {
1043
1056
  return name ? "".concat(name, ".currencyCode") : undefined;
1044
1057
  };
1045
1058
 
1059
+ var defaultProps = {
1060
+ currencies: [],
1061
+ horizontalConstraint: 'scale',
1062
+ menuPortalZIndex: 1
1063
+ };
1064
+
1046
1065
  var _ref = {
1047
1066
  name: "pw7jst",
1048
1067
  styles: "position:relative;width:100%"
@@ -1068,18 +1087,22 @@ var MoneyInput = function MoneyInput(props) {
1068
1087
  amountHasFocus = _useToggleState4[0],
1069
1088
  toggleAmountHasFocus = _useToggleState4[1];
1070
1089
 
1071
- var containerRef = react$1.useRef();
1072
- var amountInputRef = react$1.useRef();
1090
+ var containerRef = react$1.useRef(null);
1091
+ var amountInputRef = react$1.useRef(null);
1092
+ var moneyInputId = hooks.useFieldId(props.id, moneyInputSequentialId);
1093
+
1094
+ if (!props.isReadOnly) ;
1095
+
1073
1096
  var onFocus = props.onFocus;
1074
1097
  var handleAmountFocus = react$1.useCallback(function () {
1075
1098
  if (onFocus) onFocus({
1076
1099
  target: {
1077
- id: MoneyInput.getAmountInputId(props.id),
1100
+ id: MoneyInput.getAmountInputId(moneyInputId),
1078
1101
  name: getAmountInputName(props.name)
1079
1102
  }
1080
1103
  });
1081
1104
  toggleAmountHasFocus(true);
1082
- }, [toggleAmountHasFocus, onFocus, props.id, props.name]);
1105
+ }, [toggleAmountHasFocus, onFocus, moneyInputId, props.name]);
1083
1106
  var onChange = props.onChange;
1084
1107
  var handleAmountBlur = react$1.useCallback(function () {
1085
1108
  var _context5;
@@ -1096,10 +1119,9 @@ var MoneyInput = function MoneyInput(props) {
1096
1119
  if (String(formattedAmount) !== amount) {
1097
1120
  // We need to emit an event with the now formatted value
1098
1121
  var fakeEvent = {
1099
- // eslint-disable-next-line @typescript-eslint/no-empty-function
1100
1122
  persist: function persist() {},
1101
1123
  target: {
1102
- id: MoneyInput.getAmountInputId(props.id),
1124
+ id: MoneyInput.getAmountInputId(moneyInputId),
1103
1125
  name: getAmountInputName(props.name),
1104
1126
  value: formattedAmount
1105
1127
  }
@@ -1107,25 +1129,24 @@ var MoneyInput = function MoneyInput(props) {
1107
1129
  onChange(fakeEvent);
1108
1130
  }
1109
1131
  }
1110
- }, [intl.locale, onChange, props.id, props.name, props.value.amount, props.value.currencyCode, toggleAmountHasFocus]);
1132
+ }, [intl.locale, onChange, moneyInputId, props.name, props.value.amount, props.value.currencyCode, toggleAmountHasFocus]);
1111
1133
  var handleAmountChange = react$1.useCallback(function (event) {
1112
1134
  if (utils.isNumberish(event.target.value)) {
1113
1135
  onChange({
1114
- // eslint-disable-next-line @typescript-eslint/no-empty-function
1115
1136
  persist: function persist() {},
1116
1137
  target: {
1117
- id: MoneyInput.getAmountInputId(props.id),
1138
+ id: MoneyInput.getAmountInputId(moneyInputId),
1118
1139
  name: getAmountInputName(props.name),
1119
1140
  value: event.target.value
1120
1141
  }
1121
1142
  });
1122
1143
  }
1123
- }, [onChange, props.id, props.name]);
1144
+ }, [onChange, moneyInputId, props.name]);
1124
1145
  var handleCurrencyChange = react$1.useCallback(function (option) {
1125
1146
  var currencyCode = option.value;
1126
1147
 
1127
1148
  if (props.value.currencyCode !== currencyCode) {
1128
- var _context6;
1149
+ var _context6, _amountInputRef$curre;
1129
1150
 
1130
1151
  // When the user changes from a currency with 3 fraction digits to
1131
1152
  // a currency with 2 fraction digits, and when the input value was
@@ -1137,13 +1158,12 @@ var MoneyInput = function MoneyInput(props) {
1137
1158
  // or while the amount is invalid. In these cases, we don't attempt to
1138
1159
  // format the amount.
1139
1160
 
1140
- var nextAmount = isNaN(formattedAmount) ? props.value.amount : formattedAmount; // change currency code
1161
+ var nextAmount = isNaN(Number(formattedAmount)) ? props.value.amount : formattedAmount; // change currency code
1141
1162
 
1142
1163
  var fakeCurrencyEvent = {
1143
- // eslint-disable-next-line @typescript-eslint/no-empty-function
1144
1164
  persist: function persist() {},
1145
1165
  target: {
1146
- id: MoneyInput.getCurrencyDropdownId(props.id),
1166
+ id: MoneyInput.getCurrencyDropdownId(moneyInputId),
1147
1167
  name: getCurrencyDropdownName(props.name),
1148
1168
  value: currencyCode || ''
1149
1169
  }
@@ -1152,28 +1172,27 @@ var MoneyInput = function MoneyInput(props) {
1152
1172
 
1153
1173
  if (props.value.amount !== nextAmount) {
1154
1174
  onChange({
1155
- // eslint-disable-next-line @typescript-eslint/no-empty-function
1156
1175
  persist: function persist() {},
1157
1176
  target: {
1158
- id: MoneyInput.getAmountInputId(props.id),
1177
+ id: MoneyInput.getAmountInputId(moneyInputId),
1159
1178
  name: getAmountInputName(props.name),
1160
1179
  value: nextAmount
1161
1180
  }
1162
1181
  });
1163
1182
  }
1164
1183
 
1165
- amountInputRef.current.focus();
1184
+ (_amountInputRef$curre = amountInputRef.current) === null || _amountInputRef$curre === void 0 ? void 0 : _amountInputRef$curre.focus();
1166
1185
  }
1167
- }, [intl.locale, onChange, props.id, props.name, props.value.amount, props.value.currencyCode]);
1186
+ }, [intl.locale, onChange, moneyInputId, props.name, props.value.amount, props.value.currencyCode]);
1168
1187
  var handleCurrencyFocus = react$1.useCallback(function () {
1169
1188
  if (onFocus) onFocus({
1170
1189
  target: {
1171
- id: MoneyInput.getCurrencyDropdownId(props.id),
1190
+ id: MoneyInput.getCurrencyDropdownId(moneyInputId),
1172
1191
  name: getCurrencyDropdownName(props.name)
1173
1192
  }
1174
1193
  });
1175
1194
  toggleCurrencyHasFocus(true);
1176
- }, [onFocus, toggleCurrencyHasFocus, props.name, props.id]);
1195
+ }, [onFocus, toggleCurrencyHasFocus, props.name, moneyInputId]);
1177
1196
  var handleCurrencyBlur = react$1.useCallback(function () {
1178
1197
  toggleCurrencyHasFocus(false);
1179
1198
  }, [toggleCurrencyHasFocus]);
@@ -1213,27 +1232,28 @@ var MoneyInput = function MoneyInput(props) {
1213
1232
  return null;
1214
1233
  }();
1215
1234
 
1216
- var id = MoneyInput.getCurrencyDropdownId(props.id);
1217
1235
  var isHighPrecision = !MoneyInput.isEmpty(props.value) && MoneyInput.isHighPrecision(props.value, intl.locale);
1218
1236
  var onBlur = props.onBlur;
1219
1237
  var handleContainerBlur = react$1.useCallback(function (event) {
1238
+ var _containerRef$current;
1239
+
1220
1240
  // ensures that both fields are marked as touched when one of them
1221
1241
  // is blurred
1222
- if (typeof onBlur === 'function' && !containerRef.current.contains(event.relatedTarget)) {
1242
+ if (typeof onBlur === 'function' && !((_containerRef$current = containerRef.current) !== null && _containerRef$current !== void 0 && _containerRef$current.contains(event.relatedTarget))) {
1223
1243
  onBlur({
1224
1244
  target: {
1225
- id: MoneyInput.getCurrencyDropdownId(props.id),
1245
+ id: MoneyInput.getCurrencyDropdownId(moneyInputId),
1226
1246
  name: getCurrencyDropdownName(props.name)
1227
1247
  }
1228
1248
  });
1229
1249
  onBlur({
1230
1250
  target: {
1231
- id: MoneyInput.getAmountInputId(props.id),
1251
+ id: MoneyInput.getAmountInputId(moneyInputId),
1232
1252
  name: getAmountInputName(props.name)
1233
1253
  }
1234
1254
  });
1235
1255
  }
1236
- }, [onBlur, props.id, props.name]);
1256
+ }, [onBlur, moneyInputId, props.name]);
1237
1257
  var TooltipPortal = react$1.useCallback(function (remainingProps) {
1238
1258
  return jsxRuntime.jsx(Portal, _objectSpread(_objectSpread({}, remainingProps), {}, {
1239
1259
  id: props.id
@@ -1247,29 +1267,25 @@ var MoneyInput = function MoneyInput(props) {
1247
1267
  "data-testid": "money-input-container",
1248
1268
  onBlur: handleContainerBlur,
1249
1269
  children: [hasNoCurrencies ? jsxRuntime.jsx(CurrencyLabel, {
1250
- id: MoneyInput.getAmountInputId(props.id),
1270
+ id: MoneyInput.getAmountInputId(moneyInputId),
1251
1271
  isDisabled: props.isDisabled,
1252
1272
  children: option && option.label
1253
1273
  }) : jsxRuntime.jsx(Select__default["default"], {
1254
- inputId: id,
1274
+ inputId: MoneyInput.getCurrencyDropdownId(moneyInputId),
1255
1275
  name: getCurrencyDropdownName(props.name),
1256
1276
  value: option,
1257
1277
  isDisabled: props.isDisabled,
1258
1278
  isSearchable: false,
1259
1279
  components: {
1260
- // eslint-disable-next-line react/display-name
1261
1280
  SingleValue: function SingleValue(innerProps) {
1262
1281
  return jsxRuntime.jsx(_SingleValue, _objectSpread(_objectSpread({}, innerProps), {}, {
1263
- id: id
1282
+ id: MoneyInput.getCurrencyDropdownId(moneyInputId)
1264
1283
  }));
1265
1284
  },
1266
- // eslint-disable-next-line react/display-name
1267
1285
  Input: function Input(ownProps) {
1268
- return (// eslint-disable-next-line react/prop-types
1269
- jsxRuntime.jsx(Select.components.Input, _objectSpread(_objectSpread({}, ownProps), {}, {
1270
- readOnly: props.isReadOnly
1271
- }))
1272
- );
1286
+ return jsxRuntime.jsx(Select.components.Input, _objectSpread(_objectSpread({}, ownProps), {}, {
1287
+ readOnly: props.isReadOnly
1288
+ }));
1273
1289
  },
1274
1290
  DropdownIndicator: selectUtils.DropdownIndicator
1275
1291
  },
@@ -1287,7 +1303,7 @@ var MoneyInput = function MoneyInput(props) {
1287
1303
  css: _ref,
1288
1304
  children: [jsxRuntime.jsx("input", _objectSpread({
1289
1305
  ref: amountInputRef,
1290
- id: MoneyInput.getAmountInputId(props.id),
1306
+ id: MoneyInput.getAmountInputId(moneyInputId),
1291
1307
  autoComplete: props.autoComplete,
1292
1308
  name: getAmountInputName(props.name),
1293
1309
  type: "text",
@@ -1338,6 +1354,7 @@ var MoneyInput = function MoneyInput(props) {
1338
1354
  });
1339
1355
  };
1340
1356
 
1357
+ MoneyInput.propTypes = {};
1341
1358
  MoneyInput.displayName = 'MoneyInput';
1342
1359
  MoneyInput.getAmountInputId = getAmountInputName;
1343
1360
  MoneyInput.getCurrencyDropdownId = getCurrencyDropdownName;
@@ -1377,16 +1394,11 @@ MoneyInput.isTouched = function (touched) {
1377
1394
  return Boolean(touched && touched.currencyCode && touched.amount);
1378
1395
  };
1379
1396
 
1380
- MoneyInput.propTypes = {};
1381
- MoneyInput.defaultProps = {
1382
- currencies: [],
1383
- horizontalConstraint: 'scale',
1384
- menuPortalZIndex: 1
1385
- };
1397
+ MoneyInput.defaultProps = defaultProps;
1386
1398
  var MoneyInput$1 = MoneyInput;
1387
1399
 
1388
1400
  // NOTE: This string will be replaced on build time with the package version.
1389
- var version = "12.2.9";
1401
+ var version = "13.0.3";
1390
1402
 
1391
1403
  exports["default"] = MoneyInput$1;
1392
1404
  exports.version = version;