All files / components BreedSelector.tsx

100% Statements 20/20
92.3% Branches 12/13
100% Functions 8/8
100% Lines 18/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170                              2x           13x 13x   13x 13x 26x   26x   13x   1x 1x 1x         13x 13x 1x 1x     13x                                                                                                                                                                           25x   1x                                                                        
import { memo, useCallback, useState } from 'react';
import { Modal, ScrollView, TextInput, TouchableOpacity } from 'react-native';
import { Card, Text, XStack, YStack } from 'tamagui';
import { Button } from '@/src/design-system/components';
import { IconSymbol } from '@/src/components/ui/IconSymbol';
import { getBreedsBySpecies, type PetSpecies } from '@/src/constants/petBreeds';
import { primaryScale, neutralScale } from '@/src/design-system/tokens';
 
interface BreedSelectorProps {
  species: PetSpecies;
  value: string;
  onChange: (breed: string) => void;
  placeholder?: string;
}
 
export const BreedSelector = memo(function BreedSelector({
  species,
  value,
  onChange,
  placeholder = '选择或输入品种',
}: BreedSelectorProps) {
  const [modalVisible, setModalVisible] = useState(false);
  const [searchText, setSearchText] = useState('');
 
  const breeds = getBreedsBySpecies(species);
  const filteredBreeds = breeds.filter((breed) =>
    breed.label.toLowerCase().includes(searchText.toLowerCase())
  );
  const popularBreeds = breeds.filter((b) => b.popular);
 
  const handleSelectBreed = useCallback(
    (breed: string) => {
      onChange(breed);
      setModalVisible(false);
      setSearchText('');
    },
    [onChange]
  );
 
  const handleOpen = useCallback(() => setModalVisible(true), []);
  const handleClose = useCallback(() => {
    setModalVisible(false);
    setSearchText('');
  }, []);
 
  return (
    <>
      <TouchableOpacity onPress={handleOpen} activeOpacity={0.7} testID="breed-selector-trigger">
        <YStack
          borderRadius="$4"
          borderWidth={1.5}
          borderColor="$borderColor"
          backgroundColor="$background"
          paddingHorizontal="$4"
          paddingVertical="$3.5"
        >
          <XStack alignItems="center" justifyContent="space-between">
            <Text
              fontSize={15}
              color={(value ? '$foreground' : '$foregroundSubtle') as any}
              flex={1}
              testID="breed-selector-value"
            >
              {value || placeholder}
            </Text>
            <IconSymbol name="chevron.right" size={20} color="$foregroundMuted" />
          </XStack>
        </YStack>
      </TouchableOpacity>
 
      <Modal
        visible={modalVisible}
        animationType="slide"
        transparent
        onRequestClose={handleClose}
        testID="breed-selector-modal"
      >
        <YStack flex={1} backgroundColor="rgba(0, 0, 0, 0.5)" justifyContent="flex-end">
          <Card
            backgroundColor="$background"
            borderTopLeftRadius="$6"
            borderTopRightRadius="$6"
            maxHeight="80%"
            paddingBottom="$6"
          >
            {/* 头部 */}
            <XStack
              paddingHorizontal="$5"
              paddingVertical="$4"
              alignItems="center"
              justifyContent="space-between"
              borderBottomWidth={1}
              borderBottomColor={'$borderMuted' as any}
            >
              <Text fontSize={18} fontWeight="700" color={'$foreground' as any}>
                选择品种
              </Text>
              <TouchableOpacity onPress={handleClose} testID="breed-selector-close">
                <IconSymbol name="xmark.circle.fill" size={28} color="$foregroundMuted" />
              </TouchableOpacity>
            </XStack>
 
            {/* 搜索框 */}
            <YStack paddingHorizontal="$5" paddingVertical="$3">
              <YStack
                borderRadius="$3"
                borderWidth={1}
                borderColor="$borderColor"
                backgroundColor="$backgroundMuted"
                paddingHorizontal="$3"
              >
                <TextInput
                  testID="breed-selector-search"
                  value={searchText}
                  onChangeText={setSearchText}
                  placeholder="搜索品种..."
                  placeholderTextColor={neutralScale.neutral8}
                  style={{
                    height: 40,
                    fontSize: 15,
                    color: neutralScale.neutral12,
                  }}
                />
              </YStack>
            </YStack>
 
            {/* 列表 */}
            <ScrollView style={{ maxHeight: 400 }} keyboardShouldPersistTaps="handled">
              <YStack paddingHorizontal="$5" paddingBottom="$4">
                {filteredBreeds.length > 0 ? (
                  filteredBreeds.map((breed) => (
                    <TouchableOpacity
                      key={breed.label}
                      onPress={() => handleSelectBreed(breed.label)}
                      testID={`breed-item-${breed.label}`}
                    >
                      <XStack
                        paddingVertical="$3"
                        borderBottomWidth={1}
                        borderBottomColor={'$borderMuted' as any}
                        alignItems="center"
                        justifyContent="space-between"
                      >
                        <Text
                          fontSize={15}
                          color={(value === breed.label ? '$primary' : '$foreground') as any}
                          fontWeight={value === breed.label ? '600' : '400'}
                        >
                          {breed.label}
                        </Text>
                        {value === breed.label && (
                          <IconSymbol name="checkmark" size={20} color="$primary" />
                        )}
                      </XStack>
                    </TouchableOpacity>
                  ))
                ) : (
                  <YStack paddingVertical="$5" alignItems="center">
                    <Text color={'$foregroundSubtle' as any}>未找到相关品种</Text>
                  </YStack>
                )}
              </YStack>
            </ScrollView>
          </Card>
        </YStack>
      </Modal>
    </>
  );
});