Skip to content

Commit

Permalink
submit form is now working
Browse files Browse the repository at this point in the history
  • Loading branch information
iandunn committed Apr 12, 2023
1 parent 901b473 commit 6abade4
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions wp-content/mu-plugins/assets/locale-switcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,46 @@
app.$notice = $( '.wp20-locale-notice' );
app.$container = $( '.navigation-top-menu-container' );
app.$outerContainer = $( '.wp20-locale-switcher-container' );
app.$switcherForm = $( '#wp20-locale-switcher-form' );

const localeSelect = document.querySelector( '#wp20-locale-switcher' );

// todo enable a11yautocom here
accessibleAutocomplete.enhanceSelectElement( {
selectElement: document.querySelector( '#wp20-locale-switcher' ),
selectElement: localeSelect,
showAllValues: true,
// displayMenu: 'overlay' might be useful
// autoselect: false

// Submit the form.
onConfirm: function( localeName ) {
// Find the selected locale code.
// This is necessary because of https://github.com/alphagov/accessible-autocomplete/issues/387.
const selectedOption = Array.from( localeSelect.querySelectorAll( 'option' ) ).find( function( option ) {
return option.innerText === localeName
} );

if ( selectedOption ) {
$( '#wp20-locale-switcher-select' ).val( selectedOption.value );
app.$switcherForm.submit();
}
},

// defaultValue: function( value ) {
// console.log( 'defaultvalue', value );
// return 'hi';
// }

// . If your use case doesn't fit the above defaults, try reading the source and seeing if you can write your own.

// souce - use locale code too

// inputValue: function( value ) {
// console.log( 'inputValue', value );
// return value;
// },
// doesn't give locale code

// dropdownArrow: () => '' requires showallvalues true

// transleat "no results found" etc
Expand All @@ -46,12 +78,13 @@

app.$switcher.on( 'change', function() {
console.log('submitting');
// move to onConfirm
$(this).parents( 'form' ).submit();
} );

// This has to stay `select2`; `selectWoo:open` will not work.
app.$switcher.on( 'select2:open', function() {
// todo: update for new lib
// move to onConfirm
app.$container.find( 'input' ).attr( 'placeholder', app.$container.attr( 'data-placeholder' ) );

// Turn off the menu if it's open.
Expand Down

0 comments on commit 6abade4

Please sign in to comment.