From 01459c7e0e2d9674026e9fd8e9aadfc9457c4880 Mon Sep 17 00:00:00 2001 From: neodk2004 Date: Sun, 4 Oct 2026 13:09:38 +0200 Subject: [PATCH] feat(mobile): Suchfeld im Rassen-Dropdown found.md (iOS-Feedback): Hunde-/Katzen-Rassenlisten sind lang. OptionPicker zeigt ab 10 Eintraegen oben ein Suchfeld, das ohne Gross-/Kleinschreibung und Akzente filtert (filterOptionsByName). Bei offener Tastatur rueckt die Liste nach oben, damit sie nicht verdeckt wird. Wirkt in Tier anlegen, Profil bearbeiten und Explore-Filter. Co-Authored-By: Claude Opus 5.5 --- mobile/src/components/OptionPicker.tsx | 109 ++++++++++++++++++++++--- mobile/src/i18n/locales/de.json | 1 + mobile/src/i18n/locales/en.json | 1 + mobile/src/lib/filter-options.test.ts | 36 ++++++++ mobile/src/lib/filter-options.ts | 15 ++++ 5 files changed, 151 insertions(+), 11 deletions(-) create mode 100644 mobile/src/lib/filter-options.test.ts create mode 100644 mobile/src/lib/filter-options.ts diff --git a/mobile/src/components/OptionPicker.tsx b/mobile/src/components/OptionPicker.tsx index a6b1121..62b831b 100644 --- a/mobile/src/components/OptionPicker.tsx +++ b/mobile/src/components/OptionPicker.tsx @@ -1,9 +1,22 @@ import { useEffect, useMemo, useRef, useState } from "react"; -import { View, Text, Pressable, ScrollView, ActivityIndicator, Modal, StyleSheet } from "react-native"; -import { ChevronDown } from "lucide-react-native"; +import { + View, + Text, + TextInput, + Pressable, + ScrollView, + ActivityIndicator, + Modal, + StyleSheet, + Keyboard, + Platform, + useWindowDimensions, +} from "react-native"; +import { ChevronDown, Search } from "lucide-react-native"; import { useTranslation } from "react-i18next"; import { useTheme } from "../context/ThemeContext"; -import { radii, getTypography, type ColorTokens, type Typography } from "../theme/tokens"; +import { radii, fontFamily, getTypography, type ColorTokens, type Typography } from "../theme/tokens"; +import { filterOptionsByName } from "../lib/filter-options"; export type PickerOption = { id: string; name: string }; @@ -22,6 +35,11 @@ type Props = { const BUTTON_HEIGHT = 48; const LIST_GAP = 4; const MAX_LIST_HEIGHT = 220; +// Ab so vielen Einträgen gibt es oben ein Suchfeld (found.md: Hunde-/Katzen- +// Rassenlisten sind lang). Tierarten (kurze Liste) bleiben ohne. +const SEARCH_MIN_OPTIONS = 10; +const SEARCH_HEIGHT = 44; +const MIN_LIST_TOP = 48; type Anchor = { top: number; left: number; width: number }; @@ -64,6 +82,13 @@ export function OptionPicker({ const styles = useMemo(() => makeStyles(colors), [colors]); const triggerRef = useRef(null); const [anchor, setAnchor] = useState(null); + const [query, setQuery] = useState(""); + const [keyboardHeight, setKeyboardHeight] = useState(0); + const [listHeight, setListHeight] = useState(0); + const { height: windowHeight } = useWindowDimensions(); + + const hasSearch = options.length >= SEARCH_MIN_OPTIONS; + const visibleOptions = useMemo(() => filterOptionsByName(options, query), [options, query]); useEffect(() => { // No need to reset `anchor` to null when closing — the Modal already @@ -76,6 +101,38 @@ export function OptionPicker({ }); }, [open]); + // The search field brings up the keyboard; a list anchored low on the + // screen would end up underneath it, so it moves up while the keyboard is open. + useEffect(() => { + if (!open || !hasSearch) return; + const showEvent = Platform.OS === "ios" ? "keyboardWillShow" : "keyboardDidShow"; + const hideEvent = Platform.OS === "ios" ? "keyboardWillHide" : "keyboardDidHide"; + const showSub = Keyboard.addListener(showEvent, (e) => setKeyboardHeight(e.endCoordinates.height)); + const hideSub = Keyboard.addListener(hideEvent, () => setKeyboardHeight(0)); + return () => { + showSub.remove(); + hideSub.remove(); + setKeyboardHeight(0); + }; + }, [open, hasSearch]); + + const close = () => { + setQuery(""); + onToggle(); + }; + + const select = (option: PickerOption) => { + setQuery(""); + onSelect(option); + }; + + const listTop = (() => { + if (!anchor) return 0; + if (!keyboardHeight || !listHeight) return anchor.top; + const maxTop = windowHeight - keyboardHeight - listHeight - LIST_GAP; + return Math.max(MIN_LIST_TOP, Math.min(anchor.top, maxTop)); + })(); + return ( {selectedName ?? placeholder} - + {/* Backdrop and list are SIBLINGS, not parent/child. The list must not be a descendant of the backdrop Pressable at all — a Pressable @@ -99,17 +156,36 @@ export function OptionPicker({ the list's bounds hit-tests into the list's own subtree and never reaches the backdrop's Pressable underneath at all. */} - + {anchor && ( - + setListHeight(e.nativeEvent.layout.height)} + > + {hasSearch && ( + + + + + )} {isLoading ? ( - ) : options.length === 0 ? ( - {emptyLabel ?? t("common.noResultsFound")} + ) : visibleOptions.length === 0 ? ( + {emptyLabel && !query ? emptyLabel : t("common.noResultsFound")} ) : ( - - {options.map((option) => ( - onSelect(option)}> + + {visibleOptions.map((option) => ( + select(option)}> {option.name} ))} @@ -154,7 +230,18 @@ function makeStyles(colors: ColorTokens) { shadowOpacity: 0.15, shadowRadius: 8, }, + listWithSearch: { maxHeight: MAX_LIST_HEIGHT + SEARCH_HEIGHT }, scrollView: { maxHeight: MAX_LIST_HEIGHT }, + searchRow: { + height: SEARCH_HEIGHT, + flexDirection: "row", + alignItems: "center", + gap: 8, + paddingHorizontal: 14, + borderBottomWidth: 1, + borderBottomColor: colors.hairlineGray, + }, + searchInput: { flex: 1, height: SEARCH_HEIGHT, fontFamily: fontFamily.regular, fontSize: 14, color: colors.ink }, row: { paddingHorizontal: 14, paddingVertical: 12, borderBottomWidth: 1, borderBottomColor: colors.hairlineGray }, loading: { padding: 16 }, empty: { ...typography.body, color: colors.quietGray, padding: 16 }, diff --git a/mobile/src/i18n/locales/de.json b/mobile/src/i18n/locales/de.json index fc15a92..e617736 100644 --- a/mobile/src/i18n/locales/de.json +++ b/mobile/src/i18n/locales/de.json @@ -20,6 +20,7 @@ "done": "Fertig", "optional": "optional", "noResultsFound": "Keine Einträge gefunden.", + "searchPlaceholder": "Suchen …", "edit": "Bearbeiten", "authLoadTimeout": "Das dauert gerade länger als erwartet. Bitte erneut versuchen." }, diff --git a/mobile/src/i18n/locales/en.json b/mobile/src/i18n/locales/en.json index 78947de..fcffeeb 100644 --- a/mobile/src/i18n/locales/en.json +++ b/mobile/src/i18n/locales/en.json @@ -20,6 +20,7 @@ "done": "Done", "optional": "optional", "noResultsFound": "No entries found.", + "searchPlaceholder": "Search …", "edit": "Edit", "authLoadTimeout": "This is taking longer than expected. Please try again." }, diff --git a/mobile/src/lib/filter-options.test.ts b/mobile/src/lib/filter-options.test.ts new file mode 100644 index 0000000..c9b41c6 --- /dev/null +++ b/mobile/src/lib/filter-options.test.ts @@ -0,0 +1,36 @@ +import { describe, it, expect } from "vitest"; +import { filterOptionsByName, normalizeForSearch } from "./filter-options"; + +const breeds = [ + { id: "1", name: "Bichon Frisé" }, + { id: "2", name: "Shiba Inu" }, + { id: "3", name: "Europäisch Kurzhaar" }, + { id: "4", name: "Bengalkatze" }, +]; + +describe("filterOptionsByName", () => { + it("returns every option for an empty or blank query", () => { + expect(filterOptionsByName(breeds, "")).toEqual(breeds); + expect(filterOptionsByName(breeds, " ")).toEqual(breeds); + }); + + it("matches case-insensitively anywhere in the name", () => { + expect(filterOptionsByName(breeds, "INU").map((b) => b.id)).toEqual(["2"]); + expect(filterOptionsByName(breeds, "be").map((b) => b.id)).toEqual(["4"]); + }); + + it("ignores accents and umlauts", () => { + expect(filterOptionsByName(breeds, "frise").map((b) => b.id)).toEqual(["1"]); + expect(filterOptionsByName(breeds, "europaisch").map((b) => b.id)).toEqual(["3"]); + }); + + it("returns nothing when no name matches", () => { + expect(filterOptionsByName(breeds, "pudel")).toEqual([]); + }); +}); + +describe("normalizeForSearch", () => { + it("lowercases, trims and strips diacritics", () => { + expect(normalizeForSearch(" Bichon Frisé ")).toBe("bichon frise"); + }); +}); diff --git a/mobile/src/lib/filter-options.ts b/mobile/src/lib/filter-options.ts new file mode 100644 index 0000000..52d8cbb --- /dev/null +++ b/mobile/src/lib/filter-options.ts @@ -0,0 +1,15 @@ +/** Lowercase + strip diacritics, so "eu" finds "Européen" and "BICHON" finds "Bichon Frisé". */ +export function normalizeForSearch(text: string): string { + return text + .normalize("NFD") + .replace(/[̀-ͯ]/g, "") + .toLowerCase() + .trim(); +} + +/** Keeps options whose name contains the query; an empty query keeps all of them. */ +export function filterOptionsByName(options: T[], query: string): T[] { + const needle = normalizeForSearch(query); + if (!needle) return options; + return options.filter((option) => normalizeForSearch(option.name).includes(needle)); +}