Skip to content

Commit

Permalink
feat(fscomponents): Serializable Cateogry Line
Browse files Browse the repository at this point in the history
  • Loading branch information
wSedlacek committed Apr 16, 2021
1 parent b7e4219 commit 106c239
Showing 1 changed file with 102 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import React, { FunctionComponent, memo, useCallback } from 'react';
import {
AccessibilityRole,
Image,
ImageStyle,
ImageURISource,
StyleProp,
Text,
TextStyle,
View,
ViewStyle
} from 'react-native';
import { CommerceTypes } from '@brandingbrand/fscommerce';
import { TouchableHighlightLink } from '../TouchableHighlightLink';

import { style as S } from '../../styles/CategoryLine';
import { useNavigator } from '@brandingbrand/fsapp';

export interface SerializableCategoryLineProps extends CommerceTypes.Category {
accessoryStyle?: ImageStyle;
href?: string;
imageStyle?: ImageStyle;
showAccessory?: boolean;
showImage?: boolean;
style?: ViewStyle;
titleStyle?: TextStyle;
underlayColor?: string;
accessorySrc?: ImageURISource;
accessibilityLabel?: string;
accessibilityRole?: AccessibilityRole;
}

export interface CategoryLineProps extends Omit<SerializableCategoryLineProps,
'accessoryStyle' |
'imageStyle' |
'style' |
'titleStyle'
> {
accessoryStyle?: StyleProp<ImageStyle>;
imageStyle?: StyleProp<ImageStyle>;
onPress?: (item: CommerceTypes.Category) => void;
renderAccessory?: () => React.ReactNode;
style?: StyleProp<ViewStyle>;
titleStyle?: StyleProp<TextStyle>;
}

// tslint:disable-next-line:cyclomatic-complexity
export const CategoryLine: FunctionComponent<CategoryLineProps> = memo((props): JSX.Element => {

const {
renderAccessory,
showAccessory,
accessibilityLabel,
accessibilityRole,
accessorySrc,
accessoryStyle,
href,
image,
showImage,
imageStyle,
style,
title,
titleStyle,
underlayColor,
onPress
} = props;

const navigator = useNavigator();

const handlePress = useCallback(() => {
if (onPress) {
onPress(props);
} else if (href) {
navigator.open(href);
}
}, [href]);

const showAccessoryValue = showAccessory === undefined ? true : showAccessory;
const showImageValue = showImage === undefined ? true : showImage;

return (
<TouchableHighlightLink
style={[S.container, style]}
underlayColor={underlayColor || '#eee'}
onPress={handlePress}
accessibilityLabel={accessibilityLabel || title}
accessibilityRole={accessibilityRole || 'link'}
>
<View style={S.rowInner}>
{showImageValue && image && <Image source={image} style={imageStyle} />}
<Text style={[S.buttonText, titleStyle]}>
{title}
</Text>
{showAccessoryValue && accessorySrc &&
<Image source={accessorySrc} style={accessoryStyle} resizeMode='contain' />
}
{renderAccessory && renderAccessory()}
</View>
</TouchableHighlightLink>
);
});

0 comments on commit 106c239

Please sign in to comment.