Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

101
Vistas
Search input loses focus when it doesn't find any title

Whenever I try to search, I can type only the letters which are similar to the one of the titles. If any letter is different I lose focus from input. It used to work normally before, but Idk what happened now. Any suggestions?

How it first looks like:

After starting to type on search input:

When I type a letter which is not in the title:

Code:

if (props.items.length === 0) {
    return (
      <div className="files-list center">
        <Card>
          <h2>No files found.</h2>
        </Card>
      </div>
    );
  } else if (
    props.items.filter(
      (file) =>
        file.title.toLowerCase().includes(searchInput) ||
        file.title.toUpperCase().includes(searchInput)
    ).length === 0
  ) {
    return (
      <div className="filesList">
        <div className="search">
          <input
            type="text"
            placeholder="Search Files"
            onChange={(event) => setSearchInput(event.target.value)}
          />
        </div>
        <Card>
          <h2>No files found.</h2>
        </Card>
      </div>
    );
  }

  return (
    <React.Fragment>
      <div className="filesList">
        <div className="actionsBtn">
          <NavLink to="/add-file" className="addFileBtn" title="Add File">
            <FontAwesomeIcon icon={faPlus} />
          </NavLink>
          <input
            type="text"
            placeholder="Search Files"
            onChange={(event) => setSearchInput(event.target.value)}
          />
        </div>
        <ul className="files-list">
          {props.items
            .filter(
              (file) =>
                file.title.toLowerCase().includes(searchInput) ||
                file.title.toUpperCase().includes(searchInput)
            )
            .map((file) => (
              <FilesPostItem
                key={file.id}
                id={file.id}
                title={file.title}
                file={file.file}
                creator={file.creator.name}
                description={file.description}
                creatorId={file.creator}
                onDelete={props.onDeleteFile}
              />
            ))}
        </ul>
      </div>
    </React.Fragment>
  );
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

What I think happens is, your “default” return form the beginning gets reset by the return in the “if else”. So the Input is a new DOM Element and loses focus.

You might want to add something like this to your default return and remove your "if else" statement.

<ul className="files-list">
  {props.items
    .filter(
      (file) =>
        file.title.toLowerCase().includes(searchInput) ||
        file.title.toUpperCase().includes(searchInput)
    )
    .map((file) => (
      <FilesPostItem
        key={file.id}
        id={file.id}
        title={file.title}
        file={file.file}
        creator={file.creator.name}
        description={file.description}
        creatorId={file.creator}
        onDelete={props.onDeleteFile}
      />
    ))}
</ul>
{props.items
    .filter(
      (file) =>
        file.title.toLowerCase().includes(searchInput) ||
        file.title.toUpperCase().includes(searchInput)
    ).length > 0 &&
    <Card>
      <h2>No files found.</h2>
    </Card>
}

What does this do? It adds the "No files found" message to your default return and shows it only, if items is empty.

But i am pretty sure, there is a better way to do this.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda