NewPageNameInputter.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import React from 'react';
  2. import { FormGroup, Button, InputGroup } from 'react-bootstrap';
  3. import { AsyncTypeahead } from 'react-bootstrap-typeahead';
  4. import UserPicture from './User/UserPicture';
  5. import PageListMeta from './PageList/PageListMeta';
  6. import PagePath from './PageList/PagePath';
  7. import PropTypes from 'prop-types';
  8. import SearchTypeahead from './SearchTypeahead';
  9. export default class NewPageNameInputter extends React.Component {
  10. constructor(props) {
  11. super(props);
  12. this.crowi = window.crowi; // FIXME
  13. this.state = {
  14. input: '',
  15. keyword: '',
  16. searchedKeyword: '',
  17. pages: [],
  18. isLoading: false,
  19. searchError: null,
  20. };
  21. this.crowi = window.crowi; // FIXME
  22. this.onSearchSuccess = this.onSearchSuccess.bind(this);
  23. this.onSearchError = this.onSearchError.bind(this);
  24. this.restoreForm = this.restoreForm.bind(this);
  25. this.renderMenuItemChildren = this.renderMenuItemChildren.bind(this);
  26. this.onInputChange = this.onInputChange.bind(this);
  27. }
  28. componentDidMount() {
  29. }
  30. componentWillUnmount() {
  31. }
  32. onSearchSuccess(res) {
  33. this.setState({
  34. isLoading: false,
  35. keyword: '',
  36. pages: res.data,
  37. });
  38. }
  39. onSearchError(err) {
  40. this.setState({
  41. isLoading: false,
  42. searchError: err,
  43. });
  44. }
  45. onInputChange(text) {
  46. this.setState({input: text});
  47. }
  48. restoreForm() {
  49. this._searchtypeahead._typeahead.getInstance().clear();
  50. this._searchtypeahead._typeahead.getInstance().input = 'hoge';
  51. }
  52. renderMenuItemChildren(option, props, index) {
  53. const page = option;
  54. return (
  55. <span>
  56. <UserPicture user={page.revision.author} />
  57. <PagePath page={page} />
  58. <PageListMeta page={page} />
  59. </span>
  60. );
  61. }
  62. render() {
  63. const emptyLabel = (this.state.searchError !== null)
  64. ? 'Error on searching.'
  65. : 'No matches found on title...';
  66. return (
  67. <SearchTypeahead
  68. ref={searchTypeahead => this._searchtypeahead = searchTypeahead}
  69. crowi={this.crowi}
  70. onSearchSuccess={this.onSearchSuccess}
  71. onSearchError={this.onSearchError}
  72. onResetButton={this.restoreForm}
  73. emptyLabel={emptyLabel}
  74. keywordOnInit={this.props.parentPageName}
  75. />
  76. );
  77. }
  78. }
  79. NewPageNameInputter.propTypes = {
  80. parentPageName: PropTypes.string.isRequired,
  81. };
  82. NewPageNameInputter.defaultProps = {
  83. parentPageName: "",
  84. };