import React from "react"; import { Table, TableContainer, Tbody, Td, Th, Thead, Tr, Skeleton, TableCaption, Checkbox, Radio, useColorMode, } from "@chakra-ui/react"; import { TABLE_PAGINATION } from "../../Constants/Paginations"; const NormalTable = ({ data, isLoading, tableHeadRow, emptyMessage, centered, total, showRadioButton, // Prop for showing the checkboxes selectedRadio, setSelectedRadio, // State for managing selected checkboxes handleCheckboxChange: radioChange, radio }) => { const columnWidth = data && data[0] ? `${(100 / Object.keys(data[0]).length).toFixed(2)}%` : "auto"; const { colorMode} = useColorMode(); // Toggle checkbox selection for individual rows // const handleCheckboxChange = (value) => { // setSelectedRadio((prev) => { // if (prev.includes(value)) { // // Remove if already selected // return prev.filter((id) => id !== value); // } else { // // Add to selected // return [...prev, value]; // } // }); // }; // Handle "Check All" checkbox const handleCheckAllChange = () => { if (selectedRadio.length === data.length) { setSelectedRadio([]); // Deselect all if already selected } else { const allIds = data.map((item) => item.id); setSelectedRadio(allIds); // Select all } }; const handleCheckboxChange = (value) => { if (radio) { setSelectedRadio([value]); } else { setSelectedRadio((prev) => { if (prev.includes(value)) { return prev.filter((id) => id !== value); } else { return [...prev, value]; } }); } }; return ( {showRadioButton &&( )} {tableHeadRow.map((heading, index) => ( ))} {isLoading ? Array.from({ length: TABLE_PAGINATION?.size }).map( (_, index) => ( {tableHeadRow.map((_, i) => ( ))} ) ) : data?.map((item, index) => ( {showRadioButton && ( )} {tableHeadRow.map((heading, i) => ( ))} ))}
{radio? "Select":} {isLoading ? : heading}
{radio ? radioChange(item.id, item)} />: handleCheckboxChange(item.id)} />} {item[heading]}
); }; export default NormalTable;