Also remove hardcoding of attributes on frontend state fields

This commit is contained in:
Mike Jolley 2019-03-14 13:52:36 +00:00
parent f2ca110a46
commit 7c42ac0a29
1 changed files with 29 additions and 39 deletions

View File

@ -101,27 +101,17 @@ jQuery( function( $ ) {
if ( states[ country ] ) { if ( states[ country ] ) {
if ( $.isEmptyObject( states[ country ] ) ) { if ( $.isEmptyObject( states[ country ] ) ) {
var $newstate = $( '<input type="hidden" />' )
.prop( 'id', input_id )
.prop( 'name', input_name )
.prop( 'placeholder', placeholder )
.addClass( 'hidden' );
$parent.hide().find( '.select2-container' ).remove(); $parent.hide().find( '.select2-container' ).remove();
$statebox.replaceWith( $statebox.replaceWith( $newstate );
'<input type="hidden" class="hidden" name="' +
input_name +
'" id="' +
input_id +
'" value="" placeholder="' +
placeholder +
'" />'
);
$( document.body ).trigger( 'country_to_state_changed', [ country, $wrapper ] ); $( document.body ).trigger( 'country_to_state_changed', [ country, $wrapper ] );
} else { } else {
var state = states[ country ],
var options = '', $defaultOption = $( '<option value=""></option>' ).text( wc_country_select_params.i18n_select_state_text );
state = states[ country ];
for ( var index in state ) {
if ( state.hasOwnProperty( index ) ) {
options = options + '<option value="' + index + '">' + state[ index ] + '</option>';
}
}
if ( ! placeholder ) { if ( ! placeholder ) {
placeholder = wc_country_select_params.i18n_select_state_text; placeholder = wc_country_select_params.i18n_select_state_text;
@ -130,37 +120,37 @@ jQuery( function( $ ) {
$parent.show(); $parent.show();
if ( $statebox.is( 'input' ) ) { if ( $statebox.is( 'input' ) ) {
// Change for select $newstate = $( '<select></select>' )
$statebox.replaceWith( .prop( 'id', input_id )
'<select name="' + .prop( 'name', input_name )
input_name + .data( 'placeholder', placeholder )
'" id="' + .addClass( 'state_select' );
input_id + $statebox.replaceWith( $newstate );
'" class="state_select" data-placeholder="' +
placeholder +
'"></select>'
);
$statebox = $wrapper.find( '#billing_state, #shipping_state, #calc_shipping_state' ); $statebox = $wrapper.find( '#billing_state, #shipping_state, #calc_shipping_state' );
} }
$statebox.html( '<option value="">' + wc_country_select_params.i18n_select_state_text + '</option>' + options ); $statebox.empty().append( $defaultOption );
$.each( state, function( index ) {
var $option = $( '<option></option>' )
.prop( 'value', index )
.text( state[ index ] );
$statebox.append( $option );
} );
$statebox.val( value ).change(); $statebox.val( value ).change();
$( document.body ).trigger( 'country_to_state_changed', [country, $wrapper ] ); $( document.body ).trigger( 'country_to_state_changed', [country, $wrapper ] );
} }
} else { } else {
if ( $statebox.is( 'select, input[type="hidden"]' ) ) { if ( $statebox.is( 'select, input[type="hidden"]' ) ) {
var $newstate = $( '<input type="text" />' )
.prop( 'id', input_id )
.prop( 'name', input_name )
.prop( 'placeholder', placeholder )
.addClass( 'input-text' );
$parent.show().find( '.select2-container' ).remove(); $parent.show().find( '.select2-container' ).remove();
$statebox.replaceWith( $statebox.replaceWith( $newstate );
'<input type="text" class="input-text" name="' +
input_name +
'" id="' +
input_id +
'" placeholder="' +
placeholder +
'" />'
);
$( document.body ).trigger( 'country_to_state_changed', [country, $wrapper ] ); $( document.body ).trigger( 'country_to_state_changed', [country, $wrapper ] );
} }
} }