|
| 1 | +import type { ReactNode } from "react"; |
| 2 | +import { Platform, Pressable, View } from "react-native"; |
| 3 | + |
| 4 | +import { cn } from "../../lib/cn"; |
| 5 | +import { SymbolView, type AppSymbolName } from "./AppSymbol"; |
| 6 | +import { AppText as Text } from "./AppText"; |
| 7 | +import { NavigationRow } from "./NavigationRow"; |
| 8 | + |
| 9 | +/** A picker row in a grouped list; the first and last rows round the group's corners. */ |
| 10 | +export function SelectionRow(props: { |
| 11 | + readonly icon?: AppSymbolName; |
| 12 | + /** Drawn where the icon goes, for marks a symbol can't express (a label's color dot). */ |
| 13 | + readonly leading?: ReactNode; |
| 14 | + readonly onPress: () => void; |
| 15 | + readonly disabled?: boolean; |
| 16 | + readonly selected: boolean; |
| 17 | + /** "checkbox" when several rows can be selected at once. */ |
| 18 | + readonly role?: "radio" | "checkbox"; |
| 19 | + readonly isFirst?: boolean; |
| 20 | + readonly isLast?: boolean; |
| 21 | + readonly subtitle?: string; |
| 22 | + readonly title: string; |
| 23 | +}) { |
| 24 | + const role = props.role ?? "radio"; |
| 25 | + const row = |
| 26 | + Platform.OS === "android" ? ( |
| 27 | + <NavigationRow |
| 28 | + className="bg-grouped-card" |
| 29 | + title={props.title} |
| 30 | + subtitle={props.subtitle} |
| 31 | + leading={ |
| 32 | + props.icon ? ( |
| 33 | + <SymbolView name={props.icon} size={24} tintColorClassName="accent-icon-muted" /> |
| 34 | + ) : ( |
| 35 | + props.leading |
| 36 | + ) |
| 37 | + } |
| 38 | + trailing={ |
| 39 | + props.selected ? ( |
| 40 | + <SymbolView name="checkmark" size={20} tintColorClassName="accent-focus" /> |
| 41 | + ) : null |
| 42 | + } |
| 43 | + accessibilityRole={role} |
| 44 | + accessibilityState={{ checked: props.selected }} |
| 45 | + disabled={props.disabled} |
| 46 | + onPress={props.onPress} |
| 47 | + /> |
| 48 | + ) : ( |
| 49 | + <Pressable |
| 50 | + accessibilityLabel={[props.title, props.subtitle].filter(Boolean).join(", ")} |
| 51 | + accessibilityRole={role} |
| 52 | + accessibilityState={{ checked: props.selected }} |
| 53 | + className={cn( |
| 54 | + "min-h-14 flex-row items-center gap-3 bg-grouped-card px-4 py-3 active:bg-subtle", |
| 55 | + !props.isLast && "border-b border-border-subtle", |
| 56 | + )} |
| 57 | + disabled={props.disabled} |
| 58 | + onPress={props.onPress} |
| 59 | + style={{ opacity: props.disabled ? 0.45 : 1 }} |
| 60 | + > |
| 61 | + {props.icon ? ( |
| 62 | + <SymbolView |
| 63 | + name={props.icon} |
| 64 | + size={17} |
| 65 | + tintColorClassName="accent-icon-muted" |
| 66 | + type="monochrome" |
| 67 | + /> |
| 68 | + ) : ( |
| 69 | + (props.leading ?? null) |
| 70 | + )} |
| 71 | + <View className="min-w-0 flex-1 gap-0.5"> |
| 72 | + <Text className="text-base font-t3-medium text-foreground" numberOfLines={1}> |
| 73 | + {props.title} |
| 74 | + </Text> |
| 75 | + {props.subtitle ? ( |
| 76 | + <Text className="text-xs text-foreground-muted" numberOfLines={1}> |
| 77 | + {props.subtitle} |
| 78 | + </Text> |
| 79 | + ) : null} |
| 80 | + </View> |
| 81 | + {props.selected ? ( |
| 82 | + <SymbolView |
| 83 | + name="checkmark" |
| 84 | + size={16} |
| 85 | + tintColorClassName="accent-icon" |
| 86 | + type="monochrome" |
| 87 | + weight="semibold" |
| 88 | + /> |
| 89 | + ) : null} |
| 90 | + </Pressable> |
| 91 | + ); |
| 92 | + if (!props.isFirst && !props.isLast) return row; |
| 93 | + return ( |
| 94 | + <View |
| 95 | + className={cn( |
| 96 | + "overflow-hidden", |
| 97 | + props.isFirst && (Platform.OS === "android" ? "rounded-t-[28px]" : "rounded-t-2xl"), |
| 98 | + props.isLast && (Platform.OS === "android" ? "rounded-b-[28px]" : "rounded-b-2xl"), |
| 99 | + )} |
| 100 | + > |
| 101 | + {row} |
| 102 | + </View> |
| 103 | + ); |
| 104 | +} |
0 commit comments