From c49b2b697b3c98d463edc428c9a9e85d79f949af Mon Sep 17 00:00:00 2001 From: Samuel Vaillant Date: Sun, 7 Jan 2018 11:24:14 +0100 Subject: [PATCH] fix: Clear SearchBox without search as you type (#802) * feat(stories): add SearchBox without search as you type * fix(SearchBox): clear the input when reset button is clicked --- .../src/components/SearchBox.js | 40 ++++++++++--------- .../src/components/SearchBox.test.js | 38 +++++++++++++++++- stories/SearchBox.stories.js | 12 ++++++ 3 files changed, 70 insertions(+), 20 deletions(-) diff --git a/packages/react-instantsearch/src/components/SearchBox.js b/packages/react-instantsearch/src/components/SearchBox.js index 298943f500..214b9c8124 100644 --- a/packages/react-instantsearch/src/components/SearchBox.js +++ b/packages/react-instantsearch/src/components/SearchBox.js @@ -119,17 +119,6 @@ class SearchBox extends Component { ? this.props.currentRefinement : this.state.query; - setQuery = val => { - const { refine, searchAsYouType } = this.props; - if (searchAsYouType) { - refine(val); - } else { - this.setState({ - query: val, - }); - } - }; - onInputMount = input => { this.input = input; if (this.props.__inputRef) { @@ -182,20 +171,33 @@ class SearchBox extends Component { return false; }; - onChange = e => { - this.setQuery(e.target.value); + onChange = event => { + const { searchAsYouType, refine, onChange } = this.props; + const value = event.target.value; - if (this.props.onChange) { - this.props.onChange(e); + if (searchAsYouType) { + refine(value); + } else { + this.setState({ query: value }); + } + + if (onChange) { + onChange(event); } }; - onReset = () => { - this.setQuery(''); + onReset = event => { + const { searchAsYouType, refine, onReset } = this.props; + + refine(''); this.input.focus(); - if (this.props.onReset) { - this.props.onReset(); + if (!searchAsYouType) { + this.setState({ query: '' }); + } + + if (onReset) { + onReset(event); } }; diff --git a/packages/react-instantsearch/src/components/SearchBox.test.js b/packages/react-instantsearch/src/components/SearchBox.test.js index f518e8ac7d..f6108765f6 100644 --- a/packages/react-instantsearch/src/components/SearchBox.test.js +++ b/packages/react-instantsearch/src/components/SearchBox.test.js @@ -2,7 +2,7 @@ import React from 'react'; import renderer from 'react-test-renderer'; -import Enzyme, { mount } from 'enzyme'; +import Enzyme, { shallow, mount } from 'enzyme'; import Adapter from 'enzyme-adapter-react-16'; import SearchBox from './SearchBox'; @@ -251,4 +251,40 @@ describe('SearchBox', () => { instanceWithoutLoadingIndicator.unmount(); instanceWithLoadingIndicator.unmount(); }); + + it('expect to clear the query when the reset button is click with searchAsYouType=true', () => { + const refine = jest.fn(); + + const wrapper = shallow( + + ).dive(); + + // Simulate the ref + wrapper.instance().input = { focus: jest.fn() }; + + wrapper.find('button[type="reset"]').simulate('click'); + + expect(refine).toHaveBeenCalledWith(''); + expect(wrapper.instance().input.focus).toHaveBeenCalled(); + }); + + it('expect to clear the query when the reset button is click with searchAsYouType=false', () => { + const refine = jest.fn(); + + const wrapper = shallow( + + ).dive(); + + // Simulate the ref + wrapper.instance().input = { focus: jest.fn() }; + + // Simulate change event + wrapper.setState({ query: 'Hello' }); + + wrapper.find('button[type="reset"]').simulate('click'); + + expect(refine).toHaveBeenCalledWith(''); + expect(wrapper.instance().input.focus).toHaveBeenCalled(); + expect(wrapper.state()).toEqual({ query: '' }); + }); }); diff --git a/stories/SearchBox.stories.js b/stories/SearchBox.stories.js index a7577a04f5..c592c94bba 100644 --- a/stories/SearchBox.stories.js +++ b/stories/SearchBox.stories.js @@ -83,6 +83,18 @@ stories filterProps, } ) + .addWithJSX( + 'without search as you type', + () => ( + + + + ), + { + displayName, + filterProps, + } + ) .addWithJSX( 'playground', () => (