diff --git a/src/components/DataTable.tsx b/src/components/DataTable.tsx index c26c84c..c9ffec7 100644 --- a/src/components/DataTable.tsx +++ b/src/components/DataTable.tsx @@ -13,56 +13,61 @@ interface DataTableProps { }[] } +interface FileProps { + title: string + cat: string + file: string + description?: string + selected: boolean +} + const DataTable: React.FC = ({ allFiles }) => { + const initialFiles = allFiles.map((file) => { + const temp = file.data + return { ...temp, selected: false } + }) const [isCheckAll, setIsCheckAll] = useState(false) - const [isCheck, setIsCheck] = useState([]) + const [files, setFiles] = useState(initialFiles) const handleSelectAll = () => { const newIsCheckAll = !isCheckAll // Toggle isCheckAll setIsCheckAll(newIsCheckAll) - - if (newIsCheckAll && allFiles) { - // If select all is checked, set isCheck to all file values - setIsCheck(allFiles.map((file) => file.data.file)) - } else { - // If select all is unchecked, clear isCheck - setIsCheck([]) - } + const filesClone = [...files] + filesClone.forEach((file) => { + file.selected = newIsCheckAll + }) + setFiles([...filesClone]) } - const handleSelect = (value: string) => { - setIsCheck((prevIsCheck) => { - if (prevIsCheck.includes(value)) { - // Remove the item from the state if it's already checked - return prevIsCheck.filter((item) => item !== value) - } else { - // Add the item to the state if it's not checked - return [...prevIsCheck, value] - } - }) + const handleSelect = (selected: boolean, i: number) => { + const temp = files[i] + temp.selected = !selected + const filesClone = [...files] + filesClone[i] = temp + setFiles([...filesClone]) } - const selectedFiles = allFiles.map(({ data }) => { + const selectedFiles = files.map(({ title, file, selected, cat, description }, i) => { return ( - +
handleSelect(data.file)} + name={file} + id={file} + className="mx-1 w-6 h-6 border" + checked={selected} + onClick={() => handleSelect(selected, i)} > -

{data.title}

+

{title}

- {data.cat} - {data.description} + {cat} + {description} ) }) @@ -75,7 +80,7 @@ const DataTable: React.FC = ({ allFiles }) => {