Skip to content

Commit

Permalink
chore:code improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
SilverD3 committed Nov 4, 2024
1 parent 56e0afa commit 1eb095d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ const ExamForm: FC<ExamProps> = ({
<ExamRowTable
title={t("lab.resultstitle")}
headerData={rowTableHeaders}
onBlur={onBlurCallbackForTableRow}
onChange={onBlurCallbackForTableRow}
rows={examRows}
disabled={isLoading}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ const ExamForm: FC<ExamProps> = ({
<ExamRowTable
title={t("lab.resultstitle")}
headerData={rowTableHeaders}
onBlur={onBlurCallbackForTableRow()}
onChange={onBlurCallbackForTableRow()}
rows={examRows}
disabled={isLoading}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import TableHead from "@mui/material/TableHead";
import TableRow from "@mui/material/TableRow";
import { useAppSelector } from "libraries/hooks/redux";
import { debounce, isEmpty } from "lodash";
import React, { FC, useCallback } from "react";
import React, { ChangeEvent, FC, useCallback } from "react";
import { IState } from "../../../../types";
import "./styles.scss";
import { IEditableTableProps } from "./types";

const ExamRowTable: FC<IEditableTableProps> = ({
rows,
onBlur,
onChange,
headerData,
title,
disabled = false,
Expand All @@ -29,16 +29,22 @@ const ExamRowTable: FC<IEditableTableProps> = ({
state.laboratories.getLabWithRowsByCode.data?.laboratoryRowList
);

const handleOnBlur = (value: string, checked: boolean) => {
debounceUpdate(value, checked);
};

// eslint-disable-next-line react-hooks/exhaustive-deps
const debounceUpdate = useCallback(
debounce((value: string, checked: boolean) => onBlur(value, checked), 100),
debounce(
(value: string, checked: boolean) => onChange(value, checked),
100
),
[]
);

const handleChange = useCallback(
(value: string) => (_: ChangeEvent, checked: boolean) => {
debounceUpdate(value, checked);
},
[debounceUpdate]
);

return (
<Accordion disabled={disabled}>
<AccordionSummary
Expand Down Expand Up @@ -75,9 +81,7 @@ const ExamRowTable: FC<IEditableTableProps> = ({
</TableCell>
<TableCell align="right" component="td" scope="row">
<Checkbox
onChange={(e, value) => {
handleOnBlur(row.label, value);
}}
onChange={handleChange(row.label)}
defaultChecked={
!isEmpty(
labToEditRows?.filter((e) => e === row.label) ?? []
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export interface IEditableTableProps {
rows: Array<{ label: string; value: string }>;
onBlur: (value: string, checked: boolean) => void;
onChange: (value: string, checked: boolean) => void;
fieldValues?: string[];
headerData: Array<{
label: string;
Expand Down

0 comments on commit 1eb095d

Please sign in to comment.