UNPKG

4.59 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports["default"] = void 0;
7
8function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
9
10function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
11
12function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
13
14function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
15
16var AsYouTypeState =
17/*#__PURE__*/
18function () {
19 function AsYouTypeState(_ref) {
20 var _this = this;
21
22 var onCountryChange = _ref.onCountryChange,
23 onCallingCodeChange = _ref.onCallingCodeChange;
24
25 _classCallCheck(this, AsYouTypeState);
26
27 _defineProperty(this, "update", function (properties) {
28 for (var _i = 0, _Object$keys = Object.keys(properties); _i < _Object$keys.length; _i++) {
29 var key = _Object$keys[_i];
30 _this[key] = properties[key];
31 }
32 });
33
34 this.onCountryChange = onCountryChange;
35 this.onCallingCodeChange = onCallingCodeChange;
36 }
37
38 _createClass(AsYouTypeState, [{
39 key: "reset",
40 value: function reset(defaultCountry, defaultCallingCode) {
41 this.international = false;
42 this.IDDPrefix = undefined;
43 this.missingPlus = undefined;
44 this.callingCode = undefined;
45 this.digits = '';
46 this.resetNationalSignificantNumber();
47 this.initCountryAndCallingCode(defaultCountry, defaultCallingCode);
48 }
49 }, {
50 key: "resetNationalSignificantNumber",
51 value: function resetNationalSignificantNumber() {
52 this.nationalSignificantNumber = this.getNationalDigits();
53 this.nationalSignificantNumberMatchesInput = true;
54 this.nationalPrefix = undefined;
55 this.carrierCode = undefined;
56 this.complexPrefixBeforeNationalSignificantNumber = undefined;
57 }
58 }, {
59 key: "initCountryAndCallingCode",
60 value: function initCountryAndCallingCode(country, callingCode) {
61 this.setCountry(country);
62 this.setCallingCode(callingCode);
63 }
64 }, {
65 key: "setCountry",
66 value: function setCountry(country) {
67 this.country = country;
68 this.onCountryChange(country);
69 }
70 }, {
71 key: "setCallingCode",
72 value: function setCallingCode(callingCode) {
73 this.callingCode = callingCode;
74 return this.onCallingCodeChange(this.country, callingCode);
75 }
76 }, {
77 key: "startInternationalNumber",
78 value: function startInternationalNumber() {
79 // Prepend the `+` to parsed input.
80 this.international = true; // If a default country was set then reset it
81 // because an explicitly international phone
82 // number is being entered.
83
84 this.initCountryAndCallingCode();
85 }
86 }, {
87 key: "appendDigits",
88 value: function appendDigits(nextDigits) {
89 this.digits += nextDigits;
90 }
91 }, {
92 key: "appendNationalSignificantNumberDigits",
93 value: function appendNationalSignificantNumberDigits(nextDigits) {
94 this.nationalSignificantNumber += nextDigits;
95 }
96 /**
97 * Returns the part of `this.digits` that corresponds to the national number.
98 * Basically, all digits that have been input by the user, except for the
99 * international prefix and the country calling code part
100 * (if the number is an international one).
101 * @return {string}
102 */
103
104 }, {
105 key: "getNationalDigits",
106 value: function getNationalDigits() {
107 if (this.international) {
108 return this.digits.slice((this.IDDPrefix ? this.IDDPrefix.length : 0) + (this.callingCode ? this.callingCode.length : 0));
109 }
110
111 return this.digits;
112 }
113 }, {
114 key: "getDigitsWithoutInternationalPrefix",
115 value: function getDigitsWithoutInternationalPrefix() {
116 if (this.international) {
117 if (this.IDDPrefix) {
118 return this.digits.slice(this.IDDPrefix.length);
119 }
120 }
121
122 return this.digits;
123 }
124 }]);
125
126 return AsYouTypeState;
127}();
128
129exports["default"] = AsYouTypeState;
130//# sourceMappingURL=AsYouTypeState.js.map
\No newline at end of file