Skip to content

Commit 02e671b

Browse files
committed
Allow custom placeholder
1 parent b1c5bc8 commit 02e671b

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

src/components/LazyLoadImage.jsx

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,25 @@ class LazyLoadImage extends React.Component {
7070
viewport.bottom + threshold >= boundingBox.top);
7171
}
7272

73+
getPlaceholder() {
74+
const { className, height, placeholder, width } = this.props;
75+
if (placeholder) {
76+
return placeholder;
77+
}
78+
79+
return (
80+
<span className={'lazy-load-image-placeholder ' + className}
81+
ref="placeholder"
82+
style={{height, width}}>
83+
</span>
84+
);
85+
}
86+
7387
render() {
74-
const { scrollPosition, threshold, ...props } = this.props;
88+
const { placeholder, scrollPosition, threshold, ...props } = this.props;
7589

7690
if (!this.state.visible) {
77-
const { className, height, width } = this.props;
78-
return (
79-
<span className={'lazy-load-image-placeholder ' + className}
80-
ref="placeholder"
81-
style={{height, width}}>
82-
</span>
83-
);
91+
return this.getPlaceholder();
8492
}
8593

8694
return (
@@ -95,13 +103,15 @@ LazyLoadImage.propTypes = {
95103
scrollPosition: PropTypes.number.isRequired,
96104
className: PropTypes.string,
97105
height: PropTypes.number,
106+
placeholder: PropTypes.element,
98107
threshold: PropTypes.number,
99108
width: PropTypes.number
100109
};
101110

102111
LazyLoadImage.defaultProps = {
103112
className: '',
104113
height: 0,
114+
placeholder: null,
105115
threshold: 100,
106116
width: 0
107117
};

src/components/LazyLoadImage.spec.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ const {
1010
} = ReactTestUtils;
1111

1212
describe('LazyLoadImage', function() {
13-
function renderLazyLoadImage(scrollPosition) {
13+
function renderLazyLoadImage(scrollPosition = 0, placeholder = null) {
1414
return ReactTestUtils.renderIntoDocument(
1515
<LazyLoadImage
16+
placeholder={placeholder}
1617
scrollPosition={scrollPosition}
1718
src="" />
1819
);
@@ -24,22 +25,29 @@ describe('LazyLoadImage', function() {
2425
expect(img.length).toEqual(numberOfImages);
2526
}
2627

27-
function expectPlaceholders(wrapper, numberOfPlaceholders) {
28-
const placeholder = scryRenderedDOMComponentsWithClass(wrapper,
29-
'lazy-load-image-placeholder');
28+
function expectPlaceholders(wrapper, numberOfPlaceholders, placeholderClassName = 'lazy-load-image-placeholder') {
29+
const placeholder = scryRenderedDOMComponentsWithClass(wrapper, placeholderClassName);
3030

3131
expect(placeholder.length).toEqual(numberOfPlaceholders);
3232
}
3333

34-
it('renders the placeholder when it\'s not in the viewport', function() {
34+
it('renders the default placeholder when it\'s not in the viewport', function() {
3535
const lazyLoadImage = renderLazyLoadImage(-1000);
3636

3737
expectImages(lazyLoadImage, 0);
3838
expectPlaceholders(lazyLoadImage, 1);
3939
});
4040

41+
it('renders the prop placeholder when it\'s not in the viewport', function() {
42+
const placeholder = <span className="test-placeholder"></span>;
43+
const lazyLoadImage = renderLazyLoadImage(-1000, placeholder);
44+
45+
expectImages(lazyLoadImage, 0);
46+
expectPlaceholders(lazyLoadImage, 1, 'test-placeholder');
47+
});
48+
4149
it('renders the image when it\'s in the viewport', function() {
42-
const lazyLoadImage = renderLazyLoadImage(0);
50+
const lazyLoadImage = renderLazyLoadImage();
4351

4452
expectImages(lazyLoadImage, 1);
4553
expectPlaceholders(lazyLoadImage, 0);

0 commit comments

Comments
 (0)