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 <noreply@anthropic.com>
This commit is contained in:
1 parent
a2af4eda6a
commit
01459c7e0e
5 files changed
+151
-11
No files matched your search
@@ -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<View>(null);
|
||||
const [anchor, setAnchor] = useState<Anchor | null>(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 (
|
||||
<View>
|
||||
<Pressable
|
||||
@@ -87,7 +144,7 @@ export function OptionPicker({
|
||||
<Text style={selectedName ? styles.value : styles.placeholder}>{selectedName ?? placeholder}</Text>
|
||||
<ChevronDown color={colors.quietGray} size={18} />
|
||||
</Pressable>
|
||||
<Modal visible={open} transparent animationType="fade" onRequestClose={onToggle}>
|
||||
<Modal visible={open} transparent animationType="fade" onRequestClose={close}>
|
||||
{/*
|
||||
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.
|
||||
*/}
|
||||
<Pressable style={styles.backdrop} onPress={onToggle} />
|
||||
<Pressable style={styles.backdrop} onPress={close} />
|
||||
{anchor && (
|
||||
<View style={[styles.list, { top: anchor.top, left: anchor.left, width: anchor.width }]}>
|
||||
<View
|
||||
style={[styles.list, hasSearch && styles.listWithSearch, { top: listTop, left: anchor.left, width: anchor.width }]}
|
||||
onLayout={(e) => setListHeight(e.nativeEvent.layout.height)}
|
||||
>
|
||||
{hasSearch && (
|
||||
<View style={styles.searchRow}>
|
||||
<Search color={colors.quietGray} size={16} />
|
||||
<TextInput
|
||||
value={query}
|
||||
onChangeText={setQuery}
|
||||
placeholder={t("common.searchPlaceholder")}
|
||||
placeholderTextColor={colors.quietGray}
|
||||
style={styles.searchInput}
|
||||
autoCorrect={false}
|
||||
autoCapitalize="none"
|
||||
returnKeyType="search"
|
||||
clearButtonMode="while-editing"
|
||||
/>
|
||||
</View>
|
||||
)}
|
||||
{isLoading ? (
|
||||
<ActivityIndicator color={colors.marigoldOrange} style={styles.loading} />
|
||||
) : options.length === 0 ? (
|
||||
<Text style={styles.empty}>{emptyLabel ?? t("common.noResultsFound")}</Text>
|
||||
) : visibleOptions.length === 0 ? (
|
||||
<Text style={styles.empty}>{emptyLabel && !query ? emptyLabel : t("common.noResultsFound")}</Text>
|
||||
) : (
|
||||
<ScrollView style={styles.scrollView}>
|
||||
{options.map((option) => (
|
||||
<Pressable key={option.id} style={styles.row} onPress={() => onSelect(option)}>
|
||||
<ScrollView style={styles.scrollView} keyboardShouldPersistTaps="handled">
|
||||
{visibleOptions.map((option) => (
|
||||
<Pressable key={option.id} style={styles.row} onPress={() => select(option)}>
|
||||
<Text style={getTypography(colors).body}>{option.name}</Text>
|
||||
</Pressable>
|
||||
))}
|
||||
@@ -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 },
|
||||
|
||||
@@ -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."
|
||||
},
|
||||
|
||||
@@ -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."
|
||||
},
|
||||
|
||||
@@ -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");
|
||||
});
|
||||
});
|
||||
@@ -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<T extends { name: string }>(options: T[], query: string): T[] {
|
||||
const needle = normalizeForSearch(query);
|
||||
if (!needle) return options;
|
||||
return options.filter((option) => normalizeForSearch(option.name).includes(needle));
|
||||
}
|
||||
Reference in new issue
Block a user