import React from 'react'; import { FormGroup, Button, InputGroup } from 'react-bootstrap'; import { AsyncTypeahead } from 'react-bootstrap-typeahead'; import axios from 'axios' import UserPicture from '../User/UserPicture'; import PageListMeta from '../PageList/PageListMeta'; import PagePath from '../PageList/PagePath'; import PropTypes from 'prop-types'; // Header.SearchForm export default class SearchForm extends React.Component { constructor(props) { super(props); this.crowi = window.crowi; // FIXME this.state = { input: '', keyword: '', searchedKeyword: '', pages: [], searchError: null, }; this.search = this.search.bind(this); this.clearForm = this.clearForm.bind(this); this.getFormClearComponent = this.getFormClearComponent.bind(this); this.renderMenuItemChildren = this.renderMenuItemChildren.bind(this); this.onInputChange = this.onInputChange.bind(this); this.onChange = this.onChange.bind(this); } componentDidMount() { } componentWillUnmount() { } search(keyword) { if (keyword === '') { this.setState({ keyword: '', searchedKeyword: '', }); return; } this.crowi.apiGet('/search', {q: keyword}) .then(res => { this.setState({ keyword: '', pages: res.data, }); }).catch(err => { this.setState({ searchError: err }); }); } getFormClearComponent() { let isHidden = (this.state.input.length === 0); return isHidden ? : ( ); } clearForm() { this._typeahead.getInstance().clear(); this.setState({keyword: ''}); } onInputChange(text) { this.setState({input: text}); } onChange(options) { const page = options[0]; // should be single page selected // navigate to page window.location = page.path; } renderMenuItemChildren(option, props, index) { const page = option; return ( ); } render() { const emptyLabel = (this.state.searchError !== null) ? 'Error on searching.' : 'No matches found.'; const formClear = this.getFormClearComponent(); return (
this._typeahead = ref} name="q" labelKey="path" minLength={2} options={this.state.pages} placeholder="Search ... Page Title (Path) and Content" submitFormOnEnter={true} onSearch={this.search} onInputChange={this.onInputChange} onChange={this.onChange} renderMenuItemChildren={this.renderMenuItemChildren} /> {formClear}
); } } SearchForm.propTypes = { }; SearchForm.defaultProps = { };