27 lines
793 B
TypeScript
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 };
|
|
}
|