85 lines
3.1 KiB
TypeScript
85 lines
3.1 KiB
TypeScript
"use client";
|
|
|
|
import { ArticleCreate, ArticleUpsert, Manufacturer } from "../../types/catalog"
|
|
import { useFieldArray, useFormContext } from "react-hook-form"
|
|
import { InputTextR } from "../ui/inputTextR";
|
|
import ComboBoxRHF from "../ui/ComboBoxR";
|
|
import { useState } from "react";
|
|
import Modal from "../ui/modal";
|
|
import { ManufactuererForm } from "../manufacturer/manufacturer.form";
|
|
import SelectAndSearch2 from "../ui/SelectAndSearch2";
|
|
|
|
|
|
interface Params {
|
|
manufacturer: Manufacturer[]
|
|
onOpenManufacturer: (index: number) => void
|
|
}
|
|
|
|
export const ArticleVariantForm = ({ manufacturer, onOpenManufacturer }: Params) => {
|
|
|
|
const { control, register, getValues, setValue } = useFormContext<ArticleUpsert>();
|
|
const { fields, append, remove } = useFieldArray({ control, name: "variants", });
|
|
const [creatManufacturer, setCreatManufacturer] = useState(false);
|
|
|
|
const handleRemove = (index: number) => {
|
|
|
|
const id = getValues(`variants.${index}._id`);
|
|
if (!id) {
|
|
remove(index);
|
|
return;
|
|
}
|
|
const x = getValues(`variants.${index}.deleted`) ? true : false;
|
|
|
|
setValue(`variants.${index}.deleted`, !x, { shouldDirty: true })
|
|
}
|
|
|
|
const hersteller = manufacturer.map((i) => ({ id: i._id, value: i.name }))
|
|
|
|
return (
|
|
<div className="mt-4 border-1 rounded border-gray-200 p-2">
|
|
|
|
<div>
|
|
<button type="button" onClick={() => append({ manufacturerId: "", packagingUnit: "1" })} className="bg-sky-400 rounded-md p-1 mt-2 mb-2 ps-2 pe-2">
|
|
+ Neue Variante
|
|
</button>
|
|
</div>
|
|
|
|
|
|
|
|
<div className="flex flex-col gap-2 mb-2">
|
|
<div className="grid grid-cols-12 gap-4 ">
|
|
<div className="col-span-4" >
|
|
Hersteller
|
|
</div>
|
|
<div className="col-span-3" >
|
|
Packungseinheit
|
|
</div>
|
|
<div className="col-span-3" >
|
|
Aktion
|
|
</div>
|
|
</div>
|
|
<div className="border-1"></div>
|
|
{fields.map((field, index) => (
|
|
<div key={field.id} className="grid grid-cols-12 gap-4 ">
|
|
<div className="col-span-4" >
|
|
<ComboBoxRHF name={`variants.${index}.manufacturerId`} control={control} options={hersteller} placeholder="wählen..." />
|
|
|
|
<button onClick={() => onOpenManufacturer(index)}>+</button>
|
|
</div>
|
|
<div className="col-span-3" >
|
|
<InputTextR {...register(`variants.${index}.packagingUnit`)} />
|
|
</div>
|
|
<div className="col-span-3" >
|
|
<button type="button" onClick={() => handleRemove(index)} className={`bg-gray-200 rounded-md ps-2 pe-2 `}>
|
|
löschen
|
|
</button>
|
|
</div>
|
|
</div>
|
|
))}
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
)
|
|
} |