2026-02-27 09:55:36 -06:00
|
|
|
// Select Widget - maps to shadcn/ui Select
|
|
|
|
|
import type { WidgetProps } from "@rjsf/utils";
|
2026-03-25 14:14:32 -05:00
|
|
|
import { useTranslation } from "react-i18next";
|
2026-02-27 09:55:36 -06:00
|
|
|
import {
|
|
|
|
|
Select,
|
|
|
|
|
SelectContent,
|
|
|
|
|
SelectItem,
|
|
|
|
|
SelectTrigger,
|
|
|
|
|
SelectValue,
|
|
|
|
|
} from "@/components/ui/select";
|
|
|
|
|
import { getSizedFieldClassName } from "../utils";
|
|
|
|
|
|
|
|
|
|
export function SelectWidget(props: WidgetProps) {
|
|
|
|
|
const {
|
|
|
|
|
id,
|
|
|
|
|
options,
|
|
|
|
|
value,
|
|
|
|
|
disabled,
|
|
|
|
|
readonly,
|
|
|
|
|
onChange,
|
|
|
|
|
placeholder,
|
|
|
|
|
schema,
|
|
|
|
|
} = props;
|
|
|
|
|
|
2026-03-25 14:14:32 -05:00
|
|
|
const { t } = useTranslation(["views/settings"]);
|
2026-02-27 09:55:36 -06:00
|
|
|
const { enumOptions = [] } = options;
|
2026-03-25 14:14:32 -05:00
|
|
|
const enumI18nPrefix = options["enumI18nPrefix"] as string | undefined;
|
2026-02-27 09:55:36 -06:00
|
|
|
const fieldClassName = getSizedFieldClassName(options, "sm");
|
|
|
|
|
|
2026-03-25 14:14:32 -05:00
|
|
|
const getLabel = (option: { value: unknown; label: string }) => {
|
|
|
|
|
if (enumI18nPrefix) {
|
|
|
|
|
return t(`${enumI18nPrefix}.${option.value}`);
|
|
|
|
|
}
|
|
|
|
|
return option.label;
|
|
|
|
|
};
|
|
|
|
|
|
2026-02-27 09:55:36 -06:00
|
|
|
return (
|
|
|
|
|
<Select
|
|
|
|
|
value={value?.toString() ?? ""}
|
|
|
|
|
onValueChange={(val) => {
|
|
|
|
|
// Convert back to original type if needed
|
|
|
|
|
const enumVal = enumOptions.find(
|
|
|
|
|
(opt: { value: unknown }) => opt.value?.toString() === val,
|
|
|
|
|
);
|
|
|
|
|
onChange(enumVal ? enumVal.value : val);
|
|
|
|
|
}}
|
|
|
|
|
disabled={disabled || readonly}
|
|
|
|
|
>
|
|
|
|
|
<SelectTrigger id={id} className={fieldClassName}>
|
|
|
|
|
<SelectValue placeholder={placeholder || schema.title || "Select..."} />
|
|
|
|
|
</SelectTrigger>
|
|
|
|
|
<SelectContent>
|
|
|
|
|
{enumOptions.map((option: { value: unknown; label: string }) => (
|
|
|
|
|
<SelectItem key={String(option.value)} value={String(option.value)}>
|
2026-03-25 14:14:32 -05:00
|
|
|
{getLabel(option)}
|
2026-02-27 09:55:36 -06:00
|
|
|
</SelectItem>
|
|
|
|
|
))}
|
|
|
|
|
</SelectContent>
|
|
|
|
|
</Select>
|
|
|
|
|
);
|
|
|
|
|
}
|