Skip to content
This repository has been archived by the owner on Nov 27, 2021. It is now read-only.

Commit

Permalink
custom renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
Yusuke Shibata committed Nov 26, 2017
1 parent 8cddcb9 commit de14958
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 64 deletions.
32 changes: 32 additions & 0 deletions demo/src/App.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions src/component/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,24 @@ const DashedCircle = styled.circle`
`

export default (props, state, children) => {
const { yMax, y, refreshing, refreshed } = state
const p = Math.atan(y / props.max)
const pMax = Math.atan(yMax / props.max)
const { max, yRefreshing, y, refreshing, refreshed } = state
const p = Math.atan(y / max)
const pMax = Math.atan(yRefreshing / max)
const r = Math.PI * 10 * 2
const Svg = refreshing ? RotatingSvg : 'svg'
const Circle = refreshing ? DashedCircle : 'circle'
return [
<Component
key='pull'
style={{
top: Math.max(refreshed ? Math.atan(1) : p, 0) * props.max - 10,
top: Math.max(refreshed ? Math.atan(1) : p, 0) * max - 10,
transform: `translate(-50%, -100%) scale(${refreshed ? p : 1},${refreshed ? p : 1})`,
backgroundColor: props.bgColor
}}
>
<Svg
style={{
transform:`rotate(${yMax}deg)`
transform:`rotate(${yRefreshing}deg)`
}}
width={40}
height={40}
Expand Down
49 changes: 0 additions & 49 deletions src/component/test.js

This file was deleted.

25 changes: 15 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { Component } from 'react'
import PropTypes from 'prop-types'
import Spring from './spring'
import scrollTop from './scrollTop'
import defaultRender from './component'
import renderDefault from './component'

const MAX = 100

Expand All @@ -11,7 +11,8 @@ export default class PullRefresh extends Component {
super(props)
this.state = {
y: 0,
yMax: 0,
yRefreshing: 0,
max: MAX,
refreshing: false,
refreshed: false
}
Expand All @@ -24,21 +25,22 @@ export default class PullRefresh extends Component {
this._py = ey
}
async onUp(evt) {
const { y, refreshed, refreshing } = this.state
const { max, willRefresh, refreshed, refreshing } = this.state
const { onRefresh } = this.props
if(refreshed || refreshing) return
this._down = false
if(y >= MAX) {
if(willRefresh) {
this._willRefresh = true
await this._spring.to(MAX)
await this._spring.to(max)
this._spring.pause()
this._willRefresh = false
this.setState({
willRefresh: false,
refreshing: true
})
await onRefresh()
this.setState({
yMax: 0,
yRefreshing: 0,
refreshed: true,
refreshing: false
})
Expand All @@ -60,11 +62,12 @@ export default class PullRefresh extends Component {
this._py = ey
}
onSpringUpdate(spring) {
const { yMax, refreshed } = this.state
const { max, yRefreshing, refreshed } = this.state
const y = spring.currentValue
this.setState({
y,
yMax: this._willRefresh ? Math.max(y, yMax) : y
willRefresh: y >= max,
yRefreshing: this._willRefresh ? Math.max(y, yRefreshing) : y
})
if(refreshed && y === 0) this.setState({ refreshed: false })
}
Expand All @@ -74,7 +77,7 @@ export default class PullRefresh extends Component {
this._spring.onUpdate = ::this.onSpringUpdate
}
render() {
const { bgColor, color, onRefresh, disabled, as, children, ...props } = this.props
const { render, bgColor, color, onRefresh, disabled, as, children, ...props } = this.props
const Container = as
return (
<Container
Expand All @@ -86,7 +89,7 @@ export default class PullRefresh extends Component {
onTouchEnd ={!disabled && ::this.onUp}
onTouchMove ={!disabled && ::this.onMove}
>
{ defaultRender(this.props, this.state, children) }
{ render(this.props, this.state, children) }
</Container>
)
}
Expand All @@ -99,6 +102,7 @@ PullRefresh.propTypes = {
disabled: PropTypes.bool,
color: PropTypes.string,
bgColor: PropTypes.string,
render: PropTypes.func,
// max: PropTypes.number,
// offset: PropTypes.number,
// size: PropTypes.number,
Expand All @@ -114,6 +118,7 @@ PullRefresh.defaultProps = {
disabled: false,
color: '#787878',
bgColor: '#fff',
render: renderDefault,
// max: 100,
// offset: 0,
// size: 40,
Expand Down

0 comments on commit de14958

Please sign in to comment.