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

handle options with value 0 properly #1310

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
15 changes: 12 additions & 3 deletions src/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ const Select = React.createClass({
* @param {Object} nextProps - optionally specify the nextProps so the returned array uses the latest configuration
* @returns {Array} the value of the select represented in an array
*/
getValueArray (value, nextProps) {
getValueArray (value, nextProps = undefined) {

Choose a reason for hiding this comment

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

Why is this needed? is not the default behaviour?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes you're right, but my IDE was warning me that this function was sometimes called with only 1 argument. I thought making it explicit wouldn't hurt.

Copy link

Choose a reason for hiding this comment

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

This param being optional should be indicated in the jsdoc http://usejsdoc.org/tags-param.html#optional-parameters-and-default-values, which should also stop the IDE warning

/** support optionally passing in the `nextProps` so `componentWillReceiveProps` updates will function as expected */
const props = typeof nextProps === 'object' ? nextProps : this.props;
if (props.multi) {
Expand Down Expand Up @@ -881,7 +881,16 @@ const Select = React.createClass({
},

renderClear () {
if (!this.props.clearable || (!this.props.value || this.props.value === 0) || (this.props.multi && !this.props.value.length) || this.props.disabled || this.props.isLoading) return;
if (
!this.props.clearable
|| this.props.value === undefined
|| this.props.value === null
|| this.props.value === ''
|| (this.props.multi && !this.props.value.length)
|| this.props.disabled
|| this.props.isLoading
) return;

return (
<span className="Select-clear-zone" title={this.props.multi ? this.props.clearAllText : this.props.clearValueText}
aria-label={this.props.multi ? this.props.clearAllText : this.props.clearValueText}
Expand Down Expand Up @@ -1033,7 +1042,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 ? 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
13 changes: 13 additions & 0 deletions test/Select-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3625,6 +3625,19 @@ describe('Select', () => {
});
});

describe('with option value==0', () => {
beforeEach(() => {
instance = createControl({
options: [{label: 'Zero', value: 0}],
value: 0,
});
});

it('should show the clear icon', () => {
expect(ReactDOM.findDOMNode(instance), 'to contain elements matching', '.Select-clear');
});
});

describe('custom menuRenderer option', () => {
it('should render the custom menu', () => {
const instance = createControl({
Expand Down