Skip to content

Commit 6b993ad

Browse files
committed
Added conditions to focus on selected option other than clicking on the input
1 parent 8c9e09c commit 6b993ad

1 file changed

Lines changed: 19 additions & 11 deletions

File tree

src/components/combo_box/combo_box.tsx

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,21 @@ export class EuiComboBox<T> extends Component<
365365
});
366366
};
367367

368+
focusSelectedOption = () => {
369+
// If singleSelection is on and an option has been selected, set that option as active
370+
if (
371+
Boolean(this.props.singleSelection) &&
372+
this.props.selectedOptions.length === 1
373+
) {
374+
const selectedOptionIndex = this.state.matchingOptions.findIndex(
375+
option => option.label === this.props.selectedOptions[0].label
376+
);
377+
this.setState({
378+
activeOptionIndex: selectedOptionIndex,
379+
});
380+
}
381+
};
382+
368383
incrementActiveOptionIndex = (amount: number) => {
369384
// If there are no options available, do nothing.
370385
if (!this.state.matchingOptions.length) {
@@ -530,6 +545,7 @@ export class EuiComboBox<T> extends Component<
530545
}
531546

532547
this.openList();
548+
this.focusSelectedOption();
533549

534550
this.setState({ hasFocus: true });
535551
};
@@ -595,6 +611,7 @@ export class EuiComboBox<T> extends Component<
595611
this.incrementActiveOptionIndex(-1);
596612
} else {
597613
this.openList();
614+
this.focusSelectedOption();
598615
}
599616
break;
600617

@@ -605,6 +622,7 @@ export class EuiComboBox<T> extends Component<
605622
this.incrementActiveOptionIndex(1);
606623
} else {
607624
this.openList();
625+
this.focusSelectedOption();
608626
}
609627
break;
610628

@@ -726,17 +744,7 @@ export class EuiComboBox<T> extends Component<
726744
}
727745

728746
// If the user does this from a state in which an option has focus, then we need to reset it or clear it.
729-
if (
730-
Boolean(this.props.singleSelection) &&
731-
this.props.selectedOptions.length === 1
732-
) {
733-
const selectedOptionIndex = this.state.matchingOptions.findIndex(
734-
option => option.label === this.props.selectedOptions[0].label
735-
);
736-
this.setState({
737-
activeOptionIndex: selectedOptionIndex,
738-
});
739-
} else {
747+
if (!Boolean(this.props.singleSelection)) {
740748
this.clearActiveOption();
741749
}
742750
};

0 commit comments

Comments
 (0)