Skip to content

Implemented Card Component #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions src/components/Card/Card.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import React from "react";

import { Image, StyleSheet, View } from "react-native";

import { styled } from "nativewind";

import { CardType } from "./Card.types";

const Card: React.FC<CardType> = ({ type, cardColor, imageURL, children }) => {
let cardStyle = {
...style.baseCard,
...cardColor?.[0]
};

let outlinedCardStyle = {
...style.baseCard,
borderColor: cardColor?.[0].backgroundColor
};

if (type === "elevated")
cardStyle = StyleSheet.compose(cardStyle, style.elevatedCard as any);

if (type === "outlined")
cardStyle = StyleSheet.compose(
outlinedCardStyle,
style.outlinedCard as any
);

return (
<View style={cardStyle}>
{imageURL && <Image source={{ uri: imageURL }} style={style.cardImage} />}
{children && (
<View style={{ ...style.childrenContainer }}>{children}</View>
)}
</View>
);
};

export default styled(Card, {
props: {
cardColor: true
}
});

const style = StyleSheet.create({
baseCard: {
borderRadius: 8,
height: "100%",
width: "100%"
},
elevatedCard: {
shadowColor: "#000",
shadowOffset: {
width: 0,
height: 5
},
shadowOpacity: 0.36,
shadowRadius: 6.68
},
outlinedCard: {
borderWidth: 2
},
cardImage: {
width: "100%",
height: "50%",
borderTopLeftRadius: 6,
borderTopRightRadius: 6
},
childrenContainer: {
paddingVertical: 8,
paddingHorizontal: 16
}
});
8 changes: 8 additions & 0 deletions src/components/Card/Card.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import React from "react";

export interface CardType {
type?: "elevated" | "outlined" | undefined;
cardColor?: [{ backgroundColor?: string | undefined }];
imageURL?: string;
children?: React.ReactNode;
}
1 change: 1 addition & 0 deletions src/components/Card/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "./Card";
25 changes: 9 additions & 16 deletions src/components/Text/Text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,15 @@ import { styled } from "nativewind";
import { TextType } from "./Text.types";

const Text: React.FC<TextType> = ({ text, textColor, type }) => {
const generateTextStyle = (
color: { color?: string | undefined } | undefined,
type: string | undefined
): object => {
if (type === "title") {
return { ...style.title, ...color };
}
if (type === "subtitle") {
return { ...style.subtitle, ...color };
}
return { ...style.default, ...color };
};

const generatedStyle = generateTextStyle(textColor?.[0], type);

return <RNText style={generatedStyle}>{text}</RNText>;
let textStyle = {...style.default, ...textColor?.[0]}

if (type === "title")
textStyle = StyleSheet.compose(textStyle, style.title as any)

if (type === "subtitle")
textStyle = StyleSheet.compose(textStyle, style.subtitle as any)

return <RNText style={textStyle}>{text}</RNText>;
};

export default styled(Text, {
Expand Down
1 change: 1 addition & 0 deletions src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export { default as Button } from "./Button";
export { default as Loader } from "./Loader";
export { default as ItemSeparator } from "./ItemSeparator";
export { default as TextInput } from "./TextInput";
export { default as Card } from "./Card";
export { default as Text } from "./Text";
102 changes: 102 additions & 0 deletions storybook/stories/Card/Card.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Card.stories.js

import React from "react";

import { text, select, number } from "@storybook/addon-knobs";

import { storiesOf } from "@storybook/react-native";

import { Text, View } from "react-native";
import { Button, Card, ItemSeparator } from "../../../src";
import { action } from "@storybook/addon-actions";

storiesOf("Cards", module)
.addDecorator((getStory) => (
<View className="w-full h-full items-center justify-center px-4">
{getStory()}
</View>
))
.add("Elevated Card", () => {
return (
<View className="w-full h-96">
<Card
type="elevated"
cardColor="bg-white"
imageURL={text(
"Image Url",
"https://cdn.pixabay.com/photo/2014/07/28/20/39/sunset-404072__340.jpg"
)}
>
<View className="w-full">
<Text className="text-2xl font-bold">Card Title</Text>
<Text className="text-base font-light">
It is a long established fact that a reader will be distracted by
the readable content.
</Text>
<ItemSeparator separatorStyle="h-4" />
<Button
buttonColor="bg-blue-600"
textColor="text-white"
label="Action Button"
onPress={action("clicked Button")}
/>
</View>
</Card>
</View>
);
})
.add("Outlined Card", () => {
return (
<View className="w-full h-96">
<Card
type="outlined"
cardColor="bg-black"
imageURL={text(
"Image Url",
"https://cdn.pixabay.com/photo/2014/07/28/20/39/sunset-404072__340.jpg"
)}
>
<Text className="text-2xl font-bold">Card Title</Text>
<Text className="text-base font-light">
It is a long established fact that a reader will be distracted by
the readable content.
</Text>
<ItemSeparator separatorStyle="h-4" />
<Button
type="outlined"
buttonColor="bg-black"
textColor="text-black"
label="Action Button"
onPress={action("clicked Button")}
/>
</Card>
</View>
);
})
.add("Flat Card", () => {
return (
<View className="w-full h-96">
<Card
cardColor="bg-blue-600"
imageURL={text(
"Image Url",
"https://cdn.pixabay.com/photo/2014/07/28/20/39/sunset-404072__340.jpg"
)}
>
<Text className="text-2xl font-bold text-white">Card Title</Text>
<Text className="text-base font-normal text-white">
It is a long established fact that a reader will be distracted by
the readable content.
</Text>
<ItemSeparator separatorStyle="h-4" />
<Button
type="outlined"
buttonColor="bg-white"
textColor="text-white"
label="Action Button"
onPress={action("clicked Button")}
/>
</Card>
</View>
);
});
10 changes: 5 additions & 5 deletions storybook/stories/Text/Text.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ storiesOf("Text", module)
{getStory()}
</View>
))
.add("Title", () => {
.add("Default", () => {
return (
<View className="w-full px-4">
<Text
type="title"
text={text("text", "This is a title")}
text={text("text", "This is an example of body text")}
textColor={select(
"text color",
["text-black", "text-blue-600", "text-red-600", "text-gray-400"],
Expand All @@ -44,11 +43,12 @@ storiesOf("Text", module)
</View>
);
})
.add("Body", () => {
.add("Title", () => {
return (
<View className="w-full px-4">
<Text
text={text("text", "This is an example of body text")}
type="title"
text={text("text", "This is a title")}
textColor={select(
"text color",
["text-black", "text-blue-600", "text-red-600", "text-gray-400"],
Expand Down
1 change: 1 addition & 0 deletions storybook/stories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ import "./Button/Button.stories";
import "./Loader/Loader.stories";
import "./ItemSeparator/ItemSeparator.stories";
import "./TextInput/TextInput.stories";
import "./Card/Card.stories";
import "./Text/Text.stories";