diff --git a/src/components/SearchResults/SearchResults.tsx b/src/components/SearchResults/SearchResults.tsx index 8c8bd62..bb082f0 100644 --- a/src/components/SearchResults/SearchResults.tsx +++ b/src/components/SearchResults/SearchResults.tsx @@ -6,6 +6,7 @@ import React, { MouseEvent, SetStateAction, useCallback, + useMemo, useState, } from "react"; import Paper from "@mui/material/Paper"; @@ -29,6 +30,7 @@ import { } from "@mui/material"; import CheckCircleOutlineIcon from "@mui/icons-material/CheckCircleOutline"; import { decimalFormatter, integerFormatter } from "@/app/utils/formatUtil"; +import { filter, remove, uniq } from "lodash"; export interface ResultWithId { id: string | number; @@ -228,6 +230,19 @@ function SearchResults({ }; // checkbox + const currItems = useMemo(() => { + return items.length > 10 ? items + .slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage) + .map((i) => i.id) + : items.map((i) => i.id) + }, [items, page, rowsPerPage]) + + const currItemsWithChecked = useMemo(() => { + return filter(checkboxIds, function (c) { + return currItems.includes(c); + }) + }, [checkboxIds, items, page, rowsPerPage]) + const handleRowClick = useCallback( (event: MouseEvent, item: T, columns: Column[]) => { // check is disabled or not @@ -269,6 +284,18 @@ function SearchResults({ [checkboxIds], ); + const handleSelectAllClick = (event: React.ChangeEvent) => { + if (setCheckboxIds) { + const pageItemId = currItems + + if (event.target.checked) { + setCheckboxIds((prev) => uniq([...prev, ...pageItemId])) + } else { + setCheckboxIds((prev) => filter(prev, function (p) { return !pageItemId.includes(p); })) + } + } + } + const table = ( <> @@ -276,15 +303,28 @@ function SearchResults({ {columns.map((column, idx) => ( - - {column.label.split('\n').map((line, index) => ( -
{line}
// Render each line in a div - ))} -
+ isCheckboxColumn(column) ? + + 0 && currItemsWithChecked.length < currItems.length} + checked={currItems.length > 0 && currItemsWithChecked.length >= currItems.length} + onChange={handleSelectAllClick} + /> + + : + {column.label.split('\n').map((line, index) => ( +
{line}
// Render each line in a div + ))} +
))}
@@ -299,6 +339,7 @@ function SearchResults({ tabIndex={-1} key={item.id} onClick={(event) => { + console.log("first") setCheckboxIds ? handleRowClick(event, item, columns) : undefined @@ -329,7 +370,19 @@ function SearchResults({ }) : items.map((item) => { return ( - + { + setCheckboxIds + ? handleRowClick(event, item, columns) + : undefined + + if (onRowClick) { + onRowClick(item) + } + } + } + role={setCheckboxIds ? "checkbox" : undefined} + > {columns.map((column, idx) => { const columnName = column.name;