From 1d9b8a009ed837dc9d8030e00f6faf6253863231 Mon Sep 17 00:00:00 2001 From: Sebastian Uecker Date: Thu, 3 Nov 2016 11:09:12 +0100 Subject: [PATCH 1/3] Delete key removes an item when there is no input --- src/Select.js | 8 ++++++++ test/Select-test.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/Select.js b/src/Select.js index 9c22206e7c..af42e94dc7 100644 --- a/src/Select.js +++ b/src/Select.js @@ -57,6 +57,7 @@ const Select = React.createClass({ clearAllText: stringOrNode, // title for the "clear" control when multi: true clearValueText: stringOrNode, // title for the "clear" control clearable: React.PropTypes.bool, // should it be possible to reset value + deleteRemoves: React.PropTypes.bool, // whether backspace removes an item if there is no text input delimiter: React.PropTypes.string, // delimiter to use to join multiple values for the hidden field value disabled: React.PropTypes.bool, // whether the Select is disabled or not escapeClearsValue: React.PropTypes.bool, // whether escape clears the value when the menu is closed @@ -125,6 +126,7 @@ const Select = React.createClass({ clearable: true, clearAllText: 'Clear all', clearValueText: 'Clear value', + deleteRemoves: true, delimiter: ',', disabled: false, escapeClearsValue: true, @@ -520,6 +522,12 @@ const Select = React.createClass({ } this.focusStartOption(); break; + case 46: // backspace + if (!this.state.inputValue && this.props.deleteRemoves) { + event.preventDefault(); + this.popValue(); + } + return; default: return; } event.preventDefault(); diff --git a/test/Select-test.js b/test/Select-test.js index 21866b2c17..fe3c6d2706 100644 --- a/test/Select-test.js +++ b/test/Select-test.js @@ -84,6 +84,10 @@ describe('Select', () => { TestUtils.Simulate.keyDown(searchInputNode, { keyCode: 8, key: 'Backspace' }); }; + var pressDelete = () => { + TestUtils.Simulate.keyDown(searchInputNode, { keyCode: 46, key: 'Backspace' }); + }; + var pressUp = () => { TestUtils.Simulate.keyDown(getSelectControl(instance), { keyCode: 38, key: 'ArrowUp' }); }; @@ -1796,6 +1800,32 @@ describe('Select', () => { ); }); + it('removes the last selected option with delete', () => { + + setValueProp(['four','three']); + onChange.reset(); // Ignore previous onChange calls + pressDelete(); + expect(onChange, 'was called with', [{ label: 'Four', value: 'four' }]); + }); + + it('does not remove the last selected option with delete when deleteRemoves=false', () => { + + // Disable delete + wrapper.setPropsForChild({ + deleteRemoves: false, + value: ['four', 'three'] + }); + onChange.reset(); // Ignore previous onChange calls + + pressDelete(); + expect(onChange, 'was not called'); + expect(instance, 'to contain', + +
Four
+
Three
+
); + }); + it('removes an item when clicking on the X', () => { setValueProp(['four', 'three', 'two']); From a1e07518a6f5c91dfb0d8c43012538ec579ddbfc Mon Sep 17 00:00:00 2001 From: Sebastian Uecker Date: Thu, 3 Nov 2016 11:11:17 +0100 Subject: [PATCH 2/3] Fixed comment indent --- src/Select.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Select.js b/src/Select.js index af42e94dc7..578766eb42 100644 --- a/src/Select.js +++ b/src/Select.js @@ -57,7 +57,7 @@ const Select = React.createClass({ clearAllText: stringOrNode, // title for the "clear" control when multi: true clearValueText: stringOrNode, // title for the "clear" control clearable: React.PropTypes.bool, // should it be possible to reset value - deleteRemoves: React.PropTypes.bool, // whether backspace removes an item if there is no text input + deleteRemoves: React.PropTypes.bool, // whether backspace removes an item if there is no text input delimiter: React.PropTypes.string, // delimiter to use to join multiple values for the hidden field value disabled: React.PropTypes.bool, // whether the Select is disabled or not escapeClearsValue: React.PropTypes.bool, // whether escape clears the value when the menu is closed From 0465ea9d5b306e052965350d2934d468f373522d Mon Sep 17 00:00:00 2001 From: Sebastian Uecker Date: Thu, 3 Nov 2016 11:28:22 +0100 Subject: [PATCH 3/3] Added README entry for deleteRemoves prop --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 99291982bf..164cf74beb 100644 --- a/README.md +++ b/README.md @@ -351,6 +351,7 @@ function onInputKeyDown(event) { clearAllText | string | 'Clear all' | title for the "clear" control when `multi` is true clearValueText | string | 'Clear value' | title for the "clear" control resetValue | any | null | value to use when you clear the control + deleteRemoves | bool | true | whether pressing delete key removes the last item when there is no input value delimiter | string | ',' | delimiter to use to join multiple values disabled | bool | false | whether the Select is disabled or not filterOption | func | undefined | method to filter a single option: `function(option, filterString)`