import React, { useCallback } from 'react'; import { Text } from '../Text'; import { Image, StyleSheet } from 'react-native'; import { Normalize } from '../../utils/normalize'; import { theme, colors, spacing } from '../../theme/theme'; import * as Icons from 'phosphor-react-native'; import { TouchableComponent } from '../TouchableComponent'; import { Box } from '../Box'; const { IconContext } = Icons; type PhosphorIcons = keyof typeof Icons; interface Props { LeftIcon?: PhosphorIcons; label: string; RightIcon?: PhosphorIcons; selected?: boolean; disabled?: boolean; type?: 'icon' | 'avatar'; avatarSrc?: string; onIconLeftPress?: () => void; onIconRightPress?: () => void; onPress?: () => void; } const useStatusStyle = ({ disabled, selected, }: { disabled: boolean; selected: boolean; }) => { if (disabled) { return { container: styles.disabledContainer, label: styles.disabledLabel, }; } if (selected) { return { container: styles.selectedContainer, label: styles.selectedLabel, }; } return { container: styles.defaultContainer, label: styles.defaultLabel, }; }; const Icon = ({ name, disabled, ...rest }: Icons.IconProps & { name: PhosphorIcons; disabled: boolean }) => { const PhosphorIcon = Icons[name] as React.FC; return ( ); }; /** * @LeftIcon Icon rendered on the left * @label Chip title * @RightIcon Icon rendered on the right * @selected current selected status * @disabled current disabled status * @type chip variant style type **/ export const Chip = ({ LeftIcon, label, RightIcon, selected, disabled, type, avatarSrc, onIconLeftPress, onIconRightPress, onPress, }: Props) => { const statusStyles = useStatusStyle({ disabled: !!disabled, selected: !!selected, }); const renderLeftContent = useCallback(() => { if (type === 'avatar') { if (selected) return ( ); if (avatarSrc) { return ( ); } else { return ( ); } } else { if (LeftIcon) { return ( onIconLeftPress && onIconLeftPress()} activeOpacity={1} testID="leftIcon" > ); } else return <>; } }, [ LeftIcon, avatarSrc, disabled, onIconLeftPress, selected, statusStyles.label.color, type, ]); const renderRightContent = useCallback(() => { if (RightIcon) { return ( onIconRightPress && onIconRightPress()} activeOpacity={1} testID="rightIcon" > ); } return <>; }, [RightIcon, disabled, onIconRightPress]); return ( onPress && onPress()} testID={type === 'avatar' ? 'avatarChip' : 'chip'} > {renderLeftContent()} {label} {renderRightContent()} ); }; const styles = StyleSheet.create({ container: { paddingHorizontal: spacing['spacing-xs'], paddingVertical: Normalize(6), justifyContent: 'center', borderWidth: spacing['spacing-xxxxs'], alignSelf: 'flex-start', flexDirection: 'row', alignItems: 'center', }, label: { fontFamily: theme.textVariants.mediumBody.fontFamily, fontSize: theme.textVariants.mediumBody.fontSize, lineHeight: theme.textVariants.mediumBody.lineHeight, paddingHorizontal: spacing['spacing-xs'], }, defaultContainer: { borderColor: colors.gray60, }, defaultLabel: { color: colors.darkBlueGray, }, selectedContainer: { backgroundColor: colors.blue10, borderColor: 'transparent', }, selectedLabel: { color: colors.darkBlueGray, }, disabledContainer: { borderColor: colors.gray60, }, disabledLabel: { color: colors.gray60, }, avatarPlaceholder: { margin: -spacing['spacing-xxs'], marginRight: 0 }, avatarImage: { height: 24, width: 24, borderRadius: 12, margin: -spacing['spacing-xxxs'], marginRight: 0, }, });