Skip to content

Commit

Permalink
Add option to clear typeahead on select option
Browse files Browse the repository at this point in the history
  • Loading branch information
BrodrickChilds committed Jan 24, 2018
1 parent 0c3a53c commit 292c12f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ Type: `Boolean`

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

#### props.clearOnSelect

Type: `Boolean`

Set to `true` to use clear the `<input>` or `<textarea>` element after selecting an option

#### props.inputProps

Type: `Object`
Expand Down
15 changes: 10 additions & 5 deletions src/typeahead/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var Typeahead = createReactClass({
placeholder: PropTypes.string,
disabled: PropTypes.bool,
textarea: PropTypes.bool,
clearOnSelect: PropTypes.bool,
inputProps: PropTypes.object,
onOptionSelected: PropTypes.func,
onChange: PropTypes.func,
Expand Down Expand Up @@ -69,6 +70,7 @@ var Typeahead = createReactClass({
placeholder: "",
disabled: false,
textarea: false,
clearOnSelect: false,
inputProps: {},
onOptionSelected: function(option) {},
onChange: function(event) {},
Expand Down Expand Up @@ -199,11 +201,14 @@ var Typeahead = createReactClass({
var formInputOption = Accessor.generateOptionToStringFor(this.props.formInputOption || displayOption);
var formInputOptionString = formInputOption(option);

nEntry.value = optionString;
this.setState({searchResults: this.getOptionsForValue(optionString, this.props.options),
selection: formInputOptionString,
entryValue: optionString,
showResults: false});
var valueAfterSelect = this.props.clearOnSelect ? '' : optionString;
var selectionAfterSelect = this.props.clearOnSelect ? '' : formInputOptionString

nEntry.value = valueAfterSelect;
this.setState({searchResults: this.getOptionsForValue(valueAfterSelect, this.props.options),
selection: selectionAfterSelect,
entryValue: valueAfterSelect,
showResults: !!this.props.clearOnSelect});
return this.props.onOptionSelected(option, event);
},

Expand Down

0 comments on commit 292c12f

Please sign in to comment.