-
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.
added hook to handle ESC key to close cards
- Loading branch information
Showing
2 changed files
with
73 additions
and
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,67 @@ | ||
import React from "react"; | ||
import { Card } from "../types"; | ||
import useKeyPress from "../hooks/useKeyPress"; | ||
|
||
interface CardDetailsProps { | ||
selectedCard: Card; | ||
handleUpdateSelectedCard: (card: Card) => void; | ||
handleResetSelectedCard: () => void; | ||
selectedCard: Card; | ||
handleUpdateSelectedCard: (card: Card) => void; | ||
handleResetSelectedCard: () => void; | ||
} | ||
|
||
const CardDetails: React.FC<CardDetailsProps> = ({ | ||
selectedCard, | ||
handleUpdateSelectedCard, | ||
handleResetSelectedCard, | ||
selectedCard, | ||
handleUpdateSelectedCard, | ||
handleResetSelectedCard, | ||
}) => { | ||
const toggleCheck = (index: number) => { | ||
if (!selectedCard || !selectedCard.details.checklist) return; | ||
const toggleCheck = (index: number) => { | ||
if (!selectedCard || !selectedCard.details.checklist) return; | ||
|
||
const newSelectedCard = { | ||
...selectedCard, | ||
details: { | ||
...selectedCard.details, | ||
checklist: selectedCard.details.checklist.map((item, idx) => | ||
idx === index ? { ...item, checked: !item.checked } : item | ||
), | ||
}, | ||
}; | ||
const newSelectedCard = { | ||
...selectedCard, | ||
details: { | ||
...selectedCard.details, | ||
checklist: selectedCard.details.checklist.map((item, idx) => | ||
idx === index ? { ...item, checked: !item.checked } : item | ||
), | ||
}, | ||
}; | ||
|
||
handleUpdateSelectedCard(newSelectedCard); | ||
}; | ||
handleUpdateSelectedCard(newSelectedCard); | ||
}; | ||
|
||
return ( | ||
<div className="p-4 w-1/2 mx-auto bg-secondaryElements shadow-md rounded-lg"> | ||
<h2 className="text-lg font-bold mb-2">{selectedCard.cardName}</h2> | ||
<ul> | ||
{selectedCard.details.checklist?.map((item, index) => ( | ||
<li key={index} className="flex items-center mb-2"> | ||
<input | ||
type="checkbox" | ||
checked={item.checked} | ||
onChange={() => toggleCheck(index)} | ||
/> | ||
<label className="ml-2" onClick={() => toggleCheck(index)}> | ||
{item.value} | ||
</label> | ||
</li> | ||
))} | ||
</ul> | ||
<p className="mt-4">Notes: {selectedCard.details.notes}</p> | ||
<p className="mt-1"> | ||
Time Estimate: {selectedCard.details.timeEstimate} Minutes | ||
</p> | ||
<p className="mt-1">Column: {selectedCard.column}</p> | ||
<button | ||
className="mt-8 py-1.5 px-8 text-sm bg-black text-white rounded" | ||
onClick={() => handleResetSelectedCard()} | ||
> | ||
Close | ||
</button> | ||
</div> | ||
); | ||
// Use custom hook to handle ESC key | ||
useKeyPress("Escape", handleResetSelectedCard); | ||
|
||
return ( | ||
<div className="p-4 w-1/2 mx-auto bg-secondaryElements shadow-md rounded-lg"> | ||
<h2 className="text-lg font-bold mb-2">{selectedCard.cardName}</h2> | ||
<ul> | ||
{selectedCard.details.checklist?.map((item, index) => ( | ||
<li key={index} className="flex items-center mb-2"> | ||
<input | ||
type="checkbox" | ||
checked={item.checked} | ||
onChange={() => toggleCheck(index)} | ||
/> | ||
<label className="ml-2" onClick={() => toggleCheck(index)}> | ||
{item.value} | ||
</label> | ||
</li> | ||
))} | ||
</ul> | ||
<p className="mt-4">Notes: {selectedCard.details.notes}</p> | ||
<p className="mt-1"> | ||
Time Estimate: {selectedCard.details.timeEstimate} Minutes | ||
</p> | ||
<p className="mt-1">Column: {selectedCard.column}</p> | ||
<button | ||
className="mt-8 py-1.5 px-8 text-sm bg-black text-white rounded" | ||
onClick={() => handleResetSelectedCard()} | ||
> | ||
Close | ||
</button> | ||
</div> | ||
); | ||
}; | ||
|
||
export default CardDetails; |
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,20 @@ | ||
// hooks/useKeyPress.ts | ||
import { useEffect } from "react"; | ||
|
||
const useKeyPress = (targetKey: string, callback: () => void) => { | ||
useEffect(() => { | ||
const handleKeyDown = (event: KeyboardEvent) => { | ||
if (event.key === targetKey) { | ||
callback(); | ||
} | ||
}; | ||
|
||
document.addEventListener("keydown", handleKeyDown); | ||
|
||
return () => { | ||
document.removeEventListener("keydown", handleKeyDown); | ||
}; | ||
}, [targetKey, callback]); | ||
}; | ||
|
||
export default useKeyPress; |