ldc-supportweb/resources/ts/lib/mapEnumOptions.tsx

21 lines
573 B
TypeScript
Raw Normal View History

// * Other
import { LaravelEnum } from "@/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 };
}