Skip to content

Commit

Permalink
correcciones - crear componente button
Browse files Browse the repository at this point in the history
  • Loading branch information
Damian Morales committed May 31, 2024
1 parent 3e23eb7 commit fe652c1
Show file tree
Hide file tree
Showing 13 changed files with 114 additions and 182 deletions.
30 changes: 27 additions & 3 deletions App.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,33 @@
/* eslint-disable react-native/no-inline-styles */
import React from 'react';
import {View, Text} from 'react-native';
import {View} from 'react-native';
import Button from './src/components/Button';

const App = () => (
<View>
<Text>UI native</Text>
<View style={{padding: 20, flexDirection: 'row'}}>
<Button
value="Box Container"
icon="box"
type={'main'}
variant={'outlined'}
color="warning"
isLoading={false}
// iconPosition={'right'}
disabled={false}
style={{flex: 1}}
/>
<Button
value="Button with Box Icon"
icon="box"
isLoading={false}
type={'main'}
variant={'outlined'}
color={'primary'}
// iconPosition="right"
disabled={false}
style={{flex: 1}}
// iconStyle={{color: 'red', fontSize: 30}}
/>
</View>
);

Expand Down
7 changes: 3 additions & 4 deletions src/components/BaseButton/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ describe('BaseButton Component', () => {
});
});

describe('render correctly when has minimum props needed, example', () => {
describe('render correctly when has minimum props needed', () => {
it('when hasnt minimum props needed', () => {
const {toJSON} = create(
const {root} = create(
<BaseButton borderRadius={validData.borderRadius}>{validData.children}</BaseButton>
);

expect(toJSON()).toBeTruthy();
expect(root).toBeTruthy();
});
});
});
17 changes: 15 additions & 2 deletions src/components/BaseButton/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@ import {moderateScale, scaledForDevice} from '../../scale';
export interface BaseButtonProps extends PressableProps {
borderRadius?: number;
children?: ReactNode | null;
pressedStyle?: ReactNode;
style?: any;
}

const BaseButton: FC<BaseButtonProps> = ({borderRadius = 0, children = null, style, ...props}) => {
const BaseButton: FC<BaseButtonProps> = ({
borderRadius = 0,
children = null,
style,
pressedStyle,
...props
}) => {
if (!children) {
return null;
}
Expand All @@ -24,7 +31,13 @@ const BaseButton: FC<BaseButtonProps> = ({borderRadius = 0, children = null, sty
});

return (
<Pressable style={[styles.container, style]} {...props}>
<Pressable
style={({pressed}) => [
styles.container,
style,
pressed && /* istanbul ignore next */ pressedStyle,
]}
{...props}>
{children}
</Pressable>
);
Expand Down
19 changes: 0 additions & 19 deletions src/components/Button/constants/index.ts

This file was deleted.

26 changes: 0 additions & 26 deletions src/components/Button/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React from 'react';
import {create} from 'react-test-renderer';
import {Text} from 'react-native';
import Icon from '../Icon';
import BaseButton from '../BaseButton';
import Loading from '../Loading';
import Button from './';

Expand All @@ -12,8 +11,6 @@ const validData = {
icon: 'box',
};

const setIsPressed = jest.fn();
const spyUseState = jest.spyOn(React, 'useState');
jest.spyOn(React, 'useEffect').mockImplementation((f) => f());
jest.mock('react-native/Libraries/Animated/NativeAnimatedHelper');

Expand All @@ -35,29 +32,6 @@ describe('Button component', () => {
expect(name).toEqual(validData.icon);
});

it('when it is pressed', () => {
spyUseState.mockReturnValueOnce([false, setIsPressed]);

const {root} = create(
<Button
type={'main'}
variant={'contained'}
color={'primary'}
value="Button Test"
icon="box"
iconPosition={'left'}
/>
);

const ButtonComp = root.findByType(BaseButton);
const {onPressIn, onPressOut} = ButtonComp.props;

onPressIn();
onPressOut();

expect(setIsPressed).toBeCalledTimes(2);
});

it('when isLoading is true, show an loading spinner', () => {
const {root} = create(<Button icon={validData.icon} isLoading={validData.isLoading} />);
const LoadingComp = root.findByType(Loading);
Expand Down
42 changes: 12 additions & 30 deletions src/components/Button/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React, {FC, useCallback, useState} from 'react';
import React, {FC} from 'react';
import {ViewStyle, StyleSheet, TextStyle, View} from 'react-native';
import BaseButton, {BaseButtonProps} from '../BaseButton';
import getButtonStyles from './utils/getButtonStyles';
import Loading from '../Loading';
import Text from '../Text';
import Icon from '../Icon';
import {getMixedButtonStyles} from './utils';

export const types = {
main: 'main',
Expand All @@ -19,7 +19,7 @@ export const variant = {

export const color = {
primary: 'primary',
secondary: 'secondary',
black: 'black',
success: 'success',
error: 'error',
warning: 'warning',
Expand Down Expand Up @@ -55,10 +55,9 @@ interface ButtonProps extends BaseButtonProps {
iconPosition?: keyIconPosition;
disabled?: boolean;
style?: ViewStyle;
pressedStyle?: ViewStyle;
iconStyle?: TextStyle;
textStyle?: TextStyle;
onPressIn?: () => void;
onPressOut?: () => void;
}

const Button: FC<ButtonProps> = ({
Expand All @@ -67,46 +66,30 @@ const Button: FC<ButtonProps> = ({
color = 'primary',
iconPosition = 'left',
isLoading = false,
value = 'Button',
value = '',
icon = null,
disabled = false,
style,
iconStyle,
textStyle,
onPressIn = () => {},
onPressOut = () => {},
...props
}) => {
const [isPressed, setIsPressed] = useState<Boolean>(false);
const validDisabled = disabled || isLoading;
const hasIconAndText = !!icon && !!value;
const borderRadius = variant === 'text' ? 6 : 50;

const params = {
const buttonStyles = getButtonStyles({
type,
variant,
color,
iconPosition,
isLoading,
isPressed,
disabled,
hasIconAndText,
};
const buttonStyle = getMixedButtonStyles(params);
const styles = StyleSheet.create(buttonStyle);

const handleOnPressIn = useCallback(() => {
setIsPressed(true);
onPressIn();
}, [onPressIn]);
});
const styles = StyleSheet.create(buttonStyles);

const handleOnPressOut = useCallback(() => {
setIsPressed(false);
onPressOut();
}, [onPressOut]);

const LoadingCompontent = (
<Loading isLoading={isLoading} color={buttonStyle.loadingColor} size={24} />
);
const LoadingCompontent = <Loading isLoading={isLoading} color={styles.loadingColor} size={24} />;

const WrapperComponent = (
<View style={styles.direction}>
Expand All @@ -118,10 +101,9 @@ const Button: FC<ButtonProps> = ({
return (
<BaseButton
style={[styles.container, style]}
onPressIn={handleOnPressIn}
onPressOut={handleOnPressOut}
disabled={disabled || isLoading}
pressedStyle={!validDisabled && styles.pressed}
borderRadius={borderRadius}
disabled={validDisabled}
{...props}>
{isLoading ? LoadingCompontent : WrapperComponent}
</BaseButton>
Expand Down
34 changes: 0 additions & 34 deletions src/components/Button/theme/index.ts

This file was deleted.

6 changes: 2 additions & 4 deletions src/components/Button/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type {keyColor, keyIconPosition, keyType, keyVariant} from '../';

export interface SelectedColor {
light: string;
main: string;
dark: string;
}
Expand All @@ -12,21 +11,20 @@ export interface Params {
color: keyColor;
iconPosition: keyIconPosition;
isLoading: Boolean;
isPressed: Boolean;
disabled: Boolean;
hasIconAndText?: Boolean;
}

export interface ReturnStyles {
container: object;
direction: object;
pressed: object;
icon: object;
text: object;
loadingColor: any;
}

export interface ContainerStyle {
isPressed: Boolean;
disabled: Boolean;
isLoading: Boolean;
color: keyColor;
Expand All @@ -41,7 +39,7 @@ export interface TextStyle {
color: keyColor;
disabled: Boolean;
isLoading: Boolean;
isPressed: Boolean;

hasIconAndText?: Boolean;
iconPosition: keyIconPosition;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import {getMixedButtonStyles} from '.';
import getButtonStyles from './';

const validParams = {
type: 'secondary',
variant: 'outlined',
color: 'secondary',
iconPosition: 'top',
isLoading: true,
isPressed: true,
disabled: true,
hasIconAndText: true,
};
Expand All @@ -17,15 +16,14 @@ const failedParams = {
color: undefined,
iconPosition: undefined,
isLoading: false,
isPressed: false,
disabled: false,
hasIconAndText: false,
};

describe('getMixedButtonStyles util', () => {
it('return styles when params are passed correclty', () => {
// @ts-ignore
const response = getMixedButtonStyles(validParams);
const response = getButtonStyles(validParams);
expect(response).toEqual({
container: {
borderWidth: 1,
Expand All @@ -34,6 +32,7 @@ describe('getMixedButtonStyles util', () => {
paddingHorizontal: 16.5,
paddingVertical: 21,
},
pressed: {borderColor: '#2979FF', backgroundColor: '#fff'},
direction: {
justifyContent: 'center',
alignItems: 'center',
Expand All @@ -46,13 +45,13 @@ describe('getMixedButtonStyles util', () => {
fontSize: 29,
},
icon: {fontWeight: '500', textAlign: 'center', color: '#C4C6CC'},
loadingColor: '#2F2F2F',
loadingColor: '#2979FF',
});
});

it('returns default styles when params is not correct or no exist', () => {
// @ts-ignore
const response = getMixedButtonStyles(failedParams);
const response = getButtonStyles(failedParams);
expect(response).toEqual({
container: {
borderWidth: 1,
Expand All @@ -61,6 +60,7 @@ describe('getMixedButtonStyles util', () => {
paddingHorizontal: 16.5,
height: 104,
},
pressed: {borderColor: 'transparent', backgroundColor: '#1759FF'},
direction: {
justifyContent: 'center',
alignItems: 'center',
Expand Down
Loading

0 comments on commit fe652c1

Please sign in to comment.