add optional i18n prefix for select widgets

This commit is contained in:
Josh Hawkins 2026-03-25 10:24:48 -05:00
parent e7609afb54
commit 5f61c54efd

View File

@ -1,5 +1,6 @@
// Select Widget - maps to shadcn/ui Select // Select Widget - maps to shadcn/ui Select
import type { WidgetProps } from "@rjsf/utils"; import type { WidgetProps } from "@rjsf/utils";
import { useTranslation } from "react-i18next";
import { import {
Select, Select,
SelectContent, SelectContent,
@ -21,9 +22,18 @@ export function SelectWidget(props: WidgetProps) {
schema, schema,
} = props; } = props;
const { t } = useTranslation(["views/settings"]);
const { enumOptions = [] } = options; const { enumOptions = [] } = options;
const enumI18nPrefix = options["enumI18nPrefix"] as string | undefined;
const fieldClassName = getSizedFieldClassName(options, "sm"); const fieldClassName = getSizedFieldClassName(options, "sm");
const getLabel = (option: { value: unknown; label: string }) => {
if (enumI18nPrefix) {
return t(`${enumI18nPrefix}.${option.value}`);
}
return option.label;
};
return ( return (
<Select <Select
value={value?.toString() ?? ""} value={value?.toString() ?? ""}
@ -42,7 +52,7 @@ export function SelectWidget(props: WidgetProps) {
<SelectContent> <SelectContent>
{enumOptions.map((option: { value: unknown; label: string }) => ( {enumOptions.map((option: { value: unknown; label: string }) => (
<SelectItem key={String(option.value)} value={String(option.value)}> <SelectItem key={String(option.value)} value={String(option.value)}>
{option.label} {getLabel(option)}
</SelectItem> </SelectItem>
))} ))}
</SelectContent> </SelectContent>