Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BWS-205: + search input to all tables #253

Draft
wants to merge 2 commits into
base: development
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions frontend/src/components/common/input/input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type Props = {
type?: InputType;
placeholder?: string;
rows?: number;
value?: string;
onChange?: React.ChangeEventHandler<HTMLInputElement>;
};

const Input: FC<Props> = ({
Expand All @@ -28,6 +30,8 @@ const Input: FC<Props> = ({
placeholder = '',
type = InputType.TEXT,
rows,
value,
onChange,
}) => {
const { field } = useFormControl({ name, control });
const hasError = Boolean(errors[name]);
Expand Down Expand Up @@ -60,6 +64,8 @@ const Input: FC<Props> = ({
className={getValidClasses(
hasError ? styles.inputError : styles.input,
)}
value={value}
onChange={onChange}
/>
)}
</span>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { GlobalFilter } from './global-filter/global-filter';
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { FC } from 'react';
import { FilterValue, useAsyncDebounce } from 'react-table';
import { useState, useAppForm } from 'hooks/hooks';
import { Input } from 'components/common/common';
import { InputType } from 'common/enums/enums';

type Props = {
globalFilter: string;
setGlobalFilter: (filterValue: FilterValue) => void;
};

const GlobalFilter: FC<Props> = ({ globalFilter, setGlobalFilter }) => {
const { control, errors } = useAppForm({
defaultValues: { search: '' },
});

const [value, setValue] = useState(globalFilter);
const onChange = useAsyncDebounce((value) => {
setGlobalFilter(value || undefined);
}, 200);

const handleFilterInputChange = (
event: React.ChangeEvent<HTMLInputElement>,
): void => {
setValue(event.target.value);
onChange(event.target.value);
};

return (
<Input
type={InputType.TEXT}
label=""
placeholder="Search"
name={'search'}
control={control}
errors={errors}
value={value || ''}
onChange={handleFilterInputChange}
/>
);
};

export { GlobalFilter };
11 changes: 11 additions & 0 deletions frontend/src/components/common/table/table-config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
UseResizeColumnsColumnProps,
UseSortByOptions,
UseSortByColumnProps,
UseGlobalFiltersState,
} from 'react-table';

declare module 'react-table' {
Expand All @@ -20,4 +21,14 @@ declare module 'react-table' {
D extends Record<string, unknown> = Record<string, unknown>,
> extends UseResizeColumnsColumnProps<D>,
UseSortByColumnProps<D> {}

export interface TableState<
D extends Record<string, unknown> = Record<string, unknown>,
> extends UseColumnOrderState<D>,
UseGlobalFiltersState<D> {}

export interface TableInstance<
D extends Record<string, unknown> = Record<string, unknown>,
> extends UseColumnOrderInstanceProps<D>,
UseGlobalFiltersInstanceProps<D> {}
}
34 changes: 24 additions & 10 deletions frontend/src/components/common/table/table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import {
useResizeColumns,
useSortBy,
useTable,
useGlobalFilter,
} from 'react-table';
import { getValidClasses } from 'helpers/helpers';
import styles from './styles.module.scss';
import { Pagination } from 'components/pagination/pagination';
import { Loader } from 'components/common/common';
import { GlobalFilter } from './components/components';

type Props = {
columns: Column[];
Expand Down Expand Up @@ -39,16 +41,24 @@ const Table: FC<Props> = ({
dataTestid,
isLoading,
}) => {
const { getTableProps, getTableBodyProps, headerGroups, rows, prepareRow } =
useTable(
{
columns: columns as Column<Record<string, string>>[],
data: data as Record<string, string>[],
},
useSortBy,
useBlockLayout,
useResizeColumns,
);
const {
getTableProps,
getTableBodyProps,
headerGroups,
rows,
prepareRow,
state,
setGlobalFilter,
} = useTable(
{
columns: columns as Column<Record<string, string>>[],
data: data as Record<string, string>[],
},
useGlobalFilter,
useSortBy,
useBlockLayout,
useResizeColumns,
);

const hasStrPlaceholder = Boolean(placeholder);
const hasData = Boolean(data.length);
Expand All @@ -66,6 +76,10 @@ const Table: FC<Props> = ({
{children}
</header>
)}
<GlobalFilter
globalFilter={state.globalFilter}
setGlobalFilter={setGlobalFilter}
/>
<div className={styles.tableContainer}>
{isLoading ? (
<Loader />
Expand Down