SearchResult.tsx 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import React from 'react';
  2. import classNames from 'classnames';
  3. import { observer } from 'mobx-react';
  4. import { store } from 'app/stores/store';
  5. export interface SearchResultProps {
  6. search: any;
  7. }
  8. @observer
  9. export class SearchResult extends React.Component<SearchResultProps, any> {
  10. constructor(props) {
  11. super(props);
  12. this.state = {
  13. search: store.search,
  14. };
  15. store.search.query();
  16. }
  17. render() {
  18. return this.state.search.sections.map(section => {
  19. return <SearchResultSection section={section} key={section.id} />;
  20. });
  21. }
  22. }
  23. export interface SectionProps {
  24. section: any;
  25. }
  26. @observer
  27. export class SearchResultSection extends React.Component<SectionProps, any> {
  28. constructor(props) {
  29. super(props);
  30. }
  31. renderItem(item) {
  32. return (
  33. <a className="search-item" href={item.url} key={item.id}>
  34. <span className="search-item__icon">
  35. <i className="fa fa-th-large" />
  36. </span>
  37. <span className="search-item__body">
  38. <div className="search-item__body-title">{item.title}</div>
  39. </span>
  40. </a>
  41. );
  42. }
  43. toggleSection = () => {
  44. this.props.section.toggle();
  45. };
  46. render() {
  47. let collapseClassNames = classNames({
  48. fa: true,
  49. 'fa-plus': !this.props.section.expanded,
  50. 'fa-minus': this.props.section.expanded,
  51. 'search-section__header__toggle': true,
  52. });
  53. return (
  54. <div className="search-section" key={this.props.section.id}>
  55. <div className="search-section__header">
  56. <i className={classNames('search-section__header__icon', this.props.section.icon)} />
  57. <span className="search-section__header__text">{this.props.section.title}</span>
  58. <i className={collapseClassNames} onClick={this.toggleSection} />
  59. </div>
  60. {this.props.section.expanded && (
  61. <div className="search-section__items">{this.props.section.items.map(this.renderItem)}</div>
  62. )}
  63. </div>
  64. );
  65. }
  66. }