PHP Classes

File: resources/js/select.js

Recommend this page to a friend!
  Packages of Boss Ibrahim Mussa   Caelrik   resources/js/select.js   Download  
File: resources/js/select.js
Role: Auxiliary data
Content type: text/plain
Description: Auxiliary data
Class: Caelrik
Build Web user interface from PHP components
Author: By
Last change:
Date: 8 days ago
Size: 1,826 bytes
 

 

Contents

Class file image Download
import TomSelect from 'tom-select/base'; import removeButton from 'tom-select/plugins/remove_button/plugin.js'; import 'tom-select/dist/css/tom-select.bootstrap5.css'; import { buildSelectConfig } from './select-config.js'; // The base build has no plugins: add the one we use (removable tags on multiple selects). TomSelect.define('remove_button', removeButton); // Searchable selects (Field::searchable(), Field::remote()). The original <select> stays // in the form, hidden: it submits the value, and Parsley validates it. export function initSelects(form) { return [...form.querySelectorAll('select[data-caelrik-select]')].map((select) => { const options = JSON.parse(select.getAttribute('data-caelrik-select') || '{}'); const tomSelect = new TomSelect(select, buildSelectConfig(select, options)); // Help and error texts describe the search box, which has the focus. const describedBy = select.getAttribute('aria-describedby'); if (describedBy) { tomSelect.control_input.setAttribute('aria-describedby', describedBy); } // The search box is the control now (the label points to it): the hidden select is // out of the tab order, and hidden from assistive technology too. select.setAttribute('aria-hidden', 'true'); // Error summary links and Parsley focus the hidden select: move to the search box. select.addEventListener('focus', () => tomSelect.focus()); // A tags field: Enter after typing a tag that cannot be added (already there, or the // field is full) must not submit the form. if (options.tags) { tomSelect.control_input.addEventListener('keydown', (event) => { if (event.key === 'Enter' && tomSelect.control_input.value.trim() !== '') { event.preventDefault(); } }); } return tomSelect; }); }