Sha256: c26971cb034af551754061c6629e605cba248516c6de5e967d87a0fbc64a2d6b

Contents?: true

Size: 1.02 KB

Versions: 25

Compression:

Stored size: 1.02 KB

Contents

/**
// Function adds feature to datalist that you can have different id and label.
// Used with datalist_select_helper.rb#datalist_select
 * @param {HTMLElement} target - dataselect wrapper
 * @param {Function} callback - the function to be executed after a successful selection.
 * @returns {Void} - Returns nothing.
 */

export const datalistSelect = (target, callback) => {
  if (!target) {
    return;
  }

  const input = target.querySelector("input[type='hidden'");
  const textInput = target.querySelector("input[type='text']");
  const items = target.querySelector("datalist").children;

  const selectValue = (selected) => {
    for (let idx = 0; idx < items.length; idx += 1) {
      if (items[idx].innerHTML === selected) {
        const id = items[idx].dataset.value;
        input.value = id;
        if (callback) {
          return callback(id);
        }
        return true;
      }
    }
    return false;
  }

  textInput.addEventListener("input", () => {
    const selected = textInput.value;
    selectValue(selected);
  })
}

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
decidim-core-0.29.1 app/packs/src/decidim/datalist_select.js
decidim-core-0.28.4 app/packs/src/decidim/datalist_select.js
decidim-core-0.27.9 app/packs/src/decidim/datalist_select.js
decidim-core-0.29.0 app/packs/src/decidim/datalist_select.js
decidim-core-0.28.3 app/packs/src/decidim/datalist_select.js
decidim-core-0.27.8 app/packs/src/decidim/datalist_select.js
decidim-core-0.29.0.rc4 app/packs/src/decidim/datalist_select.js
decidim-core-0.29.0.rc3 app/packs/src/decidim/datalist_select.js
decidim-core-0.29.0.rc2 app/packs/src/decidim/datalist_select.js
decidim-core-0.29.0.rc1 app/packs/src/decidim/datalist_select.js
decidim-core-0.28.2 app/packs/src/decidim/datalist_select.js
decidim-core-0.27.7 app/packs/src/decidim/datalist_select.js
decidim-core-0.28.1 app/packs/src/decidim/datalist_select.js
decidim-core-0.27.6 app/packs/src/decidim/datalist_select.js
decidim-core-0.28.0 app/packs/src/decidim/datalist_select.js
decidim-core-0.27.5 app/packs/src/decidim/datalist_select.js
decidim-core-0.28.0.rc5 app/packs/src/decidim/datalist_select.js
decidim-core-0.28.0.rc4 app/packs/src/decidim/datalist_select.js
decidim-core-0.27.4 app/packs/src/decidim/datalist_select.js
decidim-core-0.27.3 app/packs/src/decidim/datalist_select.js