ldc-supportweb/resources/ts/lib/mapEnumOptions.tsx
2026-06-22 15:06:21 +02:00

27 lines
793 B
TypeScript

import { type LaravelEnum } from "@/types/global";
type StringifiableValue = string | number;
export default function mapLaravelEnumOptions<
D extends Record<string, StringifiableValue | boolean | Date | File>,
>(enumObject: LaravelEnum, data?: D[], valueKey?: keyof D) {
const options = Object.values(enumObject).map((opt) => ({
value: opt.value.toString(),
label: opt.label,
}));
const defaultValue =
data && valueKey
? data
.map((item) => {
const val = item[valueKey];
if (typeof val === "string" || typeof val === "number") {
return val.toString();
}
return undefined;
})
.filter((v): v is string => v !== undefined)
: [];
return { options, defaultValue };
}