Skip to content

Commit

Permalink
Add <AreaUnits /> component
Browse files Browse the repository at this point in the history
Component for formatting a size and it's relevant units correctly
  • Loading branch information
Richard Palmer committed Mar 2, 2017
1 parent 1959b45 commit efe62cd
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
38 changes: 38 additions & 0 deletions components/AreaUnits/AreaUnits.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React, { PropTypes } from 'react';

import { AREA_UNITS } from '../../constants/units';

const AreaUnits = ({ value, unit, className, ...rest }) => {
switch (unit) {
case AREA_UNITS.METERS_SQUARED: return (
<span
{ ...rest }
className={ className }
>
{ value }
m<sup>2</sup>
</span>
);
case AREA_UNITS.SQUARE_FOOT:
default: return (
<span
{ ...rest }
className={ className }
>
{ value } sq ft
</span>
);
}
};

AreaUnits.propTypes = {
value: PropTypes.number.isRequired,
unit: PropTypes.string.isRequired,
className: PropTypes.string,
};

AreaUnits.defaultProps = {
unit: AREA_UNITS.SQUARE_FOOT,
};

export default AreaUnits;
19 changes: 19 additions & 0 deletions components/AreaUnits/AreaUnits.story.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from 'react';
import { storiesOf } from '@kadira/storybook';
import AreaUnits from './AreaUnits';

import { AREA_UNITS } from '../../constants/units';

storiesOf('AreaUnits', module)
.add('Square foot', () => (
<AreaUnits
value="300"
unit={ AREA_UNITS.SQUARE_FOOT }
/>
))
.add('Meters squared', () => (
<AreaUnits
value="300"
unit={ AREA_UNITS.METERS_SQUARED }
/>
));
13 changes: 13 additions & 0 deletions components/AreaUnits/AreaUnits.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from 'react';
import { render } from 'react-dom';

import AreaUnits from './AreaUnits';
import { AREA_UNITS } from '../../constants/units';

it('renders without crashing', () => {
const div = document.createElement('div');
render(
<AreaUnits value={ 300 } unit={ AREA_UNITS.SQUARE_FOOT } />,
div
);
});
4 changes: 4 additions & 0 deletions constants/units.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const AREA_UNITS = {
SQUARE_FOOT: 'sqft',
METERS_SQUARED: 'm2',
};

0 comments on commit efe62cd

Please sign in to comment.