mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-10 02:54:15 +00:00
added LoadMore
component
This commit is contained in:
parent
ecb43e1a4c
commit
fbba723c2c
82
packages/app/src/components/LoadMore/index.jsx
Normal file
82
packages/app/src/components/LoadMore/index.jsx
Normal file
@ -0,0 +1,82 @@
|
||||
import React from "react"
|
||||
import PropTypes from "prop-types"
|
||||
import classnames from "classnames"
|
||||
import IntersectionObserver from "intersection-observer-polyfill"
|
||||
|
||||
import "./index.less"
|
||||
|
||||
export default class LoadMore extends React.Component {
|
||||
constructor() {
|
||||
super(...arguments)
|
||||
this.insideViewportCb = this.insideViewportCb.bind(this)
|
||||
}
|
||||
|
||||
static propTypes = {
|
||||
onBottom: PropTypes.func,
|
||||
fetching: PropTypes.bool,
|
||||
hasMore: PropTypes.bool,
|
||||
NoResult: PropTypes.func,
|
||||
Footer: PropTypes.func
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
try {
|
||||
const node = document.getElementById("bottom")
|
||||
|
||||
this.observer = new IntersectionObserver(this.insideViewportCb)
|
||||
this.observer.observe(node)
|
||||
} catch (err) {
|
||||
console.log("err in finding node", err)
|
||||
}
|
||||
|
||||
window.addEventListener("scroll", this.handleOnScroll)
|
||||
}
|
||||
|
||||
insideViewportCb(entries) {
|
||||
const { fetching, onBottom } = this.props
|
||||
|
||||
entries.forEach(element => {
|
||||
if (element.intersectionRatio > 0 && !fetching) {
|
||||
onBottom()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
if (this.observer) {
|
||||
this.observer = null
|
||||
}
|
||||
}
|
||||
|
||||
render() {
|
||||
const {
|
||||
className,
|
||||
children,
|
||||
hasMore,
|
||||
loadingComponent,
|
||||
noResultComponent,
|
||||
} = this.props
|
||||
|
||||
return <div className="infinite-scroll">
|
||||
<div className={classnames(className)}>
|
||||
{children}
|
||||
</div>
|
||||
|
||||
<div style={{ clear: "both" }} />
|
||||
|
||||
<div
|
||||
id="bottom"
|
||||
style={{ display: hasMore ? "block" : "none" }}
|
||||
>
|
||||
{loadingComponent && React.createElement(loadingComponent)}
|
||||
</div>
|
||||
|
||||
<div
|
||||
className="no-result"
|
||||
style={{ display: hasMore ? "none" : "block" }}
|
||||
>
|
||||
{noResultComponent ? React.createElement(noResultComponent) : "No more result"}
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
}
|
14
packages/app/src/components/LoadMore/index.less
Normal file
14
packages/app/src/components/LoadMore/index.less
Normal file
@ -0,0 +1,14 @@
|
||||
.infinite-scroll {
|
||||
min-height: 32vw;
|
||||
position: relative;
|
||||
|
||||
.no-result {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.list-loader {
|
||||
height: 16vw;
|
||||
margin: auto;
|
||||
width: 16vw;
|
||||
}
|
||||
}
|
@ -24,6 +24,7 @@ export { default as ImageUploader } from "./ImageUploader"
|
||||
export { default as ImageViewer } from "./ImageViewer"
|
||||
export { default as Login } from "./Login"
|
||||
export { default as Image } from "./Image"
|
||||
export { default as LoadMore } from "./LoadMore"
|
||||
|
||||
// BUTTONS
|
||||
export { default as LikeButton } from "./LikeButton"
|
||||
|
Loading…
x
Reference in New Issue
Block a user