Initial commit: NestJS NextJS pnpm monorepo
This commit is contained in:
commit
610dcf202b
175 files changed
+25172
No files matched your search
@@ -0,0 +1,26 @@
|
||||
"use client";
|
||||
|
||||
import { ArticleCreate, ArticleUpsert, Category } from "../../types/catalog"
|
||||
import { useFormContext } from "react-hook-form"
|
||||
import { InputTextR } from "../ui/inputTextR";
|
||||
import ComboBoxR from "../ui/ComboBoxR";
|
||||
|
||||
|
||||
interface Params {
|
||||
categories: Category[]
|
||||
}
|
||||
|
||||
export const ArticleBaseForm = ({ categories }: Params) => {
|
||||
|
||||
const { register, control } = useFormContext<ArticleUpsert>();
|
||||
|
||||
const values = categories.map((i) => ({ id: i._id, value: i.name }))
|
||||
return (
|
||||
<>
|
||||
<InputTextR label="Artikename" {...register("name", { required: "Pflichtfeld" })} />
|
||||
<ComboBoxR name="categoryId" control={control} options={values} placeholder="wählen..." />
|
||||
</>
|
||||
|
||||
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
"use client";
|
||||
|
||||
import { useRouter } from 'next/navigation'
|
||||
import { Article } from "../../types/catalog"
|
||||
|
||||
import { articleDelete } from "../../actions/article.actions";
|
||||
import { sites } from "../../_config/sites";
|
||||
import Link from "next/link";
|
||||
import { Eye, OctagonX } from 'lucide-react';
|
||||
|
||||
|
||||
interface Params {
|
||||
articles: Article[]
|
||||
}
|
||||
|
||||
export const ArticleList = ({ articles }: Params) => {
|
||||
|
||||
|
||||
const router = useRouter()
|
||||
|
||||
const handlerOnDelete = (id: string) => {
|
||||
articleDelete(id)
|
||||
router.refresh();
|
||||
}
|
||||
|
||||
return (
|
||||
<div className='p-4 '>
|
||||
{articles.map((a) => (
|
||||
<div key={a._id} className="flex gap-4 border-b-1 p-4 border-gray-200">
|
||||
<div className='flex gap-4 wa'>
|
||||
<div className='w-32'>
|
||||
{a.name}
|
||||
</div>
|
||||
<div className='w-8'>
|
||||
<Link href={`${sites.article.update.url}/${a._id}`}>
|
||||
<Eye className=''></Eye>
|
||||
</Link>
|
||||
|
||||
|
||||
</div>
|
||||
<div className='w-8 '>
|
||||
<button className='flex gap-2' onClick={() => handlerOnDelete(a._id)}>
|
||||
<OctagonX className='' enableBackground={'green'} color='red'></OctagonX>
|
||||
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>))}
|
||||
|
||||
</div>
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
"use client";
|
||||
|
||||
import { emptyArticleDto, Article, ArticleCreate, Category, Manufacturer, ArticleUpsert } from "../../types/catalog"
|
||||
import { FormProvider, useForm } from "react-hook-form"
|
||||
import { ArticleBaseForm } from "./articleBase.form";
|
||||
import { ArticleVariantForm } from "./articleVariants.form";
|
||||
import { articleCreate, articleUpdate } from "../../actions/article.actions";
|
||||
import { useEffect, useState } from "react";
|
||||
import Modal from "../ui/modal";
|
||||
import { ManufactuererForm } from "../manufacturer/manufacturer.form";
|
||||
|
||||
|
||||
interface Params {
|
||||
categories: Category[]
|
||||
manufacturer: Manufacturer[]
|
||||
initData?: ArticleUpsert
|
||||
debug?: boolean
|
||||
}
|
||||
|
||||
|
||||
export const ArticleMainForm = ({ categories, manufacturer, initData, debug }: Params) => {
|
||||
|
||||
const methods = useForm<ArticleUpsert>({ defaultValues: emptyArticleDto });
|
||||
const [errMsg, setErrorMsg] = useState<string[]>([])
|
||||
const [error, setError] = useState(false)
|
||||
const [creatManufacturer, setCreatManufacturer] = useState(-1);
|
||||
const [hersteller,setHersteller] = useState<Manufacturer[]>([])
|
||||
|
||||
useEffect(() => {
|
||||
setHersteller(manufacturer);
|
||||
if (initData && initData._id) {
|
||||
|
||||
methods.reset(initData);
|
||||
}
|
||||
}, [initData])
|
||||
|
||||
const handlerCreatManufacturer = (data: Manufacturer) => {
|
||||
console.log('Create')
|
||||
setHersteller((cur)=>([...cur,data]));
|
||||
|
||||
|
||||
methods.setValue(
|
||||
`variants.${creatManufacturer}.manufacturerId`,
|
||||
data._id,
|
||||
{ shouldDirty: true, shouldValidate: true }
|
||||
);
|
||||
setCreatManufacturer(-1)
|
||||
}
|
||||
|
||||
const onSubmit = async (data: ArticleUpsert) => {
|
||||
if (initData?._id) {
|
||||
|
||||
const result = await articleUpdate(initData._id, data)
|
||||
if (!result.success) {
|
||||
setError(true)
|
||||
setErrorMsg(() => result.errors)
|
||||
console.log(result)
|
||||
} else {
|
||||
setError(false)
|
||||
|
||||
}
|
||||
} else {
|
||||
console.log('create')
|
||||
|
||||
const { name, categoryId, variants, attributes } = data;
|
||||
if (categoryId) {
|
||||
const create = {
|
||||
name,
|
||||
categoryId,
|
||||
variants,
|
||||
attributes
|
||||
}
|
||||
articleCreate(create);
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="me-30 flex flex-col p-4 pt-10 ps-10" >
|
||||
<FormProvider {...methods}>
|
||||
{initData?._id && <div>{initData._id}</div>}
|
||||
<form onSubmit={methods.handleSubmit(onSubmit)}>
|
||||
<ArticleBaseForm categories={categories}></ArticleBaseForm>
|
||||
<ArticleVariantForm manufacturer={hersteller} onOpenManufacturer={(index) => setCreatManufacturer(index)}></ArticleVariantForm>
|
||||
|
||||
<div>{methods.watch("name")}</div>
|
||||
<pre>{JSON.stringify(methods.watch(), null, 2)}</pre>
|
||||
<button type="submit">Absenden</button>
|
||||
</form>
|
||||
|
||||
</FormProvider>
|
||||
<Modal open={creatManufacturer >= 0} onClose={() => setCreatManufacturer(-1)}>
|
||||
<ManufactuererForm onChangeData={(d) => handlerCreatManufacturer(d)}></ManufactuererForm>
|
||||
<button onClick={() => setCreatManufacturer(-1)}>Abbruch</button>
|
||||
</Modal>
|
||||
{error && <div className="border-1 border-red-400 bg-red-200">
|
||||
Error
|
||||
{errMsg.map((msg, id) => (<div key={id}>{msg}</div>))}
|
||||
</div>}
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
"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>
|
||||
|
||||
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react"
|
||||
import { Category, CategoryCreate, emptyCategory } from "../../types/catalog/category.types"
|
||||
import { useForm } from "react-hook-form"
|
||||
import { InputTextR } from "../ui/inputTextR";
|
||||
import { InputCheckboxR } from "../ui/inputCheckboxR";
|
||||
import { categoryCreate, categoryUpdate } from '../../actions/category.actions'
|
||||
|
||||
|
||||
interface Params {
|
||||
initData?: Category
|
||||
debug?: boolean
|
||||
}
|
||||
|
||||
export const CategoryForm = ({ initData, debug }: Params) => {
|
||||
|
||||
const { register, handleSubmit, formState: { errors }, watch, getFieldState, reset } = useForm<CategoryCreate>({ defaultValues: emptyCategory, })
|
||||
const [modeUpdate, setModeUpdate] = useState(false);
|
||||
const [id, setId] = useState<string | undefined>();
|
||||
const [error, setError] = useState<string | undefined>();
|
||||
|
||||
useEffect(() => {
|
||||
if (initData) {
|
||||
const { _id, name, description, isConsumable } = initData;
|
||||
setId(_id);
|
||||
setModeUpdate(true);
|
||||
reset({ name, description, isConsumable })
|
||||
}
|
||||
}, []);
|
||||
|
||||
const onSubmit = async (data: CategoryCreate) => {
|
||||
setError(undefined);
|
||||
//const { name, isConsumable, description } = data;
|
||||
if (modeUpdate) {
|
||||
if(id){
|
||||
categoryUpdate(id,data);
|
||||
}else{
|
||||
setError('Etwas ist falsch, keine ID');
|
||||
}
|
||||
|
||||
} else {
|
||||
categoryCreate(data);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="w-120 flex flex-col p-4 pt-10 ps-10" >
|
||||
<div className="border-1 p-4">
|
||||
<form onSubmit={handleSubmit(onSubmit)} className="max-w-md mx-auto">
|
||||
<InputTextR label="Kategorie Name" {...register("name", { required: "Pflichtfeld" })} error={errors.name?.message} />
|
||||
<InputTextR label="Beschreibung" {...register("description")} error={errors.description?.message} />
|
||||
<div className="mt-4 mb-4 p-2 border-1 border-gray-200 relative ">
|
||||
<div className="absolute -top-2 left-2 bg-white ps-1 pe-1 text-xs">Optionen</div>
|
||||
<InputCheckboxR label="Konsumierbar" {...register("isConsumable")} error={errors.isConsumable?.message} />
|
||||
</div>
|
||||
<div>
|
||||
{error && <div>{error}</div>}
|
||||
{errors.name && <div>{errors.name.message}</div>}
|
||||
</div>
|
||||
<button type="submit" className="bg-blue-500 text-white p-2 rounded mt-2 hover:bg-blue-600">Speichern</button>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
{debug &&
|
||||
<div>
|
||||
<div>{watch("name")}</div>
|
||||
<pre>touched:{getFieldState("name").isTouched ? 'true' : 'false'}</pre>
|
||||
<pre>{JSON.stringify(watch(), null, 2)}</pre>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
|
||||
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import { useEffect, useState } from "react"
|
||||
|
||||
interface ChangeValue {
|
||||
[key: string]: boolean
|
||||
}
|
||||
|
||||
interface Props {
|
||||
label?:string
|
||||
name: string
|
||||
valueInit?: boolean,
|
||||
onChange: (value: boolean) => void
|
||||
}
|
||||
|
||||
export const InputCheckbox = ({ label, valueInit, onChange, name }: Props) => {
|
||||
|
||||
const [checked, setChecked] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (valueInit) {
|
||||
setChecked(valueInit);
|
||||
}
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
const obj = { [name]: checked };
|
||||
onChange(checked);
|
||||
}, [checked])
|
||||
|
||||
return (
|
||||
<div className="border-1 border-gray-100 p-2 mb-1 mt-1 bg-green-100 flex items-center">
|
||||
<input type="checkbox" className=" p-1 me-2 w-8 h-8 " name={name} value={label} checked={checked} onChange={(e) => setChecked((pre)=>!pre)} />
|
||||
{label && <div className="text-sm font-bold text-black font-mono ps-1">{label}</div>}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import { useEffect, useState } from "react"
|
||||
|
||||
interface ChangeValue {
|
||||
[key: string]: string
|
||||
}
|
||||
|
||||
interface Props {
|
||||
label?:string
|
||||
name: string
|
||||
valueInit?: string,
|
||||
onTxtChange: (txt: ChangeValue) => void
|
||||
}
|
||||
|
||||
export const InputText = ({ label, valueInit, onTxtChange, name }: Props) => {
|
||||
|
||||
const [txt, setTxt] = useState('');
|
||||
|
||||
useEffect(() => {
|
||||
if (valueInit) {
|
||||
setTxt(valueInit);
|
||||
}
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
const obj = { [name]: txt };
|
||||
onTxtChange(obj);
|
||||
}, [txt])
|
||||
|
||||
return (
|
||||
<div className="border-1 border-gray-100 p-2 mb-1 mt-1 bg-green-100">
|
||||
{label && <div className="text-sm font-bold text-black font-mono ps-1">{label}</div>}
|
||||
<input className="bg-white border-1 rounded p-1 w-full" name={name} value={txt} onChange={(e) => setTxt(e.target.value)} />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
"use client"
|
||||
|
||||
import { useState } from "react";
|
||||
import { LocationTreeNode } from "../../types/inventory";
|
||||
|
||||
type Props = {
|
||||
node: LocationTreeNode;
|
||||
selectedId: string | null;
|
||||
focusedId: string | null;
|
||||
openMap: Record<string, boolean>;
|
||||
setOpenMap: React.Dispatch<React.SetStateAction<Record<string, boolean>>>;
|
||||
setFocusedId: (id: string) => void;
|
||||
onSelect: (id: string ) => void;
|
||||
};
|
||||
|
||||
export function LocationTreeNodeItem({ node, selectedId, focusedId, openMap, setOpenMap, setFocusedId, onSelect }: Props) {
|
||||
|
||||
const hasChildren = node.children.length > 0;
|
||||
const isOpen = openMap[node._id];
|
||||
|
||||
return (
|
||||
<div className="ml-4" >
|
||||
<div className="flex items-center gap-2">
|
||||
{hasChildren && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setOpenMap(m => ({ ...m, [node._id]: !isOpen }))}
|
||||
className="w-6"
|
||||
>
|
||||
{isOpen ? "▼" : "▶"}
|
||||
</button>
|
||||
)}
|
||||
|
||||
{!hasChildren && <span className="w-6" />}
|
||||
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => onSelect(node._id)}
|
||||
onFocus={() => setFocusedId(node._id)}
|
||||
className={`text-left px-1 rounded ${selectedId === node._id ? "font-bold text-blue-600" : ""
|
||||
} ${focusedId === node._id ? "bg-blue-100" : ""
|
||||
}`}
|
||||
>
|
||||
{node.name}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{isOpen && hasChildren && (
|
||||
<ul className="ml-6 mt-1 border-l pl-2">
|
||||
{node.children.map(child => (
|
||||
<LocationTreeNodeItem
|
||||
key={child._id}
|
||||
node={child}
|
||||
selectedId={selectedId}
|
||||
focusedId={focusedId}
|
||||
openMap={openMap}
|
||||
setOpenMap={setOpenMap}
|
||||
setFocusedId={setFocusedId}
|
||||
onSelect={onSelect}
|
||||
/>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react"
|
||||
import { Controller, useForm } from "react-hook-form"
|
||||
import { InputTextR } from "../ui/inputTextR";
|
||||
import { emptyLocation, Location, LocationCreate, LocationMutaion, LocationTreeNode, LocationUpdate } from "../../types/inventory";
|
||||
import { locationCreate, locationTree, locationUpdate } from "../../actions/location.actions";
|
||||
|
||||
import { useRouter } from "next/navigation"
|
||||
import { sites } from "../../_config/sites";
|
||||
import { LocationTreeView } from "./location.treeView";
|
||||
|
||||
interface Params {
|
||||
initData?: Location
|
||||
debug?: boolean
|
||||
|
||||
}
|
||||
|
||||
export const LocationForm = ({ initData, debug }: Params) => {
|
||||
|
||||
const [locationTreeData, setLocationTree] = useState<LocationTreeNode[]>()
|
||||
|
||||
useEffect(() => {
|
||||
const load = async () => {
|
||||
|
||||
let d = await locationTree();
|
||||
setLocationTree(d);
|
||||
}
|
||||
load();
|
||||
|
||||
if (initData) {
|
||||
const { _id, name, description, parentLocationId } = initData;
|
||||
setId(_id);
|
||||
setModeUpdate(true);
|
||||
reset({ name, description, parentLocationId })
|
||||
}
|
||||
|
||||
}, [initData])
|
||||
|
||||
|
||||
const router = useRouter();
|
||||
const {
|
||||
register,
|
||||
handleSubmit,
|
||||
formState: { errors },
|
||||
watch,
|
||||
control,
|
||||
getFieldState,
|
||||
|
||||
reset }
|
||||
= useForm<LocationMutaion>({ defaultValues: emptyLocation })
|
||||
|
||||
const [modeUpdate, setModeUpdate] = useState(false);
|
||||
const [id, setId] = useState<string | undefined>();
|
||||
const [error, setError] = useState<string | undefined>();
|
||||
|
||||
|
||||
|
||||
const onSubmit = async (data: LocationMutaion) => {
|
||||
setError(undefined);
|
||||
|
||||
if (modeUpdate) {
|
||||
if (id) {
|
||||
locationUpdate(id, data);
|
||||
} else {
|
||||
setError('Etwas ist falsch, keine ID');
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
const res = await locationCreate(data);
|
||||
/* if (!res.ok) {
|
||||
setError("_id")
|
||||
return
|
||||
}
|
||||
*/
|
||||
router.push(sites.location.url)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="w-120 flex flex-col p-4 pt-10 ps-10" >
|
||||
{modeUpdate ? <div>Update</div> : <div>Anlegen</div>}
|
||||
<div className="border-1 p-4">
|
||||
|
||||
<form onSubmit={handleSubmit(onSubmit)} className="max-w-md mx-auto">
|
||||
<InputTextR
|
||||
label="Lagerort Bezeichnung"
|
||||
{...register("name", { required: "Pflichtfeld" })}
|
||||
error={errors.name?.message}
|
||||
/>
|
||||
|
||||
<InputTextR
|
||||
label="Beschreibung"
|
||||
{...register("description")}
|
||||
error={errors.description?.message}
|
||||
/>
|
||||
{locationTreeData &&
|
||||
<Controller
|
||||
name="parentLocationId"
|
||||
control={control}
|
||||
rules={{ required: false }}
|
||||
render={({ field }) => (
|
||||
<LocationTreeView data={locationTreeData}
|
||||
onSelect={(id) => field.onChange(id)}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
}
|
||||
|
||||
<div>
|
||||
{error && <div>{error}</div>}
|
||||
{errors.name && <div>{errors.name.message}</div>}
|
||||
</div>
|
||||
|
||||
<button
|
||||
className="bg-blue-500 text-white p-2 rounded mt-2 hover:bg-blue-600"
|
||||
type="submit">
|
||||
Speichern
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
{debug &&
|
||||
<div>
|
||||
<div>{watch("name")}</div>
|
||||
<pre>touched:{getFieldState("name").isTouched ? 'true' : 'false'}</pre>
|
||||
<pre>{JSON.stringify(watch(), null, 2)}</pre>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
"use client"
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import { LocationTreeNode } from "../../types/inventory";
|
||||
import { LocationTreeNodeItem } from "./LocationTreeNodeItem";
|
||||
|
||||
|
||||
|
||||
type Props = {
|
||||
data: LocationTreeNode[];
|
||||
onSelect: (node: LocationTreeNode) => void;
|
||||
};
|
||||
|
||||
|
||||
export function LocationTreeView({ data, onSelect }: Props) {
|
||||
const [selectedId, setSelectedId] = useState<string | null>(null);
|
||||
const [focusedId, setFocusedId] = useState<string | null>(null);
|
||||
const [openMap, setOpenMap] = useState<Record<string, boolean>>({});
|
||||
|
||||
// sichtbare Nodes flach machen
|
||||
const visibleNodes = useMemo(() => {
|
||||
const result: LocationTreeNode[] = [];
|
||||
|
||||
function walk(nodes: LocationTreeNode[]) {
|
||||
for (const n of nodes) {
|
||||
result.push(n);
|
||||
if (openMap[n._id]) {
|
||||
walk(n.children);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
walk(data);
|
||||
return result;
|
||||
}, [data, openMap]);
|
||||
|
||||
const handleSelect = (id:string)=>{
|
||||
let node = visibleNodes.find((node)=> node._id === id)
|
||||
console.log('nodex')
|
||||
if(node) onSelect(node);
|
||||
}
|
||||
|
||||
// initial focus
|
||||
useEffect(() => {
|
||||
if (!focusedId && visibleNodes.length > 0) {
|
||||
setFocusedId(visibleNodes[0]._id);
|
||||
}
|
||||
console.log(visibleNodes)
|
||||
}, [visibleNodes, focusedId]);
|
||||
|
||||
function handleKeyDown(e: React.KeyboardEvent) {
|
||||
if (!focusedId) return;
|
||||
|
||||
const index = visibleNodes.findIndex(n => n._id === focusedId);
|
||||
|
||||
if (e.key === "ArrowDown") {
|
||||
console.log('down')
|
||||
e.preventDefault();
|
||||
const next = visibleNodes[index + 1];
|
||||
if (next) setFocusedId(next._id);
|
||||
}
|
||||
|
||||
if (e.key === "ArrowUp") {
|
||||
console.log('up')
|
||||
e.preventDefault();
|
||||
const prev = visibleNodes[index - 1];
|
||||
if (prev) setFocusedId(prev._id);
|
||||
}
|
||||
|
||||
if (e.key === "ArrowRight") {
|
||||
console.log('right')
|
||||
e.preventDefault();
|
||||
setOpenMap(m => ({ ...m, [focusedId]: true }));
|
||||
}
|
||||
|
||||
if (e.key === "ArrowLeft") {
|
||||
console.log('left')
|
||||
e.preventDefault();
|
||||
setOpenMap(m => ({ ...m, [focusedId]: false }));
|
||||
}
|
||||
|
||||
if (e.key === "Enter") {
|
||||
e.preventDefault();
|
||||
setSelectedId(focusedId);
|
||||
handleSelect(focusedId);
|
||||
}
|
||||
if (e.key === "Space") {
|
||||
e.preventDefault();
|
||||
setSelectedId(focusedId);
|
||||
handleSelect(focusedId);
|
||||
}
|
||||
console.log(e.key)
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
tabIndex={0}
|
||||
onKeyDown={handleKeyDown}
|
||||
className="border p-2 rounded outline-none focus:ring"
|
||||
>
|
||||
{data.map(node => (
|
||||
<LocationTreeNodeItem
|
||||
key={node._id}
|
||||
node={node}
|
||||
selectedId={selectedId}
|
||||
focusedId={focusedId}
|
||||
openMap={openMap}
|
||||
setOpenMap={setOpenMap}
|
||||
setFocusedId={setFocusedId}
|
||||
onSelect={(id) => { setSelectedId(id); setFocusedId(id); handleSelect(id); }} />
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { LocationTreeNode, Location } from "../../types/inventory";
|
||||
import { LocationTreeView } from "./location.treeView";
|
||||
import { locationFindById } from "../../actions/location.actions";
|
||||
import { LocationForm } from "./location.form";
|
||||
|
||||
|
||||
interface Params {
|
||||
data: LocationTreeNode[]
|
||||
}
|
||||
|
||||
export function LocationView({ data }: Params) {
|
||||
const [selectedLocation, setSelectedLocation] = useState<Location | undefined>();
|
||||
|
||||
const handleSelect = async (node: LocationTreeNode) => {
|
||||
console.log('node',node);
|
||||
// const node = await locationFindById(value);
|
||||
setSelectedLocation(() => node);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="w-120 flex flex-col p-4 pt-10 ps-10">
|
||||
<div>xxx</div>
|
||||
<LocationTreeView data={data} onSelect={(item) => handleSelect(item)}></LocationTreeView>
|
||||
{selectedLocation &&
|
||||
<LocationForm initData={selectedLocation}></LocationForm>
|
||||
}
|
||||
</div>
|
||||
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react"
|
||||
import { Manufacturer, ManufacturerCreate, emptyManufacturer } from "../../types/catalog/manufacturer.types"
|
||||
import { useForm } from "react-hook-form"
|
||||
import { InputTextR } from "../ui/inputTextR";
|
||||
import { manufacturerCreate, manufacturerUpdate } from '../../actions/manufacturer.actions'
|
||||
|
||||
|
||||
interface Params {
|
||||
initData?: Manufacturer
|
||||
debug?: boolean,
|
||||
onChangeData?:(data:Manufacturer)=>void
|
||||
}
|
||||
|
||||
export const ManufactuererForm = ({ onChangeData, initData, debug }: Params) => {
|
||||
|
||||
const { register, handleSubmit, formState: { errors }, watch, getFieldState, reset } = useForm<ManufacturerCreate>({ defaultValues: emptyManufacturer, })
|
||||
const [modeUpdate, setModeUpdate] = useState(false);
|
||||
const [id, setId] = useState<string | undefined>();
|
||||
const [error, setError] = useState(false);
|
||||
const [errMsg, setErrMsg] = useState<string[]>([])
|
||||
|
||||
useEffect(() => {
|
||||
if (initData) {
|
||||
const { _id, name } = initData;
|
||||
setId(_id);
|
||||
setModeUpdate(true);
|
||||
reset({ name })
|
||||
}
|
||||
}, []);
|
||||
|
||||
const onSubmit = async (data: ManufacturerCreate) => {
|
||||
setError(false);
|
||||
|
||||
let result: ActionResult = { success: false, errors: [] };
|
||||
if (modeUpdate) {
|
||||
if (!id) {
|
||||
setError(true);
|
||||
setErrMsg(['Etwas ist falsch, keine ID']);
|
||||
return;
|
||||
}
|
||||
result = await manufacturerUpdate(id, data);
|
||||
} else {
|
||||
result = await manufacturerCreate(data);
|
||||
}
|
||||
|
||||
|
||||
if (!result.success) {
|
||||
setError(true)
|
||||
setErrMsg(() => result.errors)
|
||||
} else {
|
||||
console.log(result)
|
||||
onChangeData && onChangeData({_id:result.data._id,name:result.data.name})
|
||||
|
||||
setError(false)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="w-120 flex flex-col p-4 pt-10 ps-10" >
|
||||
<div className="border-1 p-4">
|
||||
<form onSubmit={handleSubmit(onSubmit)} className="max-w-md mx-auto">
|
||||
<InputTextR label="Name" {...register("name", { required: "Pflichtfeld" })} error={errors.name?.message} />
|
||||
|
||||
<div>
|
||||
{error && <div>{error}</div>}
|
||||
{errors.name && <div>{errors.name.message}</div>}
|
||||
</div>
|
||||
<button type="submit" className="bg-blue-500 text-white p-2 rounded mt-2 hover:bg-blue-600">Speichern</button>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
{debug &&
|
||||
<div>
|
||||
<div>{watch("name")}</div>
|
||||
<pre>touched:{getFieldState("name").isTouched ? 'true' : 'false'}</pre>
|
||||
<pre>{JSON.stringify(watch(), null, 2)}</pre>
|
||||
</div>
|
||||
}
|
||||
{error && <div className="border-1 border-red-400 bg-red-200">
|
||||
Error
|
||||
{errMsg.map((msg,id)=>(<div key={id}>{msg}</div>))}
|
||||
</div>}
|
||||
</div>
|
||||
|
||||
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
"use client";
|
||||
|
||||
import { emptyArticleDto, Article, ArticleCreate, Category, Manufacturer, ArticleVariant } from "../../types/catalog"
|
||||
import { FormProvider, useForm } from "react-hook-form"
|
||||
import { articleCreate, articleUpdate } from "../../actions/article.actions";
|
||||
import { useEffect, useState } from "react";
|
||||
import Modal from "../ui/modal";
|
||||
import { ManufactuererForm } from "../manufacturer/manufacturer.form";
|
||||
import { Location, LocationTreeNode } from "../../types/inventory";
|
||||
import { emptyStock, Stock, StockCreate } from "../../types/inventory/stockContent.types";
|
||||
import { stockCreate, stockUpdate } from "../../actions/stock.actions";
|
||||
import { InputTextR } from "../ui/inputTextR";
|
||||
import { LocationView } from "../location/location.view";
|
||||
import { LocationTreeView } from "../location/location.treeView";
|
||||
|
||||
|
||||
interface Params {
|
||||
categories: Category[]
|
||||
article: Article[],
|
||||
orte: LocationTreeNode[]
|
||||
initData?: Stock
|
||||
debug?: boolean
|
||||
}
|
||||
|
||||
|
||||
export const StockForm = ({ article, categories, initData, orte, debug }: Params) => {
|
||||
|
||||
const { register, reset, formState: { errors }, handleSubmit } = useForm<StockCreate>({ defaultValues: emptyStock });
|
||||
const [errMsg, setErrorMsg] = useState<string[]>([])
|
||||
const [selectetCategory, setSelectedCategory] = useState<Category>()
|
||||
const [selectetArticle, setSelectedArticle] = useState<Article>()
|
||||
const [selectetArticleVariant, setSelectedArticleVariant] = useState<ArticleVariant>()
|
||||
const [selectetOrt, setSelectedOrt] = useState<Location>()
|
||||
|
||||
const [error, setError] = useState(false)
|
||||
|
||||
useEffect(() => {
|
||||
|
||||
if (initData && initData.id) {
|
||||
|
||||
reset(initData);
|
||||
}
|
||||
}, [initData])
|
||||
|
||||
|
||||
|
||||
|
||||
const onSubmit = async (data: StockCreate) => {
|
||||
if (initData?.id) {
|
||||
|
||||
const result = await stockUpdate(initData.id, data)
|
||||
if (!result.success) {
|
||||
setError(true)
|
||||
setErrorMsg(() => result.errors)
|
||||
console.log(result)
|
||||
} else {
|
||||
setError(false)
|
||||
|
||||
}
|
||||
} else {
|
||||
console.log('create')
|
||||
|
||||
const { articleVariantId, locationId, quantity, expiryDate, batchNumber } = data;
|
||||
if (true) {
|
||||
const create = {
|
||||
articleVariantId,
|
||||
locationId,
|
||||
quantity,
|
||||
expiryDate,
|
||||
batchNumber
|
||||
}
|
||||
stockCreate(create);
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
const handleLocationSelect = (ort: LocationTreeNode) => {
|
||||
setSelectedOrt(ort);
|
||||
}
|
||||
|
||||
|
||||
return (
|
||||
<div className="me-30 flex flex-col p-4 pt-10 ps-10" >
|
||||
|
||||
{initData?.id && <div>{initData.id}</div>}
|
||||
<form onSubmit={handleSubmit(onSubmit)}>
|
||||
|
||||
<InputTextR
|
||||
label="Menge"
|
||||
{...register("quantity", { required: "Pflichtfeld" })}
|
||||
error={errors.quantity?.message}
|
||||
/>
|
||||
|
||||
<button type="submit">Absenden</button>
|
||||
</form>
|
||||
<LocationTreeView data={orte} onSelect={(item) => handleLocationSelect(item)}></LocationTreeView>
|
||||
|
||||
{error && <div className="border-1 border-red-400 bg-red-200">
|
||||
Error
|
||||
{errMsg.map((msg, id) => (<div key={id}>{msg}</div>))}
|
||||
</div>}
|
||||
{selectetOrt && <div>{selectetOrt.name}</div>}
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react"
|
||||
import { InputText } from "../form/inputText"
|
||||
|
||||
|
||||
interface Values{
|
||||
name:string,
|
||||
sonstiges:string
|
||||
}
|
||||
|
||||
export const TestForm = ()=>{
|
||||
const [values,setValues] = useState<Values>({name:"",sonstiges:""})
|
||||
return (
|
||||
<div>
|
||||
<InputText label="Name" name="name" valueInit="wert" onTxtChange={(v)=>setValues((pre)=>({...pre,...v}))} />
|
||||
<div>
|
||||
<div>name:{values.name}</div>
|
||||
<div>sonstiges:{values.sonstiges}</div>
|
||||
|
||||
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
"use client";
|
||||
import { useState, useRef, useEffect } from "react";
|
||||
import { Control, useController } from "react-hook-form";
|
||||
|
||||
export type Option = {
|
||||
id: string;
|
||||
value: string;
|
||||
};
|
||||
|
||||
type ComboBoxRHFProps = {
|
||||
name: string;
|
||||
control: Control<any>;
|
||||
options: Option[];
|
||||
placeholder?: string;
|
||||
};
|
||||
|
||||
export default function ComboBoxR({
|
||||
name,
|
||||
control,
|
||||
options,
|
||||
placeholder = "Option wählen...",
|
||||
}: ComboBoxRHFProps) {
|
||||
const { field: { value: formValue, onChange, onBlur }, } = useController({ name, control });
|
||||
|
||||
const [open, setOpen] = useState(false);
|
||||
const [highlight, setHighlight] = useState(0);
|
||||
const [inputValue, setInputValue] = useState("");
|
||||
const wrapperRef = useRef<HTMLDivElement | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
console.log('Change Value' + formValue)
|
||||
}, [formValue])
|
||||
// Sync Input mit RHF-Wert (wenn z.B. defaultValue gesetzt ist)
|
||||
useEffect(() => {
|
||||
console.log(`FromValue(${formValue})`)
|
||||
const selected = options.find((o) => o.id === formValue);
|
||||
if (selected) {
|
||||
setInputValue(selected.value);
|
||||
}
|
||||
}, [formValue, options]);
|
||||
|
||||
// Klick außerhalb schließen
|
||||
useEffect(() => {
|
||||
function handleClickOutside(e: MouseEvent) {
|
||||
if (
|
||||
wrapperRef.current &&
|
||||
!wrapperRef.current.contains(e.target as Node)
|
||||
) {
|
||||
setOpen(false);
|
||||
onBlur();
|
||||
}
|
||||
}
|
||||
document.addEventListener("mousedown", handleClickOutside);
|
||||
return () =>
|
||||
document.removeEventListener("mousedown", handleClickOutside);
|
||||
}, [onBlur]);
|
||||
|
||||
const filtered = options.filter((opt) =>
|
||||
opt.value.toLowerCase().includes(inputValue.toLowerCase())
|
||||
);
|
||||
|
||||
function selectOption(opt: Option) {
|
||||
setInputValue(opt.value); // Text anzeigen
|
||||
onChange(opt.id); // ID ins Form-State
|
||||
setOpen(false);
|
||||
}
|
||||
|
||||
function handleKeyDown(e: React.KeyboardEvent<HTMLInputElement>) {
|
||||
if (!open) setOpen(true);
|
||||
|
||||
if (e.key === "ArrowDown") {
|
||||
e.preventDefault();
|
||||
setHighlight((h) => Math.min(h + 1, filtered.length - 1));
|
||||
}
|
||||
|
||||
if (e.key === "ArrowUp") {
|
||||
e.preventDefault();
|
||||
setHighlight((h) => Math.max(h - 1, 0));
|
||||
}
|
||||
|
||||
if (e.key === "Enter") {
|
||||
e.preventDefault();
|
||||
selectOption(filtered[highlight]);
|
||||
}
|
||||
|
||||
if (e.key === "Escape") {
|
||||
setOpen(false);
|
||||
}
|
||||
|
||||
if (e.key === "Tab") {
|
||||
setOpen(false);
|
||||
onBlur();
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div ref={wrapperRef} className="relative w-64">
|
||||
<input
|
||||
value={inputValue}
|
||||
placeholder={placeholder}
|
||||
onChange={(e) => {
|
||||
setInputValue(e.target.value); // ✅ frei tippen
|
||||
setOpen(true);
|
||||
}}
|
||||
onFocus={() => setOpen(true)}
|
||||
onBlur={(e) => {
|
||||
if (!wrapperRef.current?.contains(e.relatedTarget as Node)) {
|
||||
setOpen(false);
|
||||
onBlur();
|
||||
}
|
||||
}}
|
||||
onKeyDown={handleKeyDown}
|
||||
className="w-full border border-gray-300 rounded px-3 py-2 focus:outline-none focus:ring-2 focus:ring-blue-500"
|
||||
/>
|
||||
|
||||
{open && filtered.length > 0 && (
|
||||
<ul className="absolute top-full left-0 w-full bg-white border border-gray-300 rounded shadow-lg max-h-40 overflow-y-auto z-50">
|
||||
{filtered.map((opt, i) => (
|
||||
<li
|
||||
key={opt.id}
|
||||
onMouseDown={() => selectOption(opt)}
|
||||
onMouseEnter={() => setHighlight(i)}
|
||||
className={`px-3 py-2 cursor-pointer ${i === highlight ? "bg-blue-100" : "hover:bg-gray-100"
|
||||
}`}
|
||||
>
|
||||
{opt.value}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
"use client";
|
||||
import { useState, useRef, useEffect } from "react";
|
||||
import { Control, useController } from "react-hook-form";
|
||||
|
||||
export type Option = {
|
||||
id: string;
|
||||
value: string;
|
||||
};
|
||||
|
||||
type ComboBoxRHFProps = {
|
||||
name: string;
|
||||
control: Control<any>;
|
||||
options: Option[];
|
||||
placeholder?: string;
|
||||
};
|
||||
|
||||
export default function ComboBoxR({ name, control, options, placeholder = "Option wählen...", }: ComboBoxRHFProps) {
|
||||
|
||||
const { field: { value: formValue, onChange, onBlur }, } = useController({ name, control });
|
||||
|
||||
const [open, setOpen] = useState(false);
|
||||
const [highlight, setHighlight] = useState(0);
|
||||
const [inputValue, setInputValue] = useState("");
|
||||
const wrapperRef = useRef<HTMLDivElement | null>(null);
|
||||
const inputRef = useRef<HTMLInputElement | null>(null);
|
||||
const divRef = useRef<HTMLDivElement | null>(null);
|
||||
|
||||
|
||||
// Sync Input mit RHF-Wert (wenn z.B. defaultValue gesetzt ist)
|
||||
useEffect(() => {
|
||||
const selected = options.find((o) => o.id === formValue);
|
||||
if (selected) setInputValue(selected.value);
|
||||
}, [formValue, options]);
|
||||
|
||||
useEffect(() => {
|
||||
if (open) {
|
||||
setInputValue("");
|
||||
requestAnimationFrame(() => {
|
||||
inputRef.current?.focus();
|
||||
});
|
||||
}
|
||||
}, [open]);
|
||||
|
||||
// Klick außerhalb schließen
|
||||
useEffect(() => {
|
||||
function handleClickOutside(e: MouseEvent) {
|
||||
console.log('Close outside',open)
|
||||
if (!open) return;
|
||||
console.log('Close outside2')
|
||||
if (wrapperRef.current && !wrapperRef.current.contains(e.target as Node)) {
|
||||
closeOnly();
|
||||
}
|
||||
}
|
||||
|
||||
document.addEventListener("mousedown", handleClickOutside);
|
||||
return () =>
|
||||
document.removeEventListener("mousedown", handleClickOutside);
|
||||
}, [open]);
|
||||
|
||||
function closeOnly() {
|
||||
console.log('Aufruf close only')
|
||||
|
||||
setOpen(false);
|
||||
onBlur();
|
||||
requestAnimationFrame(() => {
|
||||
divRef.current?.focus();
|
||||
});
|
||||
}
|
||||
|
||||
function closeAndFocusDiv() {
|
||||
setOpen(false);
|
||||
onBlur();
|
||||
requestAnimationFrame(() => {
|
||||
divRef.current?.focus();
|
||||
});
|
||||
}
|
||||
const filtered = options.filter((opt) =>
|
||||
opt.value.toLowerCase().includes(inputValue.toLowerCase())
|
||||
);
|
||||
|
||||
function selectOption(opt: Option) {
|
||||
setInputValue(opt.value); // Text anzeigen
|
||||
onChange(opt.id); // ID ins Form-State
|
||||
setOpen(false);
|
||||
|
||||
requestAnimationFrame(() => {
|
||||
divRef.current?.focus();
|
||||
});
|
||||
}
|
||||
|
||||
function handleKeyDown(e: React.KeyboardEvent<HTMLInputElement>) {
|
||||
if (!open) setOpen(true);
|
||||
|
||||
if (e.key === "ArrowDown") {
|
||||
e.preventDefault();
|
||||
setHighlight((h) => Math.min(h + 1, filtered.length - 1));
|
||||
}
|
||||
|
||||
if (e.key === "ArrowUp") {
|
||||
e.preventDefault();
|
||||
setHighlight((h) => Math.max(h - 1, 0));
|
||||
}
|
||||
|
||||
if (e.key === "Enter") {
|
||||
e.preventDefault();
|
||||
selectOption(filtered[highlight]);
|
||||
}
|
||||
|
||||
if (e.key === "Escape") {
|
||||
setOpen(false);
|
||||
}
|
||||
|
||||
if (e.key === "Tab") {
|
||||
closeAndFocusDiv();
|
||||
}
|
||||
}
|
||||
|
||||
const selectedValue = options.find((item) => item.id == formValue)
|
||||
|
||||
return (
|
||||
<div ref={wrapperRef} className="relative w-64">
|
||||
{open &&
|
||||
<input
|
||||
ref={inputRef}
|
||||
value={inputValue}
|
||||
placeholder={placeholder}
|
||||
onChange={(e) => { setInputValue(e.target.value); setOpen(true); }}
|
||||
onKeyDown={handleKeyDown}
|
||||
className="w-full border border-gray-300 rounded px-3 py-2 focus:outline-none focus:ring-2 focus:ring-blue-500"
|
||||
/>
|
||||
}
|
||||
|
||||
{!open && (
|
||||
<div
|
||||
ref={divRef}
|
||||
tabIndex={0}
|
||||
onClick={() => setOpen(true)}
|
||||
onKeyDown={(e) => {
|
||||
if (e.key === "Enter" || e.key === " ") {
|
||||
e.preventDefault();
|
||||
setOpen(true);
|
||||
}
|
||||
}}
|
||||
className="border px-3 py-2 rounded cursor-pointer focus:ring-2 focus:ring-blue-500"
|
||||
>
|
||||
{selectedValue?.value || placeholder}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{open && filtered.length > 0 && (
|
||||
<ul className="absolute top-full left-0 w-full bg-white border border-gray-300 rounded shadow-lg max-h-40 overflow-y-auto z-50">
|
||||
{filtered.map((opt, i) => (
|
||||
<li
|
||||
key={opt.id}
|
||||
onMouseDown={() => selectOption(opt)}
|
||||
onMouseEnter={() => setHighlight(i)}
|
||||
className={`px-3 py-2 cursor-pointer ${i === highlight ? "bg-blue-100" : "hover:bg-gray-100"
|
||||
}`}
|
||||
>
|
||||
{opt.value}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useRef, useEffect } from "react";
|
||||
import { createPortal } from "react-dom";
|
||||
import { ChevronDown } from "lucide-react";
|
||||
|
||||
|
||||
type DropdownItem = { id: string, value: string };
|
||||
|
||||
interface Props {
|
||||
placeholder?:string
|
||||
items: DropdownItem[];
|
||||
value?: DropdownItem | null; // id
|
||||
onChange?: (item: DropdownItem) => void;
|
||||
}
|
||||
|
||||
export default function SelectAndSearch2({ placeholder,items, value, onChange }: Props) {
|
||||
const [open, setOpen] = useState(false);
|
||||
const [selected, setSelected] = useState<DropdownItem>(value ?? {id:"",value:""});
|
||||
const [query, setQuery] = useState("");
|
||||
const [highlightIndex, setHighlightIndex] = useState(0);
|
||||
const [filtered, setFiltered] = useState<DropdownItem[]>(items);
|
||||
|
||||
const buttonRef = useRef<HTMLButtonElement>(null);
|
||||
const searchRef = useRef<HTMLInputElement>(null);
|
||||
const itemRefs = useRef<HTMLButtonElement[]>([]);
|
||||
|
||||
const [position, setPosition] = useState({ top: 0, left: 0, width: 0 });
|
||||
|
||||
useEffect(() => {
|
||||
setFiltered(items);
|
||||
}, [items]);
|
||||
|
||||
// Gefilterte Liste
|
||||
useEffect(() => {
|
||||
const filtered = items.filter((cat) =>
|
||||
cat.value.toLowerCase().includes(query.toLowerCase()))
|
||||
setFiltered(() => filtered)
|
||||
}, [query])
|
||||
|
||||
// Dropdown-Position aktualisieren
|
||||
const updatePosition = () => {
|
||||
if (!buttonRef.current) return;
|
||||
const rect = buttonRef.current.getBoundingClientRect();
|
||||
setPosition({
|
||||
top: rect.bottom + window.scrollY,
|
||||
left: rect.left + window.scrollX,
|
||||
width: rect.width,
|
||||
});
|
||||
};
|
||||
|
||||
// Öffnen → positionieren & Fokus setzen
|
||||
useEffect(() => {
|
||||
if (open) {
|
||||
updatePosition();
|
||||
setTimeout(() => searchRef.current?.focus(), 0);
|
||||
window.addEventListener("resize", updatePosition);
|
||||
window.addEventListener("scroll", updatePosition);
|
||||
}
|
||||
return () => {
|
||||
window.removeEventListener("resize", updatePosition);
|
||||
window.removeEventListener("scroll", updatePosition);
|
||||
};
|
||||
}, [open]);
|
||||
|
||||
// Dropdown schließen bei Klick außerhalb
|
||||
useEffect(() => {
|
||||
const handleClick = (e: MouseEvent) => {
|
||||
if (buttonRef.current && !buttonRef.current.contains(e.target as Node)) {
|
||||
setOpen(false);
|
||||
}
|
||||
};
|
||||
document.addEventListener("click", handleClick);
|
||||
return () => document.removeEventListener("click", handleClick);
|
||||
}, []);
|
||||
|
||||
// Tastatursteuerung
|
||||
const handleKey = (e: React.KeyboardEvent) => {
|
||||
if (!open) return;
|
||||
|
||||
switch (e.key) {
|
||||
case "ArrowDown":
|
||||
e.preventDefault();
|
||||
setHighlightIndex((i) => Math.min(i + 1, filtered.length - 1));
|
||||
break;
|
||||
|
||||
case "ArrowUp":
|
||||
e.preventDefault();
|
||||
setHighlightIndex((i) => Math.max(i - 1, 0));
|
||||
break;
|
||||
|
||||
case "Enter":
|
||||
e.preventDefault();
|
||||
if (filtered[highlightIndex]) {
|
||||
choose(filtered[highlightIndex]);
|
||||
}
|
||||
break;
|
||||
|
||||
case "Escape":
|
||||
setOpen(false);
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
const el = itemRefs.current[highlightIndex];
|
||||
if (el) {
|
||||
el.scrollIntoView({ block: "nearest" });
|
||||
}
|
||||
}, [highlightIndex]);
|
||||
|
||||
const choose = (cat: DropdownItem) => {
|
||||
console.log('Change',cat)
|
||||
setSelected(cat);
|
||||
setOpen(false);
|
||||
if(onChange) onChange(cat);
|
||||
setTimeout(() => {
|
||||
buttonRef.current?.focus();
|
||||
}, 0);
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<button
|
||||
ref={buttonRef}
|
||||
onClick={() => setOpen((o) => !o)}
|
||||
onKeyDown={handleKey}
|
||||
className="flex justify-between items-center px-4 py-2 border border-gray-300 rounded-xl w-64 bg-white"
|
||||
>
|
||||
<span>{selected.value || placeholder ||"auswählen…"}</span>
|
||||
<ChevronDown className="w-4 h-4" />
|
||||
</button>
|
||||
|
||||
{open &&
|
||||
createPortal(
|
||||
<div
|
||||
className="absolute bg-white border border-gray-200 rounded-xl shadow-lg z-50"
|
||||
style={{
|
||||
top: position.top,
|
||||
left: position.left,
|
||||
width: position.width,
|
||||
}}
|
||||
>
|
||||
{/* Suchfeld */}
|
||||
<div className="p-2 border-b border-gray-100">
|
||||
<input
|
||||
ref={searchRef}
|
||||
value={query}
|
||||
onChange={(e) => {
|
||||
setQuery(e.target.value);
|
||||
setHighlightIndex(0);
|
||||
}}
|
||||
onKeyDown={handleKey}
|
||||
className="w-full px-2 py-1 border border-gray-300 rounded-md"
|
||||
placeholder="Suchen..."
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Liste */}
|
||||
<div className="max-h-60 overflow-auto">
|
||||
{filtered.map((cat, index) => (
|
||||
<button
|
||||
key={cat.id}
|
||||
ref={(el) => { itemRefs.current[index] = el!; }}
|
||||
onClick={() => choose(cat)}
|
||||
className={`w-full text-left px-4 py-2
|
||||
${index === highlightIndex
|
||||
? "bg-gray-200"
|
||||
: "hover:bg-gray-100"
|
||||
}
|
||||
`}
|
||||
>
|
||||
{cat.value}
|
||||
</button>
|
||||
))}
|
||||
|
||||
{filtered.length === 0 && (
|
||||
<div className="px-4 py-3 text-gray-500">Keine Treffer…</div>
|
||||
)}
|
||||
</div>
|
||||
</div>,
|
||||
document.body
|
||||
)}
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
"use client";
|
||||
import React, { useState, useRef, useEffect } from "react";
|
||||
|
||||
interface Option {
|
||||
id: string, value: string
|
||||
}
|
||||
interface Props {
|
||||
options: Option[]
|
||||
onSelect: (value: Option) => void
|
||||
}
|
||||
|
||||
export default function SelectTest({ options = [], onSelect }: Props) {
|
||||
const [open, setOpen] = useState(false);
|
||||
const [highlight, setHighlight] = useState(0);
|
||||
const [value, setValue] = useState("");
|
||||
const wrapperRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
|
||||
// Klick außerhalb schließen
|
||||
useEffect(() => {
|
||||
function handleClickOutside(e: any) {
|
||||
if (wrapperRef.current && !wrapperRef.current.contains(e.target)) {
|
||||
setOpen(false);
|
||||
}
|
||||
}
|
||||
document.addEventListener("mousedown", handleClickOutside);
|
||||
return () => document.removeEventListener("mousedown", handleClickOutside);
|
||||
}, []);
|
||||
|
||||
const filtered = options.filter((opt) =>
|
||||
opt.value.toLowerCase().includes(value.toLowerCase())
|
||||
);
|
||||
|
||||
function handleKeyDown(e: React.KeyboardEvent) {
|
||||
if (!open) setOpen(true);
|
||||
|
||||
if (e.key === "ArrowDown") {
|
||||
e.preventDefault();
|
||||
setHighlight((h) => Math.min(h + 1, filtered.length - 1));
|
||||
}
|
||||
if (e.key === "ArrowUp") {
|
||||
e.preventDefault();
|
||||
setHighlight((h) => Math.max(h - 1, 0));
|
||||
}
|
||||
if (e.key === "Enter") {
|
||||
e.preventDefault();
|
||||
selectOption(filtered[highlight]);
|
||||
}
|
||||
if (e.key === "Escape") {
|
||||
setOpen(false);
|
||||
}
|
||||
if (e.key === "Tab") {
|
||||
setOpen(false);
|
||||
}
|
||||
}
|
||||
|
||||
function selectOption(opt: Option) {
|
||||
if (!opt) return;
|
||||
setValue(opt.value);
|
||||
setOpen(false);
|
||||
onSelect?.(opt);
|
||||
}
|
||||
|
||||
return (
|
||||
<div ref={wrapperRef} className="relative w-64">
|
||||
<input
|
||||
value={value}
|
||||
onChange={(e) => {
|
||||
setValue(e.target.value);
|
||||
setOpen(true);
|
||||
}}
|
||||
onFocus={() => setOpen(true)}
|
||||
onBlur={(e) => {
|
||||
if (!wrapperRef.current?.contains(e.relatedTarget)) {
|
||||
setOpen(false);
|
||||
}
|
||||
}}
|
||||
|
||||
onKeyDown={handleKeyDown}
|
||||
style={{
|
||||
width: "100%",
|
||||
padding: "8px",
|
||||
border: "1px solid #ccc",
|
||||
borderRadius: 4,
|
||||
}}
|
||||
placeholder="Wähle Option..."
|
||||
/>
|
||||
|
||||
{open && filtered.length > 0 && (
|
||||
<ul
|
||||
className="absolute top-full left-0 w-full bg-white border border-gray-300 rounded shadow-lg max-h-40 overflow-y-auto z-50"
|
||||
>
|
||||
{filtered.map((opt, i) => (
|
||||
<li
|
||||
key={opt.id}
|
||||
onMouseDown={() => selectOption(opt)}
|
||||
className={`px-3 py-2 cursor-pointer ${
|
||||
i === highlight ? "bg-blue-100" : "hover:bg-gray-100"
|
||||
}`}
|
||||
onMouseEnter={() => setHighlight(i)}
|
||||
>
|
||||
{opt.value}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import { useEffect, useState } from "react"
|
||||
import { forwardRef } from "react"
|
||||
interface ChangeValue {
|
||||
[key: string]: boolean
|
||||
}
|
||||
|
||||
interface Props extends React.InputHTMLAttributes<HTMLInputElement> {
|
||||
label?: string
|
||||
error?: string | undefined
|
||||
|
||||
}
|
||||
|
||||
export const InputCheckboxR = forwardRef<HTMLInputElement, Props>(
|
||||
({ label, error, ...props }, ref) => {
|
||||
|
||||
|
||||
|
||||
return (
|
||||
<div className="p-2 mb-1 mt-1 bg-green-0 flex items-center">
|
||||
|
||||
<input ref={ref} type="checkbox" className=" me-2 w-6 h-6 " {...props} />
|
||||
{label && <div className="text-md font-normal text-gray-00 font-mono ps-1">{label}</div>}
|
||||
|
||||
</div>
|
||||
)
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,30 @@
|
||||
import { forwardRef } from "react"
|
||||
|
||||
interface InputTextProps
|
||||
extends React.InputHTMLAttributes<HTMLInputElement> {
|
||||
label?: string
|
||||
error?: string | undefined
|
||||
}
|
||||
|
||||
export const InputTextR = forwardRef<HTMLInputElement, InputTextProps>(
|
||||
({ label, error, ...props }, ref) => {
|
||||
return (
|
||||
<div className="mb-1 bg-green-00">
|
||||
{label && (
|
||||
<label className="text-sm font-normal text-gray-600 font-mono ps-1">
|
||||
{label}
|
||||
</label>
|
||||
)}
|
||||
|
||||
<input ref={ref}
|
||||
className={`w-full ps-2 h-7 border rounded ${error ? "border-red-500" : "border-gray-300"} focus:outline-none focus:ring-1 focus:ring-blue-800`}
|
||||
{...props}
|
||||
/>
|
||||
|
||||
{false && error && <p className="text-red-500 text-sm mt-1">{error}</p>}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
InputTextR.displayName = "InputR"
|
||||
@@ -0,0 +1,49 @@
|
||||
"use client";
|
||||
import { useEffect } from "react";
|
||||
|
||||
interface Props {
|
||||
open: boolean;
|
||||
onClose: () => void;
|
||||
children: React.ReactNode;
|
||||
}
|
||||
export default function Modal({ open, onClose, children }: Props) {
|
||||
|
||||
// ESC-Taste schließen
|
||||
useEffect(() => {
|
||||
function handleEsc(e: KeyboardEvent) {
|
||||
if (e.key === "Escape") onClose();
|
||||
}
|
||||
|
||||
if (open) {
|
||||
window.addEventListener("keydown", handleEsc);
|
||||
}
|
||||
|
||||
return () => window.removeEventListener("keydown", handleEsc);
|
||||
}, [open, onClose]);
|
||||
|
||||
if (!open) return null;
|
||||
|
||||
return (
|
||||
<div style={overlayStyle} onClick={onClose}>
|
||||
<div style={modalStyle} onClick={(e) => e.stopPropagation()}>
|
||||
{children}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
const overlayStyle = {
|
||||
position: "fixed" as const,
|
||||
inset: 0,
|
||||
backgroundColor: "rgba(0,0,0,0.5)",
|
||||
display: "flex",
|
||||
justifyContent: "center",
|
||||
alignItems: "center",
|
||||
};
|
||||
|
||||
const modalStyle = {
|
||||
backgroundColor: "white",
|
||||
padding: "20px",
|
||||
borderRadius: "8px",
|
||||
minWidth: "300px",
|
||||
};
|
||||
@@ -0,0 +1,39 @@
|
||||
import { forwardRef, useRef, useState } from "react"
|
||||
|
||||
interface Props
|
||||
extends React.InputHTMLAttributes<HTMLSelectElement> {
|
||||
values: { id: string, value: string }[]
|
||||
}
|
||||
|
||||
export const SelectR = forwardRef<HTMLSelectElement, Props>(
|
||||
({ values, ...props }, ref) => {
|
||||
const inputRef = useRef<HTMLInputElement>(null)
|
||||
const [open, setOpen] = useState(false);
|
||||
|
||||
return (
|
||||
<div className="border-4 border-black mb-1 p-1 bg-green-100 border-1 border-gray-200 " >
|
||||
<div>
|
||||
<input type="text" ref={inputRef} onFocus={() => setOpen(true)} className="bg-green-100"
|
||||
onBlur={() => setOpen(false)} />
|
||||
</div>
|
||||
<div className="relative">
|
||||
<select
|
||||
ref={ref}
|
||||
{...props}
|
||||
size={open ? 5 : 1}
|
||||
style={{ position: "absolute", top: "0", left: 0, width: "100%", zIndex: 1000, backgroundColor: "white", border: "1px solid #ccc", }}
|
||||
className="w-10"
|
||||
>
|
||||
{values.map(c => (
|
||||
<option key={c.id} value={c.id}>
|
||||
{c.value}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
SelectR.displayName = "SelectR"
|
||||
@@ -0,0 +1,109 @@
|
||||
"use client"
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import { LocationTreeNode } from "../../types/inventory";
|
||||
import { LocationTreeNodeItem } from "../location/LocationTreeNodeItem";
|
||||
|
||||
|
||||
|
||||
type Props = {
|
||||
data: LocationTreeNode[];
|
||||
onSelect: (id: string | null) => void;
|
||||
};
|
||||
|
||||
|
||||
export function TreeViewXXXold({ data, onSelect }: Props) {
|
||||
const [selectedId, setSelectedId] = useState<string | null>(null);
|
||||
const [focusedId, setFocusedId] = useState<string | null>(null);
|
||||
const [openMap, setOpenMap] = useState<Record<string, boolean>>({});
|
||||
|
||||
// sichtbare Nodes flach machen
|
||||
const visibleNodes = useMemo(() => {
|
||||
const result: LocationTreeNode[] = [];
|
||||
|
||||
function walk(nodes: LocationTreeNode[]) {
|
||||
for (const n of nodes) {
|
||||
result.push(n);
|
||||
if (openMap[n._id]) {
|
||||
walk(n.children);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
walk(data);
|
||||
return result;
|
||||
}, [data, openMap]);
|
||||
|
||||
// initial focus
|
||||
useEffect(() => {
|
||||
if (!focusedId && visibleNodes.length > 0) {
|
||||
setFocusedId(visibleNodes[0]._id);
|
||||
}
|
||||
console.log(visibleNodes)
|
||||
}, [visibleNodes, focusedId]);
|
||||
|
||||
function handleKeyDown(e: React.KeyboardEvent) {
|
||||
if (!focusedId) return;
|
||||
|
||||
const index = visibleNodes.findIndex(n => n._id === focusedId);
|
||||
|
||||
if (e.key === "ArrowDown") {
|
||||
console.log('down')
|
||||
e.preventDefault();
|
||||
const next = visibleNodes[index + 1];
|
||||
if (next) setFocusedId(next._id);
|
||||
}
|
||||
|
||||
if (e.key === "ArrowUp") {
|
||||
console.log('up')
|
||||
e.preventDefault();
|
||||
const prev = visibleNodes[index - 1];
|
||||
if (prev) setFocusedId(prev._id);
|
||||
}
|
||||
|
||||
if (e.key === "ArrowRight") {
|
||||
console.log('right')
|
||||
e.preventDefault();
|
||||
setOpenMap(m => ({ ...m, [focusedId]: true }));
|
||||
}
|
||||
|
||||
if (e.key === "ArrowLeft") {
|
||||
console.log('left')
|
||||
e.preventDefault();
|
||||
setOpenMap(m => ({ ...m, [focusedId]: false }));
|
||||
}
|
||||
|
||||
if (e.key === "Enter") {
|
||||
e.preventDefault();
|
||||
setSelectedId(focusedId);
|
||||
onSelect(focusedId);
|
||||
}
|
||||
if (e.key === "Space") {
|
||||
e.preventDefault();
|
||||
setSelectedId(focusedId);
|
||||
onSelect(focusedId);
|
||||
}
|
||||
console.log(e.key)
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
tabIndex={0}
|
||||
onKeyDown={handleKeyDown}
|
||||
className="border p-2 rounded outline-none focus:ring"
|
||||
>
|
||||
{data.map(node => (
|
||||
<LocationTreeNodeItem
|
||||
key={node._id}
|
||||
node={node}
|
||||
selectedId={selectedId}
|
||||
focusedId={focusedId}
|
||||
openMap={openMap}
|
||||
setOpenMap={setOpenMap}
|
||||
setFocusedId={setFocusedId}
|
||||
onSelect={(id) => { setSelectedId(id); setFocusedId(id); onSelect(id); }} />
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user