gitforge/web_src/js/modules/tom-select.ts
Markus Unterwaditzer d464d625d8 feat: Move repo topic selector to tom-select (#12736)
This is a proof of concept for replacing Formatic's buggy dropdown with
something else. There are a bunch of open gitea issues on github
discussing getting rid of Fomantic.

Tested in Firefox/Chrome. Attached screenshots are before/after. The one with the input focused in orange, and tighter letter spacing, is after.

If this is merged I plan to move the other selectors off of Fomantic.

Alternative to https://codeberg.org/forgejo/forgejo/pulls/12731, also fixes #10118

Signed-off-by: Markus Unterwaditzer <markus-tarpit+git@unterwaditzer.net>
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/12736
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
2026-06-27 23:30:59 +02:00

34 lines
1.1 KiB
TypeScript

// Copyright 2026 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: GPL-3.0-or-later
import type TomSelectType from 'tom-select';
import type {TomSettings} from 'tom-select/dist/esm/types/index.ts';
export async function createTomSelect(el: HTMLInputElement, opts: Partial<TomSettings> = {}): Promise<TomSelectType> {
const {default: TomSelect} = await import(/* webpackChunkName: "tom-select" */'tom-select');
const ts = new TomSelect(el, {
...opts,
onItemAdd(...args: unknown[]) {
ts.setTextboxValue('');
opts.onItemAdd?.apply(this, args);
},
});
// Handle comma key to create item immediately
ts.control_input.addEventListener('keydown', (e: KeyboardEvent) => {
if (e.key === ',') {
e.preventDefault();
const value = ts.inputValue().trim();
if (value) {
// Use addItem if option exists, otherwise createItem
if (ts.options[value]) {
ts.addItem(value);
} else {
ts.createItem(value);
}
ts.setTextboxValue('');
}
}
});
return ts;
}