diff --git a/src/components/SearchBar.tsx b/src/components/SearchBar.tsx index 8698e4b..fae7a83 100644 --- a/src/components/SearchBar.tsx +++ b/src/components/SearchBar.tsx @@ -7,6 +7,7 @@ import "../index.css"; import { Combobox, Transition } from "@headlessui/react"; import { Fragment, useEffect, useState } from "react"; import { Scrollbar } from "react-scrollbars-custom"; +import { Function } from "lodash"; /* -------------------------------------------------------------------------- */ /* Component props */ @@ -16,15 +17,18 @@ type Hint = { caption: string; }; -type Props = { +type Props = { onChange: (query: string) => void; - onSelected?: (value: Hint) => void; + onSelected: (value: Hint) => void; + IsEmptyItems: () => React.ReactNode; hints: Hint[]; + displayValueResolver?: (element: T) => any; disabled?: boolean; inGroup?: boolean; className?: string; maxScrollSize?: number; elementScrollSize?: number; + placeHolder?: string; }; /* -------------------------------------------------------------------------- */ /* styles */ @@ -67,17 +71,20 @@ const inputInGroup = [ /* Component implementation */ /* -------------------------------------------------------------------------- */ -export default function TextInputBox({ +export default function SearchBar({ onChange, onSelected, hints, + displayValueResolver, disabled, - inGroup, + inGroup = false, className, maxScrollSize = 140, elementScrollSize = 36, + placeHolder = "Search...", + IsEmptyItems, ...props -}: Props) { +}: Props) { const [selected, setSelected] = useState(hints); const [query, setQuery] = useState(""); @@ -90,7 +97,7 @@ export default function TextInputBox({ onSelected && onSelected(value); }; return ( -
+
@@ -101,8 +108,10 @@ export default function TextInputBox({ className, ])} onChange={(event) => setQuery(event.target.value)} - placeholder={"Search..."} - displayValue={(hint: Hint | undefined) => hint?.caption ?? ""} + placeholder={placeHolder} + displayValue={(value: T) => + displayValueResolver ? displayValueResolver(value) : value + } />
@@ -113,36 +122,35 @@ export default function TextInputBox({ leave="transition ease-in duration-100" leaveFrom="opacity-100" leaveTo="opacity-0" + afterLeave={() => setQuery("")} > - {hints.length === 0 && query !== "" ? ( -

Nothing found.

- ) : null} + {hints.length === 0 && query !== "" ? IsEmptyItems() : null} {/* */} - {hints.map((item: any, id: number) => ( - - classNames( - active - ? "text-gray-900 bg-blue-50" - : "font-normal ", - "cursor-default select-none relative py-2 pl-3 pr-9", - selected - ? "text-gray-900 bg-blue-100" - : "font-normal " - ) - } - value={item} - > -
{item.caption}
-
- ))} + {hints.map((item: any, id: number) => ( + + classNames( + active + ? "text-gray-900 bg-blue-50" + : "font-normal ", + "cursor-default select-none relative py-2 pl-3 pr-9", + selected + ? "text-gray-900 bg-blue-100" + : "font-normal " + ) + } + value={item} + > +
{item.caption}
+
+ ))} {/*
*/}