From 825536c48887239206244a34a39f6ea07f8f3be2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Geraldo=20D=2E=20F?= Date: Fri, 24 May 2024 15:47:51 -0300 Subject: [PATCH] fix: sorted supply search result by lenght to appear best results first (#300) --- .../components/SupplySearch/SupplySearch.tsx | 36 +++++++++++-------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/src/pages/EditShelterSupply/components/SupplySearch/SupplySearch.tsx b/src/pages/EditShelterSupply/components/SupplySearch/SupplySearch.tsx index 3e27cab3..b63aa36e 100644 --- a/src/pages/EditShelterSupply/components/SupplySearch/SupplySearch.tsx +++ b/src/pages/EditShelterSupply/components/SupplySearch/SupplySearch.tsx @@ -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(''); - const [selectedItem, setSelectedItem] = useState(null); + const [selectedItem, setSelectedItem] = useState( + null + ); function onChangeInputHandler(event: React.ChangeEvent) { setSearchValue(event.target.value); @@ -51,21 +53,27 @@ export const SupplySearch = ({ value={searchValue} onChange={onChangeInputHandler} /> - + {!!searchValue && !selectedItem ? (
- {supplyItems.slice(0, limit).map((item) => ( -
onSelectItemHandler(item)} - > - {item.name} -
- ))} -
a.name.length - b.name.length) + .slice(0, limit) + .map((item) => ( +
onSelectItemHandler(item)} + > + {item.name} +
+ ))} +