|
| 1 | +import React, {useMemo} from 'react'; |
| 2 | +import {Image, StyleSheet} from 'react-native'; |
| 3 | + |
| 4 | +import {CloseIcon} from '../../icons'; |
| 5 | +import {styles} from './input-chip.styles'; |
| 6 | +import {useTheme} from '../../theme/useTheme.hook'; |
| 7 | +import {type IconProps} from '../../icons/icon-props'; |
| 8 | +import {BaseChip, type BaseChipProps} from '../base-chip/BaseChip.component'; |
| 9 | + |
| 10 | +interface InputChipProps<T extends IconProps> extends BaseChipProps { |
| 11 | + selected?: boolean; |
| 12 | + imageUrl?: string; |
| 13 | + LeadingIcon?: React.FC<T>; |
| 14 | + leadingIconProps?: T; |
| 15 | + hasTrailingIcon?: boolean; |
| 16 | +} |
| 17 | + |
| 18 | +export const InputChip = <T extends IconProps>({ |
| 19 | + selected = false, |
| 20 | + LeadingIcon, |
| 21 | + imageUrl, |
| 22 | + hasTrailingIcon = true, |
| 23 | + leadingIconProps = {} as T, |
| 24 | + style, |
| 25 | + ...props |
| 26 | +}: InputChipProps<T>) => { |
| 27 | + const {surface, secondaryContainer, outline, primary} = useTheme(); |
| 28 | + |
| 29 | + const onContainerColor = selected ? secondaryContainer.text : surface.textVariant; |
| 30 | + |
| 31 | + const dynamicStyles = useMemo( |
| 32 | + () => |
| 33 | + StyleSheet.create({ |
| 34 | + container: { |
| 35 | + borderWidth: Number(!selected), |
| 36 | + borderColor: outline, |
| 37 | + backgroundColor: selected ? secondaryContainer.background : 'transparent', |
| 38 | + }, |
| 39 | + label: { |
| 40 | + color: onContainerColor, |
| 41 | + }, |
| 42 | + }), |
| 43 | + [selected, onContainerColor] |
| 44 | + ); |
| 45 | + |
| 46 | + const renderLeadingAvatar = () => (imageUrl ? <Image style={styles.leadingImage} source={{uri: imageUrl}} /> : null); |
| 47 | + |
| 48 | + return ( |
| 49 | + <BaseChip |
| 50 | + style={[dynamicStyles.container, imageUrl ? styles.chipWithAvatarContainer : [], style]} |
| 51 | + labelStyle={dynamicStyles.label} |
| 52 | + leadingIcon={LeadingIcon ? <LeadingIcon size={18} color={primary.background} {...leadingIconProps} /> : renderLeadingAvatar()} |
| 53 | + trailingIcon={hasTrailingIcon ? <CloseIcon size={18} color={onContainerColor} /> : null} |
| 54 | + {...props} |
| 55 | + /> |
| 56 | + ); |
| 57 | +}; |
0 commit comments