From 9ee8f4332bc7cee92e88459168e17ae582bf182e Mon Sep 17 00:00:00 2001 From: Rushi Vishavadia Date: Mon, 23 Dec 2024 10:08:57 +0530 Subject: [PATCH] Improve search result display when you are still loading but have data to show --- src/components/Search.jsx | 57 ++++++++++++++++------------- src/stories/Other/Search.stories.js | 5 ++- 2 files changed, 35 insertions(+), 27 deletions(-) diff --git a/src/components/Search.jsx b/src/components/Search.jsx index 96e25d710..2620e1055 100644 --- a/src/components/Search.jsx +++ b/src/components/Search.jsx @@ -3,8 +3,8 @@ import { debounce } from "lodash-es"; import PropTypes from "prop-types"; import React, { useEffect, useId, useRef, useState } from "react"; import { useHotkeys } from "react-hotkeys-hook"; -import cn from "../helpers/classnames"; import { isOSX } from "../helpers/browser"; +import cn from "../helpers/classnames"; import { useIsClient } from "../hooks/useIsClient"; import { SearchIcon } from "../icons"; import { Key } from "./Key"; @@ -135,6 +135,7 @@ export const Search = ({ useHotkeys("esc", () => inputReference.current.blur(), { enableOnTags: ["INPUT"] }); const isVisible = open || !shouldDestroyOnClose; + const hasResults = itemList.some((item) => Boolean(item.id)); return (
@@ -182,33 +183,33 @@ export const Search = ({ > {isVisible ? itemList.map((item, index) => ( -
  • - {item === submitValueItem ? ( - isLoading ? ( -
    - -
    - ) : inputValue.length < minChars ? ( -
    - {`Enter at least ${minChars} characters to begin search`} -
    - ) : null - ) : ( - children?.(item, highlightedIndex === index) - )} -
  • - )) +
  • + {item === submitValueItem ? ( + isLoading ? ( +
    + +
    + ) : inputValue.length < minChars ? ( +
    + {`Enter at least ${minChars} characters to begin search`} +
    + ) : null + ) : ( + children?.(item, highlightedIndex === index) + )} +
  • + )) : null} {isVisible && noResultFound ?
  • No results found
  • : null} - {isVisible && itemList.length < 5 ? ( -
  • + {isVisible && hasResults && ( +
  • navigate results @@ -221,8 +222,14 @@ export const Search = ({ close search + + {isLoading && ( + + Fetching {hasResults ? "more data" : "data"} + + )}
  • - ) : null} + )}
    ); diff --git a/src/stories/Other/Search.stories.js b/src/stories/Other/Search.stories.js index 6bf59a078..3236382f8 100644 --- a/src/stories/Other/Search.stories.js +++ b/src/stories/Other/Search.stories.js @@ -14,8 +14,8 @@ const SearchStories = { }; const fetchUsers = async (search) => { - const results = await fetch("https://xola.com/api/sellers", { - headers: { "X-API-KEY": "" }, // Some random dude's API key. + const results = await fetch("https://xola.com/api/sellers?limit=100", { + headers: { "X-API-KEY": "" }, // Put your API key here for testing }); const response = await results.json(); @@ -53,6 +53,7 @@ export const Default = () => { > {(item, active) => (