-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react-iconography): add size options to iconography
[Finishes #96041146]
- Loading branch information
Geoff Pleiss and Matt Royal
authored and
Geoff Pleiss and Matt Royal
committed
Jun 3, 2015
1 parent
f34fd1d
commit 5e14a23
Showing
6 changed files
with
148 additions
and
12 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
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 |
---|---|---|
@@ -1,16 +1,42 @@ | ||
require('../spec_helper'); | ||
|
||
describe('iconography', function() { | ||
beforeEach(function() { | ||
var Icon = require('../../../src/pivotal-ui-react/iconography/iconography').Icon; | ||
React.render(<Icon name="plus"/>, root); | ||
}); | ||
var Icon = require('../../../src/pivotal-ui-react/iconography/iconography').Icon; | ||
var {toBeValid} = require('../support/matchers'); | ||
|
||
describe('iconography', function() { | ||
afterEach(function() { | ||
React.unmountComponentAtNode(root); | ||
}); | ||
|
||
it('works', function() { | ||
React.render(<Icon name='plus'/>, root); | ||
expect('.fa.fa-plus').toExist(); | ||
}); | ||
|
||
describe('when a size is given', function() { | ||
beforeEach(function() { | ||
jasmine.addMatchers({toBeValid}); | ||
}); | ||
|
||
it('does not fail prop-validation', function() { | ||
for (var size of ['title', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'sm', 'xs', 'lg', '2x', '3x', '4x', '5x']) { | ||
expect(<Icon name='plus' size={size}/>, root).toBeValid(); | ||
} | ||
}); | ||
|
||
it('adds the size class to the icon', function() { | ||
for (var size of ['title', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'sm', 'xs', 'lg', '2x', '3x', '4x', '5x']) { | ||
React.render(<Icon name='plus' size={size}/>, root); | ||
expect('.fa.fa-plus').toHaveClass(`fa-${size}`); | ||
React.unmountComponentAtNode(root); | ||
} | ||
}); | ||
|
||
describe('when a className and a size are given', function() { | ||
it('adds the size class to the icon and includes the given className also', function() { | ||
React.render(<Icon name='plus' className='a-class-name' size='h6'/>, root); | ||
expect('.fa.fa-plus.a-class-name.fa-h6').toExist(); | ||
}); | ||
}); | ||
}); | ||
}); |
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,24 @@ | ||
module.exports = { | ||
toBeValid: function() { | ||
return { | ||
compare: function(element) { | ||
const Klass = element.type; | ||
|
||
const errorMessages = Object.keys(Klass.propTypes).reduce(function(memo, prop) { | ||
const error = Klass.propTypes[prop](element.props, prop); | ||
if (error) { | ||
memo.push(error.message); | ||
} | ||
return memo; | ||
}, []); | ||
|
||
var pass = !errorMessages.length; | ||
var message = pass ? | ||
`Expected element not to be a valid ${Klass.name}` : | ||
`Expected element to be a valid ${Klass.name}. Validation errors:\n ${errorMessages.join('\n ')}`; | ||
|
||
return {pass, message}; | ||
} | ||
}; | ||
} | ||
}; |
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 |
---|---|---|
@@ -1,21 +1,60 @@ | ||
const React = require('react'); | ||
const {PropTypes} = React; | ||
const ReactFaIcon = require('react-fa/dist/Icon'); | ||
const objectAssign = require('object-assign'); | ||
|
||
class Icon extends React.Component { | ||
render() { | ||
const {size, className, ...props} = this.props; | ||
|
||
return (<ReactFaIcon className={`${className} fa-${size}`.trim()} {...props} />); | ||
} | ||
} | ||
|
||
function satisfiesOneOf(...propTypes) { | ||
return function(props, propName, componentName) { | ||
var error; | ||
var errorMessages = []; | ||
for (var propType of propTypes) { | ||
error = propType(props, propName, componentName); | ||
if (!error) { | ||
return null; | ||
} else { | ||
errorMessages.push(error.message); | ||
} | ||
} | ||
|
||
return new Error(`Failed to satisfy any of the possible requirements:\n ${errorMessages.join('\n ')}`); | ||
}; | ||
} | ||
|
||
Icon.propTypes = objectAssign({}, ReactFaIcon.propTypes, { | ||
size: satisfiesOneOf( | ||
ReactFaIcon.propTypes.size, | ||
PropTypes.oneOf([ 'title', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'sm', 'xs' ]) | ||
) | ||
}); | ||
|
||
module.exports = { | ||
/** | ||
* @component Icon | ||
* @description Inserts a Font Awesome icon | ||
* | ||
* @property name {String} The name of the icon (without the fa- prefix) | ||
* @property size {String} Can either be a PUI typography size (title, h1, | ||
* h2, h3, h4, h5, h6, sm, xs) or a FA size (lg, 2x, 3x, 4x, 5x). | ||
* | ||
* @example ```js | ||
* var Icon = require('pui-react-iconography').Icon; | ||
* var MyComponent = React.createClass({ | ||
* render() { | ||
* return <Icon name="plus"/>; | ||
* return <Icon name="plus" size="h5"/>; | ||
* } | ||
* }); | ||
* ``` | ||
* | ||
* @see [Pivotal UI React](http://styleguide.pivotal.io/react_beta.html#icon_react) | ||
* @see [Pivotal UI CSS](http://styleguide.pivotal.io/elements.html#iconography) | ||
*/ | ||
Icon: require('react-fa/dist/Icon') | ||
}; | ||
Icon: Icon | ||
}; |
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 |
---|---|---|
@@ -1,9 +1,10 @@ | ||
{ | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "A React component for rendering Font Awesome icons", | ||
"homepage": "http://styleguide.pivotal.io/react_beta.html#icon_react", | ||
"dependencies": { | ||
"react-fa": "^3.0.0", | ||
"pui-css-iconography": "^0" | ||
"pui-css-iconography": "^0", | ||
"object-assign": "^2.0.0" | ||
} | ||
} |
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