JC_
JC_

Reputation: 577

Laravel 5.6: intl-tel-input compatibility issue with App.js

I have a Laravel 5.6 web page and I want to add IntlTelInput (https://github.com/jackocnr/intl-tel-input) but I have try to install it from source, composer and npm (currently using composer and mix to copy build folder) and the 3 of them seem to not initialize the plugin. Example

This is what I got from inspecting the result:

    <div class="intl-tel-input allow-dropdown">
       <div class="flag-container">
          <div tabindex="0" class="selected-flag">
             <div class="iti-flag"></div>
             <div class="iti-arrow"></div>
          </div>
          <ul class="country-list hide">
             <li data-dial-code="1" data-country-code="us" class="country preferred">
                <div class="flag-box">
                   <div class="iti-flag us"></div>
                </div>
                <span class="country-name">United States</span><span class="dial-code">+1</span>
             </li>
             <li data-dial-code="44" data-country-code="gb" class="country preferred">
                <div class="flag-box">
                   <div class="iti-flag gb"></div>
                </div>
                <span class="country-name">United Kingdom</span><span class="dial-code">+44</span>
             </li>
             <li class="divider"></li>
             <li data-dial-code="93" data-country-code="af" class="country ">
                <div class="flag-box">
                   <div class="iti-flag af"></div>
                </div>
                <span class="country-name">Afghanistan (‫افغانستان‬‎)</span><span class="dial-code">+93</span>
             </li>
             <li data-dial-code="355" data-country-code="al" class="country ">
                <div class="flag-box">
                   <div class="iti-flag al"></div>
                </div>
                <span class="country-name">Albania (Shqipëri)</span><span class="dial-code">+355</span>
             </li>
             <li data-dial-code="213" data-country-code="dz" class="country ">
                <div class="flag-box">
                   <div class="iti-flag dz"></div>
                </div>
                <span class="country-name">Algeria (‫الجزائر‬‎)</span><span class="dial-code">+213</span>
             </li>

....

                <span class="country-name">Western Sahara (‫الصحراء الغربية‬‎)</span><span class="dial-code">+212</span>
             </li>
             <li data-dial-code="967" data-country-code="ye" class="country ">
                <div class="flag-box">
                   <div class="iti-flag ye"></div>
                </div>
                <span class="country-name">Yemen (‫اليمن‬‎)</span><span class="dial-code">+967</span>
             </li>
             <li data-dial-code="260" data-country-code="zm" class="country ">
                <div class="flag-box">
                   <div class="iti-flag zm"></div>
                </div>
                <span class="country-name">Zambia</span><span class="dial-code">+260</span>
             </li>
             <li data-dial-code="263" data-country-code="zw" class="country ">
                <div class="flag-box">
                   <div class="iti-flag zw"></div>
                </div>
                <span class="country-name">Zimbabwe</span><span class="dial-code">+263</span>
             </li>
             <li data-dial-code="358" data-country-code="ax" class="country ">
                <div class="flag-box">
                   <div class="iti-flag ax"></div>
                </div>
                <span class="country-name">Åland Islands</span><span class="dial-code">+358</span>
             </li>
          </ul>
       </div>
       <input name="phone" id="phone" placeholder="Phone" value="9992715671" required="required" autocomplete="off" class="form-control" type="text">
    </div>

When I manually remove the hide class from ul class="country-list hide" it then appears but I can't interact with it, it appears that the js is not working properly I guess.

What happens if I remove the "hide":

No Hide Class

UPDATE:

Doing more testing I realized that if I comment out the "app.js" script intl-tel-input works perfectly so my question is now, what do I have to change in App.js to make this work? (I haven't added nothing to app.js, it is the same one that comes with every laravel installation)

Upvotes: 1

Views: 2765

Answers (2)

Adro
Adro

Reputation: 675

I tried removing the defer attribute but got this error:

[Vue warn]: Cannot find element: #app

Try moving your app.js import after the body tag. This will solve the issue.

<body>...</body>

<!-- Scripts -->
<script src="{{ asset('js/app.js') }}"></script>

Upvotes: 0

bdb.jack
bdb.jack

Reputation: 147

I had a similar issue after installing via yarn. My solution was to add the following:

require('intl-tel-input/build/js/utils'); require("intl-tel-input/build/js/intlTelInput");

To ./resources/assets/js/bootstrap.js right after require('bootstrap');

And then to add:

jQuery( '#phone' ).intlTelInput({ dropdownContainer: 'body', autoPlaceholder: 'polite', initialCountry: 'auto', geoIpLookup: function(callback) { jQuery.get("https://ipinfo.io", function() {}, "jsonp").always(function(resp) { var countryCode = (resp && resp.country) ? resp.country : ""; callback(countryCode); }); }, });

to the end of the file ./resources/assets/js/app.js

Note I added some arguments to mine, since I was also having issues with the automatic country detection until I sorted this out.

Upvotes: 0

Related Questions