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