Implement the custom options helper (closes #92)

This commit is contained in:
Oliver-Akins 2024-03-01 18:34:16 -07:00
parent 0e8d1615a7
commit 7516e7b42b
3 changed files with 35 additions and 8 deletions

View file

@ -0,0 +1,32 @@
/**
* @typedef {object} Option
* @property {string} [label]
* @property {string|number} value
* @property {boolean} [disabled]
*/
/**
* @param {string | number} selected
* @param {Array<Option | string>} opts
*/
export function options(selected, opts) {
selected = Handlebars.escapeExpression(selected);
const htmlOptions = [];
for (let opt of opts) {
if (foundry.utils.getType(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" : ""}
>
${opt.label}
</option>`
);
};
return htmlOptions.join(`\n`);
};