Fix initialization of TomSelect dropdowns after HTMX loading

This commit is contained in:
Jeremy Stretch 2024-02-14 16:43:22 -05:00
parent fa4f4893a3
commit a78fb8033c
5 changed files with 4 additions and 16 deletions

Binary file not shown.

Binary file not shown.

View File

@ -1,4 +1,3 @@
import { getElements, isTruthy } from './util';
import { initButtons } from './buttons'; import { initButtons } from './buttons';
import { initSelects } from './select'; import { initSelects } from './select';
import { initObjectSelector } from './objectSelector'; import { initObjectSelector } from './objectSelector';
@ -15,16 +14,5 @@ function initDepedencies(): void {
* elements. * elements.
*/ */
export function initHtmx(): void { export function initHtmx(): void {
for (const element of getElements('[hx-target]')) { document.addEventListener('htmx:afterSettle', initDepedencies);
const targetSelector = element.getAttribute('hx-target');
if (isTruthy(targetSelector)) {
for (const target of getElements(targetSelector)) {
target.addEventListener('htmx:afterSettle', initDepedencies);
}
}
}
for (const element of getElements('[hx-trigger=load]')) {
element.addEventListener('htmx:afterSettle', initDepedencies);
}
} }

View File

@ -42,7 +42,7 @@ function renderItem(data: TomOption, escape: typeof escape_html) {
// Initialize <select> elements which are populated via a REST API call // Initialize <select> elements which are populated via a REST API call
export function initDynamicSelects(): void { export function initDynamicSelects(): void {
for (const select of getElements<HTMLSelectElement>('select.api-select')) { for (const select of getElements<HTMLSelectElement>('select:not(.tomselected).api-select')) {
new DynamicTomSelect(select, { new DynamicTomSelect(select, {
...config, ...config,
valueField: VALUE_FIELD, valueField: VALUE_FIELD,

View File

@ -7,7 +7,7 @@ import { getElements } from '../util';
// Initialize <select> elements with statically-defined options // Initialize <select> elements with statically-defined options
export function initStaticSelects(): void { export function initStaticSelects(): void {
for (const select of getElements<HTMLSelectElement>( for (const select of getElements<HTMLSelectElement>(
'select:not(.api-select):not(.color-select)', 'select:not(.tomselected):not(.api-select):not(.color-select)',
)) { )) {
new TomSelect(select, { new TomSelect(select, {
...config, ...config,
@ -23,7 +23,7 @@ export function initColorSelects(): void {
)}"></span> ${escape(item.text)}</div>`; )}"></span> ${escape(item.text)}</div>`;
} }
for (const select of getElements<HTMLSelectElement>('select.color-select')) { for (const select of getElements<HTMLSelectElement>('select:not(.tomselected).color-select')) {
new TomSelect(select, { new TomSelect(select, {
...config, ...config,
render: { render: {