Skip to content

Commit

Permalink
Use prop instead of state for searchText (#1160)
Browse files Browse the repository at this point in the history
* Use prop instead of state for searchText

* Fix search tests
  • Loading branch information
manisandro authored and mbarto committed Oct 21, 2016
1 parent e58adde commit e2ef856
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
13 changes: 2 additions & 11 deletions web/client/components/mapcontrols/search/SearchBar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,8 @@ let SearchBar = React.createClass({
searchText: ""
};
},
getInitialState() {
return {
searchText: this.props.searchText || ""
};
},
onChange() {
var text = this.refs.input.getValue();
this.setState({searchText: text});
this.props.onSearchTextChange(text);
if (this.props.typeAhead) {
delay(() => {this.search(); }, this.props.delay);
Expand All @@ -89,7 +83,7 @@ let SearchBar = React.createClass({
render() {
// const innerGlyphicon = <Button onClick={this.search}></Button>;
const remove = <Glyphicon className="searchclear" glyph="remove" onClick={this.clearSearch}/>;
var showRemove = this.state.searchText !== "";
var showRemove = this.props.searchText !== "";
let placeholder;
if (!this.props.placeholder && this.context.messages) {
let placeholderLocMessage = LocaleUtils.getMessageById(this.context.messages, this.props.placeholderMsgId);
Expand All @@ -108,7 +102,7 @@ let SearchBar = React.createClass({
style={{
textOverflow: "ellipsis"
}}
value={this.state.searchText}
value={this.props.searchText}
ref="input"
addonAfter={showRemove ? remove : <Glyphicon glyph="search"/>}
onKeyDown={this.onKeyDown}
Expand All @@ -122,16 +116,13 @@ let SearchBar = React.createClass({
var text = this.refs.input.getValue();
if (text === undefined || text === "") {
this.props.onSearchReset();
this.setState({searchText: text });
} else {
this.props.onSearch(text);
this.setState({searchText: text });
}

},

clearSearch() {
this.setState({ searchText: ""});
this.props.onSearchReset();
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ describe("test the SearchBar", () => {

it('test search and reset on enter', () => {
var TestUtils = React.addons.TestUtils;
var tb;
const testHandlers = {
onSearchHandler: (text) => {return text; },
onSearchResetHandler: () => {}
onSearchHandler: (text) => { return text; },
onSearchResetHandler: () => {},
onSearchTextChangeHandler: (text) => { tb.setProps({searchText: text}); }
};

const spy = expect.spyOn(testHandlers, 'onSearchHandler');
const spyReset = expect.spyOn(testHandlers, 'onSearchResetHandler');
var tb = ReactDOM.render(<SearchBar delay={0} typeAhead={false} onSearch={testHandlers.onSearchHandler} onSearchReset={testHandlers.onSearchResetHandler}/>, document.getElementById("container"));
tb = ReactDOM.render(<SearchBar delay={0} typeAhead={false} onSearch={testHandlers.onSearchHandler} onSearchReset={testHandlers.onSearchResetHandler} onSearchTextChange={testHandlers.onSearchTextChangeHandler}/>, document.getElementById("container"));
let input = ReactDOM.findDOMNode(TestUtils.scryRenderedDOMComponentsWithTag(tb, "input")[0]);

expect(input).toExist();
Expand All @@ -53,13 +55,16 @@ describe("test the SearchBar", () => {

it('test search and reset buttons', () => {
var TestUtils = React.addons.TestUtils;
var tb;
const testHandlers = {
onSearchHandler: (text) => {return text; },
onSearchResetHandler: () => {}
onSearchHandler: (text) => { return text; },
onSearchResetHandler: () => { tb.setProps({searchText: ""}); },
onSearchTextChangeHandler: (text) => { tb.setProps({searchText: text}); }
};

const spyReset = expect.spyOn(testHandlers, 'onSearchResetHandler');
var tb = ReactDOM.render(<SearchBar delay={0} typeAhead={false} onSearch={testHandlers.onSearchHandler} onSearchReset={testHandlers.onSearchResetHandler}/>, document.getElementById("container"));
spyReset.andCallThrough();
tb = ReactDOM.render(<SearchBar delay={0} typeAhead={false} onSearch={testHandlers.onSearchHandler} onSearchReset={testHandlers.onSearchResetHandler} onSearchTextChange={testHandlers.onSearchTextChangeHandler}/>, document.getElementById("container"));
let input = ReactDOM.findDOMNode(TestUtils.scryRenderedDOMComponentsWithTag(tb, "input")[0]);
// test reset button
expect(input).toExist();
Expand All @@ -75,11 +80,13 @@ describe("test the SearchBar", () => {

it('test typeahead', (done) => {
var TestUtils = React.addons.TestUtils;
var tb;
const testHandlers = {
onSearchHandler: (text) => {return text; }
onSearchHandler: (text) => {return text; },
onSearchTextChangeHandler: (text) => { tb.setProps({searchText: text}); }
};
const spy = expect.spyOn(testHandlers, 'onSearchHandler');
var tb = ReactDOM.render(<SearchBar delay={0} typeAhead={true} onSearch={testHandlers.onSearchHandler} onSearchReset={testHandlers.onSearchResetHandler}/>, document.getElementById("container"));
tb = ReactDOM.render(<SearchBar delay={0} typeAhead={true} onSearch={testHandlers.onSearchHandler} onSearchReset={testHandlers.onSearchResetHandler} onSearchTextChange={testHandlers.onSearchTextChangeHandler}/>, document.getElementById("container"));
let input = ReactDOM.findDOMNode(TestUtils.scryRenderedDOMComponentsWithTag(tb, "input")[0]);

expect(input).toExist();
Expand Down

0 comments on commit e2ef856

Please sign in to comment.