PliWould/components/ProductTile.tsx

58 lines
1.5 KiB
TypeScript
Raw Normal View History

import { Product, priceDisplay, pricePerUnitDisplay } from "@/lib/product"
2024-07-01 04:49:41 +02:00
import { ImageBackground, StyleProp, StyleSheet, Text, TouchableHighlight, View, ViewStyle } from "react-native";
2024-06-27 23:31:59 +02:00
import { AnimatedStyle } from "react-native-reanimated";
export type OnProductSelectedFunc = (product : Product) => any;
type MyStyle = StyleProp<AnimatedStyle<StyleProp<ViewStyle>>>;
type StyleSpec = {
highlight?: MyStyle,
text?: MyStyle,
image?: MyStyle,
}
2024-06-27 23:31:59 +02:00
export type ProductTileProps = {
product: (Product),
onProductSelected?: OnProductSelectedFunc,
2024-07-01 04:49:41 +02:00
isActive: boolean,
2024-06-27 23:31:59 +02:00
}
const FALLBACK_IMAGE = "";
export function ProductTile ({product, onProductSelected, isActive} : ProductTileProps) {
const k = isActive ? "active" : "default";
const priceDisplay = pricePerUnitDisplay(product);
2024-06-27 23:31:59 +02:00
return (
2024-07-01 04:49:41 +02:00
<TouchableHighlight
aria-label={`product ${product.id}`}
style={styles[k].highlight}
2024-07-01 04:49:41 +02:00
onPress={() => onProductSelected && onProductSelected(product)}>
<Text style={styles[k].text}>{product.attributes?.name || `Product ${product.id}`} ({priceDisplay})</Text>
2024-07-01 04:49:41 +02:00
</TouchableHighlight>
2024-06-27 23:31:59 +02:00
);
}
const styles = {
active: StyleSheet.create({
highlight: {
padding: 10,
margin: 2,
color: "lightblue",
},
text: {
}
}),
default: StyleSheet.create({
highlight: {
padding: 10,
margin: 2,
backgroundColor: "lightgrey",
},
text: {
}
}),
}