ldc-groupe/resources/ts/lib/mapPivotOptions.ts

32 lines
940 B
TypeScript

import { Model } from "@/back/types/global";
export function mapPivotOptions<
T extends Model[],
D extends Record<string, any>
>(
/** The model which we want to select options */
model: T,
/** Data from the database*/
data?: D[],
/** The key name where we find the label of the model */
valueKey?: keyof D,
/** If we have a translatable field we pass the current locale */
locale?: string | null
) {
const options = (model && model.length) ? model.map((opt) => {
const raw = opt[valueKey as keyof typeof opt] as unknown as string;
return {
value: opt.id.toString(),
label: locale && typeof raw === "object"
? raw[locale]
: raw
};
}) : [];
const defaultValue: string[] = data && valueKey
? data.map((item) => {
return item.id.toString();
})
: [];
return { options, defaultValue };
}