Code coverage report for components/selector_menu/index.js

Statements: 92.86% (39 / 42)      Branches: 75% (12 / 16)      Functions: 100% (13 / 13)      Lines: 92.86% (39 / 42)      Ignored: none     

All files » components/selector_menu/ » index.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 1541 1 1 1 1 1                                 1                 1             13               21   21 42     21       13 13     13       4   4           4 4   4       4           4 4   4 9     4       4         4 4   4 4 4             1             1 1     1           18                                             1
var React = window.React || require('react/addons');
var _ = require('lodash');
var Label = require('./label');
var List = require('./list');
var Search = require('./search');
var fuzzy = require('fuzzy');
 
/*
 * Renders dropdown showing currently selected options,
 * list of items, and fuzzy-search input.
 *
 * Takes a list of options (for example, collection.toJSON());
 * an optional default selection (or don't pass anything for default 'All');
 * and an 'onSelectionChange' callback to trigger some action based on user selection.
 *
 * NOTE:
 * Options in optionsList must have either a 'title' or a 'name' property.
 * 'onSelectionChange' callback must be bound to parent context when passing
 * to SelectorMenu (ie, onSelectionChange = _.bind(this.onSelectionChangeCallback, this)).
 *
 */
 
var SelectorMenu = React.createClass({
 
  propTypes: {
    defaultSelection: React.PropTypes.string,
    optionsList: React.PropTypes.array,
    onSelectionChange: React.PropTypes.func.isRequired
  },
 
  getDefaultProps: function() {
    return {
      defaultSelection: "All",
      optionsList: []
    };
  },
 
  getInitialState: function() {
    return {
      selected: this.props.defaultSelection,
      visible: this.getOptionNames()
    };
  },
 
  getOptionNames: function() {
    // Returns a list of option names, plus the default value.
    var options = [this.props.defaultSelection];
 
    _.each(this.props.optionsList, function(option) {
      return option.title ? options.push(option.title) : options.push(option.name);
    });
 
    return _.unique(options);
  },
 
  onLabelClicked: function() {
    var domNode = this.getDOMNode();
    _.contains(domNode.classList, "expanded") ?
      domNode.classList.remove("expanded") : domNode.classList.add("expanded");
 
    this.refs.searchInput.getDOMNode().focus();
  },
 
  cleanSearchState: function() {
    this.refs.searchInput.getDOMNode().value = "";
 
    this.setState({
      visible: this.getOptionNames()
    });
  },
 
  selectOption: function(optionName) {
    this.cleanSearchState();
    this.onLabelClicked();
 
    this.setState({
      selected: optionName
    });
 
    this.props.onSelectionChange(optionName);
  },
 
  normalizeInputValue: function(val) {
    // Normalizes casing to option casing from user input.
    // Matches partials against an alphabetically sorted list.
    val = val.toLowerCase();
    var sortedNames = this.getOptionNames().sort();
 
    var selection = _.find(sortedNames, function(name) {
      return name.toLowerCase().indexOf(val) > -1;
    });
 
    Iif (!selection || selection === this.props.defaultSelection) {
      selection = this.props.defaultSelection;
    }
 
    return selection;
  },
 
  checkIfSubmit: function(ev) {
    // If user presses ENTER in input box, submit choice.
    var value = ev.target.value;
    var option = "";
 
    Eif (ev.which === 13 && value) {
      option = this.normalizeInputValue(value);
      this.selectOption(option);
    }
  },
 
  filterList: function(filterBy) {
    // If user input, returns fuzzy search-delimited list of options;
    // else, shows all options.
    Iif (!filterBy) {
      this.setState({
        visible: this.getOptionNames()
      });
      return;
    }
 
    var visible = _.map(fuzzy.filter(filterBy, this.state.visible), function(result) {
      return result.string;
    });
 
    this.setState({
      visible: visible
    });
  },
 
  render: function() {
    return (
      <div className="selector-menu-wrapper">
        <Label
          selected={this.state.selected}
          onClick={this.onLabelClicked}
        />
        <div className="inner-menu-wrapper">
          <Search
            ref="searchInput"
            onKeyDown={this.checkIfSubmit}
            filterList={this.filterList}
          />
          <List
            defaultSelection={this.props.defaultSelection}
            optionNames={this.state.visible}
            onOptionSelect={this.selectOption}
          />
        </div>
      </div>
    );
  }
});
 
module.exports = SelectorMenu;