Skip to content

Commit

Permalink
Merge pull request #94 from indec-it/feat/addWrapComponent
Browse files Browse the repository at this point in the history
feat: add warp component
  • Loading branch information
maximilianoforlenza authored Jul 3, 2022
2 parents 1ad4788 + 1fa24bb commit 97f2b6c
Show file tree
Hide file tree
Showing 13 changed files with 185 additions and 41 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@indec/react-commons",
"version": "5.3.2",
"version": "5.4.0",
"description": "Common reactjs components for apps",
"private": false,
"main": "index.js",
Expand Down
8 changes: 7 additions & 1 deletion src/components/Header/Header.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const Header = ({
redirectTo,
onLogout,
user,
attributes,
options,
hiddenTop,
activePath,
Expand Down Expand Up @@ -117,7 +118,7 @@ const Header = ({
variant="unstyled"
{...userStyles}
/>
<UserMenu user={user} onLogout={handleLogout}/>
<UserMenu user={user} attributes={attributes} onLogout={handleLogout}/>
</Menu>
)}
</HStack>
Expand All @@ -130,6 +131,10 @@ Header.propTypes = {
redirectTo: PropTypes.func.isRequired,
onLogout: PropTypes.func.isRequired,
user: PropTypes.shape({}),
attributes: PropTypes.arrayOf(PropTypes.shape({
key: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
label: PropTypes.oneOfType([PropTypes.string, PropTypes.number])
})),
options: PropTypes.arrayOf(PropTypes.shape({})),
activePath: PropTypes.string,
hiddenUserMenu: PropTypes.bool,
Expand All @@ -147,6 +152,7 @@ Header.propTypes = {
Header.defaultProps = {
user: {},
options: [],
attributes: [],
logos: [],
activePath: undefined,
hiddenUserMenu: false,
Expand Down
7 changes: 6 additions & 1 deletion src/components/Header/Header.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,10 @@ Basic.args = {
roleName: 'Admin',
stateName: 'Buenos Aires',
documentId: 21554378
}
},
attributes: [
{key: 'documentId', label: 'Documento'},
{key: 'stateName', label: 'Provincia'},
{key: 'roleName', label: 'Rol'}
]
};
47 changes: 15 additions & 32 deletions src/components/Header/UserMenu/UserMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ import React from 'react';
import PropTypes from 'prop-types';
import {FaPowerOff} from 'react-icons/fa';
import {
MenuDivider,
MenuItem,
MenuList,
Text
MenuDivider, MenuItem, MenuList, Text
} from '@chakra-ui/react';

const UserMenu = ({user, onLogout}) => (
<MenuList alignItems="center" mr={1}>
const UserMenu = ({user, attributes, onLogout}) => (
<MenuList alignItems="center" mr={1} data-testid="user-menu" isOpen>
{(user.name && user.surname) && (
<MenuItem>
<MenuItem data-testid="attribute-name">
<Text
id="logout"
display="flex"
Expand All @@ -28,30 +25,14 @@ const UserMenu = ({user, onLogout}) => (
</Text>
</MenuItem>
)}
{user.roleName && (
<MenuItem>
{attributes.map(attribute => (
<MenuItem key={attribute.key} data-testid={`attribute-${attribute.key}`}>
<Text fontSize="14px" color="brand.neutral300">
<span>Role:</span>
{user.roleName}
<span>{`${attribute.label}:`}</span>
{user[attribute.key]}
</Text>
</MenuItem>
)}
{user.stateName && (
<MenuItem>
<Text fontSize="14px" color="brand.neutral300">
<span>Provincia:</span>
{user.stateName}
</Text>
</MenuItem>
)}
{user.documentId && (
<MenuItem>
<Text fontSize="14px" color="brand.neutral300">
<span>DNI:</span>
{user.documentId}
</Text>
</MenuItem>
)}
))}
<MenuDivider/>
{onLogout && (
<MenuItem onClick={onLogout} data-testid="sign-out">
Expand All @@ -76,15 +57,17 @@ const UserMenu = ({user, onLogout}) => (
UserMenu.propTypes = {
user: PropTypes.shape({
name: PropTypes.string,
surname: PropTypes.string,
roleName: PropTypes.string,
stateName: PropTypes.string,
documentId: PropTypes.oneOfType([PropTypes.number, PropTypes.string])
surname: PropTypes.string
}),
attributes: PropTypes.arrayOf(PropTypes.shape({
key: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
label: PropTypes.oneOfType([PropTypes.string, PropTypes.number])
})),
onLogout: PropTypes.func
};

UserMenu.defaultProps = {
attributes: [],
user: {},
onLogout: undefined
};
Expand Down
99 changes: 99 additions & 0 deletions src/components/Header/UserMenu/UserMenu.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import React from 'react';
import {Menu} from '@chakra-ui/react';
import {
fireEvent, getByTestId, getByText, queryByTestId
} from '@testing-library/react';

import UserMenu from './UserMenu';

const Component = props => (
<Menu><UserMenu {...props}/></Menu>
);

describe('UserMenu', () => {
let props;
const getComponent = newProps => render(Component, newProps);
beforeEach(() => {
props = {};
});
afterEach(tearDown);

it('should not render name and surname if not defined', () => {
const {container} = getComponent(props);
expect(queryByTestId(container, 'attribute-name')).toBeNull();
});

describe('when `user.name` and `user.surname` is defined', () => {
beforeEach(() => {
props.user = {name: 'John', surname: 'Doe'};
});

it('should display `Nombre:`', () => {
const {container} = getComponent(props);
expect(getByText(container, 'Nombre:')).toBeInTheDocument();
});

it('should display `user.name`', () => {
const {container} = getComponent(props);
expect(getByText(container, 'John')).toBeInTheDocument();
});

it('should display `user.surname`', () => {
const {container} = getComponent(props);
expect(getByText(container, 'Doe')).toBeInTheDocument();
});
});

describe('when `props.onLogout` is defined', () => {
beforeEach(() => {
props.onLogout = jest.fn();
});

it('should display `Cerrar sesion`', () => {
const {container} = getComponent(props);
expect(getByText(container, 'Cerrar sesión')).toBeInTheDocument();
});

describe('when the button to sign out is clicked', () => {
beforeEach(() => {
const {container} = getComponent(props);
const button = getByTestId(container, 'sign-out');
fireEvent.click(button);
});

it('should fire `props.onLogout`', () => {
expect(props.onLogout).toHaveBeenCalled();
});
});
});

describe('when `props.onLogout` is not defined', () => {
beforeEach(() => {
props.onLogout = undefined;
});

it('should not render a button to sign out', () => {
const {container} = getComponent(props);
expect(queryByTestId(container, 'sign-out')).toBeNull();
});
});

describe('when `props.attributes` is defined', () => {
beforeEach(() => {
props.attributes = [
{key: 'username', label: 'Username'},
{key: 'documentId', label: 'DocumentId'},
{key: 'state', label: 'State'}
];
props.user = {username: 'test', documentId: '12345678', state: 'Buenos Aires'};
});

it('should render the attributes list', () => {
const {container} = getComponent(props);
props.attributes.forEach(attribute => {
expect(getByText(container, `${attribute.label}:`)).toBeInTheDocument();
expect(getByText(container, props.user[attribute.key])).toBeInTheDocument();
});
});
});
});
6 changes: 2 additions & 4 deletions src/components/SelectInput/getStyles.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const getStyles = (colors, variant, styles, size = 'md', isDisabled, hasError, r
const invalidBorderColor = getThemeColor(colors, fieldStyles._invalid?.borderColor || borderColor);

const newStyles = removeKeys(fieldStyles, KEYS);
const disabledStyles = isDisabled ? {...(fieldStyles._disabled || {}), borderColor: disabledBorderColor} : {};

return {
container: (provider, state) => {
Expand All @@ -42,17 +43,14 @@ const getStyles = (colors, variant, styles, size = 'md', isDisabled, hasError, r
...hoverStyles,
...hoverBackgroundStyles
},
'&:disabled': {
...(fieldStyles._disabled || {}),
borderColor: disabledBorderColor
},
boxShadow: state.isFocused ? focusBoxShadowColor : fieldStyles.boxShadow,
backgroundColor: getThemeColor(colors, fieldStyles.bg),
paddingInlineEnd: 0,
borderRadius: getThemeSize(fieldStyles.borderRadius, true),
...focusStyles,
...invalidStyles,
...readOnlyStyles,
...disabledStyles,
width: '100%',
transitionProperty: 'background-color,border-color,color,fill,stroke,opacity,box-shadow,transform',
transitionDuration: '200ms',
Expand Down
22 changes: 22 additions & 0 deletions src/components/Wrap/Wrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from 'react';
import PropTypes from 'prop-types';
import {Wrap as ChakraWrap, useStyleConfig} from '@chakra-ui/react';

import {childrenPropTypes} from '@/utils/propTypes';

const Wrap = ({variant, children, ...rest}) => {
const styles = useStyleConfig('Wrap', {variant});
return <ChakraWrap data-testid="wrap" {...styles} {...rest}>{children}</ChakraWrap>;
};

Wrap.propTypes = {
variant: PropTypes.string,
children: childrenPropTypes
};

Wrap.defaultProps = {
variant: undefined,
children: undefined
};

export default Wrap;
14 changes: 14 additions & 0 deletions src/components/Wrap/Wrap.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {getByTestId} from '@testing-library/react';

import {Wrap} from '@/components';

describe('<Wrap>', () => {
let props;
const getComponent = () => render(Wrap, props);
afterEach(tearDown);

it('should render Wrap component', () => {
const {container} = getComponent();
expect(getByTestId(container, 'wrap')).toBeInTheDocument();
});
});
3 changes: 3 additions & 0 deletions src/components/Wrap/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Wrap from './Wrap';

export default Wrap;
1 change: 1 addition & 0 deletions src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ export {default as SelectInput} from './SelectInput';
export {default as Table} from './Table';
export {default as Textarea} from './Textarea';
export {default as TextInput} from './TextInput';
export {default as Wrap} from './Wrap';
1 change: 1 addition & 0 deletions src/theme/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export {default as Quote} from './quote';
export {default as Table} from './table';
export {default as Text} from './text';
export {default as Textarea} from './textarea';
export {default as Wrap} from './wrap';
10 changes: 10 additions & 0 deletions src/theme/components/wrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const Wrap = {
baseStyle: {
overflow: 'initial',
align: 'center',
w: '100%',
spacing: 3
}
};

export default Wrap;
6 changes: 4 additions & 2 deletions src/theme/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
colors,
global,
Table,
Textarea
Textarea,
Wrap
} from './components';

export const theme = {
Expand All @@ -34,7 +35,8 @@ export const theme = {
FormLabel,
Text,
Box,
Table
Table,
Wrap
}
};

Expand Down

0 comments on commit 97f2b6c

Please sign in to comment.