2024-01-02 15:36:59 +01:00
|
|
|
"use client";
|
|
|
|
|
|
2024-04-25 22:06:15 +02:00
|
|
|
import { Select } from "@mantine/core";
|
2024-01-02 15:36:59 +01:00
|
|
|
|
2024-05-26 17:13:34 +02:00
|
|
|
import { translateIfNecessary } from "@homarr/translation";
|
|
|
|
|
import type { stringOrTranslation } from "@homarr/translation";
|
|
|
|
|
import { useI18n } from "@homarr/translation/client";
|
|
|
|
|
|
2024-01-02 15:36:59 +01:00
|
|
|
import type { CommonWidgetInputProps } from "./common";
|
|
|
|
|
import { useWidgetInputTranslation } from "./common";
|
|
|
|
|
import { useFormContext } from "./form";
|
|
|
|
|
|
2024-03-09 19:25:48 +01:00
|
|
|
export type SelectOption =
|
|
|
|
|
| {
|
|
|
|
|
value: string;
|
2024-05-26 17:13:34 +02:00
|
|
|
label: stringOrTranslation;
|
2024-03-09 19:25:48 +01:00
|
|
|
}
|
|
|
|
|
| string;
|
|
|
|
|
|
2024-05-19 22:38:39 +02:00
|
|
|
export type inferSelectOptionValue<TOption extends SelectOption> = TOption extends {
|
|
|
|
|
value: infer TValue;
|
|
|
|
|
}
|
|
|
|
|
? TValue
|
|
|
|
|
: TOption;
|
2024-03-09 19:25:48 +01:00
|
|
|
|
2024-05-19 22:38:39 +02:00
|
|
|
export const WidgetSelectInput = ({ property, kind, options }: CommonWidgetInputProps<"select">) => {
|
2024-05-26 17:13:34 +02:00
|
|
|
const t = useI18n();
|
|
|
|
|
const tWidget = useWidgetInputTranslation(kind, property);
|
2024-01-02 15:36:59 +01:00
|
|
|
const form = useFormContext();
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Select
|
2024-05-26 17:13:34 +02:00
|
|
|
label={tWidget("label")}
|
|
|
|
|
data={options.options.map((option) =>
|
|
|
|
|
typeof option === "string"
|
|
|
|
|
? option
|
|
|
|
|
: {
|
|
|
|
|
value: option.value,
|
2024-06-08 20:49:57 +02:00
|
|
|
label: translateIfNecessary(t, option.label) ?? option.value,
|
2024-05-26 17:13:34 +02:00
|
|
|
},
|
|
|
|
|
)}
|
|
|
|
|
description={options.withDescription ? tWidget("description") : undefined}
|
2024-03-09 19:25:48 +01:00
|
|
|
searchable={options.searchable}
|
2024-02-03 10:24:39 +01:00
|
|
|
{...form.getInputProps(`options.${property}`)}
|
2024-01-02 15:36:59 +01:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
};
|