From c251e8f38bab426f627454ba3f4576e2bb9217aa Mon Sep 17 00:00:00 2001 From: Marie-Laure Thuret Date: Mon, 3 Apr 2017 07:40:35 -0400 Subject: [PATCH] fix(SFFV): fix wrong query behaviour with slow network (#2086) --- .../react-instantsearch/src/components/List.js | 15 ++++++++++----- .../react-instantsearch/src/components/Menu.js | 9 +++++---- .../src/components/RefinementList.js | 8 +++++--- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/packages/react-instantsearch/src/components/List.js b/packages/react-instantsearch/src/components/List.js index 8a31b9ca50..27e9c64c63 100644 --- a/packages/react-instantsearch/src/components/List.js +++ b/packages/react-instantsearch/src/components/List.js @@ -51,7 +51,11 @@ class List extends Component { return extended ? limitMax : limitMin; }; - renderItem = item => { + resetQuery = () => { + this.setState({query: ''}); + } + + renderItem = (item, resetQuery) => { const items = item.items &&
{item.items.slice(0, this.getLimit()).map(child => @@ -70,7 +74,7 @@ class List extends Component { items && item.isRefined && 'itemSelectedParent' )} > - {this.props.renderItem(item)} + {this.props.renderItem(item, resetQuery)} {items}
); @@ -96,11 +100,12 @@ class List extends Component { renderSearchBox() { const {cx, searchForItems, isFromSearch, translate, items, selectItem} = this.props; + const noResults = items.length === 0 && this.state.query !== '' ?
{translate('noResults')}
: null; return
{ this.setState({query: value}); searchForItems(value); @@ -111,7 +116,7 @@ class List extends Component { e.preventDefault(); e.stopPropagation(); if (isFromSearch) { - selectItem(items[0]); + selectItem(items[0], this.resetQuery); } }} /> @@ -136,7 +141,7 @@ class List extends Component {
{searchBox}
- {items.slice(0, limit).map(item => this.renderItem(item))} + {items.slice(0, limit).map(item => this.renderItem(item, this.resetQuery))}
{this.renderShowMore()}
diff --git a/packages/react-instantsearch/src/components/Menu.js b/packages/react-instantsearch/src/components/Menu.js index 63f248881b..eb0c0b1167 100644 --- a/packages/react-instantsearch/src/components/Menu.js +++ b/packages/react-instantsearch/src/components/Menu.js @@ -41,15 +41,15 @@ class Menu extends Component { if (this.context.canRefine) this.context.canRefine(props.canRefine); } - renderItem = item => { - const {refine, createURL} = this.props; + renderItem = (item, resetQuery) => { + const {createURL} = this.props; const label = this.props.isFromSearch ? : item.label; return ( refine(item.value)} + onClick={() => this.selectItem(item, resetQuery)} href={createURL(item.value)} > @@ -63,7 +63,8 @@ class Menu extends Component { ); }; - selectItem = item => { + selectItem = (item, resetQuery) => { + resetQuery(); this.props.refine(item.value); }; diff --git a/packages/react-instantsearch/src/components/RefinementList.js b/packages/react-instantsearch/src/components/RefinementList.js index 39254dda67..6ededc9ec7 100644 --- a/packages/react-instantsearch/src/components/RefinementList.js +++ b/packages/react-instantsearch/src/components/RefinementList.js @@ -44,11 +44,12 @@ class RefinementList extends Component { if (this.context.canRefine) this.context.canRefine(props.canRefine); } - selectItem = item => { + selectItem = (item, resetQuery) => { + resetQuery(); this.props.refine(item.value); }; - renderItem = item => { + renderItem = (item, resetQuery) => { const label = this.props.isFromSearch ? : item.label; @@ -59,7 +60,7 @@ class RefinementList extends Component { {...cx('itemCheckbox', item.isRefined && 'itemCheckboxSelected')} type="checkbox" checked={item.isRefined} - onChange={() => this.selectItem(item)} + onChange={() => this.selectItem(item, resetQuery)} /> @@ -90,6 +91,7 @@ class RefinementList extends Component { 'withSearchBox', 'canRefine', ])} + query={this.state.query} />
);