matched-results.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import React from 'react';
  2. import { MatchedText } from 'weaveworks-ui-components';
  3. const SHOW_ROW_COUNT = 2;
  4. const Match = (searchTerms, match) => (
  5. <div className="matched-results-match" key={match.label}>
  6. <div className="matched-results-match-wrapper">
  7. <span className="matched-results-match-label">
  8. {match.label}
  9. :
  10. </span>
  11. <MatchedText
  12. text={match.text}
  13. matches={searchTerms}
  14. />
  15. </div>
  16. </div>
  17. );
  18. export default class MatchedResults extends React.PureComponent {
  19. render() {
  20. const { matches, searchTerms, style } = this.props;
  21. if (!matches) {
  22. return null;
  23. }
  24. let moreFieldMatches;
  25. let moreFieldMatchesTitle;
  26. if (matches.size > SHOW_ROW_COUNT) {
  27. moreFieldMatches = matches
  28. .valueSeq()
  29. .skip(SHOW_ROW_COUNT)
  30. .map(field => field.label);
  31. moreFieldMatchesTitle = `More matches:\n${moreFieldMatches.join(',\n')}`;
  32. }
  33. return (
  34. <div className="matched-results" style={style}>
  35. {matches
  36. .keySeq()
  37. .take(SHOW_ROW_COUNT)
  38. .map(fieldId => Match(searchTerms, matches.get(fieldId)))
  39. }
  40. {moreFieldMatches
  41. && (
  42. <div className="matched-results-more" title={moreFieldMatchesTitle}>
  43. {`${moreFieldMatches.size} more matches`}
  44. </div>
  45. )
  46. }
  47. </div>
  48. );
  49. }
  50. }