20 lines
617 B
TypeScript
20 lines
617 B
TypeScript
// * Packages
|
|
import { LaravelEnum } from "@/back/types/global";
|
|
|
|
type AllowedValue = string | boolean | Date | number | File;
|
|
|
|
export default function mapLaravelEnumOptions<
|
|
T extends LaravelEnum,
|
|
D extends Record<string, AllowedValue>,
|
|
>(enumObject: T, 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) => item[valueKey]?.toString()).filter(Boolean)
|
|
: [];
|
|
return { options, defaultValue };
|
|
}
|