|
| 1 | +import React, {useMemo} from 'react'; |
| 2 | + |
| 3 | +import {useTheme} from '../../theme/useTheme.hook'; |
| 4 | +import {type IconProps} from '../../icons/icon-props'; |
| 5 | +import {getDynamicStyles, styles} from './filter-chip.styles'; |
| 6 | +import {BaseChip, type BaseChipProps} from '../base-chip/BaseChip.component'; |
| 7 | +import {DoneIcon} from '../../icons'; |
| 8 | +import {CircularActivityIndicator} from '../../activity-indicators/circular-activity-indicator/CircularActivityIndicator.component'; |
| 9 | +import Animated, {FadeIn, FadeOut, LinearTransition} from 'react-native-reanimated'; |
| 10 | + |
| 11 | +export interface FilterChipProps<T extends IconProps> extends BaseChipProps { |
| 12 | + elevated?: boolean; |
| 13 | + selected?: boolean; |
| 14 | + loading?: boolean; |
| 15 | + LeadingIcon?: React.FC<T>; |
| 16 | + leadingIconProps?: T; |
| 17 | + iconSize?: number; |
| 18 | + activityIndicatorSize?: number; |
| 19 | +} |
| 20 | + |
| 21 | +export const FilterChip = <T extends IconProps>({ |
| 22 | + elevated = false, |
| 23 | + selected = false, |
| 24 | + loading = false, |
| 25 | + LeadingIcon, |
| 26 | + leadingIconProps = {} as T, |
| 27 | + iconSize = 18, |
| 28 | + activityIndicatorSize = 38, |
| 29 | + style, |
| 30 | + labelStyle, |
| 31 | + disabled = false, |
| 32 | + ...props |
| 33 | +}: FilterChipProps<T>) => { |
| 34 | + const theme = useTheme(); |
| 35 | + const dynamicStyles = useMemo(() => getDynamicStyles(selected, elevated, disabled, theme), [disabled, elevated, selected, theme]); |
| 36 | + |
| 37 | + const renerCustomLeadingIcon = () => |
| 38 | + LeadingIcon ? ( |
| 39 | + <Animated.View entering={FadeIn} exiting={FadeOut}> |
| 40 | + <LeadingIcon size={iconSize} color={dynamicStyles.leadingIcon.color} {...leadingIconProps} /> |
| 41 | + </Animated.View> |
| 42 | + ) : null; |
| 43 | + |
| 44 | + const renderLeadingIcon = () => (selected ? <DoneIcon size={iconSize} color={dynamicStyles.selectedIcon.color} /> : renerCustomLeadingIcon()); |
| 45 | + |
| 46 | + return ( |
| 47 | + <Animated.View layout={LinearTransition}> |
| 48 | + <BaseChip |
| 49 | + style={[elevated && !disabled ? styles.elevatedContainer : styles.outlinedContainer, dynamicStyles.container, style]} |
| 50 | + labelStyle={[dynamicStyles.label, labelStyle]} |
| 51 | + leadingIcon={ |
| 52 | + loading ? <CircularActivityIndicator size={activityIndicatorSize} style={{height: iconSize, width: iconSize}} /> : renderLeadingIcon() |
| 53 | + } |
| 54 | + disabled={disabled} |
| 55 | + {...props} |
| 56 | + /> |
| 57 | + </Animated.View> |
| 58 | + ); |
| 59 | +}; |
0 commit comments