@bigbinary/neeto-molecules 3.14.2 → 3.14.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.
@@ -1,28 +1,26 @@
1
1
  'use strict';
2
2
 
3
3
  var _defineProperty = require('@babel/runtime/helpers/defineProperty');
4
+ var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
4
5
  var _objectWithoutProperties = require('@babel/runtime/helpers/objectWithoutProperties');
5
- var shallow = require('zustand/shallow');
6
6
  var React = require('react');
7
- var classnames = require('classnames');
7
+ var reactQuery = require('@tanstack/react-query');
8
8
  var min = require('libphonenumber-js/min');
9
9
  var neetoCist = require('@bigbinary/neeto-cist');
10
10
  var Input = require('@bigbinary/neetoui/Input');
11
- var reactI18next = require('react-i18next');
12
- var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
11
+ var classnames = require('classnames');
13
12
  var Select = require('@bigbinary/neetoui/Select');
14
13
  var _commonjsHelpers = require('./_commonjsHelpers-BJu3ubxk.js');
15
14
  var ramda = require('ramda');
16
15
  var Tooltip = require('@bigbinary/neetoui/Tooltip');
17
16
  var Typography = require('@bigbinary/neetoui/Typography');
18
17
  var jsxRuntime = require('react/jsx-runtime');
19
- var reactQuery = require('@tanstack/react-query');
20
- var axios = require('axios');
21
- var withImmutableActions = require('@bigbinary/neeto-commons-frontend/react-utils/withImmutableActions');
22
- var zustand = require('zustand');
23
- var middleware = require('zustand/middleware');
18
+ var _asyncToGenerator = require('@babel/runtime/helpers/asyncToGenerator');
19
+ var _regeneratorRuntime = require('@babel/runtime/regenerator');
24
20
  var i18next = require('i18next');
21
+ var utils = require('@bigbinary/neeto-commons-frontend/utils');
25
22
  var yup = require('yup');
23
+ var axios = require('axios');
26
24
  var injectCss = require('./inject-css-vQvjPR2x.js');
27
25
 
28
26
  function _interopNamespaceDefault(e) {
@@ -44,6 +42,9 @@ function _interopNamespaceDefault(e) {
44
42
 
45
43
  var yup__namespace = /*#__PURE__*/_interopNamespaceDefault(yup);
46
44
 
45
+ var IP_LOOKUP_ACCESS_TOKEN = "083c694c79f119c7f227e737de9a5c8c";
46
+ var IP_LOOKUP_STORAGE_KEY = "IP_LOOKUP_DETAILS_STORAGE";
47
+
47
48
  var index_es5_min = {exports: {}};
48
49
 
49
50
  /*! countries-list v2.6.1 by Annexare | MIT */
@@ -101,25 +102,17 @@ var SingleValue = function SingleValue(_ref) {
101
102
  };
102
103
  var SingleValue$1 = /*#__PURE__*/React.memo(SingleValue);
103
104
 
104
- var _excluded$1 = ["countryCode", "setCountryCode", "onInputChange", "prefix", "phoneNumber"];
105
+ var _excluded$1 = ["countryCode", "onChange"];
105
106
  function ownKeys$1(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
106
107
  function _objectSpread$1(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys$1(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys$1(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
107
108
  var CountryPicker = function CountryPicker(_ref) {
108
109
  var countryCode = _ref.countryCode,
109
- setCountryCode = _ref.setCountryCode,
110
- onInputChange = _ref.onInputChange,
111
- prefix = _ref.prefix,
112
- phoneNumber = _ref.phoneNumber,
110
+ _onChange = _ref.onChange,
113
111
  otherProps = _objectWithoutProperties(_ref, _excluded$1);
114
112
  var _useState = React.useState(""),
115
113
  _useState2 = _slicedToArray(_useState, 2),
116
114
  searchTerm = _useState2[0],
117
115
  setSearchTerm = _useState2[1];
118
- var handleCountryChange = function handleCountryChange(_ref2) {
119
- var value = _ref2.value;
120
- setCountryCode(value);
121
- onInputChange(phoneNumber.replace(prefix, "+".concat(min.getCountryCallingCode(value))));
122
- };
123
116
  return /*#__PURE__*/jsxRuntime.jsx("div", {
124
117
  className: classnames("flex-shrink-0", {
125
118
  "w-10": !searchTerm,
@@ -142,22 +135,22 @@ var CountryPicker = function CountryPicker(_ref) {
142
135
  });
143
136
  }
144
137
  },
145
- isOptionSelected: function isOptionSelected(_ref3, _ref4) {
146
- var value = _ref3.value;
147
- var _ref5 = _slicedToArray(_ref4, 1),
148
- selectedOption = _ref5[0];
138
+ isOptionSelected: function isOptionSelected(_ref2, _ref3) {
139
+ var value = _ref2.value;
140
+ var _ref4 = _slicedToArray(_ref3, 1),
141
+ selectedOption = _ref4[0];
149
142
  return (selectedOption === null || selectedOption === void 0 ? void 0 : selectedOption.name) === value;
150
143
  },
151
- onChange: handleCountryChange,
144
+ onChange: function onChange(option) {
145
+ return _onChange(option.value);
146
+ },
152
147
  onInputChange: setSearchTerm
153
148
  }, otherProps))
154
149
  });
155
150
  };
156
151
  var CountryPicker$1 = /*#__PURE__*/React.memo(CountryPicker);
157
152
 
158
- var IP_LOOKUP_ACCESS_TOKEN = "083c694c79f119c7f227e737de9a5c8c";
159
-
160
- var fetchLocationDetails = function fetchLocationDetails() {
153
+ var fetchLocationDetails$1 = function fetchLocationDetails() {
161
154
  return axios.get("https://neeto-ip-lookup-web-production.neetodeployapp.com/location", {
162
155
  headers: {
163
156
  "X-ACCESS-TOKEN": IP_LOOKUP_ACCESS_TOKEN
@@ -165,26 +158,9 @@ var fetchLocationDetails = function fetchLocationDetails() {
165
158
  });
166
159
  };
167
160
  var neetoIpLookupApi = {
168
- fetchLocationDetails: fetchLocationDetails
161
+ fetchLocationDetails: fetchLocationDetails$1
169
162
  };
170
163
 
171
- /** @type {import("neetocommons/react-utils").ZustandStoreHook} */
172
- var useCountryCodeStore = zustand.create(withImmutableActions(middleware.persist(function (set, _) {
173
- return {
174
- countryCode: null,
175
- setCountryCode: function setCountryCode(countryCode) {
176
- return set({
177
- countryCode: countryCode
178
- });
179
- }
180
- };
181
- }, {
182
- name: "neeto-ip-location-country-code",
183
- storage: middleware.createJSONStorage(function () {
184
- return localStorage;
185
- })
186
- })));
187
-
188
164
  var isPhoneNumberValid = function isPhoneNumberValid() {
189
165
  var value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
190
166
  for (var _len = arguments.length, rest = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
@@ -214,66 +190,48 @@ var getPhoneData = function getPhoneData(value, defaultCountryCode) {
214
190
  };
215
191
  }
216
192
  };
217
- var getCursorPosition = function getCursorPosition(_ref) {
218
- var event = _ref.event,
219
- prevFormattedValue = _ref.prevFormattedValue,
220
- newFormattedValue = _ref.newFormattedValue;
221
- var inputType = event.nativeEvent.inputType,
222
- _event$target = event.target,
223
- newValue = _event$target.value,
224
- selectionStart = _event$target.selectionStart;
225
- if (selectionStart === newValue.length) return newFormattedValue.length;
226
- var n = prevFormattedValue.substring(0, selectionStart).replace(/\s/g, "").length;
227
- var cursor = 0;
228
- for (var i = 0; i < n; ++i, ++cursor) {
229
- if (newFormattedValue[cursor] === " ") ++cursor;
230
- }
231
- if (inputType !== "deleteContentBackward") {
232
- if (newFormattedValue[selectionStart - 1] === " " && prevFormattedValue[selectionStart - 1] === " ") {
233
- return cursor + 2;
234
- } else if (prevFormattedValue[selectionStart - 1] === " ") {
235
- return cursor + 1;
236
- }
237
- }
238
- return cursor;
193
+ var fetchLocationDetails = /*#__PURE__*/function () {
194
+ var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
195
+ var stored, details;
196
+ return _regeneratorRuntime.wrap(function _callee$(_context) {
197
+ while (1) switch (_context.prev = _context.next) {
198
+ case 0:
199
+ stored = utils.getFromLocalStorage(IP_LOOKUP_STORAGE_KEY);
200
+ if (!stored) {
201
+ _context.next = 3;
202
+ break;
203
+ }
204
+ return _context.abrupt("return", stored);
205
+ case 3:
206
+ _context.next = 5;
207
+ return neetoIpLookupApi.fetchLocationDetails();
208
+ case 5:
209
+ details = _context.sent;
210
+ utils.setToLocalStorage(IP_LOOKUP_STORAGE_KEY, details);
211
+ return _context.abrupt("return", details);
212
+ case 8:
213
+ case "end":
214
+ return _context.stop();
215
+ }
216
+ }, _callee);
217
+ }));
218
+ return function fetchLocationDetails() {
219
+ return _ref.apply(this, arguments);
220
+ };
221
+ }();
222
+ var isNumeric = function isNumeric(_char) {
223
+ return _char >= "0" && _char <= "9";
239
224
  };
240
-
241
- var useSetInitialCountry = function useSetInitialCountry(initialCountryCode, initialValue) {
242
- var _useCountryCodeStore = useCountryCodeStore(function (store) {
243
- return {
244
- countryCode: store["countryCode"],
245
- setCountryCode: store["setCountryCode"]
246
- };
247
- }, shallow.shallow),
248
- countryCode = _useCountryCodeStore.countryCode,
249
- setCountryCode = _useCountryCodeStore.setCountryCode;
250
- var _getPhoneData = getPhoneData(initialValue),
251
- countryCodeFromInitialValue = _getPhoneData.countryCode;
252
- var isFetchLocationEnabled = !initialCountryCode && !countryCodeFromInitialValue;
253
- var _useQuery = reactQuery.useQuery({
254
- queryKey: ["IP_LOOKUP_LOCATION"],
255
- queryFn: function queryFn() {
256
- return neetoIpLookupApi.fetchLocationDetails();
257
- },
258
- enabled: isFetchLocationEnabled
259
- }),
260
- data = _useQuery.data,
261
- isLoading = _useQuery.isLoading;
262
- React.useEffect(function () {
263
- if (isFetchLocationEnabled) return;
264
- setCountryCode(countryCodeFromInitialValue || initialCountryCode);
265
- }, []);
266
- React.useEffect(function () {
267
- if (isLoading || !isFetchLocationEnabled) return;
268
- var newCountryCode = neetoCist.findBy({
269
- value: data === null || data === void 0 ? void 0 : data.countryCode
270
- }, COUNTRY_PICKER_OPTIONS) ? data === null || data === void 0 ? void 0 : data.countryCode : countryCode || "US";
271
-
272
- // Only update state if the new country code is different from the current one
273
- if (newCountryCode !== countryCode) {
274
- setCountryCode(newCountryCode);
275
- }
276
- }, [data, isLoading, isFetchLocationEnabled, countryCode]);
225
+ var preserveCursor = function preserveCursor(callback) {
226
+ return function (event) {
227
+ var start = event.target.selectionStart;
228
+ var end = event.target.selectionEnd;
229
+ var len = event.target.value.length;
230
+ var result = callback(event);
231
+ var delta = event.target.value.length - len;
232
+ event.target.setSelectionRange(start + delta, end + delta);
233
+ return result;
234
+ };
277
235
  };
278
236
 
279
237
  var _excluded = ["value", "onChange", "className", "error", "selectProps", "initialCountryCode"];
@@ -289,65 +247,63 @@ var PhoneNumberInput = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
289
247
  selectProps = _ref.selectProps,
290
248
  initialCountryCode = _ref.initialCountryCode,
291
249
  inputProps = _objectWithoutProperties(_ref, _excluded);
292
- var _useTranslation = reactI18next.useTranslation(),
293
- t = _useTranslation.t;
294
- var internalValue = React.useRef(value);
295
- var initialValue = React.useMemo(function () {
296
- return value;
297
- }, []);
298
- var _useCountryCodeStore = useCountryCodeStore(function (store) {
299
- return {
300
- countryCode: store["countryCode"],
301
- setCountryCode: store["setCountryCode"]
302
- };
303
- }, shallow.shallow),
304
- countryCode = _useCountryCodeStore.countryCode,
305
- setCountryCode = _useCountryCodeStore.setCountryCode;
306
- useSetInitialCountry(initialCountryCode, initialValue);
307
- var prefix = countryCode ? "+".concat(min.getCountryCallingCode(countryCode)) : "";
308
- var formattedValue = min.formatIncompletePhoneNumber(value, countryCode);
309
- var handleInputChange = React.useCallback(function (value) {
310
- onChange(value);
311
- internalValue.current = value;
312
- }, []);
313
- var handleChange = function handleChange(event) {
314
- var _getPhoneData = getPhoneData(event.target.value, countryCode),
315
- phoneNumber = _getPhoneData.phoneNumber,
316
- newCountryCode = _getPhoneData.countryCode;
317
- handleInputChange(phoneNumber);
318
- setCountryCode(newCountryCode);
319
- var cursor = getCursorPosition({
320
- event: event,
321
- prevFormattedValue: formattedValue,
322
- newFormattedValue: min.formatIncompletePhoneNumber(phoneNumber, newCountryCode)
323
- });
324
- window.requestAnimationFrame(function () {
325
- return event.target.setSelectionRange(cursor, cursor);
326
- });
250
+ var _useState = React.useState(function () {
251
+ return getPhoneData(value).countryCode || initialCountryCode;
252
+ }),
253
+ _useState2 = _slicedToArray(_useState, 2),
254
+ countryCode = _useState2[0],
255
+ setCountryCode = _useState2[1];
256
+ var _useQuery = reactQuery.useQuery({
257
+ queryKey: [IP_LOOKUP_STORAGE_KEY],
258
+ queryFn: fetchLocationDetails,
259
+ enabled: !countryCode,
260
+ staleTime: Infinity,
261
+ gcTime: Infinity,
262
+ select: function select(data) {
263
+ return neetoCist.findBy({
264
+ value: data.countryCode
265
+ }, COUNTRY_PICKER_OPTIONS);
266
+ }
267
+ }),
268
+ ipCountry = _useQuery.data;
269
+ var finalCountryCode = countryCode || (ipCountry === null || ipCountry === void 0 ? void 0 : ipCountry.value) || "US";
270
+ var formattedValue = min.formatIncompletePhoneNumber(value, finalCountryCode);
271
+ var onKeyDown = preserveCursor(function (event) {
272
+ // Handles special characters in the phone number formatting like brackets and spaces.
273
+ if (event.key !== "Backspace") return;
274
+ var left = event.target.value.substring(0, event.target.selectionStart);
275
+ var right = event.target.value.substring(event.target.selectionEnd);
276
+ var index = left.length - 1;
277
+ while (index > 0 && !isNumeric(left[index])) index--;
278
+ event.target.value = left.substring(0, index + 1) + right;
279
+ });
280
+ var onInputChange = preserveCursor(function (event) {
281
+ var phoneData = getPhoneData(event.target.value, finalCountryCode);
282
+ event.target.value = min.formatIncompletePhoneNumber(event.target.value);
283
+ setCountryCode(phoneData.countryCode);
284
+ onChange(phoneData.phoneNumber);
285
+ });
286
+ var onChangeCountry = function onChangeCountry(countryCode) {
287
+ var phoneData = getPhoneData(value, finalCountryCode);
288
+ var prevPrefix = "+".concat(min.getCountryCallingCode(phoneData.countryCode));
289
+ var nextPrefix = "+".concat(min.getCountryCallingCode(countryCode));
290
+ setCountryCode(countryCode);
291
+ onChange(nextPrefix + value.replace(prevPrefix, ""));
327
292
  };
328
- React.useEffect(function () {
329
- if (internalValue.current === value) return;
330
- setCountryCode(getPhoneData(value, countryCode).countryCode);
331
- }, [value]);
332
293
  return /*#__PURE__*/jsxRuntime.jsx("div", {
333
- className: classnames([className]),
294
+ className: className,
334
295
  children: /*#__PURE__*/jsxRuntime.jsx(Input, _objectSpread({
335
296
  error: error,
297
+ onKeyDown: onKeyDown,
336
298
  ref: ref,
337
299
  "data-testid": "phonenumber-input",
338
300
  type: "tel",
339
301
  value: formattedValue,
340
- placeholder: t("neetoMolecules.phoneNumberInput.placeholder", {
341
- prefix: prefix
342
- }),
343
302
  prefix: /*#__PURE__*/jsxRuntime.jsx(CountryPicker$1, _objectSpread({
344
- countryCode: countryCode,
345
- prefix: prefix,
346
- setCountryCode: setCountryCode,
347
- phoneNumber: value,
348
- onInputChange: handleInputChange
303
+ countryCode: finalCountryCode,
304
+ onChange: onChangeCountry
349
305
  }, selectProps)),
350
- onChange: handleChange
306
+ onChange: onInputChange
351
307
  }, inputProps))
352
308
  });
353
309
  });
@@ -361,4 +317,4 @@ exports.SingleValue = SingleValue$1;
361
317
  exports.getPhoneData = getPhoneData;
362
318
  exports.isPhoneNumberValid = isPhoneNumberValid;
363
319
  exports.validation = validation;
364
- //# sourceMappingURL=phone-number-BtEbUAse.js.map
320
+ //# sourceMappingURL=phone-number-CPwIzezl.js.map