allaw-ui 4.3.2 → 4.3.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.
|
@@ -12,24 +12,26 @@ var __assign = (this && this.__assign) || function () {
|
|
|
12
12
|
import React, { useState, useRef, useEffect } from "react";
|
|
13
13
|
import styles from "./PhoneInput.module.css";
|
|
14
14
|
var formatPhone = function (value) {
|
|
15
|
-
// Garde uniquement les chiffres
|
|
15
|
+
// Garde uniquement les chiffres
|
|
16
16
|
var digits = value.replace(/\D/g, "");
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
17
|
+
if (digits.startsWith("0")) {
|
|
18
|
+
// Espace tous les 2 chiffres (06 11 22 33 44)
|
|
19
|
+
return digits.replace(/(\d{2})(?=\d)/g, "$1 ").trim();
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
// Espace tous les 2 chiffres à partir du premier chiffre (6 11 22 33 44)
|
|
23
|
+
if (digits.length === 0)
|
|
24
|
+
return "";
|
|
25
|
+
var res = digits[0];
|
|
26
|
+
var i = 1;
|
|
27
|
+
while (i < digits.length) {
|
|
28
|
+
res += (i % 2 === 1 ? " " : "") + digits[i];
|
|
29
|
+
i++;
|
|
30
|
+
}
|
|
31
|
+
return res.trim();
|
|
29
32
|
}
|
|
30
|
-
res = digits.slice(0, i) + res;
|
|
31
|
-
return res.trim();
|
|
32
33
|
};
|
|
34
|
+
var formatPhoneEnd = formatPhone;
|
|
33
35
|
var PhoneInput = function (_a) {
|
|
34
36
|
var _b = _a.defaultValue, defaultValue = _b === void 0 ? "" : _b, placeholder = _a.placeholder, isRequired = _a.isRequired, disabled = _a.disabled, validationPattern = _a.validationPattern, onChange = _a.onChange, onError = _a.onError, _c = _a.disableAutofill, disableAutofill = _c === void 0 ? true : _c, onBlur = _a.onBlur, isValid = _a.isValid;
|
|
35
37
|
var _d = useState(formatPhone(defaultValue)), value = _d[0], setValue = _d[1];
|
|
@@ -73,16 +75,13 @@ var PhoneInput = function (_a) {
|
|
|
73
75
|
var handleBlur = function () {
|
|
74
76
|
setIsTouched(true);
|
|
75
77
|
var raw = value.replace(/\s/g, "");
|
|
76
|
-
|
|
78
|
+
var formatted = raw;
|
|
77
79
|
if (raw.startsWith("0")) {
|
|
78
|
-
|
|
79
|
-
setValue(formatPhoneEnd(raw));
|
|
80
|
-
onChange === null || onChange === void 0 ? void 0 : onChange(raw);
|
|
81
|
-
}
|
|
82
|
-
else {
|
|
83
|
-
setValue(formatPhoneEnd(raw));
|
|
80
|
+
formatted = raw.slice(1);
|
|
84
81
|
}
|
|
85
|
-
|
|
82
|
+
setValue(formatPhoneEnd(formatted));
|
|
83
|
+
onChange === null || onChange === void 0 ? void 0 : onChange(formatted);
|
|
84
|
+
validate(formatted);
|
|
86
85
|
if (typeof onBlur === "function")
|
|
87
86
|
onBlur();
|
|
88
87
|
};
|
|
@@ -13,6 +13,9 @@ var PhoneNumberField = function (_a) {
|
|
|
13
13
|
var _f = useState(false), isValid = _f[0], setIsValid = _f[1];
|
|
14
14
|
var selectedCountryObj = countryItems.find(function (c) { return c.value === country; });
|
|
15
15
|
var effectivePattern = (selectedCountryObj === null || selectedCountryObj === void 0 ? void 0 : selectedCountryObj.regex) || validationPattern;
|
|
16
|
+
if ((selectedCountryObj === null || selectedCountryObj === void 0 ? void 0 : selectedCountryObj.value) === "fr") {
|
|
17
|
+
effectivePattern = /^(0)?[1-9](?:\d{8})$/;
|
|
18
|
+
}
|
|
16
19
|
var handleCountry = function (c) {
|
|
17
20
|
setCountry(c);
|
|
18
21
|
if (onChange)
|
|
@@ -22,11 +25,15 @@ var PhoneNumberField = function (_a) {
|
|
|
22
25
|
var handleNumber = function (n) {
|
|
23
26
|
setNumber(n);
|
|
24
27
|
var valid = false;
|
|
28
|
+
var testValue = n.replace(/\s/g, "");
|
|
29
|
+
if ((selectedCountryObj === null || selectedCountryObj === void 0 ? void 0 : selectedCountryObj.value) === "fr" && testValue.startsWith("0")) {
|
|
30
|
+
testValue = testValue.slice(1);
|
|
31
|
+
}
|
|
25
32
|
if (effectivePattern) {
|
|
26
33
|
var regex = typeof effectivePattern === "string"
|
|
27
34
|
? new RegExp(effectivePattern)
|
|
28
35
|
: effectivePattern;
|
|
29
|
-
valid = regex.test(
|
|
36
|
+
valid = regex.test(testValue);
|
|
30
37
|
}
|
|
31
38
|
setIsValid(valid);
|
|
32
39
|
if (onChange)
|
|
@@ -49,7 +56,7 @@ var PhoneNumberField = function (_a) {
|
|
|
49
56
|
React.createElement("div", { className: styles.selectWrapper },
|
|
50
57
|
React.createElement(CountrySelect, { items: countryItems, defaultCountry: defaultCountry, buttonWidth: selectButtonWidth, listWidth: selectListWidth, isRequired: isRequired, disabled: disabled, onSelect: handleCountry, onError: setErrorMsg })),
|
|
51
58
|
React.createElement("div", { className: styles.inputWrapper },
|
|
52
|
-
React.createElement(PhoneInput, { defaultValue: defaultNumber, placeholder: numberPlaceholder, validationPattern: effectivePattern
|
|
59
|
+
React.createElement(PhoneInput, { defaultValue: defaultNumber, placeholder: numberPlaceholder, validationPattern: effectivePattern, isRequired: isRequired, disabled: disabled, onChange: handleNumber, onError: setErrorMsg, onBlur: handleInputBlur, isValid: isValid }),
|
|
53
60
|
isTouched && !isValid && error && (React.createElement("div", { className: styles.errorMessage },
|
|
54
61
|
React.createElement(TinyInfo, { variant: "medium12", color: "actions-error", text: error })))))));
|
|
55
62
|
};
|