Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

152
Views
Auto scroll to a particular option in the Autocomplete

Suppose I have a list of items in the dropdown or Autocomplete in Material-UI, how can one auto-scroll to the desired item in the list when I just tap on the drop down, for example in the list of top100Films, I already have the items, meaning when I open the dropdown, it first shows:

{ label: 'The Shawshank Redemption', year: 1994 }

What I want to achieve is, when I open the dropdown the list of items should start at an Item I want e.g:

{ label: 'Inception', year: 2010 }

Should appear as the first, but the list of the dropdown should auto-scroll to that item.

Below is the entire code:

import * as React from 'react';
import TextField from '@mui/material/TextField';
import Autocomplete from '@mui/material/Autocomplete';

export default function ComboBox() {
  return (
    <Autocomplete
      disablePortal
      id="combo-box-demo"
      options={top100Films}
      sx={{ width: 300 }}
      renderInput={(params) => <TextField {...params} label="Movie" />}
    />
  );
}

// Top 100 films as rated by IMDb users. http://www.imdb.com/chart/top
const top100Films = [
  { label: 'The Shawshank Redemption', year: 1994 },
  ...
];

How best can this be achieved?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If you want to scroll to the option, you need to identify the equivalent element on the DOM tree, then call Element.scrollIntoView().

You can do that by attaching a data attribute to each option element using renderOption, then every time the user opens, use document.querySelector() to find the option based on the attribute and scroll to it:

<Autocomplete
  onOpen={() => {
    setTimeout(() => {
      const optionEl = document.querySelector(
        `[data-name="${movieToScrollTo}"]`
      );
      optionEl?.scrollIntoView();
    }, 1);
  }}
  renderOption={(props, option) => {
    return (
      <li {...props} data-name={option.label}>
        {option.label}
      </li>
    );
  }}
/>

Live Demo

Codesandbox Demo

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!