Skip to content

Commit

Permalink
[added] images component
Browse files Browse the repository at this point in the history
image testing done

add docs

add comment for props

* Merge previous three samples (ImageCircle, ImageRounded, ImageThumbnail) to one sample file (ImageShape)

* Add ImageResponsive and ImageShape in ReactPlayground.js

* Add Image source include in Samples.js

fix the comment

* Fix eslint issue on Image.js
* Fix eslint issue on ImageSpec.js

* remove src and alt
* refactor test code to test attribute not props

fix eslint
  • Loading branch information
deerawan committed Sep 13, 2015
1 parent 360fceb commit 674d67e
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 0 deletions.
5 changes: 5 additions & 0 deletions docs/examples/ImageResponsive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const imageResponsiveInstance = (
<Image src="/assets/thumbnail.png" responsive />
);

React.render(imageResponsiveInstance, mountNode);
17 changes: 17 additions & 0 deletions docs/examples/ImageShape.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const imageShapeInstance = (
<Grid>
<Row>
<Col xs={6} md={4}>
<Image src="/assets/thumbnail.png" rounded />
</Col>
<Col xs={6} md={4}>
<Image src="/assets/thumbnail.png" circle />
</Col>
<Col xs={6} md={4}>
<Image src="/assets/thumbnail.png" thumbnail />
</Col>
</Row>
</Grid>
);

React.render(imageShapeInstance, mountNode);
17 changes: 17 additions & 0 deletions docs/src/ComponentsPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,22 @@ const ComponentsPage = React.createClass({
<h4><Anchor id="utilities-fade-props">Props</Anchor></h4>
<PropTable component="Fade"/>
</div>

{/* Images */}
<div className="bs-docs-section">
<h1 className="page-header"><Anchor id="images">Images</Anchor></h1>

<h3><Anchor id="image-shape">Shape</Anchor></h3>
<p>Use the <code>rounded</code>, <code>circle</code> and <code>thumbnail</code> props to customise the image.</p>
<ReactPlayground codeText={Samples.ImageShape} />

<h3><Anchor id="image-responsive">Responsive</Anchor></h3>
<p>Use the <code>responsive</code> to scale image nicely to the parent element.</p>
<ReactPlayground codeText={Samples.ImageResponsive} />

<h3><Anchor id="image-props">Props</Anchor></h3>
<PropTable component="Image"/>
</div>
</div>


Expand Down Expand Up @@ -948,6 +964,7 @@ const ComponentsPage = React.createClass({
<NavItem href="#tables" key={25}>Tables</NavItem>
<NavItem href="#input" key={26}>Input</NavItem>
<NavItem href="#utilities" key={28}>Utilities</NavItem>
<NavItem href="#images" key={29}>Images</NavItem>
</Nav>
<a className="back-to-top" href="#top">
Back to top
Expand Down
1 change: 1 addition & 0 deletions docs/src/ReactPlayground.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const FormControls = require('../../src/FormControls');
const Glyphicon = require('../../src/Glyphicon');
const Grid = require('../../src/Grid');
const Input = require('../../src/Input');
const Image = require('../../src/Image');
const Jumbotron = require('../../src/Jumbotron');
const Label = require('../../src/Label');
const ListGroup = require('../../src/ListGroup');
Expand Down
2 changes: 2 additions & 0 deletions docs/src/Samples.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ export default {
InputHorizontal: require('fs').readFileSync(__dirname + '/../examples/InputHorizontal.js', 'utf8'),
InputWrapper: require('fs').readFileSync(__dirname + '/../examples/InputWrapper.js', 'utf8'),
MenuItem: require('fs').readFileSync(__dirname + '/../examples/MenuItem.js', 'utf8'),
ImageResponsive: require('fs').readFileSync(__dirname + '/../examples/ImageResponsive.js', 'utf8'),
ImageShape: require('fs').readFileSync(__dirname + '/../examples/ImageShape.js', 'utf8'),

Overlay: require('fs').readFileSync(__dirname + '/../examples/Overlay.js', 'utf8'),
OverlayCustom: require('fs').readFileSync(__dirname + '/../examples/OverlayCustom.js', 'utf8')
Expand Down
52 changes: 52 additions & 0 deletions src/Image.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import React from 'react';
import classNames from 'classnames';

const Image = React.createClass({

propTypes: {

/**
* Sets image as responsive image
*/
responsive: React.PropTypes.bool,

/**
* Sets image shape as rounded
*/
rounded: React.PropTypes.bool,

/**
* Sets image shape as circle
*/
circle: React.PropTypes.bool,

/**
* Sets image shape as thumbnail
*/
thumbnail: React.PropTypes.bool
},

getDefaultProps() {
return {
responsive: false,
rounded: false,
circle: false,
thumbnail: false
};
},

render() {
const classes = {
'img-responsive': this.props.responsive,
'img-rounded': this.props.rounded,
'img-circle': this.props.circle,
'img-thumbnail': this.props.thumbnail
};

return (
<img {...this.props} className={classNames(this.props.className, classes)} />
);
}
});

export default Image;
61 changes: 61 additions & 0 deletions test/ImageSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import React from 'react';
import ReactTestUtils from 'react/lib/ReactTestUtils';
import Image from '../src/Image';

describe('Image', function() {

it('should be an image', function() {
let instance = ReactTestUtils.renderIntoDocument(
<Image />
);
let image = React.findDOMNode(instance);

image.nodeName.should.equal('IMG');
});

it('should provide src and alt prop', function() {
let instance = ReactTestUtils.renderIntoDocument(
<Image src="image.jpg" alt="this is alt" />
);
let image = React.findDOMNode(instance);

assert.equal(image.getAttribute('src'), 'image.jpg');
assert.equal(image.getAttribute('alt'), 'this is alt');
});

it('should have correct class when responsive prop is set', function() {
let instance = ReactTestUtils.renderIntoDocument(
<Image responsive />
);
let imageClassName = React.findDOMNode(instance).className;

imageClassName.should.match(/\bimg-responsive\b/);
});

it('should have correct class when rounded prop is set', function() {
let instance = ReactTestUtils.renderIntoDocument(
<Image rounded />
);
let imageClassName = React.findDOMNode(instance).className;

imageClassName.should.match(/\bimg-rounded\b/);
});

it('should have correct class when circle prop is set', function() {
let instance = ReactTestUtils.renderIntoDocument(
<Image circle />
);
let imageClassName = React.findDOMNode(instance).className;

imageClassName.should.match(/\bimg-circle\b/);
});

it('should have correct class when thumbnail prop is set', function() {
let instance = ReactTestUtils.renderIntoDocument(
<Image thumbnail />
);
let imageClassName = React.findDOMNode(instance).className;

imageClassName.should.match(/\bimg-thumbnail\b/);
});
});

0 comments on commit 674d67e

Please sign in to comment.