Skip to content

Commit

Permalink
refactor table search with custom hook useTableSearch
Browse files Browse the repository at this point in the history
  • Loading branch information
fdelemarre committed Jul 2, 2024
1 parent 7ab9741 commit 101d20c
Showing 1 changed file with 18 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,11 @@ export const PerformanceOverviewTable: React.FC<PerformanceOverviewTableProps> =
({ rows, columns, columnRules, editRiskAssessmentColumns }) => {
const [searchTerm, setSearchTerm] = useState<string>("");
const [filterValue, setFilterValue] = useState("");
const [filteredRows, setFilteredRows] = useState(rows);
// const [filteredRows, setFilteredRows] = useState(rows);

const calculateColumns = [...editRiskAssessmentColumns, ...Object.keys(columnRules)];

useEffect(() => {
if (searchTerm === "") {
setFilteredRows(rows);
} else {
const filtered = _(rows)
.filter(row => {
return _(Object.values(row)).some(cell => {
return cell.toLowerCase().includes(searchTerm.toLowerCase());
});
})
.value();
setFilteredRows(filtered);
}
}, [searchTerm, rows]);
const filteredRows = useTableSearch(rows, searchTerm);

const getCellColor = (cellValue: Maybe<string>, column: TableColumn["value"]) => {
// Return "orange" for empty Edit Risk Assessment column
Expand Down Expand Up @@ -185,6 +172,22 @@ export const PerformanceOverviewTable: React.FC<PerformanceOverviewTableProps> =
}
);

const useTableSearch = (rows: PerformanceOverviewTableProps["rows"], searchTerm: string) => {
return useMemo(() => {
if (searchTerm === "") {
return rows;
} else {
return _(rows)
.filter(row => {
return _(Object.values(row)).some(cell => {
return cell.toLowerCase().includes(searchTerm.toLowerCase());
});
})
.value();
}
}, [rows, searchTerm]);
};

const StyledTableContainer = styled(TableContainer)`
border-radius: 4px;
border: 1px solid ${props => props.theme.palette.common.grey1};
Expand Down

0 comments on commit 101d20c

Please sign in to comment.