SearchResultList.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import React from 'react';
  2. import PageBody from '../Page/PageBody.js';
  3. export default class SearchResultList extends React.Component {
  4. constructor(props) {
  5. super(props);
  6. this.getHighlightBody = this.getHighlightBody.bind(this);
  7. }
  8. getHighlightBody(body) {
  9. let returnBody = body;
  10. this.props.searchingKeyword.split(' ').forEach((keyword) => {
  11. const keywordExp = new RegExp('(' + keyword.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") + ')', 'g');
  12. returnBody = returnBody.replace(keyword, '<span style="highlighted">$&</span>');
  13. });
  14. //console.log(this.props.searchingKeyword, body);
  15. return returnBody;
  16. }
  17. render() {
  18. const resultList = this.props.pages.map((page) => {
  19. const pageBody = this.getHighlightBody(page.revision.body);
  20. //console.log('resultList.page.path', page.path);
  21. //console.log('resultList.pageBody', pageBody);
  22. return (
  23. <div id={page._id}>
  24. <h2>{page.path}</h2>
  25. <div>
  26. <PageBody page={page} pageBody={pageBody} />
  27. </div>
  28. </div>
  29. );
  30. });
  31. return (
  32. <div>
  33. {resultList}
  34. </div>
  35. );
  36. }
  37. }
  38. SearchResultList.propTypes = {
  39. pages: React.PropTypes.array.isRequired,
  40. searchingKeyword: React.PropTypes.string.isRequired,
  41. };
  42. SearchResultList.defaultProps = {
  43. pages: [],
  44. searchingKeyword: '',
  45. };