update
This commit is contained in:
@@ -0,0 +1,545 @@
|
||||
"use client";
|
||||
|
||||
// @/components/index.tsx
|
||||
import { useState, useEffect } from "react";
|
||||
import { motion, AnimatePresence } from "framer-motion";
|
||||
import {
|
||||
Plus,
|
||||
Check,
|
||||
RotateCcw,
|
||||
Download,
|
||||
Upload,
|
||||
Trash2,
|
||||
Edit2,
|
||||
Save,
|
||||
X,
|
||||
} from "lucide-react";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from "@/components/ui/select";
|
||||
import { useToast } from "@/components/ui/use-toast";
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogTrigger,
|
||||
} from "@/components/ui/dialog";
|
||||
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
|
||||
import Toast from "@/components/ui/toast"; // 确保导入 Toast 组件
|
||||
|
||||
interface ChecklistItem {
|
||||
id: string;
|
||||
text: string;
|
||||
completed: boolean;
|
||||
}
|
||||
|
||||
interface Checklist {
|
||||
id: string;
|
||||
name: string;
|
||||
items: ChecklistItem[];
|
||||
}
|
||||
|
||||
interface ToastOptions {
|
||||
title: string;
|
||||
description?: string;
|
||||
variant?: "default" | "destructive";
|
||||
}
|
||||
|
||||
export function CombinedChecklistAppComponent() {
|
||||
const [checklists, setChecklists] = useState<Checklist[]>([]);
|
||||
const [currentChecklist, setCurrentChecklist] = useState<string>("");
|
||||
const [newItemText, setNewItemText] = useState("");
|
||||
const [newChecklistName, setNewChecklistName] = useState("");
|
||||
const [editingId, setEditingId] = useState<string | null>(null);
|
||||
const [editName, setEditName] = useState("");
|
||||
const { toasts, showToast } = useToast(); // 使用 toasts 状态
|
||||
|
||||
useEffect(() => {
|
||||
const savedChecklists = localStorage.getItem("checklists");
|
||||
if (savedChecklists) {
|
||||
setChecklists(JSON.parse(savedChecklists));
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
localStorage.setItem("checklists", JSON.stringify(checklists));
|
||||
}, [checklists]);
|
||||
|
||||
const addChecklist = () => {
|
||||
if (newChecklistName.trim() === "") return;
|
||||
const newChecklist: Checklist = {
|
||||
id: Date.now().toString(),
|
||||
name: newChecklistName,
|
||||
items: [],
|
||||
};
|
||||
setChecklists([...checklists, newChecklist]);
|
||||
setNewChecklistName("");
|
||||
setCurrentChecklist(newChecklist.id);
|
||||
showToast({
|
||||
title: "新检查单已创建",
|
||||
description: `"${newChecklistName}" 已添加到您的检查单列表。`,
|
||||
} as ToastOptions);
|
||||
};
|
||||
|
||||
const addItem = () => {
|
||||
if (newItemText.trim() === "" || currentChecklist === "") return;
|
||||
const newItem: ChecklistItem = {
|
||||
id: Date.now().toString(),
|
||||
text: newItemText,
|
||||
completed: false,
|
||||
};
|
||||
setChecklists(
|
||||
checklists.map((list) =>
|
||||
list.id === currentChecklist
|
||||
? { ...list, items: [...list.items, newItem] }
|
||||
: list
|
||||
)
|
||||
);
|
||||
setNewItemText("");
|
||||
};
|
||||
|
||||
const toggleItem = (itemId: string) => {
|
||||
setChecklists(
|
||||
checklists.map((list) =>
|
||||
list.id === currentChecklist
|
||||
? {
|
||||
...list,
|
||||
items: list.items.map((item) =>
|
||||
item.id === itemId
|
||||
? { ...item, completed: !item.completed }
|
||||
: item
|
||||
),
|
||||
}
|
||||
: list
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
const deleteItem = (itemId: string) => {
|
||||
setChecklists(
|
||||
checklists.map((list) =>
|
||||
list.id === currentChecklist
|
||||
? {
|
||||
...list,
|
||||
items: list.items.filter((item) => item.id !== itemId),
|
||||
}
|
||||
: list
|
||||
)
|
||||
);
|
||||
showToast({
|
||||
title: "检查项已删除",
|
||||
description: "该项目已从您的检查单中移除。",
|
||||
} as ToastOptions);
|
||||
};
|
||||
|
||||
const resetChecklist = () => {
|
||||
setChecklists(
|
||||
checklists.map((list) =>
|
||||
list.id === currentChecklist
|
||||
? {
|
||||
...list,
|
||||
items: list.items.map((item) => ({ ...item, completed: false })),
|
||||
}
|
||||
: list
|
||||
)
|
||||
);
|
||||
showToast({
|
||||
title: "检查单已重置",
|
||||
description: "所有项目已标记为未完成。",
|
||||
} as ToastOptions);
|
||||
};
|
||||
|
||||
const exportData = () => {
|
||||
const dataStr = JSON.stringify(checklists);
|
||||
const dataUri =
|
||||
"data:application/json;charset=utf-8," + encodeURIComponent(dataStr);
|
||||
const exportFileDefaultName = "checklists.json";
|
||||
|
||||
const linkElement = document.createElement("a");
|
||||
linkElement.setAttribute("href", dataUri);
|
||||
linkElement.setAttribute("download", exportFileDefaultName);
|
||||
linkElement.click();
|
||||
showToast({
|
||||
title: "数据已导出",
|
||||
description: "您的检查单数据已成功导出为 JSON 文件。",
|
||||
} as ToastOptions);
|
||||
};
|
||||
|
||||
const importData = (event: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const file = event.target.files?.[0];
|
||||
if (file) {
|
||||
const reader = new FileReader();
|
||||
reader.onload = (e) => {
|
||||
const content = e.target?.result;
|
||||
if (typeof content === "string") {
|
||||
try {
|
||||
const importedChecklists = JSON.parse(content);
|
||||
setChecklists(importedChecklists);
|
||||
showToast({
|
||||
title: "数据导入成功",
|
||||
description: "您的检查单已成功导入。",
|
||||
} as ToastOptions);
|
||||
} catch {
|
||||
showToast({
|
||||
title: "导入失败",
|
||||
description: "无法解析导入的文件。请确保它是有效的 JSON 格式。",
|
||||
variant: "destructive",
|
||||
} as ToastOptions);
|
||||
}
|
||||
}
|
||||
};
|
||||
reader.readAsText(file);
|
||||
}
|
||||
};
|
||||
|
||||
const startEditing = (id: string, name: string) => {
|
||||
setEditingId(id);
|
||||
setEditName(name);
|
||||
};
|
||||
|
||||
const saveEdit = (id: string) => {
|
||||
setChecklists(
|
||||
checklists.map((list) =>
|
||||
list.id === id ? { ...list, name: editName } : list
|
||||
)
|
||||
);
|
||||
setEditingId(null);
|
||||
showToast({
|
||||
title: "检查单已更新",
|
||||
description: `检查单名称已更改为 "${editName}"。`,
|
||||
} as ToastOptions);
|
||||
};
|
||||
|
||||
const cancelEdit = () => {
|
||||
setEditingId(null);
|
||||
setEditName("");
|
||||
};
|
||||
|
||||
const deleteChecklist = (id: string) => {
|
||||
setChecklists(checklists.filter((list) => list.id !== id));
|
||||
if (currentChecklist === id) {
|
||||
setCurrentChecklist("");
|
||||
}
|
||||
showToast({
|
||||
title: "检查单已删除",
|
||||
description: "该检查单及其所有项目已被移除。",
|
||||
variant: "destructive",
|
||||
} as ToastOptions);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="container mx-auto p-4 max-w-4xl">
|
||||
<motion.h1
|
||||
className="text-3xl font-bold mb-6 text-center text-primary"
|
||||
initial={{ opacity: 0, y: -20 }}
|
||||
animate={{ opacity: 1, y: 0 }}
|
||||
transition={{ duration: 0.5 }}
|
||||
>
|
||||
检查单应用
|
||||
</motion.h1>
|
||||
|
||||
<Tabs defaultValue="checklist" className="w-full">
|
||||
<TabsList className="grid w-full grid-cols-2 mb-6">
|
||||
<TabsTrigger value="checklist">检查单</TabsTrigger>
|
||||
<TabsTrigger value="manage">管理检查单</TabsTrigger>
|
||||
</TabsList>
|
||||
<TabsContent value="checklist">
|
||||
<motion.div
|
||||
className="mb-6 p-4 bg-card rounded-lg shadow-md"
|
||||
initial={{ opacity: 0, scale: 0.9 }}
|
||||
animate={{ opacity: 1, scale: 1 }}
|
||||
transition={{ duration: 0.3 }}
|
||||
>
|
||||
<Label
|
||||
htmlFor="new-checklist"
|
||||
className="text-lg font-semibold mb-2 block"
|
||||
>
|
||||
新建检查单
|
||||
</Label>
|
||||
<div className="flex mt-1">
|
||||
<Input
|
||||
id="new-checklist"
|
||||
value={newChecklistName}
|
||||
onChange={(e) => setNewChecklistName(e.target.value)}
|
||||
placeholder="输入检查单名称"
|
||||
className="mr-2 flex-grow"
|
||||
/>
|
||||
<Button
|
||||
onClick={addChecklist}
|
||||
className="bg-primary hover:bg-primary/90"
|
||||
>
|
||||
<Plus className="w-4 h-4 mr-2" /> 添加
|
||||
</Button>
|
||||
</div>
|
||||
</motion.div>
|
||||
|
||||
<motion.div
|
||||
className="mb-6 p-4 bg-card rounded-lg shadow-md"
|
||||
initial={{ opacity: 0, scale: 0.9 }}
|
||||
animate={{ opacity: 1, scale: 1 }}
|
||||
transition={{ duration: 0.3, delay: 0.1 }}
|
||||
>
|
||||
<Label
|
||||
htmlFor="checklist-select"
|
||||
className="text-lg font-semibold mb-2 block"
|
||||
>
|
||||
选择检查单
|
||||
</Label>
|
||||
<Select
|
||||
value={currentChecklist}
|
||||
onValueChange={setCurrentChecklist}
|
||||
>
|
||||
<SelectTrigger id="checklist-select" className="w-full">
|
||||
<SelectValue placeholder="选择一个检查单" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{checklists.map((list) => (
|
||||
<SelectItem key={list.id} value={list.id}>
|
||||
{list.name}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</motion.div>
|
||||
|
||||
{currentChecklist && (
|
||||
<motion.div
|
||||
initial={{ opacity: 0, y: 20 }}
|
||||
animate={{ opacity: 1, y: 0 }}
|
||||
transition={{ duration: 0.5 }}
|
||||
>
|
||||
<div className="mb-6 p-4 bg-card rounded-lg shadow-md">
|
||||
<Label
|
||||
htmlFor="new-item"
|
||||
className="text-lg font-semibold mb-2 block"
|
||||
>
|
||||
添加检查项
|
||||
</Label>
|
||||
<div className="flex mt-1">
|
||||
<Input
|
||||
id="new-item"
|
||||
value={newItemText}
|
||||
onChange={(e) => setNewItemText(e.target.value)}
|
||||
placeholder="输入新的检查项"
|
||||
className="mr-2 flex-grow"
|
||||
/>
|
||||
<Button
|
||||
onClick={addItem}
|
||||
className="bg-primary hover:bg-primary/90"
|
||||
>
|
||||
<Plus className="w-4 h-4 mr-2" /> 添加
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<motion.ul className="space-y-2 mb-6">
|
||||
<AnimatePresence>
|
||||
{checklists
|
||||
.find((list) => list.id === currentChecklist)
|
||||
?.items.map((item) => (
|
||||
<motion.li
|
||||
key={item.id}
|
||||
initial={{ opacity: 0, x: -20 }}
|
||||
animate={{ opacity: 1, x: 0 }}
|
||||
exit={{ opacity: 0, x: 20 }}
|
||||
transition={{ duration: 0.3 }}
|
||||
className={`flex items-center p-3 rounded-lg transition-all duration-300 ${
|
||||
item.completed
|
||||
? "bg-green-100 dark:bg-green-900"
|
||||
: "bg-card"
|
||||
} shadow-sm hover:shadow-md`}
|
||||
>
|
||||
<motion.div
|
||||
className={`w-6 h-6 rounded-full border-2 mr-3 flex items-center justify-center cursor-pointer ${
|
||||
item.completed
|
||||
? "border-green-500 bg-green-500"
|
||||
: "border-gray-400"
|
||||
}`}
|
||||
onClick={() => toggleItem(item.id)}
|
||||
whileHover={{ scale: 1.1 }}
|
||||
whileTap={{ scale: 0.9 }}
|
||||
>
|
||||
{item.completed && (
|
||||
<Check className="w-4 h-4 text-white" />
|
||||
)}
|
||||
</motion.div>
|
||||
<span
|
||||
className={`flex-grow ${
|
||||
item.completed
|
||||
? "line-through text-muted-foreground"
|
||||
: ""
|
||||
}`}
|
||||
>
|
||||
{item.text}
|
||||
</span>
|
||||
<motion.button
|
||||
onClick={() => deleteItem(item.id)}
|
||||
className="text-destructive hover:text-destructive/90 p-1 rounded-full"
|
||||
whileHover={{ scale: 1.1 }}
|
||||
whileTap={{ scale: 0.9 }}
|
||||
>
|
||||
<Trash2 className="w-4 h-4" />
|
||||
</motion.button>
|
||||
</motion.li>
|
||||
))}
|
||||
</AnimatePresence>
|
||||
</motion.ul>
|
||||
|
||||
<div className="flex space-x-2 mb-4">
|
||||
<Button
|
||||
onClick={resetChecklist}
|
||||
variant="outline"
|
||||
className="flex-1"
|
||||
>
|
||||
<RotateCcw className="w-4 h-4 mr-2" /> 重置
|
||||
</Button>
|
||||
<Button
|
||||
onClick={exportData}
|
||||
variant="outline"
|
||||
className="flex-1"
|
||||
>
|
||||
<Download className="w-4 h-4 mr-2" /> 导出
|
||||
</Button>
|
||||
<Button variant="outline" className="relative flex-1">
|
||||
<Upload className="w-4 h-4 mr-2" /> 导入
|
||||
<input
|
||||
type="file"
|
||||
onChange={importData}
|
||||
className="absolute inset-0 w-full h-full opacity-0 cursor-pointer"
|
||||
accept=".json"
|
||||
/>
|
||||
</Button>
|
||||
</div>
|
||||
</motion.div>
|
||||
)}
|
||||
</TabsContent>
|
||||
<TabsContent value="manage">
|
||||
<motion.div
|
||||
className="space-y-4"
|
||||
initial={{ opacity: 0 }}
|
||||
animate={{ opacity: 1 }}
|
||||
transition={{ duration: 0.5, delay: 0.2 }}
|
||||
>
|
||||
<AnimatePresence>
|
||||
{checklists.map((checklist) => (
|
||||
<motion.div
|
||||
key={checklist.id}
|
||||
className="bg-card rounded-lg shadow-md p-4"
|
||||
initial={{ opacity: 0, y: 20 }}
|
||||
animate={{ opacity: 1, y: 0 }}
|
||||
exit={{ opacity: 0, y: -20 }}
|
||||
transition={{ duration: 0.3 }}
|
||||
>
|
||||
{editingId === checklist.id ? (
|
||||
<div className="flex items-center space-x-2">
|
||||
<Input
|
||||
value={editName}
|
||||
onChange={(e) => setEditName(e.target.value)}
|
||||
className="flex-grow"
|
||||
placeholder="输入新的检查单名称"
|
||||
/>
|
||||
<Button
|
||||
onClick={() => saveEdit(checklist.id)}
|
||||
size="icon"
|
||||
variant="ghost"
|
||||
>
|
||||
<Save className="h-4 w-4" />
|
||||
</Button>
|
||||
<Button onClick={cancelEdit} size="icon" variant="ghost">
|
||||
<X className="h-4 w-4" />
|
||||
</Button>
|
||||
</div>
|
||||
) : (
|
||||
<div className="flex items-center justify-between">
|
||||
<span className="text-lg font-medium">
|
||||
{checklist.name}
|
||||
</span>
|
||||
<div className="space-x-2">
|
||||
<Button
|
||||
onClick={() =>
|
||||
startEditing(checklist.id, checklist.name)
|
||||
}
|
||||
size="icon"
|
||||
variant="ghost"
|
||||
>
|
||||
<Edit2 className="h-4 w-4" />
|
||||
</Button>
|
||||
<Dialog>
|
||||
<DialogTrigger asChild>
|
||||
<Button size="icon" variant="ghost">
|
||||
<Trash2 className="h-4 w-4 text-destructive" />
|
||||
</Button>
|
||||
</DialogTrigger>
|
||||
<DialogContent>
|
||||
<DialogHeader>
|
||||
<DialogTitle>确认删除</DialogTitle>
|
||||
</DialogHeader>
|
||||
<p className="text-muted-foreground">
|
||||
您确定要删除 "{checklist.name}"
|
||||
检查单吗?此操作无法撤销。
|
||||
</p>
|
||||
<div className="flex justify-end space-x-2 mt-4">
|
||||
<Button variant="outline" onClick={() => {}}>
|
||||
取消
|
||||
</Button>
|
||||
<Button
|
||||
variant="destructive"
|
||||
onClick={() => deleteChecklist(checklist.id)}
|
||||
>
|
||||
删除
|
||||
</Button>
|
||||
</div>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
<p className="text-sm text-muted-foreground mt-2">
|
||||
共 {checklist.items.length} 个项目,
|
||||
{
|
||||
checklist.items.filter((item) => item.completed).length
|
||||
}{" "}
|
||||
个已完成
|
||||
</p>
|
||||
</motion.div>
|
||||
))}
|
||||
</AnimatePresence>
|
||||
</motion.div>
|
||||
|
||||
{checklists.length === 0 && (
|
||||
<motion.p
|
||||
className="text-center text-muted-foreground mt-8"
|
||||
initial={{ opacity: 0 }}
|
||||
animate={{ opacity: 1 }}
|
||||
transition={{ duration: 0.5, delay: 0.5 }}
|
||||
>
|
||||
暂无检查单。请在检查单标签页创建新的检查单。
|
||||
</motion.p>
|
||||
)}
|
||||
</TabsContent>
|
||||
</Tabs>
|
||||
|
||||
{/* Toast 组件 */}
|
||||
<div className="fixed bottom-4 right-4 space-y-2">
|
||||
<AnimatePresence>
|
||||
{toasts.map((toast) => (
|
||||
<Toast
|
||||
key={toast.id}
|
||||
title={toast.title}
|
||||
description={toast.description}
|
||||
variant={toast.variant}
|
||||
/>
|
||||
))}
|
||||
</AnimatePresence>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
import * as React from "react"
|
||||
import { Slot } from "@radix-ui/react-slot"
|
||||
import { cva, type VariantProps } from "class-variance-authority"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
const buttonVariants = cva(
|
||||
"inline-flex items-center justify-center gap-2 whitespace-nowrap rounded-md text-sm font-medium transition-colors focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0",
|
||||
{
|
||||
variants: {
|
||||
variant: {
|
||||
default:
|
||||
"bg-primary text-primary-foreground shadow hover:bg-primary/90",
|
||||
destructive:
|
||||
"bg-destructive text-destructive-foreground shadow-sm hover:bg-destructive/90",
|
||||
outline:
|
||||
"border border-input bg-background shadow-sm hover:bg-accent hover:text-accent-foreground",
|
||||
secondary:
|
||||
"bg-secondary text-secondary-foreground shadow-sm hover:bg-secondary/80",
|
||||
ghost: "hover:bg-accent hover:text-accent-foreground",
|
||||
link: "text-primary underline-offset-4 hover:underline",
|
||||
},
|
||||
size: {
|
||||
default: "h-9 px-4 py-2",
|
||||
sm: "h-8 rounded-md px-3 text-xs",
|
||||
lg: "h-10 rounded-md px-8",
|
||||
icon: "h-9 w-9",
|
||||
},
|
||||
},
|
||||
defaultVariants: {
|
||||
variant: "default",
|
||||
size: "default",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
export interface ButtonProps
|
||||
extends React.ButtonHTMLAttributes<HTMLButtonElement>,
|
||||
VariantProps<typeof buttonVariants> {
|
||||
asChild?: boolean
|
||||
}
|
||||
|
||||
const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
|
||||
({ className, variant, size, asChild = false, ...props }, ref) => {
|
||||
const Comp = asChild ? Slot : "button"
|
||||
return (
|
||||
<Comp
|
||||
className={cn(buttonVariants({ variant, size, className }))}
|
||||
ref={ref}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
)
|
||||
Button.displayName = "Button"
|
||||
|
||||
export { Button, buttonVariants }
|
||||
@@ -0,0 +1,122 @@
|
||||
"use client"
|
||||
|
||||
import * as React from "react"
|
||||
import * as DialogPrimitive from "@radix-ui/react-dialog"
|
||||
import { X } from "lucide-react"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
const Dialog = DialogPrimitive.Root
|
||||
|
||||
const DialogTrigger = DialogPrimitive.Trigger
|
||||
|
||||
const DialogPortal = DialogPrimitive.Portal
|
||||
|
||||
const DialogClose = DialogPrimitive.Close
|
||||
|
||||
const DialogOverlay = React.forwardRef<
|
||||
React.ElementRef<typeof DialogPrimitive.Overlay>,
|
||||
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Overlay>
|
||||
>(({ className, ...props }, ref) => (
|
||||
<DialogPrimitive.Overlay
|
||||
ref={ref}
|
||||
className={cn(
|
||||
"fixed inset-0 z-50 bg-black/80 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
))
|
||||
DialogOverlay.displayName = DialogPrimitive.Overlay.displayName
|
||||
|
||||
const DialogContent = React.forwardRef<
|
||||
React.ElementRef<typeof DialogPrimitive.Content>,
|
||||
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content>
|
||||
>(({ className, children, ...props }, ref) => (
|
||||
<DialogPortal>
|
||||
<DialogOverlay />
|
||||
<DialogPrimitive.Content
|
||||
ref={ref}
|
||||
className={cn(
|
||||
"fixed left-[50%] top-[50%] z-50 grid w-full max-w-lg translate-x-[-50%] translate-y-[-50%] gap-4 border bg-background p-6 shadow-lg duration-200 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[state=closed]:slide-out-to-left-1/2 data-[state=closed]:slide-out-to-top-[48%] data-[state=open]:slide-in-from-left-1/2 data-[state=open]:slide-in-from-top-[48%] sm:rounded-lg",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
{children}
|
||||
<DialogPrimitive.Close className="absolute right-4 top-4 rounded-sm opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:pointer-events-none data-[state=open]:bg-accent data-[state=open]:text-muted-foreground">
|
||||
<X className="h-4 w-4" />
|
||||
<span className="sr-only">Close</span>
|
||||
</DialogPrimitive.Close>
|
||||
</DialogPrimitive.Content>
|
||||
</DialogPortal>
|
||||
))
|
||||
DialogContent.displayName = DialogPrimitive.Content.displayName
|
||||
|
||||
const DialogHeader = ({
|
||||
className,
|
||||
...props
|
||||
}: React.HTMLAttributes<HTMLDivElement>) => (
|
||||
<div
|
||||
className={cn(
|
||||
"flex flex-col space-y-1.5 text-center sm:text-left",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
DialogHeader.displayName = "DialogHeader"
|
||||
|
||||
const DialogFooter = ({
|
||||
className,
|
||||
...props
|
||||
}: React.HTMLAttributes<HTMLDivElement>) => (
|
||||
<div
|
||||
className={cn(
|
||||
"flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
DialogFooter.displayName = "DialogFooter"
|
||||
|
||||
const DialogTitle = React.forwardRef<
|
||||
React.ElementRef<typeof DialogPrimitive.Title>,
|
||||
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Title>
|
||||
>(({ className, ...props }, ref) => (
|
||||
<DialogPrimitive.Title
|
||||
ref={ref}
|
||||
className={cn(
|
||||
"text-lg font-semibold leading-none tracking-tight",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
))
|
||||
DialogTitle.displayName = DialogPrimitive.Title.displayName
|
||||
|
||||
const DialogDescription = React.forwardRef<
|
||||
React.ElementRef<typeof DialogPrimitive.Description>,
|
||||
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Description>
|
||||
>(({ className, ...props }, ref) => (
|
||||
<DialogPrimitive.Description
|
||||
ref={ref}
|
||||
className={cn("text-sm text-muted-foreground", className)}
|
||||
{...props}
|
||||
/>
|
||||
))
|
||||
DialogDescription.displayName = DialogPrimitive.Description.displayName
|
||||
|
||||
export {
|
||||
Dialog,
|
||||
DialogPortal,
|
||||
DialogOverlay,
|
||||
DialogTrigger,
|
||||
DialogClose,
|
||||
DialogContent,
|
||||
DialogHeader,
|
||||
DialogFooter,
|
||||
DialogTitle,
|
||||
DialogDescription,
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import * as React from "react"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
const Input = React.forwardRef<HTMLInputElement, React.ComponentProps<"input">>(
|
||||
({ className, type, ...props }, ref) => {
|
||||
return (
|
||||
<input
|
||||
type={type}
|
||||
className={cn(
|
||||
"flex h-9 w-full rounded-md border border-input bg-transparent px-3 py-1 text-base shadow-sm transition-colors file:border-0 file:bg-transparent file:text-sm file:font-medium file:text-foreground placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50 md:text-sm",
|
||||
className
|
||||
)}
|
||||
ref={ref}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
)
|
||||
Input.displayName = "Input"
|
||||
|
||||
export { Input }
|
||||
@@ -0,0 +1,26 @@
|
||||
"use client"
|
||||
|
||||
import * as React from "react"
|
||||
import * as LabelPrimitive from "@radix-ui/react-label"
|
||||
import { cva, type VariantProps } from "class-variance-authority"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
const labelVariants = cva(
|
||||
"text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
|
||||
)
|
||||
|
||||
const Label = React.forwardRef<
|
||||
React.ElementRef<typeof LabelPrimitive.Root>,
|
||||
React.ComponentPropsWithoutRef<typeof LabelPrimitive.Root> &
|
||||
VariantProps<typeof labelVariants>
|
||||
>(({ className, ...props }, ref) => (
|
||||
<LabelPrimitive.Root
|
||||
ref={ref}
|
||||
className={cn(labelVariants(), className)}
|
||||
{...props}
|
||||
/>
|
||||
))
|
||||
Label.displayName = LabelPrimitive.Root.displayName
|
||||
|
||||
export { Label }
|
||||
@@ -0,0 +1,159 @@
|
||||
"use client"
|
||||
|
||||
import * as React from "react"
|
||||
import * as SelectPrimitive from "@radix-ui/react-select"
|
||||
import { Check, ChevronDown, ChevronUp } from "lucide-react"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
const Select = SelectPrimitive.Root
|
||||
|
||||
const SelectGroup = SelectPrimitive.Group
|
||||
|
||||
const SelectValue = SelectPrimitive.Value
|
||||
|
||||
const SelectTrigger = React.forwardRef<
|
||||
React.ElementRef<typeof SelectPrimitive.Trigger>,
|
||||
React.ComponentPropsWithoutRef<typeof SelectPrimitive.Trigger>
|
||||
>(({ className, children, ...props }, ref) => (
|
||||
<SelectPrimitive.Trigger
|
||||
ref={ref}
|
||||
className={cn(
|
||||
"flex h-9 w-full items-center justify-between whitespace-nowrap rounded-md border border-input bg-transparent px-3 py-2 text-sm shadow-sm ring-offset-background placeholder:text-muted-foreground focus:outline-none focus:ring-1 focus:ring-ring disabled:cursor-not-allowed disabled:opacity-50 [&>span]:line-clamp-1",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
{children}
|
||||
<SelectPrimitive.Icon asChild>
|
||||
<ChevronDown className="h-4 w-4 opacity-50" />
|
||||
</SelectPrimitive.Icon>
|
||||
</SelectPrimitive.Trigger>
|
||||
))
|
||||
SelectTrigger.displayName = SelectPrimitive.Trigger.displayName
|
||||
|
||||
const SelectScrollUpButton = React.forwardRef<
|
||||
React.ElementRef<typeof SelectPrimitive.ScrollUpButton>,
|
||||
React.ComponentPropsWithoutRef<typeof SelectPrimitive.ScrollUpButton>
|
||||
>(({ className, ...props }, ref) => (
|
||||
<SelectPrimitive.ScrollUpButton
|
||||
ref={ref}
|
||||
className={cn(
|
||||
"flex cursor-default items-center justify-center py-1",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
<ChevronUp className="h-4 w-4" />
|
||||
</SelectPrimitive.ScrollUpButton>
|
||||
))
|
||||
SelectScrollUpButton.displayName = SelectPrimitive.ScrollUpButton.displayName
|
||||
|
||||
const SelectScrollDownButton = React.forwardRef<
|
||||
React.ElementRef<typeof SelectPrimitive.ScrollDownButton>,
|
||||
React.ComponentPropsWithoutRef<typeof SelectPrimitive.ScrollDownButton>
|
||||
>(({ className, ...props }, ref) => (
|
||||
<SelectPrimitive.ScrollDownButton
|
||||
ref={ref}
|
||||
className={cn(
|
||||
"flex cursor-default items-center justify-center py-1",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
<ChevronDown className="h-4 w-4" />
|
||||
</SelectPrimitive.ScrollDownButton>
|
||||
))
|
||||
SelectScrollDownButton.displayName =
|
||||
SelectPrimitive.ScrollDownButton.displayName
|
||||
|
||||
const SelectContent = React.forwardRef<
|
||||
React.ElementRef<typeof SelectPrimitive.Content>,
|
||||
React.ComponentPropsWithoutRef<typeof SelectPrimitive.Content>
|
||||
>(({ className, children, position = "popper", ...props }, ref) => (
|
||||
<SelectPrimitive.Portal>
|
||||
<SelectPrimitive.Content
|
||||
ref={ref}
|
||||
className={cn(
|
||||
"relative z-50 max-h-96 min-w-[8rem] overflow-hidden rounded-md border bg-popover text-popover-foreground shadow-md data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2",
|
||||
position === "popper" &&
|
||||
"data-[side=bottom]:translate-y-1 data-[side=left]:-translate-x-1 data-[side=right]:translate-x-1 data-[side=top]:-translate-y-1",
|
||||
className
|
||||
)}
|
||||
position={position}
|
||||
{...props}
|
||||
>
|
||||
<SelectScrollUpButton />
|
||||
<SelectPrimitive.Viewport
|
||||
className={cn(
|
||||
"p-1",
|
||||
position === "popper" &&
|
||||
"h-[var(--radix-select-trigger-height)] w-full min-w-[var(--radix-select-trigger-width)]"
|
||||
)}
|
||||
>
|
||||
{children}
|
||||
</SelectPrimitive.Viewport>
|
||||
<SelectScrollDownButton />
|
||||
</SelectPrimitive.Content>
|
||||
</SelectPrimitive.Portal>
|
||||
))
|
||||
SelectContent.displayName = SelectPrimitive.Content.displayName
|
||||
|
||||
const SelectLabel = React.forwardRef<
|
||||
React.ElementRef<typeof SelectPrimitive.Label>,
|
||||
React.ComponentPropsWithoutRef<typeof SelectPrimitive.Label>
|
||||
>(({ className, ...props }, ref) => (
|
||||
<SelectPrimitive.Label
|
||||
ref={ref}
|
||||
className={cn("px-2 py-1.5 text-sm font-semibold", className)}
|
||||
{...props}
|
||||
/>
|
||||
))
|
||||
SelectLabel.displayName = SelectPrimitive.Label.displayName
|
||||
|
||||
const SelectItem = React.forwardRef<
|
||||
React.ElementRef<typeof SelectPrimitive.Item>,
|
||||
React.ComponentPropsWithoutRef<typeof SelectPrimitive.Item>
|
||||
>(({ className, children, ...props }, ref) => (
|
||||
<SelectPrimitive.Item
|
||||
ref={ref}
|
||||
className={cn(
|
||||
"relative flex w-full cursor-default select-none items-center rounded-sm py-1.5 pl-2 pr-8 text-sm outline-none focus:bg-accent focus:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
<span className="absolute right-2 flex h-3.5 w-3.5 items-center justify-center">
|
||||
<SelectPrimitive.ItemIndicator>
|
||||
<Check className="h-4 w-4" />
|
||||
</SelectPrimitive.ItemIndicator>
|
||||
</span>
|
||||
<SelectPrimitive.ItemText>{children}</SelectPrimitive.ItemText>
|
||||
</SelectPrimitive.Item>
|
||||
))
|
||||
SelectItem.displayName = SelectPrimitive.Item.displayName
|
||||
|
||||
const SelectSeparator = React.forwardRef<
|
||||
React.ElementRef<typeof SelectPrimitive.Separator>,
|
||||
React.ComponentPropsWithoutRef<typeof SelectPrimitive.Separator>
|
||||
>(({ className, ...props }, ref) => (
|
||||
<SelectPrimitive.Separator
|
||||
ref={ref}
|
||||
className={cn("-mx-1 my-1 h-px bg-muted", className)}
|
||||
{...props}
|
||||
/>
|
||||
))
|
||||
SelectSeparator.displayName = SelectPrimitive.Separator.displayName
|
||||
|
||||
export {
|
||||
Select,
|
||||
SelectGroup,
|
||||
SelectValue,
|
||||
SelectTrigger,
|
||||
SelectContent,
|
||||
SelectLabel,
|
||||
SelectItem,
|
||||
SelectSeparator,
|
||||
SelectScrollUpButton,
|
||||
SelectScrollDownButton,
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
"use client"
|
||||
|
||||
import * as React from "react"
|
||||
import * as TabsPrimitive from "@radix-ui/react-tabs"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
const Tabs = TabsPrimitive.Root
|
||||
|
||||
const TabsList = React.forwardRef<
|
||||
React.ElementRef<typeof TabsPrimitive.List>,
|
||||
React.ComponentPropsWithoutRef<typeof TabsPrimitive.List>
|
||||
>(({ className, ...props }, ref) => (
|
||||
<TabsPrimitive.List
|
||||
ref={ref}
|
||||
className={cn(
|
||||
"inline-flex h-9 items-center justify-center rounded-lg bg-muted p-1 text-muted-foreground",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
))
|
||||
TabsList.displayName = TabsPrimitive.List.displayName
|
||||
|
||||
const TabsTrigger = React.forwardRef<
|
||||
React.ElementRef<typeof TabsPrimitive.Trigger>,
|
||||
React.ComponentPropsWithoutRef<typeof TabsPrimitive.Trigger>
|
||||
>(({ className, ...props }, ref) => (
|
||||
<TabsPrimitive.Trigger
|
||||
ref={ref}
|
||||
className={cn(
|
||||
"inline-flex items-center justify-center whitespace-nowrap rounded-md px-3 py-1 text-sm font-medium ring-offset-background transition-all focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 data-[state=active]:bg-background data-[state=active]:text-foreground data-[state=active]:shadow",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
))
|
||||
TabsTrigger.displayName = TabsPrimitive.Trigger.displayName
|
||||
|
||||
const TabsContent = React.forwardRef<
|
||||
React.ElementRef<typeof TabsPrimitive.Content>,
|
||||
React.ComponentPropsWithoutRef<typeof TabsPrimitive.Content>
|
||||
>(({ className, ...props }, ref) => (
|
||||
<TabsPrimitive.Content
|
||||
ref={ref}
|
||||
className={cn(
|
||||
"mt-2 ring-offset-background focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
))
|
||||
TabsContent.displayName = TabsPrimitive.Content.displayName
|
||||
|
||||
export { Tabs, TabsList, TabsTrigger, TabsContent }
|
||||
@@ -0,0 +1,33 @@
|
||||
// @/components/ui/toast.tsx
|
||||
import React from 'react';
|
||||
// 定义 ToastProps 类型
|
||||
export interface ToastProps {
|
||||
title: string;
|
||||
description?: string;
|
||||
variant?: 'default' | 'success' | 'warning' | 'destructive';
|
||||
}
|
||||
|
||||
export interface ToasterToast extends ToastProps {
|
||||
id: string;
|
||||
onOpenChange?: (open: boolean) => void;
|
||||
}
|
||||
|
||||
// 定义 ToastActionElement 类型
|
||||
export interface ToastActionElement {
|
||||
label: string;
|
||||
onClick: () => void;
|
||||
}
|
||||
|
||||
|
||||
const Toast = ({ title, description, variant = 'default' }: ToastProps) => {
|
||||
const className = `p-4 rounded-lg shadow-md ${variant === 'success' ? 'bg-green-100' : variant === 'warning' ? 'bg-yellow-100' : variant === 'destructive' ? 'bg-red-100' : 'bg-gray-100'}`;
|
||||
|
||||
return (
|
||||
<div className={className}>
|
||||
<h3 className="text-lg font-medium">{title}</h3>
|
||||
{description && <p className="mt-1 text-sm text-gray-700">{description}</p>}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default Toast;
|
||||
@@ -0,0 +1,48 @@
|
||||
// @/components/ui/use-toast.tsx
|
||||
|
||||
import { useState } from "react";
|
||||
|
||||
import type { ToastActionElement, ToastProps } from "@/components/ui/toast";
|
||||
|
||||
interface ToasterToast extends ToastProps {
|
||||
id: string;
|
||||
description?: string;
|
||||
onOpenChange?: (open: boolean) => void;
|
||||
}
|
||||
|
||||
const useToast = () => {
|
||||
const [toasts, setToasts] = useState<ToasterToast[]>([]);
|
||||
|
||||
const showToast = (props: ToastProps) => {
|
||||
const id = Math.random().toString(36).substr(2, 9);
|
||||
const newToast: ToasterToast = {
|
||||
...props,
|
||||
id,
|
||||
onOpenChange: (open) => {
|
||||
if (!open) dismiss(id);
|
||||
},
|
||||
};
|
||||
setToasts((prevToasts) => [...prevToasts, newToast]);
|
||||
|
||||
// 设置定时器,五秒后移除该 Toast
|
||||
setTimeout(() => {
|
||||
removeToast(id);
|
||||
}, 2000);
|
||||
};
|
||||
|
||||
const removeToast = (id: string) => {
|
||||
setToasts((prevToasts) => prevToasts.filter((toast) => toast.id !== id));
|
||||
};
|
||||
|
||||
const dismiss = (id: string) => {
|
||||
setToasts((prevToasts) =>
|
||||
prevToasts.map((toast) =>
|
||||
toast.id === id ? { ...toast, onOpenChange: undefined } : toast
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
return { toasts, showToast, removeToast };
|
||||
};
|
||||
|
||||
export { useToast, ToastProps, ToastActionElement };
|
||||
Reference in New Issue
Block a user