global: snapshot

This commit is contained in:
nym21
2026-02-27 23:00:43 +01:00
parent d5ec291579
commit 85c7933ad6
41 changed files with 534 additions and 583 deletions

View File

@@ -238,10 +238,12 @@ export function createRadios({
* @param {(choice: T) => string} [args.toKey]
* @param {(choice: T) => string} [args.toLabel]
* @param {boolean} [args.sorted]
* @param {{ label: string, items: T[] }[]} [args.groups]
*/
export function createSelect({
id,
choices: unsortedChoices,
groups,
initialValue,
onChange,
sorted,
@@ -271,15 +273,27 @@ export function createSelect({
select.name = id ?? "";
field.append(select);
choices.forEach((choice) => {
/** @param {T} choice */
const createOption = (choice) => {
const option = window.document.createElement("option");
option.value = toKey(choice);
option.textContent = toLabel(choice);
if (toKey(choice) === initialKey) {
option.selected = true;
}
select.append(option);
});
return option;
};
if (groups) {
groups.forEach(({ label, items }) => {
const optgroup = window.document.createElement("optgroup");
optgroup.label = label;
items.forEach((choice) => optgroup.append(createOption(choice)));
select.append(optgroup);
});
} else {
choices.forEach((choice) => select.append(createOption(choice)));
}
select.addEventListener("change", () => {
onChange?.(fromKey(select.value));