Files
tanami-admin-panel/src/Components/DataTable/NormalTable.jsx

206 lines
6.2 KiB
React
Raw Normal View History

2024-08-01 13:52:03 +05:30
import React from "react";
2024-08-02 13:00:11 +05:30
import {
Table,
TableContainer,
Tbody,
Td,
Th,
Thead,
Tr,
Skeleton,
TableCaption,
2024-10-02 18:41:38 +05:30
Checkbox,
2024-10-09 17:57:11 +05:30
Radio,
2024-08-02 13:00:11 +05:30
} from "@chakra-ui/react";
2024-08-01 13:52:03 +05:30
import EmptySearchList from "../EmptySearchList";
2024-08-12 12:22:01 +05:30
import { TABLE_PAGINATION } from "../../Constants/Paginations";
2024-08-01 13:52:03 +05:30
2024-10-02 18:41:38 +05:30
const NormalTable = ({
2024-08-02 13:00:11 +05:30
data,
isLoading,
tableHeadRow,
emptyMessage,
centered,
2024-08-26 18:59:29 +05:30
total,
2024-10-02 18:41:38 +05:30
showRadioButton, // Prop for showing the checkboxes
selectedRadio,
setSelectedRadio, // State for managing selected checkboxes
2024-10-09 17:57:11 +05:30
handleCheckboxChange: radioChange,
radio
2024-08-02 13:00:11 +05:30
}) => {
const columnWidth =
data && data[0]
? `${(100 / Object.keys(data[0]).length).toFixed(2)}%`
: "auto";
2024-10-02 18:41:38 +05:30
// Toggle checkbox selection for individual rows
2024-10-09 17:57:11 +05:30
// 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];
// }
// });
// };
2024-10-02 18:41:38 +05:30
// 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
}
};
2024-10-09 17:57:11 +05:30
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];
}
});
}
};
2024-08-01 13:52:03 +05:30
return (
2024-10-02 18:41:38 +05:30
<TableContainer overflowX={"auto"} className="h-auto w-100 table-scroll">
2024-08-01 13:52:03 +05:30
{data?.length === 0 ? (
<EmptySearchList message={emptyMessage} />
) : (
2024-08-02 13:00:11 +05:30
<Table size="sm">
2024-08-26 18:59:29 +05:30
<TableCaption p={total ? 0 : null}>
{total ? total : "Tanami v1.0.0"}
</TableCaption>
2024-10-02 18:41:38 +05:30
<Thead bg="forestGreen.100">
2024-08-01 13:52:03 +05:30
<Tr>
2024-10-09 17:57:11 +05:30
{showRadioButton &&(
2024-10-02 18:41:38 +05:30
<Th
color={"purple.900"}
textAlign={"center"}
p={4}
whiteSpace="normal"
wordBreak="normal"
overflowWrap="normal"
textTransform={"none"}
>
2024-10-09 17:57:11 +05:30
{radio? "Select":<Checkbox
isChecked={selectedRadio?.length === data?.length}
onChange={handleCheckAllChange}
colorScheme="forestGreen"
2024-10-02 18:41:38 +05:30
2024-10-09 17:57:11 +05:30
/>}
2024-10-02 18:41:38 +05:30
</Th>
)}
2024-08-01 13:52:03 +05:30
{tableHeadRow.map((heading, index) => (
2024-10-02 18:41:38 +05:30
<Th
color={"purple.900"}
2024-08-02 13:00:11 +05:30
textAlign={
tableHeadRow.length - 1 === index || centered
? "center"
: "left"
}
key={index}
2024-10-02 18:41:38 +05:30
p={4}
whiteSpace="normal"
wordBreak="normal"
overflowWrap="normal"
2024-08-26 18:59:29 +05:30
textTransform={"none"}
2024-08-02 13:00:11 +05:30
>
2024-08-01 13:52:03 +05:30
{isLoading ? <Skeleton height="20px" /> : heading}
</Th>
))}
</Tr>
</Thead>
<Tbody className="web-text-small">
{isLoading
2024-08-26 18:59:29 +05:30
? Array.from({ length: TABLE_PAGINATION?.size }).map(
(_, index) => (
2024-10-02 18:41:38 +05:30
<Tr
bg={index % 2 === 0 ? "white" : "forestGreen.50"}
key={index}
>
2024-08-26 18:59:29 +05:30
{tableHeadRow.map((_, i) => (
<Td
key={i}
style={{
whiteSpace: "nowrap",
textOverflow: "ellipsis",
}}
className="web-text-small"
>
<Skeleton height="20px" mb={1} mt={1} />
</Td>
))}
</Tr>
)
)
2024-08-01 13:52:03 +05:30
: data?.map((item, index) => (
2024-10-02 18:41:38 +05:30
<Tr
cursor={"pointer"}
transition={"0.2s all"}
maxH={8}
bg={index % 2 === 0 ? "" : "forestGreen.50"}
key={index}
>
{showRadioButton && (
<Td textAlign={"center"}>
2024-10-09 17:57:11 +05:30
{radio ? <Radio
bg={"#fff"}
colorScheme="forestGreen"
value={item.id}
isChecked={selectedRadio.includes(item.id)}
onChange={() => radioChange(item.id, item)}
/>:<Checkbox
2024-10-02 18:41:38 +05:30
bg={"#fff"}
colorScheme="forestGreen"
value={item.id}
isChecked={selectedRadio.includes(item.id)}
onChange={() => handleCheckboxChange(item.id)}
2024-10-09 17:57:11 +05:30
/>}
2024-10-02 18:41:38 +05:30
</Td>
)}
2024-08-01 13:52:03 +05:30
{tableHeadRow.map((heading, i) => (
2024-10-02 18:41:38 +05:30
<Td
2024-08-02 13:00:11 +05:30
textAlign={
tableHeadRow?.length - 1 === i || centered
? "center"
: "left"
}
color={"gray.600"}
key={i}
2024-10-02 18:41:38 +05:30
fontWeight={500}
2024-08-02 13:00:11 +05:30
style={{
whiteSpace: "nowrap",
textOverflow: "ellipsis",
}}
className="web-text-small"
>
2024-08-01 13:52:03 +05:30
{item[heading]}
</Td>
))}
</Tr>
))}
</Tbody>
</Table>
)}
</TableContainer>
);
};
2024-10-02 18:41:38 +05:30
export default NormalTable;