Skip to content

Commit

Permalink
Set input value on selection
Browse files Browse the repository at this point in the history
Co-authored-by: master-elodin <[email protected]>
  • Loading branch information
PatrickDesign and master-elodin committed Dec 16, 2020
1 parent 8f0fbf8 commit 8284b1e
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ export default class FuzzySearch extends Component {
isOpen: !this.props.showDropdownAtStart,
results: [],
selectedIndex: 0,
selectedValue: {},
value: '',
};
this.handleChange = this.handleChange.bind(this);
Expand Down Expand Up @@ -201,33 +200,33 @@ export default class FuzzySearch extends Component {

// Handle ENTER
} else if (e.keyCode === 13) {
if (results[selectedIndex]) {
this.props.onSelect(results[this.state.selectedIndex]);
this.setState({
selectedValue: results[this.state.selectedIndex],
});
}
this.setState({
results: [],
selectedIndex: 0,
value: results[this.state.selectedIndex].item ? results[this.state.selectedIndex].item.value : '',
});
this.selectItem();
}
}

handleMouseClick(clickedIndex) {
selectItem(index) {
const { results } = this.state;

if (results[clickedIndex]) {
this.props.onSelect(results[clickedIndex]);
const selectedIndex = index || this.state.selectedIndex;
const result = results[selectedIndex];
if (result) {
// send result to onSelectMethod
this.props.onSelect(result);
// and set it as input value
this.setState({
value: result[this.props.keyForDisplayName],
});
}
// hide dropdown
this.setState({
results: [],
selectedIndex: 0,
value: results[this.state.selectedIndex].item ? results[this.state.selectedIndex].item.value : '',
});
}

handleMouseClick(clickedIndex) {
this.selectItem(clickedIndex);
}

render() {
const {
autoFocus,
Expand Down

0 comments on commit 8284b1e

Please sign in to comment.