Skip to content

Commit 674d67e

Browse files
committed
[added] images component
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
1 parent 360fceb commit 674d67e

File tree

7 files changed

+155
-0
lines changed

7 files changed

+155
-0
lines changed

docs/examples/ImageResponsive.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const imageResponsiveInstance = (
2+
<Image src="/assets/thumbnail.png" responsive />
3+
);
4+
5+
React.render(imageResponsiveInstance, mountNode);

docs/examples/ImageShape.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const imageShapeInstance = (
2+
<Grid>
3+
<Row>
4+
<Col xs={6} md={4}>
5+
<Image src="/assets/thumbnail.png" rounded />
6+
</Col>
7+
<Col xs={6} md={4}>
8+
<Image src="/assets/thumbnail.png" circle />
9+
</Col>
10+
<Col xs={6} md={4}>
11+
<Image src="/assets/thumbnail.png" thumbnail />
12+
</Col>
13+
</Row>
14+
</Grid>
15+
);
16+
17+
React.render(imageShapeInstance, mountNode);

docs/src/ComponentsPage.js

+17
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,22 @@ const ComponentsPage = React.createClass({
904904
<h4><Anchor id="utilities-fade-props">Props</Anchor></h4>
905905
<PropTable component="Fade"/>
906906
</div>
907+
908+
{/* Images */}
909+
<div className="bs-docs-section">
910+
<h1 className="page-header"><Anchor id="images">Images</Anchor></h1>
911+
912+
<h3><Anchor id="image-shape">Shape</Anchor></h3>
913+
<p>Use the <code>rounded</code>, <code>circle</code> and <code>thumbnail</code> props to customise the image.</p>
914+
<ReactPlayground codeText={Samples.ImageShape} />
915+
916+
<h3><Anchor id="image-responsive">Responsive</Anchor></h3>
917+
<p>Use the <code>responsive</code> to scale image nicely to the parent element.</p>
918+
<ReactPlayground codeText={Samples.ImageResponsive} />
919+
920+
<h3><Anchor id="image-props">Props</Anchor></h3>
921+
<PropTable component="Image"/>
922+
</div>
907923
</div>
908924

909925

@@ -948,6 +964,7 @@ const ComponentsPage = React.createClass({
948964
<NavItem href="#tables" key={25}>Tables</NavItem>
949965
<NavItem href="#input" key={26}>Input</NavItem>
950966
<NavItem href="#utilities" key={28}>Utilities</NavItem>
967+
<NavItem href="#images" key={29}>Images</NavItem>
951968
</Nav>
952969
<a className="back-to-top" href="#top">
953970
Back to top

docs/src/ReactPlayground.js

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const FormControls = require('../../src/FormControls');
2626
const Glyphicon = require('../../src/Glyphicon');
2727
const Grid = require('../../src/Grid');
2828
const Input = require('../../src/Input');
29+
const Image = require('../../src/Image');
2930
const Jumbotron = require('../../src/Jumbotron');
3031
const Label = require('../../src/Label');
3132
const ListGroup = require('../../src/ListGroup');

docs/src/Samples.js

+2
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ export default {
105105
InputHorizontal: require('fs').readFileSync(__dirname + '/../examples/InputHorizontal.js', 'utf8'),
106106
InputWrapper: require('fs').readFileSync(__dirname + '/../examples/InputWrapper.js', 'utf8'),
107107
MenuItem: require('fs').readFileSync(__dirname + '/../examples/MenuItem.js', 'utf8'),
108+
ImageResponsive: require('fs').readFileSync(__dirname + '/../examples/ImageResponsive.js', 'utf8'),
109+
ImageShape: require('fs').readFileSync(__dirname + '/../examples/ImageShape.js', 'utf8'),
108110

109111
Overlay: require('fs').readFileSync(__dirname + '/../examples/Overlay.js', 'utf8'),
110112
OverlayCustom: require('fs').readFileSync(__dirname + '/../examples/OverlayCustom.js', 'utf8')

src/Image.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import React from 'react';
2+
import classNames from 'classnames';
3+
4+
const Image = React.createClass({
5+
6+
propTypes: {
7+
8+
/**
9+
* Sets image as responsive image
10+
*/
11+
responsive: React.PropTypes.bool,
12+
13+
/**
14+
* Sets image shape as rounded
15+
*/
16+
rounded: React.PropTypes.bool,
17+
18+
/**
19+
* Sets image shape as circle
20+
*/
21+
circle: React.PropTypes.bool,
22+
23+
/**
24+
* Sets image shape as thumbnail
25+
*/
26+
thumbnail: React.PropTypes.bool
27+
},
28+
29+
getDefaultProps() {
30+
return {
31+
responsive: false,
32+
rounded: false,
33+
circle: false,
34+
thumbnail: false
35+
};
36+
},
37+
38+
render() {
39+
const classes = {
40+
'img-responsive': this.props.responsive,
41+
'img-rounded': this.props.rounded,
42+
'img-circle': this.props.circle,
43+
'img-thumbnail': this.props.thumbnail
44+
};
45+
46+
return (
47+
<img {...this.props} className={classNames(this.props.className, classes)} />
48+
);
49+
}
50+
});
51+
52+
export default Image;

test/ImageSpec.js

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import React from 'react';
2+
import ReactTestUtils from 'react/lib/ReactTestUtils';
3+
import Image from '../src/Image';
4+
5+
describe('Image', function() {
6+
7+
it('should be an image', function() {
8+
let instance = ReactTestUtils.renderIntoDocument(
9+
<Image />
10+
);
11+
let image = React.findDOMNode(instance);
12+
13+
image.nodeName.should.equal('IMG');
14+
});
15+
16+
it('should provide src and alt prop', function() {
17+
let instance = ReactTestUtils.renderIntoDocument(
18+
<Image src="image.jpg" alt="this is alt" />
19+
);
20+
let image = React.findDOMNode(instance);
21+
22+
assert.equal(image.getAttribute('src'), 'image.jpg');
23+
assert.equal(image.getAttribute('alt'), 'this is alt');
24+
});
25+
26+
it('should have correct class when responsive prop is set', function() {
27+
let instance = ReactTestUtils.renderIntoDocument(
28+
<Image responsive />
29+
);
30+
let imageClassName = React.findDOMNode(instance).className;
31+
32+
imageClassName.should.match(/\bimg-responsive\b/);
33+
});
34+
35+
it('should have correct class when rounded prop is set', function() {
36+
let instance = ReactTestUtils.renderIntoDocument(
37+
<Image rounded />
38+
);
39+
let imageClassName = React.findDOMNode(instance).className;
40+
41+
imageClassName.should.match(/\bimg-rounded\b/);
42+
});
43+
44+
it('should have correct class when circle prop is set', function() {
45+
let instance = ReactTestUtils.renderIntoDocument(
46+
<Image circle />
47+
);
48+
let imageClassName = React.findDOMNode(instance).className;
49+
50+
imageClassName.should.match(/\bimg-circle\b/);
51+
});
52+
53+
it('should have correct class when thumbnail prop is set', function() {
54+
let instance = ReactTestUtils.renderIntoDocument(
55+
<Image thumbnail />
56+
);
57+
let imageClassName = React.findDOMNode(instance).className;
58+
59+
imageClassName.should.match(/\bimg-thumbnail\b/);
60+
});
61+
});

0 commit comments

Comments
 (0)