You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[autocomplete] With autoComplete + autoHighlight the highlighted option should appears inline #22648
Hi,there. I'm not sure this is a bug or feature. When <Autocomplete autoComplete autoHighlight />, the first option not appears inline after the input cursor immediately. It work as expected when selection move up/down.
The issue is present in the latest release.
I have searched the issues of this repository and believe that this is not a duplicate.
Current Behavior 😯
If this is a bug, the default highlighted option NOT inline after the input cursor immediately when autoHighlight is on.
Expected Behavior 🤔
If this is a bug, the default highlighted option should inline after the input cursor immediately when autoHighlight is on.
changed the title [-]<Autocomplete /> autoComplete + autoHighlight the default highlighted option not appears inline after the input cursor.[/-][+][Autocomplete] With `autoComplete` + `autoHighlight` the highlighted option should appears inline[/+]on Sep 20, 2020
diff --git a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
index a9d8d48de3..a9dc88789d 100644
--- a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js+++ b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js@@ -15,6 +15,20 @@ function stripDiacritics(string) {
: string;
}
+function usePrevious(value) {+ // The ref object is a generic container whose current property is mutable ...+ // ... and can hold any value, similar to an instance property on a class+ const ref = React.useRef();++ // Store current value in ref+ React.useEffect(() => {+ ref.current = value;+ }, [value]); // Only re-run if value changes++ // Return previous value (happens before update in useEffect above)+ return ref.current;+}+
export function createFilterOptions(config = {}) {
const {
ignoreAccents = true,
@@ -829,6 +843,29 @@ export default function useAutocomplete(props) {
}
};
+ const prevInputValue = usePrevious(inputValue);++ React.useEffect(() => {+ if (+ autoComplete &&+ prevInputValue !== undefined &&+ inputValue.length > prevInputValue.length &&+ filteredOptions[highlightedIndexRef.current]+ ) {+ const option = getOptionLabel(filteredOptions[highlightedIndexRef.current]);++ // The portion of the selected suggestion that has not been typed by the user,+ // a completion string, appears inline after the input cursor in the textbox.+ const index = option.toLowerCase().indexOf(inputValue.toLowerCase());+ if (index === 0 && inputValue.length > 0) {+ setTimeout(() => {+ inputRef.current.value = option;+ inputRef.current.setSelectionRange(inputValue.length, option.length);+ });+ }+ }+ }, [prevInputValue, inputValue, filteredOptions, autoComplete, getOptionLabel]);+
const handleOptionMouseOver = (event) => {
setHighlightedIndex({
event,
It reproduces how Chrome URL bar behaves. It was pretty hard to figure out! If somebody could confirm that it's the expected behavior, it would be great, we would also need to add test cases.
@Morteza-Jenabzadeh It's not a simple one but definitely :). We would need to clean up the proposed diff, e.g. Clear timeout, make sure it behave correctly and add end to e'd tests.
it('Change input in a case filter has a result and autocomplete ans autoHighlight',()=>{consthandleHighlightChange=spy();consthandleInputChange=spy();constoptions=['one','two','three'];const{ getByRole }=render(<AutocompleteonHighlightChange={handleHighlightChange}onInputChange={handleInputChange}options={options}autoCompleteautoHighlightopenrenderInput={(params)=><TextField{...params}autoFocus/>}/>,);consttextbox=getByRole('textbox');fireEvent.change(document.activeElement,{target: {value: 'on'}});expect(handleHighlightChange.callCount).to.equal(3);// expect(handleHighlightChange.args[2][0]).to.not.equal(undefined);expect(handleHighlightChange.args[2][1]).to.equal(options[0]);expect(handleHighlightChange.args[2][2]).to.equal('auto');fireEvent.keyDown(textbox,{key: 'Enter'});expect(textbox).toHaveFocus();expect(handleHighlightChange.callCount).to.equal(4);expect(handleInputChange.args[1][0]).to.not.equal(undefined);expect(handleInputChange.args[1][1]).to.equal(options[0]);expect(handleInputChange.callCount).to.equal(2);constinput=getByRole('textbox');expect(input.value).to.equal('one');});
changed the title [-][Autocomplete] With `autoComplete` + `autoHighlight` the highlighted option should appears inline[/-][+][autocomplete] With `autoComplete` + `autoHighlight` the highlighted option should appears inline[/+]on Apr 27, 2026
I'm picking this up. On current master (daaa525c), I reproduced the missing inline completion in a React/JSDOM hook harness: with autoComplete and autoHighlight, typing a or and highlights Andorra but leaves only the typed text in the input. Arrow navigation completes it correctly.
I'll keep the change focused on this interaction and add real-browser/E2E coverage for typing through the completion, deletion/editing, keyboard navigation, and controlled input. I'll preserve the existing freeSolo behavior and account for the one-line approach rejected earlier in this thread. This work will use AI assistance.
This issue has been closed. If you have a similar problem but not exactly the same, please open a new issue.
Now, if you have additional information related to this issue or things that could help future readers, feel free to leave a comment.
@michael-land Thanks again for creating this issue! If you have a moment, we would love to hear your thoughts on how we handled it with this short feedback form.
Activity
[-]<Autocomplete /> autoComplete + autoHighlight the default highlighted option not appears inline after the input cursor.[/-][+][Autocomplete] With `autoComplete` + `autoHighlight` the highlighted option should appears inline[/+]oliviertassinari commentedon Sep 20, 2020
michael-land commentedon Sep 20, 2020
Sure, will do it
oliviertassinari commentedon Sep 20, 2020
@xiaoyu-tamu I'm not sure the behavior is exactly the one you expect, so please let us know.
oliviertassinari commentedon Sep 20, 2020
One of the issues I have seen can be observed on https://codesandbox.io/s/staging-haze-w6wps?file=/src/App.js. Yeah, rereading the issue description, the proposed change is off probably not something we want to change.
oliviertassinari commentedon Sep 20, 2020
This one should work better:
It reproduces how Chrome URL bar behaves. It was pretty hard to figure out! If somebody could confirm that it's the expected behavior, it would be great, we would also need to add test cases.
morijenab commentedon Sep 22, 2020
can I work on this?
oliviertassinari commentedon Sep 22, 2020
@Morteza-Jenabzadeh It's not a simple one but definitely :). We would need to clean up the proposed diff, e.g. Clear timeout, make sure it behave correctly and add end to e'd tests.
morijenab commentedon Sep 22, 2020
I try my best :)
morijenab commentedon Sep 27, 2020
I have come up with this test :
but this line yield error
I cant figure out why event is undefined !
I'll be happy if you give me a hint about this and also have not got any clue about when we should clear time out.
oliviertassinari commentedon Nov 13, 2020
@f1nan You are good to go to experiment with it :).
vividh commentedon Oct 9, 2021
@oliviertassinari I would like to claim this.
rupam71 commentedon Apr 2, 2022
Is this feature available now?
[-][Autocomplete] With `autoComplete` + `autoHighlight` the highlighted option should appears inline[/-][+][autocomplete] With `autoComplete` + `autoHighlight` the highlighted option should appears inline[/+]dvd233 commentedon Oct 5, 2026
I'm picking this up. On current master (
daaa525c), I reproduced the missing inline completion in a React/JSDOM hook harness: withautoCompleteandautoHighlight, typingaorandhighlightsAndorrabut leaves only the typed text in the input. Arrow navigation completes it correctly.I'll keep the change focused on this interaction and add real-browser/E2E coverage for typing through the completion, deletion/editing, keyboard navigation, and controlled input. I'll preserve the existing freeSolo behavior and account for the one-line approach rejected earlier in this thread. This work will use AI assistance.
github-actions commentedon Oct 13, 2026
This issue has been closed. If you have a similar problem but not exactly the same, please open a new issue.
Now, if you have additional information related to this issue or things that could help future readers, feel free to leave a comment.
@michael-land Thanks again for creating this issue! If you have a moment, we would love to hear your thoughts on how we handled it with this short feedback form.