ui-soxo-bootstrap-core 2.6.1-dev.3 → 2.6.1

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.
@@ -6,48 +6,79 @@
6
6
  * Ensure to follow a minimal standard
7
7
  */
8
8
  import React, { useState, useEffect, useRef } from 'react';
9
+
9
10
  import PhoneInput from 'react-phone-input-2';
11
+
10
12
  import 'react-phone-input-2/lib/style.css';
11
- import PropTypes from 'prop-types';
13
+
14
+ import PropTypes from "prop-types";
15
+
12
16
  import './phone-input.scss';
13
17
 
14
- export default function CountryPhoneInput({ value, onChange, disabled, disableCountryCode, required, defaultCountryCode, onBlur,className }) {
15
- const inputRef = useRef();
16
- const hasInitializedRef = useRef(false); // IMPORTANT
17
18
 
18
- const [phone, setPhone] = useState('');
19
+ export default function CountryPhoneInput({ value, onChange, disabled, disableCountryCode, required, defaultCountryCode, onBlur }) {
20
+
21
+ let inputRef = useRef();
22
+
23
+ const [phone, setPhone] = useState();
19
24
 
20
- /**
21
- * run ONLY ONCE when initial value has dial code
22
- */
23
25
  useEffect(() => {
24
- if (
25
- !hasInitializedRef.current &&
26
- value?.code?.dialCode &&
27
- value?.value
28
- ) {
26
+ if (value && value.code && value.code.dialCode !== null) {
27
+ // To prepopulate phone number ,concat dialCode and phone number
29
28
  setPhone(value.code.dialCode + value.value);
30
- hasInitializedRef.current = true; // 👈 mark as done
29
+
30
+ // In case of update , to identify the country code that is getting selected
31
+ // We are setting a timeout
32
+ setTimeout(() => {
33
+ // Using the reference , we get the internal state of the library component
34
+ let selectedCountry = null;
35
+
36
+ let selectedCode = null;
37
+
38
+ if (
39
+ inputRef.current &&
40
+ inputRef.current.state &&
41
+ inputRef.current.state.selectedCountry
42
+ ) {
43
+ selectedCountry = inputRef.current.state.selectedCountry;
44
+
45
+ // Once we get the selected country ,we format it to the form that is received for normal onChange
46
+ selectedCode = {
47
+ countryCode: selectedCountry.iso2,
48
+ dialCode: selectedCountry.dialCode,
49
+ name: selectedCountry.name,
50
+ };
51
+ }
52
+
53
+ // Below being the format expected , we trigger the onChange to manually update the form
54
+ let updatedValue = {
55
+ value: value.code.dialCode + value.value,
56
+ code: selectedCode,
57
+ };
58
+
59
+ onChange(updatedValue);
60
+ }, 0);
31
61
  }
32
- }, [value]);
62
+ }, []);
33
63
 
34
64
  /**
35
- * handle user input
65
+ * To get value in parent component
66
+ * @param {*} value
36
67
  */
37
- function handleInput(inputValue, code) {
38
- if (!code?.dialCode) return;
39
-
40
- const phoneNumber = inputValue.slice(code.dialCode.length);
41
-
42
- // remove autofill class when user edits field (if you use it)
43
- onChange({
44
- value: phoneNumber,
68
+ function handleInput(value, code) {
69
+ if (code.dialCode) {
70
+ value = value.substring(code.dialCode.length);
71
+ }
72
+ // To get country code with phone number in parent component
73
+ value = {
74
+ value,
45
75
  code,
46
- });
76
+ };
77
+ onChange(value);
47
78
  }
48
79
 
49
80
  return (
50
- <div className={`phone-input ${className || ''}`}>
81
+ <div className="phone-input">
51
82
  <PhoneInput
52
83
  ref={inputRef}
53
84
  country={defaultCountryCode || 'in'}
@@ -12,20 +12,6 @@
12
12
  // }
13
13
  }
14
14
 
15
- .autofilled-field {
16
- .react-tel-input {
17
- .form-control {
18
- border-color: #fa8c16 !important; // AntD warning orange
19
- background-color: #fff7e6;
20
- box-shadow: none;
21
- }
22
-
23
- .flag-dropdown {
24
- border-color: #fa8c16 !important;
25
- background-color: #fff7e6;
26
- }
27
- }
28
- }
29
15
  // /* For tablets and smaller screens (max-width: 1024px) */
30
16
  // @media (max-width: 1024px) {
31
17
  // .phone-input {