-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
4 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
/> | ||
)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export const AREA_UNITS = { | ||
SQUARE_FOOT: 'sqft', | ||
METERS_SQUARED: 'm2', | ||
}; |