Guest User

Untitled

a guest
Jun 21st, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. import React from 'react';
  2. import { AsyncTypeahead } from 'react-bootstrap-typeahead';
  3.  
  4. class ProjectAutocomplete extends React.Component {
  5. constructor(props) {
  6. super(props);
  7. this.state = {
  8. onChange: props.onChange,
  9. searchUrl: props.searchUrl,
  10. allowNew: false,
  11. isLoading: false,
  12. multiple: false,
  13. options: props.defaultSelected
  14. };
  15. this.clear = this.clear.bind(this);
  16. ProjectAutocomplete.formatLabel = ProjectAutocomplete.formatLabel.bind(this);
  17. }
  18.  
  19. clear() {
  20. this.refs.projectAutocomplete.getInstance().clear();
  21. this.refs.projectAutocomplete.getInstance().blur();
  22. }
  23.  
  24. componentDidMount() {
  25. const defaultOptions = this.state.options;
  26. if (defaultOptions.length === 1) {
  27. const option = defaultOptions[0];
  28. this.refs.projectAutocomplete.getInstance()._updateText(ProjectAutocomplete.formatLabel(option));
  29. }
  30. }
  31.  
  32. static formatLabel(option) {
  33. return option ? `${option.projectKey} [${option.type}]: ${option.summary}` : "";
  34. }
  35.  
  36.  
  37. render() {
  38. return (
  39. <div>
  40. <AsyncTypeahead
  41. ref="projectAutocomplete"
  42. labelKey={option => ProjectAutocomplete.formatLabel(option)}
  43. align={'left'}
  44. isLoading={this.state.isLoading}
  45. onChange={this.state.onChange}
  46. onSearch={query => {
  47. this.setState({isLoading: true});
  48. fetch(this.state.searchUrl + "?term=" + query)
  49. .then(resp => resp.json())
  50. .then(json => this.setState({
  51. isLoading: false,
  52. options: json,
  53. }));
  54. }}
  55. options={this.state.options}/>
  56. </div>
  57. );
  58. }
  59.  
  60. }
  61.  
  62. export default ProjectAutocomplete;
Add Comment
Please, Sign In to add comment