Add my typical options helper for ease of use

This commit is contained in:
Oliver-Akins 2025-07-26 10:51:55 -06:00
parent 61f41d610e
commit 40820988da
2 changed files with 36 additions and 0 deletions

View file

@ -1,5 +1,7 @@
import { filePath } from "../consts.mjs";
import { options } from "./options.mjs";
export default {
systemFilePath: filePath,
"taf-options": options,
};

View file

@ -0,0 +1,34 @@
/**
* @typedef {object} Option
* @property {string} [label]
* @property {string|number} value
* @property {boolean} [disabled]
*/
/**
* @param {string | number} selected The selected value
* @param {Array<Option | string>} opts The options that are valid
* @param {any} meta The Handlebars meta processing
*/
export function options(selected, opts, meta) {
const { localize = false } = meta.hash;
selected = Handlebars.escapeExpression(selected);
const htmlOptions = [];
for (let opt of opts) {
if (typeof opt === `string`) {
opt = { label: opt, value: opt };
};
opt.value = Handlebars.escapeExpression(opt.value);
htmlOptions.push(
`<option
value="${opt.value}"
${selected === opt.value ? `selected` : ``}
${opt.disabled ? `disabled` : ``}
>
${localize ? game.i18n.format(opt.label) : opt.label}
</option>`,
);
};
return new Handlebars.SafeString(htmlOptions.join(`\n`));
};