SearchResultList.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. console.log('getHighlightBody', this.props.searchingKeyword);
  10. console.log('getHighlightBody', this.props.searchingKeyword.split(' '));
  11. this.props.searchingKeyword.split(' ').forEach((keyword) => {
  12. console.log(keyword);
  13. const keywordExp = new RegExp('(' + keyword + ')', 'g');
  14. console.log(keywordExp);
  15. console.log(body.repalce(keywordExp, 'hoge hoge'));
  16. //body = body.repalce(keywordExp, '<span style="highlighted">$1</span>');
  17. });
  18. //console.log(this.props.searchingKeyword, body);
  19. return body;
  20. }
  21. render() {
  22. const resultList = this.props.pages.map((page) => {
  23. const pageBody = this.getHighlightBody(page.revision.body);
  24. //console.log('resultList.page.path', page.path);
  25. //console.log('resultList.pageBody', pageBody);
  26. return (
  27. <div id={page._id}>
  28. <h2>{page.path}</h2>
  29. <div>
  30. <PageBody page={page} pageBody={pageBody} />
  31. </div>
  32. </div>
  33. );
  34. });
  35. return (
  36. <div>
  37. {resultList}
  38. </div>
  39. );
  40. }
  41. }
  42. SearchResultList.propTypes = {
  43. pages: React.PropTypes.array.isRequired,
  44. searchingKeyword: React.PropTypes.string.isRequired,
  45. };
  46. SearchResultList.defaultProps = {
  47. pages: [],
  48. searchingKeyword: '',
  49. };