Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- import React from 'react';
- import DropdownList from 'react-widgets/lib/DropdownList';
- import score from 'string-score';
- class RankedDropdownList extends DropdownList.BaseDropdownList {
- constructor(...args) {
- super(...args);
- // Overwrite the DropdownList filter method (inherited from mixins/DataFilterMixin.js)
- this.filter = function (items, searchTerm) {
- // empty search string, return all items
- if (searchTerm.length == 0) return items;
- // The Waffle House of array function chains:
- // Scored - score search item vs item.name, wrap object around item with score property
- // Filtered - filter scored items, removing those with a score of 0 (zero)
- // Sorted - sort scored items by score value desc
- // Stripped - strip wrapper object returning only items
- return items
- .map(function (item) { // Scored
- return {score: score(item.name, searchTerm), value: item};
- })
- .filter(function (item) { // Filtered
- return item.score > 0;
- })
- .sort(function (a, b) { // Sorted
- return a.score >= b.score ? -1 : 1
- })
- .map(function (item) { // Stripped
- return item.value;
- });
- };
- }
- }
- // Extend BaseDropdownList propTypes to force requirement of the following props
- // necessary for component functionality. This is required since BaseDropdownList
- // does not automatically handle this functionality internally.
- RankedDropdownList.propTypes = Object.assign({},
- DropdownList.propTypes,{
- open: React.PropTypes.bool.isRequired,
- searchTerm: React.PropTypes.string.isRequired,
- value: React.PropTypes.any,
- onChange: React.PropTypes.func.isRequired,
- onSearch: React.PropTypes.func.isRequired,
- onToggle: React.PropTypes.func.isRequired
- });
- export default RankedDropdownList;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement