Skip to content

Commit 5d305e9

Browse files
author
Boris
committed
[+] Add props resetOnSelect to reset the input on selecting an item set to false by default
1 parent 58a334d commit 5d305e9

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ Change valueExtractor and pass the data to Autocomplete without fetchDataUrl
133133
autoCorrect | Disable auto-correct | Boolean | true
134134
highlightText | Highlight search results | Boolean | true
135135
rightContent | Render additional text to the right of the item | Boolean | false
136+
resetOnSelect | Reset the input after choosing an item | Boolean | false
136137
minimumCharactersCount | Perform API request after certain number of characters entered | Number | 2
137138
waitInterval | Timeout between user finished typing and new data fetch | Number | 400
138139
placeholder | Autocomplete input placeholder text | String | Add Item

components/Autocomplete/index.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,15 @@ class Autocomplete extends Component {
100100
}
101101

102102
setItem(value) {
103-
const {index, handleSelectItem, valueExtractor} = this.props;
104-
const capitalizedValue = capitalizeFirstLetter(valueExtractor(value));
105-
this.setState({inputValue: capitalizedValue});
106-
103+
const { index, handleSelectItem, valueExtractor, resetOnSelect } = this.props;
107104
handleSelectItem(value, index);
105+
106+
if (resetOnSelect) {
107+
this.setState({ inputValue: '' });
108+
} else {
109+
const capitalizedValue = capitalizeFirstLetter(valueExtractor(value));
110+
this.setState({inputValue: capitalizedValue});
111+
}
108112
}
109113

110114
componentDidMount() {
@@ -195,6 +199,7 @@ Autocomplete.defaultProps = {
195199
minimumCharactersCount: 2,
196200
highlightText: true,
197201
waitInterval: WAIT_INTERVAL,
202+
resetOnSelect: false,
198203
};
199204

200205
Autocomplete.propTypes = {
@@ -208,6 +213,7 @@ Autocomplete.propTypes = {
208213
highlightText: bool,
209214
rightContent: bool,
210215
autoCorrect: bool,
216+
resetOnSelect: bool,
211217

212218
valueExtractor: func,
213219
renderIcon: func,

0 commit comments

Comments
 (0)