Skip to content

Commit c5d36b7

Browse files
committed
Add autocomplete option in the dependency search
Fixes codesandbox#1508
1 parent 6f0a369 commit c5d36b7

File tree

2 files changed

+11
-8
lines changed
  • packages

2 files changed

+11
-8
lines changed

packages/app/src/app/pages/Sandbox/SearchDependencies/RawAutoComplete/index.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Downshift from 'downshift';
33

44
import { Pagination } from 'react-instantsearch/dom';
55

6-
import { ENTER } from 'common/utils/keycodes';
6+
import { ENTER, TAB, ARROW_RIGHT } from 'common/utils/keycodes';
77

88
import DependencyHit from '../DependencyHit';
99
import { AutoCompleteInput, SuggestionInput } from './elements';
@@ -63,6 +63,12 @@ class RawAutoComplete extends React.Component {
6363
const version = getVersion(this.state.value, hit);
6464
const isValid = getIsValid(this.state.value, hit, version);
6565

66+
const autoCompletedQuery = isExplicitVersion(this.state.value)
67+
? null
68+
: hit && isValid
69+
? hit.name + '@' + hit.version
70+
: null;
71+
6672
return (
6773
<Downshift itemToString={h => (h ? h.name : h)} onSelect={onSelect}>
6874
{({ getInputProps, getItemProps, highlightedIndex }) => (
@@ -125,13 +131,9 @@ class RawAutoComplete extends React.Component {
125131
onKeyUp: e => {
126132
// If enter with no selection
127133
if (e.keyCode === ENTER) {
128-
onManualSelect(
129-
isExplicitVersion(this.state.value)
130-
? e.target.value
131-
: hit && isValid
132-
? hit.name + '@' + hit.version
133-
: e.target.value
134-
);
134+
onManualSelect(autoCompletedQuery || e.target.value);
135+
} else if (autoCompletedQuery && e.keyCode === ARROW_RIGHT) {
136+
this.setState({ value: autoCompletedQuery });
135137
}
136138
},
137139
})}

packages/common/utils/keycodes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export const BACKSPACE = 8;
2+
export const TAB = 9;
23
export const ENTER = 13;
34
export const ESC = 27;
45
export const ARROW_LEFT = 37;

0 commit comments

Comments
 (0)