2016-12-06 18:06:29 +01:00
|
|
|
import { registerComponent } from 'meteor/nova:lib';
|
2016-03-22 11:04:59 +09:00
|
|
|
import React, { PropTypes, Component } from 'react';
|
2016-06-09 17:42:20 +09:00
|
|
|
import { intlShape } from 'react-intl';
|
2016-03-22 11:04:59 +09:00
|
|
|
import Formsy from 'formsy-react';
|
|
|
|
import FRC from 'formsy-react-components';
|
2016-06-14 10:01:44 +09:00
|
|
|
import { withRouter } from 'react-router'
|
2016-03-22 11:04:59 +09:00
|
|
|
|
|
|
|
const Input = FRC.Input;
|
|
|
|
|
|
|
|
// see: http://stackoverflow.com/questions/1909441/jquery-keyup-delay
|
|
|
|
const delay = (function(){
|
|
|
|
var timer = 0;
|
|
|
|
return function(callback, ms){
|
|
|
|
clearTimeout (timer);
|
|
|
|
timer = setTimeout(callback, ms);
|
|
|
|
};
|
|
|
|
})();
|
|
|
|
|
|
|
|
class SearchForm extends Component{
|
|
|
|
|
2016-06-14 10:01:44 +09:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2016-03-22 11:04:59 +09:00
|
|
|
this.search = this.search.bind(this);
|
2016-06-14 10:01:44 +09:00
|
|
|
this.state = {
|
|
|
|
search: props.router.location.query.query || ''
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
this.setState({
|
|
|
|
search: this.props.router.location.query.query || ''
|
|
|
|
});
|
2016-03-22 11:04:59 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
search(data) {
|
|
|
|
|
2016-06-14 10:01:44 +09:00
|
|
|
const router = this.props.router;
|
|
|
|
const query = data.searchQuery === '' ? {} : {query: data.searchQuery};
|
|
|
|
|
|
|
|
delay(() => {
|
2016-06-14 10:17:11 +09:00
|
|
|
router.push({pathname: "/", query: query});
|
2016-03-22 11:04:59 +09:00
|
|
|
}, 700 );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div className="search-form">
|
|
|
|
<Formsy.Form onChange={this.search}>
|
|
|
|
<Input
|
|
|
|
name="searchQuery"
|
2016-06-14 10:01:44 +09:00
|
|
|
value={this.state.search}
|
2016-06-09 17:42:20 +09:00
|
|
|
placeholder={this.context.intl.formatMessage({id: "posts.search"})}
|
2016-03-22 11:04:59 +09:00
|
|
|
type="text"
|
2016-03-30 10:06:12 +09:00
|
|
|
layout="elementOnly"
|
2016-03-22 11:04:59 +09:00
|
|
|
/>
|
|
|
|
</Formsy.Form>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 12:44:50 +09:00
|
|
|
SearchForm.contextTypes = {
|
2016-06-09 17:42:20 +09:00
|
|
|
intl: intlShape
|
2016-11-15 18:33:16 +01:00
|
|
|
};
|
2016-05-03 12:44:50 +09:00
|
|
|
|
2016-12-06 18:06:29 +01:00
|
|
|
registerComponent('SearchForm', SearchForm, withRouter);
|