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

Filter Table Row with Input query #1034

Open
wants to merge 6 commits into
base: 4.2.0
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/paginated-table/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export {
onRowSeclect,
setTotalRowCount,
customPaginationAction,
onInputSearchQueryChange,
} from './table/state/Actions';
export { default as Wrapper, types } from './wrapper/Wrapper';
export { btnTypes } from './wrapper/components/AddFiles';
Expand Down
23 changes: 22 additions & 1 deletion packages/paginated-table/src/table/ClientController.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const TableController = ((props) => {
sortBy,
sortOrder,
groupBy,
searchQuery,
} = table;

/**
Expand Down Expand Up @@ -115,18 +116,38 @@ const TableController = ((props) => {
return alphaNumericSort(b, a);
};

// filter rows based on input serch query
const filterBySearchQuery = (rows) => {
if (!searchQuery) return rows;
const query = searchQuery.toLowerCase();
const viewRows = rows.filter((row) => {
const rowValues = Object.keys(row)
.map((key) => `${row[key]}`.toLowerCase()).join(' ');
return rowValues.includes(query);
});
return viewRows;
};

// sort based on row grouping
// groupBy column name
const sortedRows = (groupBy) ? sortWithRowGrouping(updateRows)
: [...updateRows].sort((a, b) => sortRows(a, b));

// filter by search Query
const filterRows = filterBySearchQuery(sortedRows);

const startIndex = page * rowsPerPage;
const endIndex = startIndex + rowsPerPage;
const displayRows = [...sortedRows].slice(startIndex, endIndex);
const displayRows = [...filterRows].slice(startIndex, endIndex);

return (
<>
<TableView
{...props}
table={{
...table,
totalRowCount: filterRows.length,
}}
totalRows={tblRows}
tableRows={displayRows}
/>
Expand Down
13 changes: 13 additions & 0 deletions packages/paginated-table/src/table/PaginatedTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
onColumnViewChange,
onRowDelete,
onClearCart,
onInputSearchQueryChange,
} from './state/Actions';
import { TableContext } from './ContextProvider';
import reducer from './state/Reducer';
Expand Down Expand Up @@ -91,6 +92,7 @@ const PaginatedTable = ({
customizeColumnViewChange,
customizeDeleteRow,
customizeDeleteAllRows,
customizeSearchQueryChange,
} = paginationOptions;

/**
Expand Down Expand Up @@ -181,6 +183,15 @@ const PaginatedTable = ({
}));
};

/**
* Filter table rows based on Search query
*/
const handleSeachQueryChange = (value) => {
dispatch(onInputSearchQueryChange({
searchQuery: value,
}));
};

/**
* A. client table
* table data provide by bento app (tblRows)
Expand All @@ -200,6 +211,7 @@ const PaginatedTable = ({
onColumnViewChange={customizeColumnViewChange || handleColumnViewChange}
onDeleteRow={customizeDeleteRow || onDeleteRow}
onDeleteAllFiles={customizeDeleteAllRows || onDeleteAllRows}
onSearchQueryChange={customizeSearchQueryChange || handleSeachQueryChange}
themeConfig={themeConfig}
/>
</>
Expand Down Expand Up @@ -232,6 +244,7 @@ const PaginatedTable = ({
onColumnViewChange={customizeColumnViewChange || handleColumnViewChange}
onDeleteRow={customizeDeleteRow || onDeleteRow}
onDeleteAllFiles={customizeDeleteAllRows || onDeleteAllRows}
onSearchQueryChange={customizeSearchQueryChange || handleSeachQueryChange}
themeConfig={themeConfig}
/>
</>
Expand Down
5 changes: 4 additions & 1 deletion packages/paginated-table/src/table/TableService.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ const getPaginatedQueryVariables = (queryVariables, table) => {
rowsPerPage,
sortBy,
sortOrder,
searchQuery,
} = table;
const offset = page * rowsPerPage;
variables.offset = offset;
variables.order_by = sortBy;
variables.first = rowsPerPage;
variables.sort_direction = sortOrder;
variables.search_query = searchQuery;
return variables;
};

Expand All @@ -54,6 +56,7 @@ export const getTableData = ({ queryVariables, table }) => {
sortOrder,
query,
sortBy,
searchQuery = '',
} = table;
async function getData() {
const paginatedqueryVariable = getPaginatedQueryVariables(queryVariables, table);
Expand All @@ -78,6 +81,6 @@ export const getTableData = ({ queryVariables, table }) => {
// cancel the request before component unmounts
controller.abort();
};
}, [queryVariables, page, rowsPerPage, sortOrder, sortBy]);
}, [queryVariables, page, rowsPerPage, sortOrder, sortBy, searchQuery]);
return { tableData };
};
6 changes: 6 additions & 0 deletions packages/paginated-table/src/table/state/Actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const actionTypes = {
CUSTOMIZE_PAGINATION_ACTION: 'CUSTOMIZE_PAGINATION_ACTION',
ON_ROWS_DELETE: 'ON_ROWS_DELETE',
ON_ROW_DELETE: 'ON_ROW_DELETE',
ON_SEARCH_QUERY_CHANGE: 'ON_SEARCH_QUERY_CHANGE',
};

export const onColumnViewChange = (columns) => ({
Expand Down Expand Up @@ -66,3 +67,8 @@ export const onRowDelete = (value) => ({
type: actionTypes.ON_ROW_DELETE,
payload: value,
});

export const onInputSearchQueryChange = (value) => ({
type: actionTypes.ON_SEARCH_QUERY_CHANGE,
payload: value,
});
5 changes: 5 additions & 0 deletions packages/paginated-table/src/table/state/Reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ const reducer = (state, action) => {
deletedRows: payload.deleteRows,
selectedRows: payload.selectedRows,
};
case actionTypes.ON_SEARCH_QUERY_CHANGE:
return {
...state,
searchQuery: payload.searchQuery,
};
default:
return state;
}
Expand Down
9 changes: 9 additions & 0 deletions packages/table/src/ExtendedView.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import CustomPagination from './pagination/CustomPagination';
import ManageColumnView from './toolbar/ManageColumnView';
import defaultTheme from './DefaultThemConfig';
import DownloadButton from './toolbar/DownloadButtonView';
import SearchInputView from './toolbar/SearchInputView';

const ExtendedView = ({
table,
rows,
onColumnViewChange,
onRowsPerPageChange,
onPageChange,
onSearchQueryChange,
customTheme,
queryVariables,
server,
Expand All @@ -28,12 +30,19 @@ const ExtendedView = ({
download = false,
manageViewColumns = false,
pagination = false,
searchInput = false,
} = extendedViewConfig;

const themeConfig = createTheme({ overrides: { ...defaultTheme(), ...customTheme } });

return (
<ThemeProvider theme={themeConfig}>
{ (searchInput) && (
<SearchInputView
onSearchQueryChange={onSearchQueryChange}
table={table}
/>
)}
{(download || manageViewColumns) && (
<Toolbar
className="downloadAndColumnView"
Expand Down
2 changes: 2 additions & 0 deletions packages/table/src/TableView.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const TableView = ({
onColumnViewChange,
onDeleteRow,
onDeleteAllFiles,
onSearchQueryChange,
themeConfig = {},
queryVariables,
server,
Expand All @@ -50,6 +51,7 @@ const TableView = ({
onColumnViewChange={onColumnViewChange}
onRowsPerPageChange={onRowsPerPageChange}
onPageChange={onPageChange}
onSearchQueryChange={onSearchQueryChange}
numSelected={table?.selectedRows?.length || 0}
customTheme={themeConfig.extendedView}
queryVariables={queryVariables}
Expand Down
67 changes: 67 additions & 0 deletions packages/table/src/toolbar/SearchInputView.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* eslint-disable jsx-quotes */
import React, { useEffect, useState } from 'react';
import {
InputAdornment,
TextField,
Box,
} from '@material-ui/core';
import SearchIcon from '@material-ui/icons/Search';
import ClearIcon from '@material-ui/icons/Clear';

const SearchInputView = ({
onSearchQueryChange,
table,
}) => {
const [searchText, setSearchText] = useState('');
const handleChange = (event) => {
setSearchText(event.target.value);
if (event.key === 'Enter') {
onSearchQueryChange(event.target.value);
}
};

const handleClick = () => {
onSearchQueryChange('');
setSearchText('');
};

// update input search query
useEffect(() => {
if (table?.searchQuery) {
setSearchText(table?.searchQuery);
}
}, [table?.searchQuery]);

return (
<Box id='table_serach_input'>
<TextField
size='small'
variant='outlined'
onChange={handleChange}
onKeyDown={handleChange}
value={searchText}
InputProps={{
endAdornment: (
<>
{
(table?.searchQuery) && (
<InputAdornment
position='end'
onClick={handleClick}
>
<ClearIcon />
</InputAdornment>
)
}
<InputAdornment position='start'>
<SearchIcon onClick={handleChange} />
</InputAdornment>
</>
),
}}
/>
</Box>
);
};

export default SearchInputView;