Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sorted supply search result by lenght to appear best results first #300

Merged
merged 1 commit into from
May 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ import { IUseSuppliesData } from '@/hooks/useSupplies/types';
import { Search, PlusCircle, X } from 'lucide-react';
import { useState } from 'react';
import { Fragment } from 'react/jsx-runtime';
import {ISupplySearchProps} from './types';
import { ISupplySearchProps } from './types';

export const SupplySearch = ({
supplyItems,
limit = 10,
onSearch,
onSelectItem,
onAddNewItem
onAddNewItem,
}: ISupplySearchProps) => {
const [searchValue, setSearchValue] = useState<string>('');
const [selectedItem, setSelectedItem] = useState<IUseSuppliesData | null>(null);
const [selectedItem, setSelectedItem] = useState<IUseSuppliesData | null>(
null
);

function onChangeInputHandler(event: React.ChangeEvent<HTMLInputElement>) {
setSearchValue(event.target.value);
Expand Down Expand Up @@ -51,21 +53,27 @@ export const SupplySearch = ({
value={searchValue}
onChange={onChangeInputHandler}
/>
<X className="h-4 w-4 ml-2 hover:cursor-pointer" onClick={onClearClickHandler} />
<X
className="h-4 w-4 ml-2 hover:cursor-pointer"
onClick={onClearClickHandler}
/>
</div>

{!!searchValue && !selectedItem ? (
<div className="flex-col items-center rounded-md border border-input p-3 bg-slate-50 mt-1">
{supplyItems.slice(0, limit).map((item) => (
<div
key={item.id}
className="h-10 flex items-center rounded-md p-2 hover:bg-slate-100 hover:cursor-pointer"
onClick={() => onSelectItemHandler(item)}
>
<span className="text-sm">{item.name}</span>
</div>
))}
<div
{supplyItems
.sort((a, b) => a.name.length - b.name.length)
.slice(0, limit)
.map((item) => (
<div
key={item.id}
className="h-10 flex items-center rounded-md p-2 hover:bg-slate-100 hover:cursor-pointer"
onClick={() => onSelectItemHandler(item)}
>
<span className="text-sm">{item.name}</span>
</div>
))}
<div
className="h-10 flex items-center rounded-md p-2 hover:bg-slate-100 hover:cursor-pointer"
onClick={onAddNewItemHandler}
>
Expand Down