Add Handlebars helpers

This commit is contained in:
Oliver-Akins 2024-12-21 23:46:26 -07:00
parent 06c5320786
commit edd4e49f62
4 changed files with 87 additions and 0 deletions

View file

@ -0,0 +1,11 @@
import { handlebarsLocalizer, localizer } from "../utils/Localizer.mjs";
import { options } from "./options.mjs";
export default {
// #region Complex
"rc-i18n": handlebarsLocalizer,
"rc-options": options,
// #region Simple
"rc-empty-state": (v) => v ?? localizer(`RipCrypt.common.empty`),
};

View file

@ -0,0 +1,36 @@
import { localizer } from "../utils/Localizer.mjs";
/**
* @typedef {object} Option
* @property {string} [label]
* @property {string|number} value
* @property {boolean} [disabled]
*/
/**
* @param {string | number} selected
* @param {Array<Option | string>} opts
* @param {any} meta
*/
export function options(selected, opts, meta) {
const { localize = false } = meta;
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 ? localizer(opt.label) : opt.label}
</option>`,
);
};
};