2026-02-12 15:42:22 +01:00
|
|
|
// * Packages
|
2025-11-12 14:32:50 +01:00
|
|
|
import { LaravelEnum } from "@/back/types/global";
|
|
|
|
|
|
2026-02-12 15:42:22 +01:00
|
|
|
type AllowedValue = string | boolean | Date | number | File;
|
|
|
|
|
|
|
|
|
|
export default function mapLaravelEnumOptions<
|
2025-11-12 14:32:50 +01:00
|
|
|
T extends LaravelEnum,
|
2026-02-12 15:42:22 +01:00
|
|
|
D extends Record<string, AllowedValue>,
|
2026-02-10 15:52:48 +01:00
|
|
|
>(enumObject: T, data?: D[], valueKey?: keyof D) {
|
2025-11-12 14:32:50 +01:00
|
|
|
const options = Object.values(enumObject).map((opt) => ({
|
|
|
|
|
value: opt.value.toString(),
|
|
|
|
|
label: opt.label,
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
const defaultValue =
|
2026-02-10 15:52:48 +01:00
|
|
|
data && valueKey
|
|
|
|
|
? data.map((item) => item[valueKey]?.toString()).filter(Boolean)
|
|
|
|
|
: [];
|
2025-11-12 14:32:50 +01:00
|
|
|
return { options, defaultValue };
|
|
|
|
|
}
|