Skip to content

Commit

Permalink
add debouncing to list searching
Browse files Browse the repository at this point in the history
  • Loading branch information
Jexsie committed Aug 7, 2023
1 parent 52bd419 commit 4ff2932
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ const PatientListList: React.FC = () => {
isValidating={isValidating}
headers={tableHeaders}
patientLists={patientLists}
searchTerm={currentSearchTerm}
setSearchTerm={handleSearch}
refetch={mutate}
error={error}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { CSSProperties, useState } from 'react';
import React, { CSSProperties, useMemo, useState } from 'react';
import {
DataTable,
DataTableCustomRenderProps,
Expand Down Expand Up @@ -28,11 +28,12 @@ import {
useConfig,
} from '@openmrs/esm-framework';
import { ConfigSchema } from '../config-schema';
import styles from './patient-list-list.scss';
import { PatientList } from '../api/types';
import { updatePatientList } from '../api/api-remote';
import { PatientListEmptyState } from './empty-state/empty-state.component';
import { useTranslation } from 'react-i18next';
import debounce from 'lodash-es/debounce';
import styles from './patient-list-list.scss';

interface PatientListTableContainerProps {
style?: CSSProperties;
Expand All @@ -44,7 +45,6 @@ interface PatientListTableContainerProps {
handleCreate?: () => void;
error: Error;
isValidating: boolean;
searchTerm: string;
setSearchTerm: (searchString: string) => void;
}

Expand All @@ -58,7 +58,6 @@ const PatientListTableContainer: React.FC<PatientListTableContainerProps> = ({
handleCreate,
error,
isValidating,
searchTerm,
setSearchTerm,
}) => {
const { t } = useTranslation();
Expand All @@ -85,10 +84,14 @@ const PatientListTableContainer: React.FC<PatientListTableContainerProps> = ({
}
const { paginated, goTo, results, currentPage } = usePagination(sortedData, pageSize);

const handleSearch = (str) => {
setSearchTerm(str);
goTo(1);
};
const handleSearch = useMemo(
() =>
debounce((searchTerm) => {
goTo(1);
setSearchTerm(searchTerm);
}, 300),
[],
);

return (
<div>
Expand All @@ -101,7 +104,6 @@ const PatientListTableContainer: React.FC<PatientListTableContainerProps> = ({
size={isDesktop(layout) ? 'sm' : 'lg'}
className={styles.search}
onChange={(evnt) => handleSearch(evnt.target.value)}
value={searchTerm}
placeholder={t('searchThisList', 'Search this list')}
/>
</Layer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ const PatientTable: React.FC<PatientTableProps> = ({
id="patient-list-search"
placeholder={search.placeHolder}
labelText=""
size={isDesktop(layout) ? 'sm' : 'xl'}
size={isDesktop(layout) ? 'sm' : 'lg'}
className={styles.searchOverrides}
onChange={(evnt) => handleSearch(evnt.target.value)}
defaultValue={search.currentSearchTerm}
Expand Down

0 comments on commit 4ff2932

Please sign in to comment.