Skip to content
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
10 changes: 9 additions & 1 deletion examples/src/components/Multiselect.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var MultiSelectField = React.createClass({
},
getInitialState () {
return {
remove: true,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: use same removeSelected name for state key.

disabled: false,
crazy: false,
options: FLAVOURS,
Expand All @@ -31,6 +32,9 @@ var MultiSelectField = React.createClass({
console.log('You\'ve selected:', value);
this.setState({ value });
},
toggleRemove (e) {
this.setState({ remove: e.target.checked });
},
toggleDisabled (e) {
this.setState({ disabled: e.target.checked });
},
Expand All @@ -45,9 +49,13 @@ var MultiSelectField = React.createClass({
return (
<div className="section">
<h3 className="section-heading">{this.props.label}</h3>
<Select multi simpleValue disabled={this.state.disabled} value={this.state.value} placeholder="Select your favourite(s)" options={this.state.options} onChange={this.handleSelectChange} />
<Select multi simpleValue removeSelected={this.state.remove} disabled={this.state.disabled} value={this.state.value} placeholder="Select your favourite(s)" options={this.state.options} onChange={this.handleSelectChange} />

<div className="checkbox-list">
<label className="checkbox">
<input type="checkbox" className="checkbox-control" checked={this.state.remove} onChange={this.toggleRemove} />
<span className="checkbox-label">Remove selected options</span>
</label>
<label className="checkbox">
<input type="checkbox" className="checkbox-control" checked={this.state.disabled} onChange={this.toggleDisabled} />
<span className="checkbox-label">Disable the control</span>
Expand Down
38 changes: 34 additions & 4 deletions src/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,29 @@ function stringifyValue (value) {
}
}

if (!Array.prototype.findIndex) {
Array.prototype.findIndex = function(predicate) {
if (this === null) {
throw new TypeError('Array.prototype.findIndex called on null or undefined');
}
if (typeof predicate !== 'function') {
throw new TypeError('predicate must be a function');
}
var list = Object(this);
var length = list.length >>> 0;
var thisArg = arguments[1];
var value;

for (var i = 0; i < length; i++) {
value = list[i];
if (predicate.call(thisArg, value, i, list)) {
return i;
}
}
return -1;
};
}

const stringOrNode = React.PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.node
Expand Down Expand Up @@ -101,6 +124,7 @@ const Select = React.createClass({
options: React.PropTypes.array, // array of options
pageSize: React.PropTypes.number, // number of entries to page when using page up/down keys
placeholder: stringOrNode, // field placeholder, displayed when there's no value
removeSelected: React.PropTypes.bool, // whether the selected option is removed from the dropdown on multi selects
required: React.PropTypes.bool, // applies HTML5 required attribute when needed
resetValue: React.PropTypes.any, // value to use when you clear the control
scrollMenuIntoView: React.PropTypes.bool, // boolean to enable the viewport to shift so that the full menu fully visible when engaged
Expand Down Expand Up @@ -152,6 +176,7 @@ const Select = React.createClass({
optionComponent: Option,
pageSize: 5,
placeholder: 'Select...',
removeSelected: true,
required: false,
scrollMenuIntoView: true,
searchable: true,
Expand Down Expand Up @@ -617,7 +642,12 @@ const Select = React.createClass({
inputValue: '',
focusedIndex: null
}, () => {
this.addValue(value);
var valueArray = this.getValueArray(this.props.value);
if (valueArray.find(i => i[this.props.valueKey] === value[this.props.valueKey])) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also update this to use the some method?

this.removeValue(value);
} else {
this.addValue(value);
}
});
} else {
this.setState({
Expand Down Expand Up @@ -653,7 +683,7 @@ const Select = React.createClass({

removeValue (value) {
var valueArray = this.getValueArray(this.props.value);
this.setValue(valueArray.filter(i => i !== value));
this.setValue(valueArray.filter(i => i[this.props.valueKey] !== value[this.props.valueKey]));
this.focus();
},

Expand Down Expand Up @@ -1056,7 +1086,7 @@ const Select = React.createClass({

render () {
let valueArray = this.getValueArray(this.props.value);
let options = this._visibleOptions = this.filterOptions(this.props.multi ? this.getValueArray(this.props.value) : null);
let options = this._visibleOptions = this.filterOptions(this.props.multi && this.props.removeSelected ? valueArray : null);
let isOpen = this.state.isOpen;
if (this.props.multi && !options.length && valueArray.length && !this.state.inputValue) isOpen = false;
const focusedOptionIndex = this.getFocusableOptionIndex(valueArray[0]);
Expand Down Expand Up @@ -1116,7 +1146,7 @@ const Select = React.createClass({
{this.renderClear()}
{this.renderArrow()}
</div>
{isOpen ? this.renderOuter(options, !this.props.multi ? valueArray : null, focusedOption) : null}
{isOpen ? this.renderOuter(options, valueArray, focusedOption) : null}
</div>
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/defaultMenuRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function menuRenderer ({
let Option = optionComponent;

return options.map((option, i) => {
let isSelected = valueArray && valueArray.indexOf(option) > -1;
let isSelected = valueArray && valueArray.findIndex(x => x[valueKey] == option[valueKey]) > -1;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of including a polyfill what about just using the some method from Array?

let isFocused = option === focusedOption;
let optionClass = classNames(optionClassName, {
'Select-option': true,
Expand Down