react-simple-phone-input 4.6.3 → 4.6.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.
- package/dist/cjs/index.js +86 -55
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/index.js +86 -55
- package/dist/esm/index.js.map +1 -1
- package/dist/{cjs/index.css → style.css} +0 -0
- package/package.json +1 -1
- package/dist/esm/index.css +0 -154
package/dist/cjs/index.js
CHANGED
|
@@ -2,6 +2,32 @@
|
|
|
2
2
|
|
|
3
3
|
var React = require('react');
|
|
4
4
|
|
|
5
|
+
/******************************************************************************
|
|
6
|
+
Copyright (c) Microsoft Corporation.
|
|
7
|
+
|
|
8
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
9
|
+
purpose with or without fee is hereby granted.
|
|
10
|
+
|
|
11
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
12
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
13
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
14
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
15
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
16
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
17
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
18
|
+
***************************************************************************** */
|
|
19
|
+
|
|
20
|
+
var __assign = function() {
|
|
21
|
+
__assign = Object.assign || function __assign(t) {
|
|
22
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
23
|
+
s = arguments[i];
|
|
24
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
|
25
|
+
}
|
|
26
|
+
return t;
|
|
27
|
+
};
|
|
28
|
+
return __assign.apply(this, arguments);
|
|
29
|
+
};
|
|
30
|
+
|
|
5
31
|
var jsxRuntime = {exports: {}};
|
|
6
32
|
|
|
7
33
|
var reactJsxRuntime_production_min = {};
|
|
@@ -1357,7 +1383,7 @@ function requireReactJsxRuntime_development () {
|
|
|
1357
1383
|
}
|
|
1358
1384
|
} (jsxRuntime));
|
|
1359
1385
|
|
|
1360
|
-
|
|
1386
|
+
var countryData = [
|
|
1361
1387
|
{
|
|
1362
1388
|
country: "Afghanistan",
|
|
1363
1389
|
countryCode: "AF",
|
|
@@ -2541,50 +2567,52 @@ const countryData = [
|
|
|
2541
2567
|
];
|
|
2542
2568
|
|
|
2543
2569
|
//Get default country function
|
|
2544
|
-
|
|
2545
|
-
|
|
2570
|
+
var getDefaultCountry = function (code) {
|
|
2571
|
+
var result = countryData.find(function (item) { return item.countryCode === code; });
|
|
2546
2572
|
return result;
|
|
2547
2573
|
};
|
|
2548
2574
|
//Get country data by search
|
|
2549
|
-
|
|
2550
|
-
|
|
2575
|
+
var getBySearch = function (search, onlyCountry, excludeCountry) {
|
|
2576
|
+
var countries = [];
|
|
2551
2577
|
if (excludeCountry && excludeCountry.length > 0) {
|
|
2552
|
-
countries = countryData.filter((item)
|
|
2578
|
+
countries = countryData.filter(function (item) { return !(excludeCountry === null || excludeCountry === void 0 ? void 0 : excludeCountry.includes(item.countryCode)); });
|
|
2553
2579
|
}
|
|
2554
|
-
else if (onlyCountry && onlyCountry
|
|
2555
|
-
countries = countryData.filter((item)
|
|
2580
|
+
else if (onlyCountry && (onlyCountry === null || onlyCountry === void 0 ? void 0 : onlyCountry.length) > 0) {
|
|
2581
|
+
countries = countryData.filter(function (item) { return onlyCountry === null || onlyCountry === void 0 ? void 0 : onlyCountry.includes(item.countryCode); });
|
|
2556
2582
|
}
|
|
2557
2583
|
else {
|
|
2558
2584
|
countries = countryData;
|
|
2559
2585
|
}
|
|
2560
|
-
|
|
2561
|
-
|
|
2562
|
-
|
|
2586
|
+
var result = countries.filter(function (item) {
|
|
2587
|
+
var escapeRegExp = function (str) { return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); };
|
|
2588
|
+
var regex = new RegExp(escapeRegExp(search), "gi");
|
|
2563
2589
|
return item.country.match(regex);
|
|
2564
2590
|
});
|
|
2565
2591
|
return result;
|
|
2566
2592
|
};
|
|
2567
2593
|
//Get country data by filter
|
|
2568
|
-
|
|
2569
|
-
|
|
2594
|
+
var getCountryByFilter = function (onlyCountry, excludeCountry, preferredCountry) {
|
|
2595
|
+
var countries = [];
|
|
2570
2596
|
if (excludeCountry && excludeCountry.length > 0) {
|
|
2571
|
-
countries = countryData.filter((item)
|
|
2597
|
+
countries = countryData.filter(function (item) { return !(excludeCountry === null || excludeCountry === void 0 ? void 0 : excludeCountry.includes(item.countryCode)); });
|
|
2572
2598
|
}
|
|
2573
|
-
else if (onlyCountry && onlyCountry
|
|
2574
|
-
countries = countryData.filter((item)
|
|
2599
|
+
else if (onlyCountry && (onlyCountry === null || onlyCountry === void 0 ? void 0 : onlyCountry.length) > 0) {
|
|
2600
|
+
countries = countryData.filter(function (item) { return onlyCountry === null || onlyCountry === void 0 ? void 0 : onlyCountry.includes(item.countryCode); });
|
|
2575
2601
|
}
|
|
2576
2602
|
else {
|
|
2577
2603
|
countries = countryData;
|
|
2578
2604
|
}
|
|
2579
|
-
|
|
2580
|
-
|
|
2605
|
+
var result = countries.sort(function (a, b) {
|
|
2606
|
+
return (Number(preferredCountry === null || preferredCountry === void 0 ? void 0 : preferredCountry.includes(b.countryCode)) - Number(preferredCountry === null || preferredCountry === void 0 ? void 0 : preferredCountry.includes(a.countryCode)))
|
|
2607
|
+
|| ((preferredCountry === null || preferredCountry === void 0 ? void 0 : preferredCountry.indexOf(a.countryCode)) - (preferredCountry === null || preferredCountry === void 0 ? void 0 : preferredCountry.indexOf(b.countryCode)));
|
|
2608
|
+
});
|
|
2581
2609
|
return result;
|
|
2582
2610
|
};
|
|
2583
2611
|
|
|
2584
2612
|
//Click out side hook
|
|
2585
2613
|
function useOnClickOutside(ref, handler) {
|
|
2586
|
-
React.useEffect(()
|
|
2587
|
-
|
|
2614
|
+
React.useEffect(function () {
|
|
2615
|
+
var listener = function (event) {
|
|
2588
2616
|
if (!ref.current || ref.current.contains(event.target)) {
|
|
2589
2617
|
return;
|
|
2590
2618
|
}
|
|
@@ -2592,7 +2620,7 @@ function useOnClickOutside(ref, handler) {
|
|
|
2592
2620
|
};
|
|
2593
2621
|
document.addEventListener("mousedown", listener);
|
|
2594
2622
|
document.addEventListener("touchstart", listener);
|
|
2595
|
-
return ()
|
|
2623
|
+
return function () {
|
|
2596
2624
|
document.removeEventListener("mousedown", listener);
|
|
2597
2625
|
document.removeEventListener("touchstart", listener);
|
|
2598
2626
|
};
|
|
@@ -2600,22 +2628,23 @@ function useOnClickOutside(ref, handler) {
|
|
|
2600
2628
|
}
|
|
2601
2629
|
|
|
2602
2630
|
//Component
|
|
2603
|
-
|
|
2631
|
+
var PhoneInput = function (_a) {
|
|
2632
|
+
var placeholder = _a.placeholder, country = _a.country, onChange = _a.onChange, value = _a.value, iconComponent = _a.iconComponent, inputProps = _a.inputProps, onlyCountries = _a.onlyCountries, excludeCountries = _a.excludeCountries, preferredCountries = _a.preferredCountries, _b = _a.showDropdownIcon, showDropdownIcon = _b === void 0 ? true : _b, _c = _a.dialCodeInputField, dialCodeInputField = _c === void 0 ? false : _c, _d = _a.search, search = _d === void 0 ? true : _d, _e = _a.searchPlaceholder, searchPlaceholder = _e === void 0 ? "Search country" : _e, _f = _a.showSearchIcon, showSearchIcon = _f === void 0 ? true : _f, searchIconComponent = _a.searchIconComponent, searchProps = _a.searchProps, _g = _a.searchNotFound, searchNotFound = _g === void 0 ? "Not found" : _g;
|
|
2604
2633
|
//State
|
|
2605
|
-
|
|
2606
|
-
|
|
2607
|
-
|
|
2608
|
-
|
|
2609
|
-
|
|
2634
|
+
var _h = React.useState({}), selected = _h[0], setSelected = _h[1];
|
|
2635
|
+
var _j = React.useState(false), isDropdown = _j[0], setDropdown = _j[1];
|
|
2636
|
+
var _k = React.useState(value || ""), inputValue = _k[0], setInputValue = _k[1];
|
|
2637
|
+
var _l = React.useState(countryData), countryDataInfo = _l[0], setCountryData = _l[1];
|
|
2638
|
+
var _m = React.useState(0), cursor = _m[0], setCursor = _m[1];
|
|
2610
2639
|
//Ref
|
|
2611
|
-
|
|
2612
|
-
|
|
2640
|
+
var dropdownRef = React.useRef(null);
|
|
2641
|
+
var listRef = React.useRef(null);
|
|
2613
2642
|
//Custom Hook Call
|
|
2614
|
-
useOnClickOutside(dropdownRef, ()
|
|
2643
|
+
useOnClickOutside(dropdownRef, function () { return setDropdown(false); });
|
|
2615
2644
|
//Handler
|
|
2616
2645
|
//---Input Onchange Handler//
|
|
2617
|
-
|
|
2618
|
-
|
|
2646
|
+
var handleChange = function (e) {
|
|
2647
|
+
var onlyNumber = e.target.value.replace(/\D/g, '');
|
|
2619
2648
|
if (dialCodeInputField) {
|
|
2620
2649
|
onChange("+" + onlyNumber);
|
|
2621
2650
|
setInputValue("+" + onlyNumber);
|
|
@@ -2626,14 +2655,14 @@ const PhoneInput = ({ placeholder, country, onChange, value, iconComponent, inpu
|
|
|
2626
2655
|
}
|
|
2627
2656
|
};
|
|
2628
2657
|
//---Search Input Onchange Handler//
|
|
2629
|
-
|
|
2630
|
-
|
|
2658
|
+
var onSearchHandler = function (e) {
|
|
2659
|
+
var search = e.target.value;
|
|
2631
2660
|
setCountryData(getBySearch(search, onlyCountries, excludeCountries));
|
|
2632
2661
|
};
|
|
2633
2662
|
//---Set Selected Handler//
|
|
2634
|
-
|
|
2663
|
+
var handleSelected = function (item, i) {
|
|
2635
2664
|
if (dialCodeInputField) {
|
|
2636
|
-
|
|
2665
|
+
var result = inputValue === null || inputValue === void 0 ? void 0 : inputValue.replace(selected.callingCode, item.callingCode);
|
|
2637
2666
|
setInputValue(result.length > 0 ? result : item.callingCode);
|
|
2638
2667
|
onChange(result.length > 0 ? result : item.callingCode);
|
|
2639
2668
|
}
|
|
@@ -2645,10 +2674,11 @@ const PhoneInput = ({ placeholder, country, onChange, value, iconComponent, inpu
|
|
|
2645
2674
|
setCursor(i);
|
|
2646
2675
|
};
|
|
2647
2676
|
//---Scroll To View Handler//
|
|
2648
|
-
|
|
2677
|
+
var scrollIntoView = function (position) {
|
|
2678
|
+
var _a;
|
|
2649
2679
|
if (search) {
|
|
2650
2680
|
if (countryDataInfo.length > 0) {
|
|
2651
|
-
listRef.current
|
|
2681
|
+
(_a = listRef.current) === null || _a === void 0 ? void 0 : _a.scrollTo({
|
|
2652
2682
|
top: position,
|
|
2653
2683
|
behavior: "smooth"
|
|
2654
2684
|
});
|
|
@@ -2656,15 +2686,15 @@ const PhoneInput = ({ placeholder, country, onChange, value, iconComponent, inpu
|
|
|
2656
2686
|
}
|
|
2657
2687
|
};
|
|
2658
2688
|
//---Keyboard Navigation//
|
|
2659
|
-
|
|
2689
|
+
var keyBoardNav = function (e) {
|
|
2660
2690
|
if (isDropdown) {
|
|
2661
2691
|
if (e.key === "ArrowDown") {
|
|
2662
2692
|
e.preventDefault();
|
|
2663
|
-
setCursor(c
|
|
2693
|
+
setCursor(function (c) { return (c < countryDataInfo.length - 1 ? c + 1 : c); });
|
|
2664
2694
|
}
|
|
2665
2695
|
if (e.key === "ArrowUp") {
|
|
2666
2696
|
e.preventDefault();
|
|
2667
|
-
setCursor(c
|
|
2697
|
+
setCursor(function (c) { return (c > 0 ? c - 1 : 0); });
|
|
2668
2698
|
}
|
|
2669
2699
|
if (e.key === "Escape") {
|
|
2670
2700
|
setDropdown(false);
|
|
@@ -2677,39 +2707,40 @@ const PhoneInput = ({ placeholder, country, onChange, value, iconComponent, inpu
|
|
|
2677
2707
|
};
|
|
2678
2708
|
//React Hook
|
|
2679
2709
|
//---Scroll to view hook//
|
|
2680
|
-
React.useEffect(()
|
|
2710
|
+
React.useEffect(function () {
|
|
2711
|
+
var _a;
|
|
2681
2712
|
if (cursor < 0 || cursor > countryDataInfo.length || !listRef) {
|
|
2682
|
-
return ()
|
|
2713
|
+
return function () {
|
|
2683
2714
|
};
|
|
2684
2715
|
}
|
|
2685
2716
|
if (isDropdown && listRef) {
|
|
2686
2717
|
if (countryDataInfo.length > 0) {
|
|
2687
|
-
|
|
2718
|
+
var listItems = Array.from((_a = listRef.current) === null || _a === void 0 ? void 0 : _a.children);
|
|
2688
2719
|
listItems[cursor] && scrollIntoView(listItems[cursor].offsetTop - 65);
|
|
2689
2720
|
}
|
|
2690
2721
|
}
|
|
2691
2722
|
}, [cursor]);
|
|
2692
2723
|
//---Dial Code Field Effect//
|
|
2693
|
-
React.useMemo(()
|
|
2724
|
+
React.useMemo(function () {
|
|
2694
2725
|
if (dialCodeInputField) {
|
|
2695
|
-
|
|
2726
|
+
var result = inputValue === null || inputValue === void 0 ? void 0 : inputValue.replace(selected.callingCode, getDefaultCountry(country).callingCode);
|
|
2696
2727
|
console.log(result);
|
|
2697
2728
|
setInputValue(result.length > 0 ? result : getDefaultCountry(country).callingCode);
|
|
2698
2729
|
}
|
|
2699
2730
|
setSelected(getDefaultCountry(country));
|
|
2700
2731
|
}, [country, dialCodeInputField]);
|
|
2701
2732
|
//---Country search hook//
|
|
2702
|
-
React.useMemo(()
|
|
2733
|
+
React.useMemo(function () {
|
|
2703
2734
|
setCountryData(getCountryByFilter(onlyCountries, excludeCountries, preferredCountries));
|
|
2704
2735
|
}, [onlyCountries, excludeCountries, preferredCountries]);
|
|
2705
|
-
return (jsxRuntime.exports.jsx("div", { className: "simple-phone-input-sri198-container", onKeyDown: (e)
|
|
2706
|
-
jsxRuntime.exports.jsx("span", { className: "simple-phone-input-sri198-selected-code", children: selected.callingCode }), showDropdownIcon &&
|
|
2707
|
-
jsxRuntime.exports.jsx("div", { className: isDropdown ? "simple-phone-input-sri198-dropdown-icon" : "simple-phone-input-sri198-dropdown-icon active", children: iconComponent ? iconComponent : (jsxRuntime.exports.jsx("svg", { xmlns: "http://www.w3.org/2000/svg", width: "1em", height: "1em", preserveAspectRatio: "xMidYMid meet", viewBox: "0 0 24 24", children: jsxRuntime.exports.jsx("path", { fill: "currentColor", d: "m7 10l5 5l5-5z" }) })) })] }), jsxRuntime.exports.jsxs("ul", { className: isDropdown ? "simple-phone-input-sri198-dropdown active" : "simple-phone-input-sri198-dropdown", ref: listRef, children: [search &&
|
|
2708
|
-
jsxRuntime.exports.jsxs("div", { className: "simple-phone-input-sri198-search-container", children: [jsxRuntime.exports.jsx("input", { placeholder: searchPlaceholder,
|
|
2709
|
-
jsxRuntime.exports.jsx("div", { className: "simple-phone-input-sri198-search-icon", children: searchIconComponent
|
|
2710
|
-
jsxRuntime.exports.jsx("div", { className: "simple-phone-input-sri198-not-found", children: searchNotFound }), countryDataInfo.map((item, i)
|
|
2736
|
+
return (jsxRuntime.exports.jsx("div", __assign({ className: "simple-phone-input-sri198-container", onKeyDown: function (e) { return keyBoardNav(e); }, tabIndex: -1 }, { children: jsxRuntime.exports.jsxs("div", __assign({ className: "simple-phone-input-sri198-main" }, { children: [jsxRuntime.exports.jsxs("div", __assign({ className: "simple-phone-input-sri198-dropdown-container", ref: dropdownRef }, { children: [jsxRuntime.exports.jsxs("div", __assign({ onClick: function () { return setDropdown(!isDropdown); }, className: dialCodeInputField ? "simple-phone-input-sri198-dropdown-container-button dial" : "simple-phone-input-sri198-dropdown-container-button" }, { children: [jsxRuntime.exports.jsx("img", { src: "https://cdn.jsdelivr.net/gh/siamahnaf198/country-flags@main/img/" + selected.countryCode + ".svg", alt: selected.country, width: "20px" }), !dialCodeInputField &&
|
|
2737
|
+
jsxRuntime.exports.jsx("span", __assign({ className: "simple-phone-input-sri198-selected-code" }, { children: selected.callingCode })), showDropdownIcon &&
|
|
2738
|
+
jsxRuntime.exports.jsx("div", __assign({ className: isDropdown ? "simple-phone-input-sri198-dropdown-icon" : "simple-phone-input-sri198-dropdown-icon active" }, { children: iconComponent ? iconComponent : (jsxRuntime.exports.jsx("svg", __assign({ xmlns: "http://www.w3.org/2000/svg", width: "1em", height: "1em", preserveAspectRatio: "xMidYMid meet", viewBox: "0 0 24 24" }, { children: jsxRuntime.exports.jsx("path", { fill: "currentColor", d: "m7 10l5 5l5-5z" }) }))) }))] })), jsxRuntime.exports.jsxs("ul", __assign({ className: isDropdown ? "simple-phone-input-sri198-dropdown active" : "simple-phone-input-sri198-dropdown", ref: listRef }, { children: [search &&
|
|
2739
|
+
jsxRuntime.exports.jsxs("div", __assign({ className: "simple-phone-input-sri198-search-container" }, { children: [jsxRuntime.exports.jsx("input", __assign({ placeholder: searchPlaceholder }, searchProps, { onChange: onSearchHandler })), showSearchIcon &&
|
|
2740
|
+
jsxRuntime.exports.jsx("div", __assign({ className: "simple-phone-input-sri198-search-icon" }, { children: searchIconComponent !== null && searchIconComponent !== void 0 ? searchIconComponent : (jsxRuntime.exports.jsxs("svg", __assign({ xmlns: "http://www.w3.org/2000/svg", width: "1em", height: "1em", preserveAspectRatio: "xMidYMid meet", viewBox: "0 0 48 48" }, { children: [jsxRuntime.exports.jsxs("g", __assign({ fill: "#616161" }, { children: [jsxRuntime.exports.jsx("path", { d: "m29.175 31.99l2.828-2.827l12.019 12.019l-2.828 2.827z" }), jsxRuntime.exports.jsx("circle", { cx: "20", cy: "20", r: "16" })] })), jsxRuntime.exports.jsx("path", { fill: "#37474F", d: "m32.45 35.34l2.827-2.828l8.696 8.696l-2.828 2.828z" }), jsxRuntime.exports.jsx("circle", { cx: "20", cy: "20", r: "13", fill: "#64B5F6" }), jsxRuntime.exports.jsx("path", { fill: "#BBDEFB", d: "M26.9 14.2c-1.7-2-4.2-3.2-6.9-3.2s-5.2 1.2-6.9 3.2c-.4.4-.3 1.1.1 1.4c.4.4 1.1.3 1.4-.1C16 13.9 17.9 13 20 13s4 .9 5.4 2.5c.2.2.5.4.8.4c.2 0 .5-.1.6-.2c.4-.4.4-1.1.1-1.5z" })] }))) }))] })), countryDataInfo.length === 0 &&
|
|
2741
|
+
jsxRuntime.exports.jsx("div", __assign({ className: "simple-phone-input-sri198-not-found" }, { children: searchNotFound })), countryDataInfo.map(function (item, i) { return (jsxRuntime.exports.jsxs("li", __assign({ onClick: function () { return handleSelected(item, i); }, className: i === cursor ? "active" : "" }, { children: [jsxRuntime.exports.jsx("img", { src: "https://cdn.jsdelivr.net/gh/siamahnaf198/country-flags@main/img/" + item.countryCode + ".svg", alt: item.country, width: "20px" }), jsxRuntime.exports.jsx("span", __assign({ className: "simple-phone-input-sri198-dropdown-text" }, { children: item.country })), jsxRuntime.exports.jsx("span", __assign({ className: "simple-phone-input-sri198-dropdown-country-code" }, { children: item.callingCode }))] }), i)); })] }))] })), jsxRuntime.exports.jsx("input", __assign({ className: "simple-phone-input-sri198-input", placeholder: placeholder, onChange: handleChange, type: "tel", onInput: function (e) {
|
|
2711
2742
|
if (dialCodeInputField) {
|
|
2712
|
-
|
|
2743
|
+
var oldVal = inputValue.slice(selected.callingCode.length);
|
|
2713
2744
|
if (e.target.value.startsWith(selected.callingCode)) {
|
|
2714
2745
|
e.target.value = e.target.value;
|
|
2715
2746
|
}
|
|
@@ -2717,7 +2748,7 @@ const PhoneInput = ({ placeholder, country, onChange, value, iconComponent, inpu
|
|
|
2717
2748
|
e.target.value = selected.callingCode + oldVal;
|
|
2718
2749
|
}
|
|
2719
2750
|
}
|
|
2720
|
-
}, onKeyDown: (e)
|
|
2751
|
+
}, onKeyDown: function (e) { return keyBoardNav(e); }, value: inputValue }, inputProps))] })) })));
|
|
2721
2752
|
};
|
|
2722
2753
|
|
|
2723
2754
|
exports.PhoneInput = PhoneInput;
|