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

73 lines
1.7 KiB
React
Raw Normal View History

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};
// if (router.location.pathName !== "/") {
// router.push({pathName: "/"});
// }
2016-03-22 11:04:59 +09:00
2016-06-14 10:01:44 +09:00
delay(() => {
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>
)
}
}
SearchForm.contextTypes = {
currentRoute: React.PropTypes.object,
2016-06-09 17:42:20 +09:00
currentUser: React.PropTypes.object,
intl: intlShape
}
2016-06-14 10:01:44 +09:00
module.exports = withRouter(SearchForm);
export default withRouter(SearchForm);