/** * Single / multiple select with search and sorting, tags components * @requires https://github.com/Choices-js/Choices */ import Choices from 'choices.js' export default (() => { let selects = document.querySelectorAll('[data-select]') if (selects.length === 0) return const defaultOptions = { allowHTML: true, searchPlaceholderValue: 'Search...', removeItemButton: true, editItems: true, searchEnabled: false, shouldSort: false, itemSelectText: '', classNames: { containerInner: 'form-select', }, } selects.forEach((select) => { let dataAttr = select.getAttribute('data-select'), template = select.getAttribute('data-select-template'), userOptions, options if (dataAttr !== '') userOptions = JSON.parse(dataAttr) if (template !== null) { options = { ...defaultOptions, ...userOptions, /* eslint-disable indent */ callbackOnCreateTemplates: function (template) { return { item: ({ classNames }, data) => { return template(`
${data.placeholder || !data.customProperties?.selected ? data.label : data.customProperties.selected} ${userOptions.removeItemButton === false ? '' : ``}
`) }, choice: ({ classNames }, data) => { return template(`
0 ? 'role="treeitem"' : 'role="option"' }>
${data.label} ${(() => { let output = '' if (data.customProperties) { for (const key in data.customProperties) { if ( Object.prototype.hasOwnProperty.call( data.customProperties, key ) && key !== 'selected' ) { output += data.customProperties[key] } } } return output })()}
`) }, } }, /* eslint-enable indent */ } } else { options = { ...defaultOptions, ...userOptions } } /* eslint-disable no-unused-vars, no-undef */ const choices = new Choices(select, options) /* eslint-enable no-unused-vars, no-undef */ }) })()