Advertisement
Guest User

Untitled

a guest
Sep 1st, 2015
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. 'use strict';
  2.  
  3. import React from 'react';
  4. import DropdownList from 'react-widgets/lib/DropdownList';
  5. import score from 'string-score';
  6.  
  7. class RankedDropdownList extends DropdownList.BaseDropdownList {
  8.  
  9. constructor(...args) {
  10. super(...args);
  11.  
  12. // Overwrite the DropdownList filter method (inherited from mixins/DataFilterMixin.js)
  13. this.filter = function (items, searchTerm) {
  14. // empty search string, return all items
  15. if (searchTerm.length == 0) return items;
  16.  
  17. // The Waffle House of array function chains:
  18. // Scored - score search item vs item.name, wrap object around item with score property
  19. // Filtered - filter scored items, removing those with a score of 0 (zero)
  20. // Sorted - sort scored items by score value desc
  21. // Stripped - strip wrapper object returning only items
  22. return items
  23. .map(function (item) { // Scored
  24. return {score: score(item.name, searchTerm), value: item};
  25. })
  26. .filter(function (item) { // Filtered
  27. return item.score > 0;
  28. })
  29. .sort(function (a, b) { // Sorted
  30. return a.score >= b.score ? -1 : 1
  31. })
  32. .map(function (item) { // Stripped
  33. return item.value;
  34. });
  35. };
  36. }
  37. }
  38.  
  39. // Extend BaseDropdownList propTypes to force requirement of the following props
  40. // necessary for component functionality. This is required since BaseDropdownList
  41. // does not automatically handle this functionality internally.
  42.  
  43. RankedDropdownList.propTypes = Object.assign({},
  44. DropdownList.propTypes,{
  45. open: React.PropTypes.bool.isRequired,
  46. searchTerm: React.PropTypes.string.isRequired,
  47. value: React.PropTypes.any,
  48. onChange: React.PropTypes.func.isRequired,
  49. onSearch: React.PropTypes.func.isRequired,
  50. onToggle: React.PropTypes.func.isRequired
  51. });
  52.  
  53. export default RankedDropdownList;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement