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 10 additions and 22 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1,4 +1,3 @@
import { getElements, isTruthy } from './util';
import { initButtons } from './buttons';
import { initSelects } from './select';
import { initObjectSelector } from './objectSelector';
@ -15,16 +14,5 @@ function initDepedencies(): void {
* elements.
*/
export function initHtmx(): void {
for (const element of getElements('[hx-target]')) {
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);
}
document.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
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, {
...config,
valueField: VALUE_FIELD,

View File

@ -7,7 +7,7 @@ import { getElements } from '../util';
// Initialize <select> elements with statically-defined options
export function initStaticSelects(): void {
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, {
...config,
@ -23,7 +23,7 @@ export function initColorSelects(): void {
)}"></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, {
...config,
render: {