Currently, I have a 3 rows in a table. Each row has two column: file name and a button
file name is just a dummy link.
button will hide show a menu.
My requirements are following:
- Click a button, it will toggle the menu. i.e. if prev it is close, it should be open. If prev is open, close.
- When you are click on this button, if other menus are open, they should be close.
- Each time, only 1 menu is open.
git clone, npm install, npm start
I have the following code
import React, {useState, useEffect} from 'react';
function Menu({buttonName, menuIndex, currRowInd, setCurrRowInd}) {
// inside menu
const [open, setOpen] = useState(false);
const [showMenu, setShowMenu] = useState(false);
const menuItems = {download: 'download', view: 'view', delete: 'delete'};
useEffect(() => {
if (open && menuIndex === currRowInd) {
setShowMenu(true);
} else {
setShowMenu(false);
}
}, [open, currRowInd]);
return (
<div>
<button
onClick={event => {
// it is mouse click
if (event.pageX !== 0 && event.pageY !== 0) {
// toggle
setOpen(!open);
setCurrRowInd(menuIndex);
}
}}
>
{buttonName}
</button>
{showMenu && (
<ul style={{padding: '5px', margin: '10px', border: '1px solid #ccc'}}>
{Object.keys(menuItems).map((item, itemIndex) => {
return (
<li
tabIndex="0"
key={itemIndex}
style={{
listStyle: 'none',
padding: '5px',
backgroundColor: 'blue'
}}
>
{item}
</li>
);
})}
</ul>
)}
</div>
);
}
function TableElement() {
const [currRowInd, setCurrRowInd] = useState('');
const items = [
{
file: 'file1',
button: 'button1'
},
{
file: 'file2',
button: 'button2'
},
{
file: 'file3',
button: 'button3'
}
];
return (
<table style={{borderCollapse: 'collapse', border: '1px solid black'}}>
<tbody>
{items.map((item, index) => {
return (
<tr key={index}>
<td style={{border: '1px solid black'}}>
<a href="#">{item.file}</a>
</td>
<td style={{border: '1px solid black'}}>
<Menu
buttonName={item.button}
menuIndex={index}
currRowInd={currRowInd}
setCurrRowInd={setCurrRowInd}
/>
</td>
</tr>
);
})}
</tbody>
</table>
);
}
function App() {
return (
<>
<TableElement />
</>
);
}
export default App;
I have a bug:
- Click button 1, it opens 1st menu (good)
- Click button 2, it opens 2nd menu and close 1st menu (good)
- Click button 1 again, 2nd menu close (good so far), but 1st menu is not open.
Any idea?