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

@ -4,6 +4,7 @@ import { detailsExpanded } from "./detailsExpanded.mjs";
import { objectValue } from "./objectValue.mjs"; import { objectValue } from "./objectValue.mjs";
import { toFriendlyDuration } from "./toFriendlyDuration.mjs"; import { toFriendlyDuration } from "./toFriendlyDuration.mjs";
import { localizer } from "../utils/localizer.mjs"; import { localizer } from "../utils/localizer.mjs";
import { options } from "./options.mjs";
export default { export default {
@ -14,6 +15,7 @@ export default {
"dd-objectValue": objectValue, "dd-objectValue": objectValue,
"dd-expanded": detailsExpanded, "dd-expanded": detailsExpanded,
"dd-i18n": localizer, "dd-i18n": localizer,
"dd-options": options,
// Simple helpers // Simple helpers
"dd-stringify": v => JSON.stringify(v, null, ` `), "dd-stringify": v => JSON.stringify(v, null, ` `),

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`);
};

View file

@ -19,14 +19,7 @@
name="system.stats.{{stat.key}}" name="system.stats.{{stat.key}}"
class="e-2dp" class="e-2dp"
> >
{{#select stat.value}} {{{dd-options stat.value stat.dieOptions}}}
<option value="">---</option>
{{#each stat.dieOptions as | die |}}
<option value="{{die.value}}" {{disabled die.disabled}}>
{{die.label}}
</option>
{{/each}}
{{/select}}
</select> </select>
<button type="button" class="e-2dp"> <button type="button" class="e-2dp">
Roll Roll