NewPageNameInput.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import SearchTypeahead from './SearchTypeahead';
  4. export default class NewPageNameInput extends React.Component {
  5. constructor(props) {
  6. super(props);
  7. this.state = {
  8. searchError: null,
  9. };
  10. this.crowi = this.props.crowi;
  11. this.onSearchError = this.onSearchError.bind(this);
  12. this.onSubmit = this.onSubmit.bind(this);
  13. this.getParentPageName = this.getParentPageName.bind(this);
  14. }
  15. componentDidMount() {
  16. }
  17. componentWillUnmount() {
  18. }
  19. onSearchError(err) {
  20. this.setState({
  21. searchError: err,
  22. });
  23. }
  24. onSubmit(query) {
  25. // get the closest form element
  26. const elem = this.refs.rootDom;
  27. const form = elem.closest('form');
  28. // submit with jQuery
  29. $(form).submit();
  30. }
  31. getParentPageName(path) {
  32. if (path == '/') {
  33. return path;
  34. }
  35. if (path.match(/.+\/$/)) {
  36. return path;
  37. }
  38. return path + '/';
  39. }
  40. render() {
  41. const emptyLabel = (this.state.searchError !== null)
  42. ? 'Error on searching.'
  43. : 'No matches found on title...';
  44. return (
  45. <div ref='rootDom'>
  46. <SearchTypeahead
  47. ref={this.searchTypeaheadDom}
  48. crowi={this.crowi}
  49. onSearchError={this.onSearchError}
  50. onSubmit={this.onSubmit}
  51. emptyLabel={emptyLabel}
  52. placeholder="Input page name"
  53. keywordOnInit={this.getParentPageName(this.props.parentPageName)}
  54. />
  55. </div>
  56. );
  57. }
  58. }
  59. NewPageNameInput.propTypes = {
  60. crowi: PropTypes.object.isRequired,
  61. parentPageName: PropTypes.string,
  62. };
  63. NewPageNameInput.defaultProps = {
  64. parentPageName: '',
  65. };