Skip to content

Commit

Permalink
correccion 1 - crear componente button
Browse files Browse the repository at this point in the history
  • Loading branch information
Damian Morales committed Apr 17, 2024
1 parent 9bbb575 commit 1281fa1
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 69 deletions.
14 changes: 5 additions & 9 deletions src/components/Button/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@ import {Text} from 'react-native';
import Icon from '../Icon';
import BaseButton from '../BaseButton';
import Loading from '../Loading';
import Button, {Color, IconPosition, Type, Variant} from './';
import Button from './';

const validData = {
type: Type.Main,
variant: Variant.Contained,
color: Color.Primary,
isLoading: true,
value: 'Button Test',
icon: 'box',
iconPosition: IconPosition.Left,
};

const setIsPressed = jest.fn();
Expand Down Expand Up @@ -44,12 +40,12 @@ describe('Button component', () => {

const {root} = create(
<Button
type={Type.Main}
variant={Variant.Contained}
color={Color.Primary}
type={'main'}
variant={'contained'}
color={'primary'}
value="Button Test"
icon="box"
iconPosition={IconPosition.Left}
iconPosition={'left'}
/>
);

Expand Down
74 changes: 43 additions & 31 deletions src/components/Button/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,53 @@ import Text from '../Text';
import Icon from '../Icon';
import {getMixedButtonStyles} from './utils';

export enum Type {
Main = 'main',
Secondary = 'secondary',
}
export const types = {
main: 'main',
secondary: 'secondary',
};

export enum Variant {
Contained = 'contained',
Outlined = 'outlined',
Text = 'text',
}
export const variant = {
contained: 'contained',
outlined: 'outlined',
text: 'text',
};

export enum Color {
Primary = 'primary',
Secondary = 'secondary',
Success = 'success',
Error = 'error',
Warning = 'warning',
Alert = 'alert',
}
export const color = {
primary: 'primary',
secondary: 'secondary',
success: 'success',
error: 'error',
warning: 'warning',
alert: 'alert',
};

export enum IconPosition {
Left = 'left',
Right = 'right',
Top = 'top',
Bottom = 'bottom',
}
export const iconPosition = {
top: 'top',
bottom: 'bottom',
left: 'left',
right: 'right',
};

export type buttonType = typeof types;
export type keyType = keyof buttonType;

export type buttonVariant = typeof variant;
export type keyVariant = keyof buttonVariant;

export type buttonColor = typeof color;
export type keyColor = keyof buttonColor;

export type buttonIconPosition = typeof iconPosition;
export type keyIconPosition = keyof buttonIconPosition;

interface ButtonProps extends BaseButtonProps {
type?: Type;
variant?: Variant;
color?: Color;
type?: keyType;
variant?: keyVariant;
color?: keyColor;
isLoading?: boolean;
value?: string | null;
icon?: string;
iconPosition?: IconPosition;
iconPosition?: keyIconPosition;
disabled?: boolean;
style?: ViewStyle;
iconStyle?: TextStyle;
Expand All @@ -50,10 +62,10 @@ interface ButtonProps extends BaseButtonProps {
}

const Button: FC<ButtonProps> = ({
type = Type.Main,
variant = Variant.Contained,
color = Color.Primary,
iconPosition = IconPosition.Left,
type = 'main',
variant = 'contained',
color = 'primary',
iconPosition = 'left',
isLoading = false,
value = 'Button',
icon = null,
Expand Down
2 changes: 1 addition & 1 deletion src/components/Button/theme/configs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export const colorConfig = (selectedColor: SelectedColor) => {
};
};

export const stlyeConfig = {
export const styleConfig = {
container: {
height: {
main: scaledForDevice(50, moderateScale),
Expand Down
30 changes: 15 additions & 15 deletions src/components/Button/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {Color, IconPosition, Type, Variant} from '../';
import type {keyColor, keyIconPosition, keyType, keyVariant} from '../';

export interface SelectedColor {
light: string;
Expand All @@ -7,10 +7,10 @@ export interface SelectedColor {
}

export interface Params {
type: Type;
variant: Variant;
color: Color;
iconPosition: IconPosition;
type: keyType;
variant: keyVariant;
color: keyColor;
iconPosition: keyIconPosition;
isLoading: Boolean;
isPressed: Boolean;
disabled: Boolean;
Expand All @@ -29,27 +29,27 @@ export interface ContainerStyle {
isPressed: Boolean;
disabled: Boolean;
isLoading: Boolean;
color: Color;
variant: Variant;
type: Type;
iconPosition: IconPosition;
color: keyColor;
variant: keyVariant;
type: keyType;
iconPosition: keyIconPosition;
}

export interface TextStyle {
type: Type;
variant: Variant;
color: Color;
type: keyType;
variant: keyVariant;
color: keyColor;
disabled: Boolean;
isLoading: Boolean;
isPressed: Boolean;
hasIconAndText?: Boolean;
iconPosition: IconPosition;
iconPosition: keyIconPosition;
}

export interface LoadingStyle {
color: Color;
color: keyColor;
}

export interface DirectionStyle {
iconPosition: IconPosition;
iconPosition: keyIconPosition;
}
10 changes: 5 additions & 5 deletions src/components/Button/utils/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import {getMixedButtonStyles} from '.';
import {Color, IconPosition, Type, Variant} from '..';

const validParams = {
type: Type.Secondary,
variant: Variant.Outlined,
color: Color.Secondary,
iconPosition: IconPosition.Top,
type: 'secondary',
variant: 'outlined',
color: 'secondary',
iconPosition: 'top',
isLoading: true,
isPressed: true,
disabled: true,
Expand All @@ -25,6 +24,7 @@ const failedParams = {

describe('getMixedButtonStyles util', () => {
it('return styles when params are passed correclty', () => {
// @ts-ignore
const response = getMixedButtonStyles(validParams);
expect(response).toEqual({
container: {
Expand Down
10 changes: 5 additions & 5 deletions src/components/Button/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {moderateScale, scaledForDevice} from '../../../scale';
import {colorConfig, stlyeConfig} from '../theme/configs';
import {colorConfig, styleConfig} from '../theme/configs';
import {themeColors} from '../theme';
import {
defaultColor,
Expand Down Expand Up @@ -32,7 +32,7 @@ const containerStyle = ({
const selectedColor = themeColors[color] || themeColors[defaultColor];
const {main, pressed, disabled} = colorConfig(selectedColor);

const {container} = stlyeConfig;
const {container} = styleConfig;

const validType = validTypes.includes(type) ? type : defaultType;
const validVariant = validVariants.includes(variant) ? variant : defaultVariant;
Expand Down Expand Up @@ -68,7 +68,7 @@ const containerStyle = ({
};

const directionWrapperStyle = ({iconPosition}: DirectionStyle) => {
const {directionWrapper} = stlyeConfig;
const {directionWrapper} = styleConfig;

const flexCenter = directionWrapper.center;

Expand All @@ -94,7 +94,7 @@ const baseTextStyle = ({
const selectedColor = themeColors[color] || themeColors[defaultColor];
const {main, pressed, disabled} = colorConfig(selectedColor);

const {text: textStyle} = stlyeConfig;
const {text: textStyle} = styleConfig;

const validType = validTypes.includes(type) ? type : defaultType;
const validVariant = validVariants.includes(variant) ? variant : defaultVariant;
Expand All @@ -118,7 +118,7 @@ const textStyle = (params: TextStyle) => ({

const iconStyle = (params: TextStyle) => {
const {hasIconAndText, iconPosition} = params;
const {icon} = stlyeConfig;
const {icon} = styleConfig;

return {
...baseTextStyle(params),
Expand Down
6 changes: 3 additions & 3 deletions src/components/Select/Components/Modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {Modal as ModalComponent, StyleSheet, View} from 'react-native';
import {base} from '../../../../theme/palette';
import {DropdownProps} from '../Dropdown';
import {moderateScale, scaledForDevice} from '../../../../scale';
import Button, {Type, Variant} from '../../../Button';
import Button from '../../../Button';

interface ModalProps extends DropdownProps {
isMulti: boolean;
Expand Down Expand Up @@ -63,8 +63,8 @@ const Modal: FC<ModalProps> = ({show, setShow, isMulti, modalAcceptText, childre
<View style={styles.buttonWrapper}>
<Button
value={modalAcceptText}
variant={Variant.Text}
type={Type.Secondary}
variant={'text'}
type={'secondary'}
textStyle={styles.buttonText}
onPress={() => setShow(false)}
/>
Expand Down

0 comments on commit 1281fa1

Please sign in to comment.