Skip to content

Commit b1667c4

Browse files
Add option to clear typeahead on select option
1 parent 0c3a53c commit b1667c4

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ Type: `Boolean`
112112

113113
Set to `true` to use a `<textarea>` element rather than an `<input>` element
114114

115+
#### props.clearOnSelect
116+
117+
Type: `Boolean`
118+
119+
Set to `true` to use clear the `<input>` or `<textarea>` element after selecting an option
120+
115121
#### props.inputProps
116122

117123
Type: `Object`

src/typeahead/index.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var Typeahead = createReactClass({
2626
placeholder: PropTypes.string,
2727
disabled: PropTypes.bool,
2828
textarea: PropTypes.bool,
29+
clearOnSelect: PropTypes.bool,
2930
inputProps: PropTypes.object,
3031
onOptionSelected: PropTypes.func,
3132
onChange: PropTypes.func,
@@ -69,6 +70,7 @@ var Typeahead = createReactClass({
6970
placeholder: "",
7071
disabled: false,
7172
textarea: false,
73+
clearOnSelect: false,
7274
inputProps: {},
7375
onOptionSelected: function(option) {},
7476
onChange: function(event) {},
@@ -199,11 +201,14 @@ var Typeahead = createReactClass({
199201
var formInputOption = Accessor.generateOptionToStringFor(this.props.formInputOption || displayOption);
200202
var formInputOptionString = formInputOption(option);
201203

202-
nEntry.value = optionString;
203-
this.setState({searchResults: this.getOptionsForValue(optionString, this.props.options),
204-
selection: formInputOptionString,
205-
entryValue: optionString,
206-
showResults: false});
204+
var valueAfterSelect = this.props.clearOnSelect ? '' : optionString;
205+
var selectionAfterSelect = this.props.clearOnSelect ? '' : formInputOptionString;
206+
207+
nEntry.value = valueAfterSelect;
208+
this.setState({searchResults: this.getOptionsForValue(valueAfterSelect, this.props.options),
209+
selection: selectionAfterSelect,
210+
entryValue: valueAfterSelect,
211+
showResults: !!this.props.clearOnSelect});
207212
return this.props.onOptionSelected(option, event);
208213
},
209214

@@ -310,7 +315,7 @@ var Typeahead = createReactClass({
310315

311316
componentWillReceiveProps: function(nextProps) {
312317
var searchResults = this.getOptionsForValue(this.state.entryValue, nextProps.options);
313-
var showResults = Boolean(searchResults.length) && this.state.isFocused;
318+
var showResults = !!this.props.clearOnSelect || (Boolean(searchResults.length) && this.state.isFocused);
314319
this.setState({
315320
searchResults: searchResults,
316321
showResults: showResults

0 commit comments

Comments
 (0)