diff --git a/compass/components/Table/RowOpenAction.tsx b/compass/components/Table/RowOpenAction.tsx index 8283516..31a5403 100644 --- a/compass/components/Table/RowOpenAction.tsx +++ b/compass/components/Table/RowOpenAction.tsx @@ -27,7 +27,7 @@ export function RowOpenAction({ updateRoute, }: RowOpenActionProps) { return ( -
+
{title} { - const [menuOpen, setMenuOpen] = useState(false); - const openMenu = () => setMenuOpen(true); - const closeMenu = () => setMenuOpen(false); +interface RowOptionMenuProps { + onDelete?: () => void; + onHide: () => void; + visible: boolean; +} - // TODO: Hide menu if clicked elsewhere +export const RowOptionMenu = ({ + onDelete, + onHide, + visible, +}: RowOptionMenuProps) => { + const [menuOpen, setMenuOpen] = useState(false); + const menuRef = useRef(null); + const buttonRef = useRef(null); + + useEffect(() => { + const handleClickOutside = (event: MouseEvent) => { + if ( + menuRef.current && + buttonRef.current && + !menuRef.current.contains(event.target as Node) && + !buttonRef.current.contains(event.target as Node) + ) { + setMenuOpen(false); + } + }; + + document.addEventListener("mousedown", handleClickOutside); + return () => { + document.removeEventListener("mousedown", handleClickOutside); + }; + }, []); return ( <>
*]:rounded z-10" + (!menuOpen ? " invisible" : "") } > - - + )} + {/* { - /* handle open */ - }} + onClick={() => {}} + /> */} + -
); diff --git a/compass/components/Table/Table.tsx b/compass/components/Table/Table.tsx index 538e546..b20dc00 100644 --- a/compass/components/Table/Table.tsx +++ b/compass/components/Table/Table.tsx @@ -97,9 +97,9 @@ export default function Table({ return response; }; - // /** Sorting function based on visibility */ - // const visibilitySort = (a: T, b: T) => - // a.visible === b.visible ? 0 : a.visible ? -1 : 1; + /** Sorting function based on visibility */ + const visibilitySort = (a: T, b: T) => + a.visible === b.visible ? 0 : a.visible ? -1 : 1; // // Sort data on load // useEffect(() => { @@ -115,36 +115,43 @@ export default function Table({ // ); // }; - // const hideData = (dataId: number) => { - // console.log(`Toggling visibility for data with ID: ${dataId}`); - // setData((currentData) => { - // const newData = currentData - // .map((data) => - // data.id === dataId - // ? { ...data, visible: !data.visible } - // : data - // ) - // .sort(visibilitySort); + const hideData = (dataId: number) => { + console.log(`Toggling visibility for data with ID: ${dataId}`); + setData((currentData) => { + const newData = currentData + .map((data) => + data.id === dataId + ? { ...data, visible: !data.visible } + : data + ) + .sort((a, b) => { + // First sort by visibility + const visibilityResult = visibilitySort(a, b); + if (visibilityResult !== 0) return visibilityResult; + // Then sort by id + return a.id - b.id; + }); - // console.log(newData); - // return newData; - // }); - // }; + console.log(newData); + return newData; + }); + }; // Add data manipulation options to the first column - columns.unshift( - columnHelper.display({ - id: "options", - cell: (props) => ( - {}} - onHide={() => {}} - // onDelete={() => deleteData(props.row.original.id)} - // onHide={() => hideData(props.row.original.id)} - /> - ), - }) - ); + if (isAdmin) { + columns.unshift( + columnHelper.display({ + id: "options", + cell: (props) => ( + console.log("delete")} + onHide={() => hideData(props.row.original.id)} + visible={props.row.original.visible} + /> + ), + }) + ); + } // Searching const [query, setQuery] = useState(""); @@ -186,7 +193,12 @@ export default function Table({ scope="col" className={ "p-2 border-gray-200 border-y font-medium " + - (1 < i && i < columns.length - 1 + ((!isAdmin && + 0 < i && + i < columns.length - 1) || + (isAdmin && + 1 < i && + i < columns.length - 1) ? "border-x" : "") } @@ -215,9 +227,11 @@ export default function Table({ {row.getVisibleCells().map((cell, i) => ( {flexRender( cell.column.columnDef.cell,