Skip to content

Enhancement/button custom style #15

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 1 commit into from
Sep 28, 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
30 changes: 22 additions & 8 deletions src/components/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,41 @@ const Button: React.FC<ButtonType>= ({
isLoading = false,
leftIcon = null,
rightIcon = null,
customButtonStyle = null,
customTextStyle = null
}) => {
const generateButtonStyle = (borderColor: { backgroundColor?: string | undefined} | undefined, type: string): object => {
if(customButtonStyle){
return customButtonStyle
}
if (type === 'outlined') {
return {...style.baseButton, borderColor: borderColor?.backgroundColor, ...style.outlinedButton}
} else if (type === 'text'){
}
if (type === 'text'){
return {...style.baseButton}
} else {
return {...style.baseButton, ...borderColor}
}
}

return {...style.baseButton, ...borderColor}
}

const generatedStyle = generateButtonStyle(buttonColor?.[0], type);

const textStyle = customTextStyle ? customTextStyle : textColor;

return (
<PressableOpacity onPress={onPress} disabled={disabled} isLoading={isLoading}>
<PressableOpacity
onPress={onPress}
disabled={disabled}
isLoading={isLoading}
customStyle={customButtonStyle}>
<View style={generatedStyle}>
<View style={style.iconContainer}>
{leftIcon && leftIcon}
</View>
<View>
{isLoading ?
<Loader color={textColor?.[0]?.color}/> :
<Text style={textColor}>{label}</Text>
<Text style={textStyle}>{label}</Text>
}
</View>
<View style={style.iconContainer}>
Expand All @@ -60,7 +73,9 @@ export default styled(Button, {
props: {
buttonColor: true,
textColor: true,
type: true
type: true,
customButtonStyle: true,
customTextStyle: true,
}
});

Expand All @@ -81,7 +96,6 @@ const style = StyleSheet.create({
},
iconContainer: {
width: 52,
height: 52,
},

})
2 changes: 2 additions & 0 deletions src/components/Button/Button.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ export interface ButtonType {
isLoading?: boolean;
leftIcon?: React.ReactNode;
rightIcon?: React.ReactNode;
customButtonStyle?: object;
customTextStyle?: object;
}
18 changes: 11 additions & 7 deletions src/components/PressableOpacity/PressableOpacity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import {
import useAnimation from "./useAnimation";
import { PressableOpacityTypes } from "./PressableOpacity.types";

const PressableOpacity = ({ children, isLoading, ...props }: PressableOpacityTypes) => {
const PressableOpacity = ({
children,
isLoading,
disabled,
customStyle
}: PressableOpacityTypes) => {
const {
fadeIn,
fadeOut,
Expand All @@ -18,21 +23,20 @@ const PressableOpacity = ({ children, isLoading, ...props }: PressableOpacityTyp

return (
<Pressable
disabled={disabled}
onPressIn={fadeIn}
onPressOut={fadeOut}
style={style.pressableContainer}
{...props}>
style={[style.pressableContainer, customStyle]}>
{children}
{!isLoading && <Animated.View
style={[style.animatedView, {opacity: props?.disabled ? .4 : opacityValue}]} />
style={[style.animatedView, {opacity: disabled ? .6 : opacityValue}]} />
}
</Pressable>
);
};

const style = StyleSheet.create({
pressableContainer: {
width: '100%',
position: 'relative',
justifyContent: 'center',
alignItems: 'center',
Expand All @@ -41,8 +45,8 @@ const style = StyleSheet.create({
position: 'absolute',
left: 0,
top: 0,
width: '100%',
height: '100%',
right: 0,
bottom: 0,
backgroundColor: 'gray',
borderRadius: 4
}
Expand Down
1 change: 1 addition & 0 deletions src/components/PressableOpacity/PressableOpacity.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ import { PressableProps } from 'react-native';
export interface PressableOpacityTypes extends PressableProps {
children: React.ReactNode;
isLoading?: boolean;
customStyle?: object | null;
}
2 changes: 1 addition & 1 deletion src/components/PressableOpacity/useAnimation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from "react";
import { Animated } from "react-native";

const FADE_IN_ANIMATION_CONFIG = {
toValue: .7,
toValue: .3,
duration: 100,
useNativeDriver: false,
};
Expand Down
15 changes: 15 additions & 0 deletions storybook/stories/Button/Button.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,18 @@ storiesOf("Buttons", module)
</View>
)}
)
.add("Custom Button", () => {
return (
<View className="w-full px-4">
<Button
label={(text("label", "Button Label"))}
onPress={action("clicked-text")}
buttonColor="bg-green-700"
textColor="text-white"
disabled={boolean("Disabled?", false)}
isLoading={boolean("Is Loading?", false)}
customButtonStyle="rounded-full px-2 py-1 bg-purple-200 w-32 justify-center items-center"
customTextStyle="text-black"/>
</View>
)}
)