import type { icons } from 'lucide-react-native'
import type { ReactElement, ReactNode } from 'react'
import type { ViewStyle } from 'react-native'
import { Children, cloneElement, isValidElement } from 'react'
import { Text, TouchableOpacity, View } from 'react-native'
import LucideIcon from './LucideIcon'
/* ================= Cell ================= */
export interface CellProps {
left?: ReactNode
right?: ReactNode
title?: string
leftIcon?: keyof typeof icons
rightIcon?: keyof typeof icons
rightText?: string
isLink?: boolean
className?: string
style?: ViewStyle
showDivider?: boolean // 由 CellGroup 注入
onPress?: () => void
}
export function Cell({
left,
right,
title,
leftIcon,
rightIcon = 'ChevronRight',
rightText,
isLink = false,
className,
style,
showDivider = false,
onPress,
}: CellProps) {
return (
{left || (
{leftIcon && }
{title && {title}}
)}
{right || (
{rightText && {rightText}}
{isLink && rightIcon && (
)}
)}
{showDivider && (
)}
)
}
/* ================= CellGroup ================= */
export interface CellGroupProps {
className?: string
style?: ViewStyle
children?: ReactNode
showDivider?: boolean // 整组是否显示下划线
}
export function CellGroup({
className,
style,
children,
showDivider = true,
}: CellGroupProps) {
const elements = Children.toArray(children)
return (
{elements.map((child, index) => {
if (!isValidElement(child))
return child
const isLast = index === elements.length - 1
return cloneElement(child as ReactElement, {
showDivider: showDivider && !isLast,
})
})}
)
}