-
Notifications
You must be signed in to change notification settings - Fork 4.1k
removeSelected toggle (for multi). Includes example #882
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6e7b245
removeSelected toggle and examples
bbe4032
Fix removeSelected propType
655b9ee
.is-selected class bugfix for 'value' objects that don't have exactly…
936da8a
Value should be checked against props.valueKey, not the entire object…
6c11ef0
Merge remote-tracking branch 'upstream/master' into remove-selected
dherran-hn c12e658
Re-apply bugfixes in 655b9ee74bdb93e240b33af67c8fd936a27e5eda and 936…
dherran-hn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -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 | ||
|
|
@@ -152,6 +176,7 @@ const Select = React.createClass({ | |
| optionComponent: Option, | ||
| pageSize: 5, | ||
| placeholder: 'Select...', | ||
| removeSelected: true, | ||
| required: false, | ||
| scrollMenuIntoView: true, | ||
| searchable: true, | ||
|
|
@@ -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])) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you also update this to use the |
||
| this.removeValue(value); | ||
| } else { | ||
| this.addValue(value); | ||
| } | ||
| }); | ||
| } else { | ||
| this.setState({ | ||
|
|
@@ -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(); | ||
| }, | ||
|
|
||
|
|
@@ -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]); | ||
|
|
@@ -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> | ||
| ); | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of including a polyfill what about just using the |
||
| let isFocused = option === focusedOption; | ||
| let optionClass = classNames(optionClassName, { | ||
| 'Select-option': true, | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: use same
removeSelectedname for state key.