Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

110
Visualizações
Using inputRef with MUI's useAutocomplete

How can I pass "inputRef" to Material UI's useAutocomplete?

I need to assign a custom ref on the input, however the getInputProps() from useAutocomplete already sets an required ref.

I have tried several things, like adding the ref. Then my other code will work, but useAutocomplete will fail to function, because it's dependent on it's own "ref" prop being set on the input.

There's also not much documentation around useAutocomplete or the proviable options, so it's difficult to debug.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

We can get ref of inpuRef by getInputProps().ref.

For example, to use Hot Key ('control+q' focus and select input value),
I used below code. (I hope below helpful to anyone to use useAutocomplete)

import {useHotkeys} from 'react-hotkeys-hook';
...
const {
    getRootProps,
    getInputLabelProps,
    getInputProps,
    getListboxProps,
    getOptionProps,
    groupedOptions,
    value
  } = useAutocomplete({
    id: 'use-autocomplete-demo',
    options: options,
    limit: 10,
    freeSolo: true,
    autoSelect: false,
    clearOnBlur: true,
    selectOnFocus: true,
    filterOptions: options => options,
    getOptionLabel: (option) => {
      if(typeof(option) === 'object'){
        return `${option.artistName} ${option.songName}`
      }
      return option;
    }
  });

const inputRef = getInputProps().ref;   
useHotkeys('ctrl+q',() => {   
    inputRef.current.focus()   
    inputRef.current.select()
}, [inputRef.current]);
...
...
...
  return (
    <div>
      <div {...getRootProps()}>
        <Input 
          onKeyDownCapture={handleKeyDown} 
          onKeyPressCapture={handleKeyPressed} 
          onFocus={handleFocus}
          placeholder="control + q"
          {...getInputProps()} 
        />
      </div>
      ....
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda