del add unknown city
This commit is contained in:
@@ -28,7 +28,7 @@ export default function CityFilter({ cities, value, onChange }) {
|
||||
return () => document.removeEventListener("mousedown", onDocMouseDown);
|
||||
}, [open]);
|
||||
|
||||
const { options, canAddCustom } = useCityOptions(cities, value, query);
|
||||
const { options, typedCity } = useCityOptions(cities, value, query);
|
||||
|
||||
const toggle = (city) => {
|
||||
if (value.includes(city)) {
|
||||
@@ -40,20 +40,19 @@ export default function CityFilter({ cities, value, onChange }) {
|
||||
inputRef.current?.focus();
|
||||
};
|
||||
|
||||
const addCustom = () => {
|
||||
const city = query.trim();
|
||||
if (city && !value.includes(city)) {
|
||||
onChange([...value, city]);
|
||||
}
|
||||
// Города нет в списке — объявлений там нет, но фильтр всё равно применяем:
|
||||
// лента покажет «В этом городе нет объявлений»
|
||||
const applyTyped = () => {
|
||||
onChange([...value, typedCity]);
|
||||
setQuery("");
|
||||
inputRef.current?.focus();
|
||||
setOpen(false);
|
||||
};
|
||||
|
||||
const handleKeyDown = (e) => {
|
||||
if (e.key === "Enter") {
|
||||
e.preventDefault();
|
||||
if (canAddCustom) {
|
||||
addCustom();
|
||||
if (typedCity) {
|
||||
applyTyped();
|
||||
} else if (options.length) {
|
||||
toggle(options[0]);
|
||||
}
|
||||
@@ -138,23 +137,10 @@ export default function CityFilter({ cities, value, onChange }) {
|
||||
|
||||
{open && (
|
||||
<div className="f-city-ms-dropdown">
|
||||
{options.length === 0 && !canAddCustom && (
|
||||
{options.length === 0 && (
|
||||
<div className="f-city-ms-empty">{t('feed.filterCitiesEmpty')}</div>
|
||||
)}
|
||||
|
||||
{canAddCustom && (
|
||||
<button type="button" className="f-city-ms-option f-city-ms-add"
|
||||
onClick={() => {
|
||||
addCustom();
|
||||
setOpen(false);
|
||||
}}>
|
||||
<span className="f-city-ms-check">+</span>
|
||||
<span className="f-city-ms-label">
|
||||
{t('feed.filterCitiesAdd', { city: query.trim() })}
|
||||
</span>
|
||||
</button>
|
||||
)}
|
||||
|
||||
{options.map(city => {
|
||||
const selected = value.includes(city);
|
||||
return (
|
||||
|
||||
@@ -42,7 +42,7 @@ export default function CitySheet({ open, onClose, cities, value, onChange }) {
|
||||
return () => document.removeEventListener("keydown", onKeyDown);
|
||||
}, [open, onClose]);
|
||||
|
||||
const { options, canAddCustom } = useCityOptions(cities, draft, query);
|
||||
const { options, typedCity } = useCityOptions(cities, draft, query);
|
||||
|
||||
// Города, добавленные свободным вводом, в общем списке отсутствуют — показываем их сверху
|
||||
const q = query.trim().toLowerCase();
|
||||
@@ -54,14 +54,10 @@ export default function CitySheet({ open, onClose, cities, value, onChange }) {
|
||||
setDraft(d => d.includes(city) ? d.filter(c => c !== city) : [...d, city]);
|
||||
};
|
||||
|
||||
const addCustom = () => {
|
||||
const city = query.trim();
|
||||
if (city && !draft.includes(city)) setDraft(d => [...d, city]);
|
||||
setQuery("");
|
||||
};
|
||||
|
||||
// Введённый город, которого нет в списке, тоже применяется: лента покажет
|
||||
// «В этом городе нет объявлений»
|
||||
const apply = () => {
|
||||
onChange(draft);
|
||||
onChange(typedCity ? [...draft, typedCity] : draft);
|
||||
onClose();
|
||||
};
|
||||
|
||||
@@ -122,27 +118,18 @@ export default function CitySheet({ open, onClose, cities, value, onChange }) {
|
||||
placeholder={t("feed.filterCitiesSearch")}
|
||||
onChange={e => setQuery(e.target.value)}
|
||||
onKeyDown={e => {
|
||||
if (e.key === "Enter" && canAddCustom) {
|
||||
if (e.key === "Enter") {
|
||||
e.preventDefault();
|
||||
addCustom();
|
||||
apply();
|
||||
}
|
||||
}}
|
||||
autoComplete="off"
|
||||
enterKeyHint="done"
|
||||
enterKeyHint="search"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="f-sheet-list">
|
||||
{canAddCustom && (
|
||||
<button type="button" className="f-sheet-row f-sheet-row-add" onClick={addCustom}>
|
||||
<span className="f-sheet-check">+</span>
|
||||
<span className="f-sheet-row-label">
|
||||
{t("feed.filterCitiesAdd", { city: query.trim() })}
|
||||
</span>
|
||||
</button>
|
||||
)}
|
||||
|
||||
{rows.length === 0 && !canAddCustom && (
|
||||
{rows.length === 0 && (
|
||||
<div className="f-sheet-empty">{t("feed.filterCitiesEmpty")}</div>
|
||||
)}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user