SearchResult.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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
  57. className={classNames(
  58. "search-section__header__icon",
  59. this.props.section.icon
  60. )}
  61. />
  62. <span className="search-section__header__text">
  63. {this.props.section.title}
  64. </span>
  65. <i className={collapseClassNames} onClick={this.toggleSection} />
  66. </div>
  67. {this.props.section.expanded && (
  68. <div className="search-section__items">
  69. {this.props.section.items.map(this.renderItem)}
  70. </div>
  71. )}
  72. </div>
  73. );
  74. }
  75. }