Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch from lodash to ES5 native (+object-assign polyfill) #124

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"dependencies": {
"classnames": "^1.2.0",
"lodash": "^3.5.0",
"object-assign": "^2.0.0",
"react-input-autosize": "^0.4.3"
},
"devDependencies": {
Expand Down
40 changes: 22 additions & 18 deletions src/Select.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var _ = require('lodash'),
var assign = require('object-assign'),
React = require('react'),
Input = require('react-input-autosize'),
classes = require('classnames'),
Expand Down Expand Up @@ -213,7 +213,9 @@ var Select = React.createClass({
}

return values.map(function(val) {
return (typeof val === 'string') ? val = _.findWhere(options, { value: val }) || { value: val, label: val } : val;
return (typeof val === 'string') ? val = options.filter(function (option) {
return option.value === val;
})[0] || { value: val, label: val } : val;
});
},

Expand All @@ -239,11 +241,13 @@ var Select = React.createClass({
},

popValue: function() {
this.setValue(_.initial(this.state.values));
this.setValue(this.state.values.slice(0, -1));
},

removeValue: function(value) {
this.setValue(_.without(this.state.values, value));
this.setValue(this.state.values.filter(function(val){
return val !== value;
}));
},

clearValue: function(event) {
Expand Down Expand Up @@ -386,7 +390,7 @@ var Select = React.createClass({
isOpen: true,
inputValue: event.target.value,
filteredOptions: filteredOptions,
focusedOption: _.contains(filteredOptions, this.state.focusedOption) ? this.state.focusedOption : filteredOptions[0]
focusedOption: filteredOptions.indexOf(this.state.focusedOption) > -1 ? this.state.focusedOption : filteredOptions[0]
}, this._bindCloseMenuIfClickedOutside);
}
},
Expand All @@ -408,11 +412,11 @@ var Select = React.createClass({
var options = this._optionsCache[cacheKey].options;
var filteredOptions = this.filterOptions(options);

this.setState(_.extend({
this.setState({
options: options,
filteredOptions: filteredOptions,
focusedOption: _.contains(filteredOptions, this.state.focusedOption) ? this.state.focusedOption : filteredOptions[0]
}, state));
focusedOption: filteredOptions.indexOf(this.state.focusedOption) > -1 ? this.state.focusedOption : filteredOptions[0]
});
if(callback) callback({});
return;
}
Expand All @@ -429,11 +433,11 @@ var Select = React.createClass({
}
var filteredOptions = this.filterOptions(data.options);

this.setState(_.extend({
this.setState({
options: data.options,
filteredOptions: filteredOptions,
focusedOption: _.contains(filteredOptions, this.state.focusedOption) ? this.state.focusedOption : filteredOptions[0]
}, state));
focusedOption: filteredOptions.indexOf(this.state.focusedOption) > -1 ? this.state.focusedOption : filteredOptions[0]
});

if(callback) callback({});

Expand All @@ -453,7 +457,7 @@ var Select = React.createClass({
return this.props.filterOptions.call(this, options, filterValue, exclude);
} else {
var filterOption = function(op) {
if (this.props.multi && _.contains(exclude, op.value)) return false;
if (this.props.multi && exclude.indexOf(op.value) > -1) return false;
if (this.props.filterOption) return this.props.filterOption.call(this, op, filterValue);
var valueTest = String(op.value), labelTest = String(op.label);
return !filterValue || (this.props.matchPos === 'start') ? (
Expand All @@ -463,8 +467,8 @@ var Select = React.createClass({
(this.props.matchProp !== 'label' && valueTest.toLowerCase().indexOf(filterValue.toLowerCase()) >= 0) ||
(this.props.matchProp !== 'value' && labelTest.toLowerCase().indexOf(filterValue.toLowerCase()) >= 0)
);
};
return _.filter(options, filterOption, this);
}.bind(this);
return (options||[]).filter(filterOption);
}
},

Expand Down Expand Up @@ -546,7 +550,7 @@ var Select = React.createClass({
focusedValue = focusedValue == null ? this.state.filteredOptions[0] : focusedValue;
}

var ops = _.map(this.state.filteredOptions, function(op) {
var ops = this.state.filteredOptions.map(function(op) {
var isFocused = focusedValue === op.value;

var optionClass = classes({
Expand All @@ -562,7 +566,7 @@ var Select = React.createClass({

return <div ref={ref} key={'option-' + op.value} className={optionClass} onMouseEnter={mouseEnter} onMouseLeave={mouseLeave} onMouseDown={mouseDown} onClick={mouseDown}>{op.label}</div>;

}, this);
}.bind(this));

return ops.length ? ops : (
<div className="Select-noresults">
Expand Down Expand Up @@ -594,7 +598,7 @@ var Select = React.createClass({

if (this.props.multi) {
this.state.values.forEach(function(val) {
var props = _.extend({
var props = assign({
key: val.value,
optionLabelClick: !!this.props.onOptionLabelClick,
onOptionLabelClick: this.handleOptionLabelClick.bind(this, val),
Expand Down Expand Up @@ -629,7 +633,7 @@ var Select = React.createClass({
}

var input;
var inputProps = _.extend({
var inputProps = assign({
ref: 'input',
className: 'Select-input',
tabIndex: this.props.tabIndex || 0,
Expand Down