Live Search in React js - Select with Mouse or Keyboard

By: Niraj Dhungana

Aug 3 2022

#react#tailwind css


Displaying the live search results to the user while they are typing in the search bar is a great user experience. But creating the live search field with all the features. Like, select with a mouse, move selections on key up down and hide the search bar on blur.

Final Live Search Result

This is a little bit of a challenging task. So, let's try to go through all the challenges and create this awesome live search field. Where users can select the result with a mouse or by using the keyboard also the results will be hidden if we click outside.

The things we will cover

React & Tailwind Project Setup

To create this demo we are going to use obviously the React JS and Tailwind CSS for styling. If you want to you can use any other CSS framework or library. This logic will work for all of them.

If you don't know Tailwind CSS is a framework with a bunch of utility class names to style our HTML.

If you already have a project, you can directly add this logic to your existing project as well. But if you are starting from the ground like me run these commands to initialize a new project.

1npx create-react-app live-search --template typescript
2# or
3npx create-react-app live-search // if you are using JavaScript

Once you run one of the commands, the project will be initialized. Now you can install Tailwind CSS. First, change the directory with cd live-search and run these commands.

1npm install -D tailwindcss postcss autoprefixer
2npx tailwindcss init -p

Then you have to go to your tailwind.config.js file and add these things inside your content.

1// tailwind.config.js
2module.exports = {
3  content: ["./src/**/*.{js,jsx,ts,tsx}",],

Then go to your index.css and replace all of the code with these three lines of code.

1// index.css
2// remove old CSS and add them
3@tailwind base;
4@tailwind components;
5@tailwind utilities;

Creating a Search Bar

A search bar is just an input element. But we will add a little bit of style to it so that it looks a little bit nice. For that, let's create a file called SearchBar.tsx (or jsx) inside the src folder and add these codes.

1// inside SearchBar.tsx
2import React, { FC } from "react";
4interface Props<T> {
5  value?: string;
6  notFound?: boolean;
7  onChange?: React.ChangeEventHandler;
8  results?: T[];
9  renderItem: (item: T) => JSX.Element;
10  onSelect?: (item: T) => void;
13const SearchBar = <T extends object>({ }: Props<T>): JSX.Element => {
14  return (
15    {/* Search bar */}
16    <input
17      type="text"
18      className="w-full p-2 text-lg rounded border-2 border-gray-500 outline-none"
19      placeholder="Search here..."
20    />
21  );

I don't think I have to explain these class names. They are explaining themselves. But these interface Props might look a little scary. This is just a Typescript thing which I will explain when we need them. So, let's render this component inside our App.tsx

1// inside App.tsx
2const App = () => {
3  return (
4    <div className="max-w-3xl mx-auto mt-10">
5      <SearchBar />
6    </div>
7  );
10export default App;

Here I am using the fixed width for the div so that I can use this mx-auto to bring this div to the center of the webpage. If you try to run this code it will throw an error. Because renderItem is the required prop. Comment it out if you want to see the change.

Container to Render Search Results

Now we need a container to render our results. So for that, we will wrap our search input inside a div with class relative. And we will render a div below the input element with class absolute. With these classes, we will render the search container right below the input field.

1// SearchBar.tsx
3const SearchBar = <T extends object>({ }: Props<T>): JSX.Element => {
4  return (
5    <div className="relative">
6      <input ...  />
7     {/* Results Wrapper */}
8      <div className="absolute mt-1 w-full p-2 bg-white shadow-lg rounded-bl rounded-br max-h-36 overflow-y-auto"></div>
9    </div>
10  );

With the help of other classes, like max-h-36 overflow-y-auto. It will create a container with a fixed height and show the scroll bar if the results are longer.

Creating a Fully Customizable Component

Now let's use the first props from inside the interface. As you see here we are using a little weird syntax for the interface and SearchBar components.

1interface Props<T> { 
2  results?: T[];
3  renderItem: (item: T) => JSX.Element;
4  onSelect?: (item: T) => void;
7const SearchBar = <T extends object>({}:Props<T>) ...

This is because we want to make this search bar so customizable. So that, it can take, render and give the same data type back to our renderItem and onSelect methods. Now you don't need to worry about types anymore. Typescript will grab the type from results and distribute it to renderItem and onSelect.

Now finally see how we can render our results.

1// inside SearchBar.tsx
2const SearchBar = <T extends object>({ results = [], renderItem }: Props<T>): JSX.Element => {
3  return (
4    <div className="relative">
5      {/* Search bar */}
6      <input ... />
7      {/* Results Wrapper */}
8      <div className="...">
9        {/* Results Lists Goes here */}
10        <div className="space-y-2">
11          {, index) => {
12            return (
13              <div
14                key={index}
15                className="cursor-pointer hover:bg-black hover:bg-opacity-10 p-2"
16              >
17                {renderItem(item)}
18              </div>
19            );
20          })}
21        </div>
22      </div>
23    </div>
24  );

Very first thing, don't forget to give your results prop a default value empty array []. It will prevent unwanted errors. Then we are using to render our results. Where we have renderItem method to render results. We have to pass this method from App.tsx which we will see later.

Then we have a wrapper div for renderItem with classes, hover:bg-black hover:bg-opacity-10. It is very important to give the user feedback on results hover. These classes will add background-color of black with opacity: 0.1 on the result hover.

Ok, now let's try to pass some data to our SearchBar. Because we used that <T> generic type we can pass any type of data but inside an array.

1const profiles = [
2  { id: "1", name: "Allie Grater" },
3  { id: "2", name: "Aida Bugg" },
4  { id: "3", name: "Gabrielle" },
5  { id: "4", name: "Grace" },
6  { id: "5", name: "Hannah" },
7  { id: "6", name: "Heather" },
8  { id: "7", name: "John Doe" },
9  { id: "8", name: "Anne Teak" },
10  { id: "9", name: "Audie Yose" },
11  { id: "10", name: "Addie Minstra" },
12  { id: "11", name: "Anne Ortha" },
14const App = () => {
15  return (
16    <div className="max-w-3xl mx-auto mt-10">
17      <SearchBar results={profiles} renderItem={(item) => <p>{}</p>} />
18    </div>
19  );

As I told you earlier you don't need to worry about the types for renderItem and onSelect. Here is the result if you hover over an item you will see its type. Cool right?

typescript in action

Now if you save your file and run the project with npm start. You will see something like this.

search results

Conditionally Rendering Results

Currently, these results are always visible so, let's solve this issue. For that, we can simply add a state called showResults and display the results according to the value of this state.

1// inside SearchBar.tsx
2const SearchBar = ... {
3  const [showResults, setShowResults] = useState(true);
5  // To toggle the search results as result changes
6  useEffect(() => {
7    if (results.length > 0 && !showResults) setShowResults(true);
9    if (results.length <= 0) setShowResults(false);
10  }, [results]);
12   return ...

The logic is pretty straightforward if there are results and the showResults state is false only then make it true. Otherwise, make it false. These logics will now hide and show our search results as the result changes its length.

But for that, we have to add the condition if showResults is true only then render the results.

1const SearchBar = ... {
2 ...
3  return (
4    <div className="relative">
5      {/* Search bar */}
6      <input ... />
7      {/* Results Wrapper */}
8      {showResults ? <div className="...">
9        {/* Results Lists Goes here */}
10        <div className="space-y-2">...</div>
11      </div> : null}
12    </div>
13  );

Adding a Live Search Logic

Now let's write a little piece of code to add a fake search logic.

1// SearchBar.tsx
2const SearchBar = <T extends object>({onChange, ... }: Props<T>): JSX.Element => {
3return (...
4  <input
5     onChange={onChange}
6   />
9// App.tsx
11const profiles = [...]
12const App = () => {
13  const [results, setResults] = useState<{ id: string; name: string }[]>();
15type changeHandler = React.ChangeEventHandler<HTMLInputElement>;
16  const handleChange: changeHandler = (e) => {
17    const { target } = e;
18    if (!target.value.trim()) return setResults([]);
20    const filteredValue = profiles.filter((profile) =>
22    );
23    setResults(filteredValue);
24  };
26  return (
27    <div className="max-w-3xl mx-auto mt-10">
28      <SearchBar
29        onChange={handleChange}
30        results={results}
31        renderItem={(item) => <p>{}</p>}
32      />
33    </div>
34  );

Even though this is too much code the logic is simple. First, we destructure the onChange inside SearchBar component and assigned it to the input element.

Then inside App.tsx we are handling the change event for SearchBar. So for the handleChange we are just using the array filter method to filter out the matching results.

If we found the results we will add them to the new state called results and if we have an empty input field we will reset those results to an empty array.

And at the end instead of the profiles, we will pass the results state to the results prop. If you do this you should see the search results only if you type out the correct profile names.

live searchresults

Inside the handleChange you can also fetch the data from the server. The only important thing is how will you add or reset your results state. Because if you forgot to reset the results state the results inside your live search container will be visible all the time.

Selecting Results on Key up-down

It's nice we have our search bar where we can search and render the results. But now we need to add a keyboard event to move our selection up and down.

1const SearchBar = ... => {
2const handleKeyDown: React.KeyboardEventHandler<HTMLDivElement> = (e) => {
3    const { key } = e;
5    // move down
6    if (key === "ArrowDown"){ }
8    // move up
9    if (key === "ArrowUp"){ }
11   // hide search results
12    if (key === "Escape"){ }
14   // select the current item
15    if (key === "Enter"){ }
18return (
20      tabIndex={1}
21      onKeyDown={handleKeyDown}
22      className="relative outline-none"
23    >
24   <input ... />
25   ...

We want to now listen to the keyDown event and we are doing that to the wrapping div inside SearchBar component. But we can not directly listen to the keyDown for divs. So we need to use the tabIndex prop. Also to avoid unwanted outlines on div focus (because we are using tabIndex there will be an outline) I am using the outline-none class.

Let's write down the keyboard logic one by one.

Handling the keyboard event is not that straightforward. Because while we are moving up or down we have to handle multiple things. Like displaying the correct UI, and scrolling in the right position if there are multiple results.

So for that first, we need a state called focusedIndex. As you can see the default value for this state is -1. Because later we will use this state to move our focus to the correct place. And we don't want to focus on anything at first. So -1 will work fine.

1// inside SearchBar.tsx
2const SearchBar = ... => {
3  const [focusedIndex, setFocusedIndex] = useState(-1);

Now with the logic below it will update our focused index with the correct timing. Also if you notice I am not using -1 or +1 to update our value. Instead, we are using the modulo (%) operater.

This is a small formula that we can use to update our count only within the range according to the length of the results. So if you have results with the length 6 and if you press the down key then the nextCount will start from 0 and goes to 5. If you press up this will be the opposite starts from 5 and goes to 0.

Which is the exact value that we need to be inside the bound of the result array. If we go beyond the length we will get index bound exception.

1  const handleKeyDown: React.KeyboardEventHandler<HTMLDivElement> = (e) => {
2    let nextCount = 0;
3    if (key === "ArrowDown") nextCount = (focusedIndex + 1) % results.length;
5    if (key === "ArrowUp") nextCount = (focusedIndex + results.length - 1) % results.length;
7    if (key === "Escape") setShowResults(false);
9    if (key === "Enter") {
10      e.preventDefault();
11    }
13    setFocusedIndex(nextCount);
14  };

Also, there is one more thing happening. If we press the Escape key we will hide the search results. But we are not handling the Enter we will do that later.

Styling the Focused Item

Now according to the focusedIndex we need to style the result. For that, we have to take a reference of specific results and change the style.

1// SearchBar.tsx
2const SearchBar = ... => {
3 const resultContainer = useRef<HTMLDivElement>(null);
4 return (
5 ...
6 {, index) => {
7  return ( 
8   <div
9    ref={index === focusedIndex ? resultContainer : null}
10    style={{ backgroundColor: index === focusedIndex ? "rgba(0,0,0,0.1)" : "" }}
11    key={index}
12    className="cursor-pointer hover:bg-black hover:bg-opacity-10 p-2"
13    >
14      {renderItem(item)}
15    </div>
16    );

The main thing for this div is the ref and the style prop. What basically we are doing? If the index is equal to the focusedIndex we are assigning this div to the ref called resultContainer otherwise we are making it null. Why? You will understand it later.

Then we are changing the background color to black with an opacity of 10% if the index is equal to the focusedIndex.

Now with this logic, if you try to move up or down you will see the change but the results will stay in one place only. It will not scroll down or up as you update your index. To fix this use this logic.

1// SearchBar.tsx
2  useEffect(() => {
3    if (!resultContainer.current) return;
5    resultContainer.current.scrollIntoView({
6      block: "center",
7    });
8  }, [focusedIndex]);

If there is resultContainer we will use scrollIntoView method to scroll in to the exact point and we will make the focused item in the center with the option block: center.

Now you should have something like this.

live search animated gif

Selecting the Results

One of the important things for live search is selecting the result so let's see how we can do that.

1// SearchBar.tsx
3  const handleSelection = (selectedIndex: number) => {
4    const selectedItem = results[selectedIndex];
5    if (!selectedItem) resetSearchComplete();
6    onSelect && onSelect(selectedItem);
7    resetSearchComplete();
8  };
10  const resetSearchComplete = useCallback(() => {
11    setFocusedIndex(-1);
12    setShowResults(false);
13  }, []);

We need to now handle two things for selection first select the correct thing and send it back to the App.tsx. Because this is the component where we are using it right now. Then we also need to reset the search results.

For that, inside handleSelection we are accepting selectedIndex and extracting the selected item from inside the results array. If there is nothing just return otherwise call the onSelect with the selected result. Make sure you are destructuring it from the props.

1const SearchBar = ({ onSelect, ...}) => 

And then simply reset the states to their previous values. focusedIndex to -1 and showResults to false.

Now we need to add this handleSelection in two places. Inside Enter key press and on mouse down for the result item.

1// SearchBar.tsx
2// to select on enter
3  const handleKeyDown: React.KeyboardEventHandler<HTMLDivElement> = (e) => {
4   ...
5    if (key === "Enter") {
6      e.preventDefault();
7      handleSelection(focusedIndex);
8    }
10    setFocusedIndex(nextCount);
11  };
14return  (
16{, index) => {
17  return ( 
18   <div
19    onMouseDown={() => handleSelection(index)}
20    ref={index === focusedIndex ? resultContainer : null}
21    style={{ backgroundColor: index === focusedIndex ? "rgba(0,0,0,0.1)" : "" }}
22    ...
23    >
24      {renderItem(item)}
25    </div>
26    );

With this now if you try to select with a mouse or keyboard you will select the correct item. If you want to see that add onSelect inside your SearchBar and log the result to the console.

1// App.tsx
2const App = () => {
3  return (
4    <div className="max-w-3xl mx-auto mt-10">
5      <SearchBar
6        ...
7        onSelect={(item) => console.log(item)}
8        />
9    </div>
10  );

Hiding Results onBlur

Now if you are thinking why onMouseDown why not onClick? Instead of giving you the answer, I will show you that.

Just add onBlur to your top wrapper div inside SearchBar component. And pass resetSearchComplete method to it.

1// SearchBar.tsx
3return (
4    <div
5      tabIndex={1}
6      onKeyDown={handleKeyDown}
7      onBlur={resetSearchComplete}
8      className="relative outline-none"
9    >

Now if you click outside the search results will be gone which is the perfect result we want. Also, you will see the selected results inside your console (if you select any). But now if you change that onMouseDown to onClick results will be gone from the screen but you will not get the selected item inside the console.

Hmmm, why is that? This is because before onClick the onBlur will be fired. So, handleSelection will be never called.

Updating the value inside the search bar

Everything looks fine here. The only thing that we need now is to display the selected profile name inside our search bar. So, create a state called defaultValue inside SearchBar.tsx. Then pass this value to the input element.

1// SearchBar.tsx
2const SearchBar = ... ({ value }) => {
3  const [defaultValue, setDefaultValue] = useState("");
5return ...
6   <input
7     ...
8     value={defaultValue}
9   />

If you remember we are also accepting a prop called value for our SearchBar component. So, if there is value prop available let's assign that to the defaultValue.

1// SearchBar.tsx
2  useEffect(() => {
3    if (value) setDefaultValue(value);
4  }, [value]);

Now if the value changes this useEffect hook will update the defaultValue to the value.

Let's do one more thing inside App.tsx. Add a state called selectedProfile and update the value inside onSelect. Then pass to the value prop.

1// App.tsx
2const App = () => {
3  const [selectedProfile, setSelectedProfile] = useState<{ id: string; name: string }>();   
4   return (
5       ...
6      <SearchBar
7         ...
8        onSelect={(item) => setSelectedProfile(item)}
9        value={selectedProfile?.name}
10      />

Now ladies and gentlemen you should see something like this.

live search updating input value

But there is one big problem. Once you have selected the result you can not make any changes to the input field.

To solve this issue we have to handle the onChange event directly inside our SearchBar component.

1// SearchBar.tsx
3const SearchBar = ({ onChange }) => {
4  ...
5  type changeHandler = React.ChangeEventHandler<HTMLInputElement>;
6  const handleChange: changeHandler = (e) => {
7    setDefaultValue(;
8    onChange && onChange(e);
9  };
11return (
13      {/* Search bar */}
14      <input
15        onChange={handleChange}

Here instead of passing onChange prop directly to the input element. Now we will pass handlChange and inside here we will update the defaultValue and then call the onChange prop. With this now you can update the value with onSelect and this SearchBar will automatically reset the value when the onChange occurs.

And I think this is more than enough for this project. You can see the final look and if you have anything to say at the bottom you will find my social media links.

Final Live Search Result


Niraj Dhungana

I hope you enjoyed reading this post and learned something new. If not then tell me how can I improve. @fsniraj