import React from "react"; import { Table, TableContainer, Tbody, Td, Th, Thead, Tr, Skeleton, TableCaption, Checkbox, Radio, } from "@chakra-ui/react"; import EmptySearchList from "../EmptySearchList"; 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"; // 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) { // If radio is true, select only one option setSelectedRadio([value]); // Set the selected radio to this value only } else { // Handle multiple selection for checkboxes setSelectedRadio((prev) => { if (prev.includes(value)) { // Remove if already selected return prev.filter((id) => id !== value); } else { // Add to selected return [...prev, value]; } }); } }; return ( {data?.length === 0 ? ( ) : ( {total ? total : "Tanami v1.0.0"} {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;