Skip to content

Commit

Permalink
Bugfix/issue 1005 prevent page reset on search close (gregnb#1010)
Browse files Browse the repository at this point in the history
* Use new method of updating when closing search bar

This allows us to branch the functionality so we can more easily assign different behavior for closing the search bar

* Update toolbar test to accommodate search close function

* Add test for search close functionality not reseting page

* Add test for reseting search text on search bar close

* Prettify files
  • Loading branch information
gabrielliwerant authored and wuhuizuo committed Oct 22, 2019
1 parent b2eb1c4 commit 6899731
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/MUIDataTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,23 @@ class MUIDataTable extends React.Component {
);
};

searchClose = () => {
this.setState(
prevState => ({
searchText: null,
displayData: this.options.serverSide
? prevState.displayData
: this.getDisplayData(prevState.columns, prevState.data, prevState.filterList, null),
}),
() => {
this.setTableAction('search');
if (this.options.onSearchChange) {
this.options.onSearchChange(this.state.searchText);
}
},
);
};

searchTextUpdate = text => {
this.setState(
prevState => ({
Expand Down Expand Up @@ -1298,6 +1315,7 @@ class MUIDataTable extends React.Component {
resetFilters={this.resetFilters}
searchText={searchText}
searchTextUpdate={this.searchTextUpdate}
searchClose={this.searchClose}
tableRef={this.getTableContentRef}
title={title}
toggleViewColumn={this.toggleViewColumn}
Expand Down
2 changes: 1 addition & 1 deletion src/components/TableToolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ class TableToolbar extends React.Component {

this.props.setTableAction('onSearchClose');
if (onSearchClose) onSearchClose();
this.props.searchTextUpdate(null);
this.props.searchClose();

this.setState(() => ({
iconActive: null,
Expand Down
65 changes: 65 additions & 0 deletions test/MUIDataTable.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,71 @@ describe('<MUIDataTable />', function() {
assert.deepEqual(JSON.stringify(state.displayData), expectedResult);
});

it('should properly set searchText when hiding the search bar', () => {
const options = {
rowsPerPage: 1,
textLabels,
};
const shallowWrapper = shallow(<MUIDataTable columns={columns} data={data} />);
const table = shallowWrapper.dive();
const instance = table.instance();
const shallowWrapperTableToolbar = shallow(
<TableToolbar
options={options}
searchTextUpdate={spy()}
searchClose={instance.searchClose}
columns={columns}
data={data}
setTableAction={spy()}
/>,
);
const instanceTableToolbar = shallowWrapperTableToolbar.dive().instance();

instance.searchTextUpdate('Joe');
table.update();
instanceTableToolbar.searchButton = { focus: () => {} };
instanceTableToolbar.hideSearch();
table.update();
const searchText = table.state().searchText;

assert.strictEqual(searchText, null);
});

it('should not change page when hiding the search bar', () => {
const options = {
rowsPerPage: 1,
textLabels,
};
const shallowWrapper = shallow(<MUIDataTable columns={columns} data={data} options={options} />);
const table = shallowWrapper.dive();
const instance = table.instance();
const shallowWrapperTableToolbar = shallow(
<TableToolbar
options={options}
searchTextUpdate={spy()}
searchClose={instance.searchClose}
columns={columns}
data={data}
setTableAction={spy()}
/>,
);
const instanceTableToolbar = shallowWrapperTableToolbar.dive().instance();

// Simulate a search that has multiple pages of results
instanceTableToolbar.setActiveIcon('search');
instanceTableToolbar.searchButton = { focus: () => {} };
instance.searchTextUpdate('j');
table.update();

// Simulate changing the page and then hiding the search bar
instance.changePage(1);
instanceTableToolbar.hideSearch();
table.update();

const page = table.state().page;
assert.strictEqual(page, 1);
});

it('should filter displayData when searchText is set', () => {
const options = {
searchText: 'Joe',
Expand Down
1 change: 1 addition & 0 deletions test/MUIDataTableToolbar.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ describe('<TableToolbar />', function() {
const searchTextUpdate = () => {};
const shallowWrapper = shallow(
<TableToolbar
searchClose={() => {}}
searchTextUpdate={searchTextUpdate}
columns={columns}
data={data}
Expand Down

0 comments on commit 6899731

Please sign in to comment.