No Code Attached Yet
avatar stAn47
stAn47
16 May 2025

Is your feature request related to a problem? Please describe.

Current version bundled with J5 of choices JS does not support refresh when the select's options are handled by external scripts (for example options html injection directly via innerHTML or similar) and the state of the select rendered via chosesjs cannot be refreshed properly.

this piece of code would not work with current version in J5.3 while it works OK with latest choicesjs :

jQuery(wrapper.select).on('chosen:updated', function (e) {
          wrapper.choicesInstance.refresh();
        });

where wrapper is the Joomla's custom element with already initialized choicesjs.

Describe the solution you'd like

Please upgrade the choicesjs to the latest per:
https://github.com/Choices-js/Choices
specifically:
https://cdn.jsdelivr.net/npm/choices.js@9.0.1/public/assets/scripts/choices.min.js

Additional context

See below code to convert all selects into joomla-field-fancy-select custom element.


  function wrapSelect(select) {
    // Avoid wrapping if already inside a <joomla-field-fancy-select>
    if (
      select.parentElement?.tagName.toLowerCase() === 'joomla-field-fancy-select'
    ) return;
    
    if (select.className.indexOf('choices__input') >=0 ) return; 

    // Create the wrapper
    const wrapper = document.createElement('joomla-field-fancy-select');
    var placeholderTest = select.querySelector('option[value="0"]'); 
    var placeholderText = '';
    if (placeholderTest) {
       placeholderText = placeholderTest.value;
    }
    select.setAttribute('data-placeholder', ''); //remove unlisted text 

    wrapper.innerHTML = select.outerHTML;
     
    select.replaceWith(wrapper);
   
    //const newSelect = wrapper.querySelector('select');
    wrapper.select.addEventListener('chosen:updated', function (e) {
        console.log('chosen updated', newSelect); 
        wrapper.doConnect(); 
      // Put your specific code here
    });
     wrapper.addEventListener('chosen:updated', function (e) {
        console.log('chosen updated', wrapper); 
        wrapper.doConnect(); 
      // Put your specific code here
    });
    jQuery(wrapper.select).on('chosen:updated', function (e) {
            console.log('Caught in jQuery');
          wrapper.choicesInstance.refresh();
            
            
            
        });
  }

  function wrapAllExistingSelects() {
    document.querySelectorAll('select').forEach(wrapSelect);
  }

  function observeNewSelects() {
    const observer = new MutationObserver((mutations) => {
      for (const mutation of mutations) {
        mutation.addedNodes.forEach((node) => {
          if (node.nodeType !== 1) return;

          // Single select
          if (node.tagName.toLowerCase() === 'select') {
            wrapSelect(node);
          }

          // Or container that includes selects
          node.querySelectorAll?.('select').forEach(wrapSelect);
        });
      }
    });

    observer.observe(document.body, {
      childList: true,
      subtree: true,
    });
  }

  document.addEventListener('DOMContentLoaded', () => {
    wrapAllExistingSelects();
    observeNewSelects();
  });

  //stAn - we disable chosen if fancy select is loaded
  if (typeof jQuery !== 'undefined') {
    Object.defineProperty(jQuery.fn, 'chosen', {
      value: function () { 
        //this.parentElement.doConnect(); 
        //this.wrapper.doConnect(); 
        return this; },
      writable: false,
      configurable: false,
    });
  }
avatar stAn47 stAn47 - open - 16 May 2025
avatar joomla-cms-bot joomla-cms-bot - change - 16 May 2025
Labels Added: No Code Attached Yet
avatar joomla-cms-bot joomla-cms-bot - labeled - 16 May 2025
avatar Hackwar
Hackwar - comment - 18 May 2025

We currently are using version 9.1.0. You linked version 9.0.1. Can you check that again? What I can say is, that we won't be able to update to 10.0 or the latest 11.1 in the 5.x series of Joomla per our b/c promise. So this would have to go into 6.0 if it is later than version 9.

avatar stAn47
stAn47 - comment - 19 May 2025

Hello, you are right - i've used version v11.1 (latest) which supports "refresh" method.

i've created override for the 11.1 version and i don't really really see any issues with it (using original CSS, original joomla loader and new version 11.1 )

it's a huge problem that the core version doesn't support any kind of "refresh" method thus for any 3rd party plugins/extensions working with huge selects on J5 it would make sense to include own version of choicesjs - but is this really intentional ? even destroy method on the core version is broken. (select an option via js and destroy choices and you get non-consitant select). i don't really see why J5 shoud keep this broken library and not to update it to the latest.

latest compiled version is
https://cdn.jsdelivr.net/npm/choices.js/public/assets/scripts/choices.min.js
as referenced from:
https://github.com/Choices-js/Choices

thanks, best regards, stan

Add a Comment

Login with GitHub to post a comment