question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Autocomplete] With `autoComplete` + `autoHighlight` the highlighted option should appears inline

See original GitHub issue

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.

Steps to Reproduce 🕹

Steps:

  1. https://codesandbox.io/s/material-demo-forked-pifom
  2. type a
  3. the input should be become Andorra

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:3
  • Comments:13 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
morijenabcommented, Sep 22, 2020

can I work on this?

1reaction
oliviertassinaricommented, Sep 20, 2020

This one should work better:

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,

after

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Material UI Autocomplete: highlighted option should become ...
Try this (I made some changes to your code like adding reason and isOptionEqualToValue ): export default function ComboBox() { const [input, ...
Read more >
Autocomplete API - Material UI - MUI
Name Type Default options * array renderInput * func autoComplete bool false
Read more >
Vue AutoComplete Component - PrimeFaces
Highlights automatically the first item of the dropdown to be selected. *Deprecated since v3.16.0. multiple, boolean, false, Specifies if multiple values can be ......
Read more >
Autocomplete INPUT Widget - How do I do X? - Appsmith
The inline completion string is visually highlighted and has a selected state. autoHighlight | bool | false | If true, the first option...
Read more >
Angular PrimeNG Form AutoComplete Component
Grouped: In this type of variation, the option will appear in the ... of selected items on multiple modes. autocomplete: This property is ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found