Files
rubix-explore/src/components/DataTable/NormalTable.jsx

199 lines
5.8 KiB
React
Raw Normal View History

2024-10-18 20:11:34 +05:30
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 (
2024-10-23 18:00:34 +05:30
<TableContainer boxShadow={'md'} rounded={5} overflowX={"auto"} className="h-auto w-100 table-scroll" bg={colorMode === "light" ? "#F2EFFF" : "#312F35"}>
2024-10-22 13:32:27 +05:30
<Table size="sm" variant="striped" colorScheme={colorMode === "light" ? "whiteScheme" : "darkPurple"}>
2024-10-23 18:00:34 +05:30
<Thead >
2024-10-22 13:32:27 +05:30
<Tr bg={colorMode === "light"? "#230A79" : "#312F35"}>
2024-10-18 20:11:34 +05:30
{showRadioButton &&(
<Th
textAlign={"center"}
p={4}
whiteSpace="normal"
wordBreak="normal"
overflowWrap="normal"
textTransform={"none"}
>
{radio? "Select":<Checkbox
isChecked={selectedRadio?.length === data?.length}
onChange={handleCheckAllChange}
2024-10-22 13:32:27 +05:30
2024-10-18 20:11:34 +05:30
/>}
</Th>
)}
{tableHeadRow.map((heading, index) => (
<Th
color={"#fff"}
2024-11-05 15:31:38 +05:30
fontWeight={400}
2024-10-18 20:11:34 +05:30
textAlign={
tableHeadRow.length - 1 === index || centered
2024-10-23 18:00:34 +05:30
? "left"
2024-10-18 20:11:34 +05:30
: "left"
}
key={index}
p={4}
2024-10-23 18:00:34 +05:30
2024-10-18 20:11:34 +05:30
whiteSpace="normal"
wordBreak="normal"
overflowWrap="normal"
textTransform={"none"}
2024-11-05 15:31:38 +05:30
fontSize={"15px"}
2024-10-18 20:11:34 +05:30
>
{isLoading ? <Skeleton height="20px" /> : heading}
</Th>
))}
</Tr>
</Thead>
<Tbody className="web-text-small">
{isLoading
? Array.from({ length: TABLE_PAGINATION?.size }).map(
(_, index) => (
<Tr
bg={index % 2 === 0 ? "white" : "forestGreen.50"}
key={index}
>
{tableHeadRow.map((_, i) => (
<Td
key={i}
style={{
whiteSpace: "nowrap",
textOverflow: "ellipsis",
}}
className="web-text-small"
>
<Skeleton height="20px" mb={1} mt={1} />
</Td>
))}
</Tr>
)
)
: data?.map((item, index) => (
<Tr
cursor={"pointer"}
transition={"0.2s all"}
maxH={8}
bg={index % 2 === 0 ? "" : "forestGreen.50"}
key={index}
>
{showRadioButton && (
<Td textAlign={"center"} >
{radio ? <Radio
bg={"#fff"}
2024-10-22 13:32:27 +05:30
2024-10-18 20:11:34 +05:30
value={item.id}
isChecked={selectedRadio.includes(item.id)}
onChange={() => radioChange(item.id, item)}
/>:<Checkbox
bg={"#fff"}
2024-10-22 13:32:27 +05:30
2024-10-18 20:11:34 +05:30
value={item.id}
isChecked={selectedRadio.includes(item.id)}
onChange={() => handleCheckboxChange(item.id)}
/>}
</Td>
)}
{tableHeadRow.map((heading, i) => (
<Td p={3}
color={colorMode === "light"? "#000000" : "#FFFFFF"}
textAlign={
tableHeadRow?.length - 1 === i || centered
2024-10-23 18:00:34 +05:30
? "left"
2024-10-18 20:11:34 +05:30
: "left"
}
key={i}
fontWeight={colorMode === "light"? 500 : 400}
style={{
whiteSpace: "nowrap",
textOverflow: "ellipsis",
}}
className="web-text-small"
>
{item[heading]}
</Td>
))}
</Tr>
))}
</Tbody>
</Table>
</TableContainer>
);
};
export default NormalTable;