"use client"; import { useState } from "react"; import { Location } from "@/types"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; interface LocationManagerProps { locations: Location[]; onAdd?: (location: Omit) => void; onEdit?: (location: Location) => void; onDelete?: (id: string) => void; } export default function LocationManager({ locations, onAdd, onEdit, onDelete, }: LocationManagerProps) { const [isAddOpen, setIsAddOpen] = useState(false); const [newLocation, setNewLocation] = useState({ name: "", code: "", parentId: "" as string | undefined, }); const handleAdd = () => { if (onAdd && newLocation.name && newLocation.code) { onAdd(newLocation); setNewLocation({ name: "", code: "", parentId: undefined }); setIsAddOpen(false); } }; // 获取父位置的路径 const getParentPath = (parentId?: string) => { if (!parentId) return ""; const parent = locations.find((l) => l.id === parentId); return parent ? parent.path + "/" : ""; }; // 筛选可作为父级的位置(一级位置) const parentLocations = locations.filter((l) => !l.parentId); return (

位置管理

添加新位置
setNewLocation({ ...newLocation, name: e.target.value }) } placeholder="例如:客厅、卧室" />
setNewLocation({ ...newLocation, code: e.target.value.toUpperCase() }) } placeholder="例如:LR、BR" />
{newLocation.name && newLocation.code && (
完整路径:{getParentPath(newLocation.parentId)} {newLocation.name}
)}
{locations.map((location) => (
{location.name}

{location.code}

{location.parentId && ( 子位置 )}

路径:{location.path}

))}
); }