Skip to content

Commit

Permalink
MON-560820 Update fuzzy collection on render, sync input value to sel…
Browse files Browse the repository at this point in the history
…ection

- Updates the fuse (fuzzy) collection on render instead of class constuction
- Sets the input value to the current selection
- Exposes prettier config from eslint to prettier bin
  • Loading branch information
robatron committed Feb 22, 2019
1 parent 84b7526 commit 8e8e38f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
1 change: 1 addition & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("./.eslintrc").rules["prettier/prettier"][1];
41 changes: 29 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export default class FuzzySearch extends Component {
results: [],
selectedIndex: 0,
selectedValue: {},
value: '',
};
this.handleChange = this.handleChange.bind(this);
this.handleKeyDown = this.handleKeyDown.bind(this);
Expand Down Expand Up @@ -156,29 +157,39 @@ export default class FuzzySearch extends Component {

getResultsTemplate() {
return this.state.results.map((val, i) => {
const style = this.state.selectedIndex === i
? styles.selectedResultStyle
: styles.resultsStyle;
return <div key={i} style={style}>{val.title}</div>;
const style =
this.state.selectedIndex === i ? styles.selectedResultStyle : styles.resultsStyle;
return (
<div key={i} style={style}>
{val.title}
</div>
);
});
}

handleChange(e) {
this.setState({
results: this.fuse.search(e.target.value).slice(0, this.props.maxResults - 1),
value: e.target.value,
});
}

handleKeyDown(e) {
const { results, selectedIndex } = this.state;

// Handle DOWN arrow
if (e.keyCode === 40 && selectedIndex < results.length - 1) {
this.setState({
selectedIndex: selectedIndex + 1,
});

// Handle UP arrow
} else if (e.keyCode === 38 && selectedIndex > 0) {
this.setState({
selectedIndex: selectedIndex - 1,
});

// Handle ENTER
} else if (e.keyCode === 13) {
if (results[selectedIndex]) {
this.props.onSelect(results[this.state.selectedIndex]);
Expand All @@ -189,6 +200,7 @@ export default class FuzzySearch extends Component {
this.setState({
results: [],
selectedIndex: 0,
value: results[this.state.selectedIndex].item.value,
});
}
}
Expand All @@ -206,27 +218,32 @@ export default class FuzzySearch extends Component {
}

render() {
const { className, width, resultsTemplate, placeholder, autoFocus } = this.props;
const { autoFocus, className, list, placeholder, resultsTemplate, width } = this.props;

// Update the search space list
if (this.fuse.setCollection && list) {
this.fuse.setCollection(list);
}

const mainClass = classNames('react-fuzzy-search', className);

return (
<div className={mainClass} style={{ width }} onKeyDown={this.handleKeyDown}>
<div style={styles.searchBoxWrapper}>
<input
type="text"
style={styles.searchBoxStyle}
autoFocus={autoFocus}
onChange={this.handleChange}
placeholder={placeholder}
autoFocus={autoFocus}
value={this.state.selectedValue && this.state.selectedValue.title}
style={styles.searchBoxStyle}
type="text"
value={this.state.value}
/>
</div>
{this.state.results &&
this.state.results.length > 0 &&
{this.state.results && this.state.results.length > 0 && (
<div style={styles.resultsWrapperStyle}>
{resultsTemplate(this.props, this.state, styles, this.handleMouseClick)}
</div>}
</div>
)}
</div>
);
}
Expand Down

0 comments on commit 8e8e38f

Please sign in to comment.