Skip to content

Commit

Permalink
fix not clearing when given invalid values, from #1756
Browse files Browse the repository at this point in the history
  • Loading branch information
gwyneplaine committed Oct 26, 2017
1 parent 6ad485c commit 8ebda3a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,11 @@ class Select extends React.Component {
}

renderClear () {
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;
const valueArray = this.getValueArray(this.props.value);
if (!this.props.clearable
|| !valueArray.length
|| this.props.disabled
|| this.props.isLoading) return;
const clear = this.props.clearRenderer();

return (
Expand Down
23 changes: 23 additions & 0 deletions test/Select-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1938,6 +1938,17 @@ describe('Select', () => {
TestUtils.Simulate.mouseDown(ReactDOM.findDOMNode(instance).querySelector('.Select-clear'), { button: 0 });
expect(onChange, 'was called with', []);
});
describe('if supplied with an invalid starting value', () => {
it('should not render the clear componnet', () => {
wrapper = createControlWithWrapper({
options: options,
value: 'nonsense, someothernonsense',
multi: true,
clearable: true,
});
expect(ReactDOM.findDOMNode(instance), 'to contain no elements matching', '.Select-clear');
});
});
});

describe('with multi=true and searchable=false', () => {
Expand Down Expand Up @@ -2069,6 +2080,16 @@ describe('Select', () => {
'to have items satisfying', 'to have text', 'Three');

});
describe('if supplied with an invalid starting value', () => {
it('should not render the clear componnet', () => {
wrapper = createControlWithWrapper({
options: defaultOptions,
value: 'nonsense',
clearable: true,
});
expect(ReactDOM.findDOMNode(instance), 'to contain no elements matching', '.Select-clear');
});
});

describe('on pressing escape', () => {

Expand Down Expand Up @@ -4070,4 +4091,6 @@ describe('Select', () => {
});
});
});


});

0 comments on commit 8ebda3a

Please sign in to comment.