-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed annoying dialog and context menu open bug (shadcn-ui/ui#1859)
- Loading branch information
towelie
committed
Dec 17, 2024
1 parent
ec1745c
commit 2891d40
Showing
12 changed files
with
388 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import React, { createContext, useContext, useState, ReactNode } from 'react' | ||
import { Dialog, DialogContent } from "./ui/dialog" | ||
|
||
interface DialogContextType { | ||
openDialog: (content: ReactNode) => void | ||
closeDialog: () => void | ||
} | ||
|
||
const DialogContext = createContext<DialogContextType | undefined>(undefined) | ||
|
||
export function useDialog() { | ||
const context = useContext(DialogContext) | ||
if (!context) { | ||
throw new Error('useDialog must be used within a DialogProvider') | ||
} | ||
return context | ||
} | ||
|
||
export function DialogProvider({ children }: { children: ReactNode }) { | ||
const [isOpen, setIsOpen] = useState(false) | ||
const [dialogContent, setDialogContent] = useState<ReactNode | null>(null) | ||
|
||
const openDialog = (content: ReactNode) => { | ||
setDialogContent(content) | ||
setIsOpen(true) | ||
} | ||
|
||
const closeDialog = () => { | ||
setIsOpen(false) | ||
setDialogContent(null) | ||
} | ||
|
||
return ( | ||
<DialogContext.Provider value={{ openDialog, closeDialog }}> | ||
{children} | ||
<Dialog open={isOpen} onOpenChange={(open) => !open && closeDialog()}> | ||
<DialogContent> | ||
{dialogContent} | ||
</DialogContent> | ||
</Dialog> | ||
</DialogContext.Provider> | ||
) | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,110 @@ | ||
"use client" | ||
|
||
import { Item } from "@/lib/supabase/complex_types" | ||
import { Card, CardHeader } from "./ui/card" | ||
import { EllipsisVertical } from "lucide-react" | ||
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "./ui/card" | ||
import { MoreHorizontal } from 'lucide-react' | ||
import { Button } from "./ui/button" | ||
import { | ||
DropdownMenu, | ||
DropdownMenuContent, | ||
DropdownMenuItem, | ||
DropdownMenuTrigger, | ||
} from "@/components/ui/dropdown-menu" | ||
import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog" | ||
import { useCallback, useState } from "react" | ||
import EditItemForm from "./forms/EditItemForm" | ||
import { DialogProvider, useDialog } from "./DialogProvider" | ||
|
||
interface ItemListProps { | ||
items: Item[] | ||
} | ||
|
||
function ItemListContent({ items }: ItemListProps) { | ||
const [open, setOpen] = useState(false) | ||
const [openId, setOpenId] = useState("") | ||
const { closeDialog } = useDialog() | ||
const [isDialogOpen, setIsDialogOpen] = useState(false); | ||
const [editingItem, setEditingItem] = useState<Item | null>(null); | ||
|
||
const handleEditClick = useCallback((item: Item) => { | ||
setEditingItem(item); | ||
setIsDialogOpen(true); | ||
}, []); | ||
|
||
const handleEditClose = useCallback(() => { | ||
setIsDialogOpen(false); | ||
setEditingItem(null); | ||
closeDialog(); | ||
}, [closeDialog]); | ||
|
||
export default function ItemList({ items }: { items: Item[] }) { | ||
return ( | ||
<div className="grid grid-cols-6 gap-5"> | ||
{ | ||
items.map((item: Item) => ( | ||
<> | ||
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4"> | ||
{items.map((item) => ( | ||
<Card key={item.id}> | ||
<CardHeader> | ||
<div className="flex flex-row gap-3 items-center"> | ||
<h1 className="flex-1"> | ||
{item.name} | ||
</h1> | ||
<EllipsisVertical /> | ||
</div> | ||
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2"> | ||
<CardTitle className="text-sm font-medium"> | ||
{item.name} | ||
</CardTitle> | ||
<DropdownMenu | ||
open={item.id === openId && open} | ||
onOpenChange={(isOpen) => { | ||
setOpen(isOpen) | ||
setOpenId(item.id) | ||
}}> | ||
<DropdownMenuTrigger asChild> | ||
<Button variant="ghost" className="h-8 w-8 p-0"> | ||
<span className="sr-only">Open menu</span> | ||
<MoreHorizontal className="h-4 w-4" /> | ||
</Button> | ||
</DropdownMenuTrigger> | ||
<DropdownMenuContent align="end"> | ||
<DropdownMenuItem onClick={() => { | ||
setOpen(false) | ||
}} onSelect={() => handleEditClick(item)}> | ||
Edit | ||
</DropdownMenuItem> | ||
<DropdownMenuItem>Delete</DropdownMenuItem> | ||
</DropdownMenuContent> | ||
</DropdownMenu> | ||
</CardHeader> | ||
<CardContent> | ||
<CardDescription>Type: {item.type_id}</CardDescription> | ||
<CardDescription>Store: {item.store_id}</CardDescription> | ||
</CardContent> | ||
<CardFooter> | ||
<CardDescription>Favorite: {item.is_favorite ? 'Yes' : 'No'}</CardDescription> | ||
</CardFooter> | ||
</Card> | ||
)) | ||
} | ||
</div> | ||
))} | ||
</div> | ||
<Dialog open={isDialogOpen} onOpenChange={(open) => { | ||
if (!open) { | ||
handleEditClose() | ||
} | ||
}}> | ||
<DialogContent> | ||
<DialogHeader> | ||
<DialogTitle>Edit Item</DialogTitle> | ||
</DialogHeader> | ||
{editingItem && ( | ||
<EditItemForm | ||
item={editingItem} | ||
onClose={handleEditClose} | ||
/> | ||
)} | ||
</DialogContent> | ||
</Dialog> | ||
</> | ||
) | ||
} | ||
|
||
export default function ItemList(props: ItemListProps) { | ||
return ( | ||
<DialogProvider> | ||
<ItemListContent {...props} /> | ||
</DialogProvider> | ||
) | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.