This repository was archived by the owner on Jun 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 364
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add lazyload component * Better lazy load * pass tests
- Loading branch information
Showing
5 changed files
with
150 additions
and
72 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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import React, { Component, PropTypes } from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
|
||
import debug from '../../helpers/debug'; | ||
|
||
export default class LazyLoad extends Component { | ||
static propTypes = { | ||
isLoading: PropTypes.bool.isRequired, | ||
isEnd: PropTypes.bool.isRequired, | ||
isLoaded: PropTypes.bool, | ||
onLazyLoad: PropTypes.func.isRequired, | ||
loadingComponent: PropTypes.any, | ||
endComponent: PropTypes.any, | ||
offset: PropTypes.number | ||
} | ||
|
||
static defaultProps = { | ||
loadingComponent: 'Loading...', | ||
endComponent: 'End.', | ||
offset: 1000 | ||
} | ||
|
||
componentDidMount() { | ||
if (__CLIENT__) { | ||
window.removeEventListener('scroll', this.onScroll, true); | ||
window.addEventListener('scroll', this.onScroll, true); | ||
} | ||
} | ||
|
||
onScroll = () => { | ||
const { isLoading, isEnd, offset } = this.props; | ||
const dom = ReactDOM.findDOMNode(this); | ||
|
||
if ((!isLoading && !isEnd) && (dom.offsetParent || dom).offsetTop - (window.pageYOffset + window.innerHeight) < offset) { | ||
debug('component:LazyLoad', 'onLazyLoad called'); | ||
return this.props.onLazyLoad(); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
render() { | ||
const { isEnd, loadingComponent, endComponent } = this.props; | ||
|
||
if (isEnd) { | ||
return endComponent; | ||
} | ||
|
||
return loadingComponent; | ||
} | ||
} |
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,58 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import { mount } from 'enzyme'; | ||
|
||
import LazyLoad from './index'; | ||
|
||
let wrapper; | ||
let onLazyLoad; | ||
let makeComponent; | ||
|
||
describe('<LazyLoad />', () => { | ||
beforeEach(() => { | ||
makeComponent = (isEnd = false, isLoading = false) => { | ||
onLazyLoad = sinon.stub(); | ||
wrapper = mount( | ||
<LazyLoad | ||
onLazyLoad={onLazyLoad} | ||
isEnd={isEnd} | ||
isLoading={isLoading} | ||
endComponent={<p>End</p>} | ||
loadingComponent={<p>Loading</p>} | ||
/> | ||
); | ||
}; | ||
|
||
makeComponent(); | ||
}); | ||
|
||
it('should render', () => { | ||
expect(wrapper).to.be.ok; | ||
}); | ||
|
||
it('should show loading component', () => { | ||
expect(wrapper.text()).to.eql('Loading'); | ||
}); | ||
|
||
it('should show end component when no more lazy loading needed', () => { | ||
makeComponent(true); | ||
expect(wrapper.text()).to.eql('End'); | ||
}); | ||
|
||
it('should call onLazyLoad when not end and not loading', () => { | ||
wrapper.instance().onScroll(); | ||
expect(wrapper.props().onLazyLoad).to.have.been.called; | ||
}); | ||
|
||
it('should not call onLazyLoad when at end', () => { | ||
makeComponent(true); | ||
wrapper.instance().onScroll(); | ||
expect(wrapper.props().onLazyLoad).not.to.have.been.called; | ||
}); | ||
|
||
it('should not call onLazyLoad when loading', () => { | ||
makeComponent(false, true); | ||
wrapper.instance().onScroll(); | ||
expect(wrapper.props().onLazyLoad).not.to.have.been.called; | ||
}); | ||
}); |
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