Vulcan/packages/nova-base-components/lib/common/SearchForm.jsx

67 lines
No EOL
1.5 KiB
JavaScript

import Telescope from 'meteor/nova:lib';
import React, { PropTypes, Component } from 'react';
import { intlShape } from 'react-intl';
import Formsy from 'formsy-react';
import FRC from 'formsy-react-components';
import { withRouter } from 'react-router'
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{
constructor(props) {
super(props);
this.search = this.search.bind(this);
this.state = {
search: props.router.location.query.query || ''
}
}
componentWillReceiveProps(nextProps) {
this.setState({
search: this.props.router.location.query.query || ''
});
}
search(data) {
const router = this.props.router;
const query = data.searchQuery === '' ? {} : {query: data.searchQuery};
delay(() => {
router.push({pathname: "/", query: query});
}, 700 );
}
render() {
return (
<div className="search-form">
<Formsy.Form onChange={this.search}>
<Input
name="searchQuery"
value={this.state.search}
placeholder={this.context.intl.formatMessage({id: "posts.search"})}
type="text"
layout="elementOnly"
/>
</Formsy.Form>
</div>
)
}
}
SearchForm.contextTypes = {
intl: intlShape
};
Telescope.registerComponent('SearchForm', SearchForm, withRouter);