Skip to content

Commit

Permalink
feat(bar): make Bar 'size' prop accept %, px
Browse files Browse the repository at this point in the history
  • Loading branch information
arturbien committed Apr 9, 2020
1 parent cd2bbc6 commit 4286c3a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
10 changes: 5 additions & 5 deletions src/components/Bar/Bar.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import React from 'react';
import propTypes from 'prop-types';
import styled from 'styled-components';
import { blockSizes } from '../common/system';

const StyledBar = styled.div`
display: inline-block;
box-sizing: border-box;
height: ${props => blockSizes[props.size]};
height: ${({ size }) => size};
width: 5px;
border-top: 2px solid ${({ theme }) => theme.borderLightest};
border-left: 2px solid ${({ theme }) => theme.borderLightest};
Expand All @@ -17,16 +16,17 @@ const StyledBar = styled.div`
// TODO: add horizontal variant
// TODO: allow user to specify number of bars (like 3 horizontal bars for drag handle)
const Bar = React.forwardRef(function Bar(props, ref) {
const { size, ...otherProps } = props;
const { size: sizeProp, ...otherProps } = props;
const size = typeof sizeProp === 'number' ? `${sizeProp}px` : sizeProp;

return <StyledBar size={size} ref={ref} {...otherProps} />;
});

Bar.defaultProps = {
size: 'md'
size: '100%'
};
Bar.propTypes = {
size: propTypes.oneOf(['sm', 'md', 'lg'])
size: propTypes.oneOf([propTypes.string, propTypes.number])
};

export default Bar;
36 changes: 20 additions & 16 deletions src/components/Bar/Bar.spec.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,18 @@
import React from 'react';
import { render } from '@testing-library/react';
import { blockSizes } from '../common/system';
import { renderWithTheme } from '../../../test/utils';

import Bar from './Bar';

describe('<Bar />', () => {
it('should render bar', () => {
const { container } = render(<Bar />);
const { container } = renderWithTheme(<Bar />);
const barEl = container.firstChild;

expect(barEl).toBeInTheDocument();
});

it('should handle bar with correct size', () => {
const { container, rerender } = render(<Bar size='sm' />);
const barEl = container.firstChild;

expect(barEl).toHaveStyleRule('height', blockSizes.sm);

rerender(<Bar size='lg' />);

expect(barEl).toHaveStyleRule('height', blockSizes.lg);
});

it('should handle custom style', () => {
const { container } = render(
const { container } = renderWithTheme(
<Bar style={{ backgroundColor: 'papayawhip' }} />
);
const barEl = container.firstChild;
Expand All @@ -34,9 +22,25 @@ describe('<Bar />', () => {

it('should handle custom props', () => {
const customProps = { title: 'potatoe' };
const { container } = render(<Bar {...customProps} />);
const { container } = renderWithTheme(<Bar {...customProps} />);
const barEl = container.firstChild;

expect(barEl).toHaveAttribute('title', 'potatoe');
});

describe('prop: size', () => {
it('should set proper size', () => {
const { container } = renderWithTheme(<Bar size='85%' />);
const avatarEl = container.firstChild;

expect(avatarEl).toHaveStyleRule('height', '85%');
});

it('when passed a number, sets size in px', () => {
const { container } = renderWithTheme(<Bar size={25} />);
const avatarEl = container.firstChild;

expect(avatarEl).toHaveStyleRule('height', '25px');
});
});
});
4 changes: 2 additions & 2 deletions src/components/Bar/Bar.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ storiesOf('Bar', module)
.add('default', () => (
<AppBar>
<Toolbar>
<Bar />
<Bar size={35} />
<Button variant='menu'>Edit</Button>
<Button variant='menu' disabled>
Save
</Button>
<Bar />
<Bar size={35} />
</Toolbar>
</AppBar>
));

0 comments on commit 4286c3a

Please sign in to comment.