Skip to content

Commit

Permalink
fix(field): Check that the phone/country field exists before initiali…
Browse files Browse the repository at this point in the history
…zing (Fixes #51) (#52)

chore(lint): Minor linting on field.js
chore(plugin): Bump plugin version to 1.2.0
  • Loading branch information
Log1x authored Oct 14, 2022
1 parent 1ceefb3 commit 32bc376
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 10 deletions.
18 changes: 11 additions & 7 deletions assets/js/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import intlTelInput from 'intl-tel-input'
/**
* Load Events
*/
;(function($) {
;(function ($) {
if (typeof acf.add_action === 'undefined') {
return
}
Expand All @@ -18,35 +18,39 @@ import intlTelInput from 'intl-tel-input'
* @param element jQuery element which contains the ACF fields
* @return void
*/
acf.add_action('ready append', function(element) {
acf.add_action('ready append', function (element) {
acf
.get_fields(
{
type: 'phone_number'
type: 'phone_number',
},
element
)
.each(function() {
.each(function () {
let phone = $(this).find('[name$="[number]"]')
let country = $(this).find('[name$="[country]"]')

if (phone.length == 0 || country.length == 0) {
return
}

let tel = intlTelInput(phone.get(0), {
utilsScript:
'https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.3/js/utils.min.js',
initialCountry: country.data('default-country') || 'us',
formatOnDisplay: true,
nationalMode: false
nationalMode: false,
})

if (!country.val()) {
country.val(tel.getSelectedCountryData().iso2)
}

phone.on('countrychange', function() {
phone.on('countrychange', function () {
country.val(tel.getSelectedCountryData().iso2)
})

phone.on('keyup change', function() {
phone.on('keyup change', function () {
if (typeof intlTelInputUtils === 'undefined') {
return
}
Expand Down
2 changes: 1 addition & 1 deletion plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Plugin Name: Advanced Custom Fields: Phone Number
* Plugin URI: https://github.com/log1x/acf-phone-number
* Description: A real ACF phone number field.
* Version: 1.1.9
* Version: 1.2.0
* Author: Brandon Nifong
* Author URI: https://github.com/log1x
*/
Expand Down
2 changes: 1 addition & 1 deletion public/js/field.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/mix-manifest.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"/js/field.js": "/js/field.js?id=709594ccdd09b1fea6c3",
"/js/field.js": "/js/field.js?id=71731e9b4bfa23e705f6",
"/css/field.css": "/css/field.css?id=cecba8ef58b775493cb8"
}
1 change: 1 addition & 0 deletions src/PhoneNumberField.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public function render_field_settings($field)
acf_render_field_setting($field, [
'label' => __('Default Country', 'acf-phone-number'),
'instructions' => __('The default country value for the phone number.', 'acf-phone-number'),
'placeholder' => __('Select a default country...', 'acf-phone-number'),
'type' => 'select',
'ui' => 1,
'name' => 'default_country',
Expand Down

0 comments on commit 32bc376

Please sign in to comment.