Skip to content

Commit b587e68

Browse files
committed
fix: use keyboard-selected suggestion in Autocomplete onSubmit
Resolve the active suggestion before calling onSubmit and onChange in onKeyDown, so Enter submits the highlighted item instead of stale typed text. Update RestConsole onSubmit to sync the provided endpoint to state before making the request.
1 parent f5f4921 commit b587e68

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/components/Autocomplete/Autocomplete.react.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,15 +250,20 @@ export default class Autocomplete extends Component {
250250
const { userInput } = this.state;
251251

252252
if (e.keyCode === 13 || e.key === 'Enter') {
253-
if (userInput && userInput.length > 0 && this.props.onSubmit) {
254-
this.props.onSubmit(userInput);
253+
const resolvedInput = filteredSuggestions[activeSuggestion] || userInput;
254+
255+
if (resolvedInput && resolvedInput.length > 0) {
256+
this.props.onChange && this.props.onChange(resolvedInput);
257+
if (this.props.onSubmit) {
258+
this.props.onSubmit(resolvedInput);
259+
}
255260
}
256261

257262
this.setState({
258263
active: true,
259264
activeSuggestion: 0,
260265
showSuggestions: false,
261-
userInput: filteredSuggestions[activeSuggestion] || userInput,
266+
userInput: resolvedInput,
262267
});
263268
} else if (e.keyCode === 9) {
264269
// Tab

src/dashboard/Data/ApiConsole/RestConsole.react.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,9 @@ export default class RestConsole extends Component {
290290
}}
291291
placeholder={'classes/_User'}
292292
onChange={endpoint => this.setState({ endpoint })}
293-
onSubmit={() => {
293+
onSubmit={(endpoint) => {
294294
if (!hasError) {
295-
this.makeRequest();
295+
this.setState({ endpoint }, () => this.makeRequest());
296296
}
297297
}}
298298
buildSuggestions={input => this.buildEndpointSuggestions(input)}

0 commit comments

Comments
 (0)