fixed city search
This commit is contained in:
26
src/hooks/useCityOptions.js
Normal file
26
src/hooks/useCityOptions.js
Normal file
@@ -0,0 +1,26 @@
|
||||
import { useMemo } from "react";
|
||||
|
||||
/**
|
||||
* Общая логика выбора городов для CityFilter (десктоп) и CitySheet (мобильная шторка).
|
||||
*
|
||||
* @param {string[]} cities — доступные города (из /posts/cities)
|
||||
* @param {string[]} value — выбранные города
|
||||
* @param {string} query — введённый текст поиска
|
||||
* @returns {{ options: string[], canAddCustom: boolean }}
|
||||
* options — города, совпадающие с текстом (выбранные помечаются в UI галочкой);
|
||||
* canAddCustom — введён текст, которого нет ни в списке, ни среди выбранных
|
||||
*/
|
||||
export function useCityOptions(cities, value, query) {
|
||||
const normalizedQuery = query.trim().toLowerCase();
|
||||
|
||||
const options = useMemo(() => {
|
||||
const q = normalizedQuery;
|
||||
return cities.filter(c => c && (!q || c.toLowerCase().includes(q)));
|
||||
}, [cities, normalizedQuery]);
|
||||
|
||||
const canAddCustom = normalizedQuery.length > 0 &&
|
||||
!value.some(c => c.toLowerCase() === normalizedQuery) &&
|
||||
!cities.some(c => c.toLowerCase() === normalizedQuery);
|
||||
|
||||
return { options, canAddCustom };
|
||||
}
|
||||
Reference in New Issue
Block a user