CustomScrollbars.js 5.5 KB
import React,{PropTypes} from 'react';
import { Scrollbars } from 'react-custom-scrollbars';
import cx from 'classnames';
import s from './customScrollbarsLess.les'



export class CustomScrollbars extends React.Component {
	constructor(props) {
        super(props);
        this.onScrollStart=this.onScrollStart.bind(this);
        this.onScrollStop=this.onScrollStop.bind(this);
        this.checkScroll=this.checkScroll.bind(this);
        this.getRef=this.getRef.bind(this);
        this.state={
        	vScrolling:false,
        	hScrolling:false,
			showScroll:false
        }
    }
    static propTypes = {
    	customName:PropTypes.string,
    	onScrollStart:PropTypes.func,
    	onScrollStop:PropTypes.func,
    	loadWay:PropTypes.string,
		children:PropTypes.oneOfType([
	      PropTypes.element,
	      PropTypes.array
	    ]),
		loadMore:PropTypes.func,
		isLoad:PropTypes.bool,
		viewStyle:PropTypes.object,
		theme:PropTypes.string
	}
	static defaultProps={
		customName:'custom_scroll',
        onScrollStart:()=>{},
        onScrollStop:()=>{},
        loadMore:()=>{},
        loadWay:'up',//'down'
		isLoad:false,
		viewStyle:{
			paddingRight:'10px'
		},
		theme:'black'	   //white
    }
	onScrollStart(){
		const {onScrollStart}=this.props;
		const {my_scrollbar}=this.refs;
		const {top,scrollTop,scrollHeight,clientHeight,left}=my_scrollbar.getValues();
		this._preScrollTop=scrollTop;
		this._preLeft=left;
		this._preTop=top;
		this.setState({
			vScrolling:true,
			hScrolling:true
		})
		onScrollStart();
	}
	onScrollStop(){
		const {onScrollStart}=this.props;
		const {my_scrollbar}=this.refs;
		const {top,scrollTop,scrollHeight,clientHeight}=my_scrollbar.getValues();
		const {loadMore,isLoad,loadWay}=this.props;
		const preTop=this._preScrollTop;
		if(!isLoad&&loadWay=='up'&&top<0.1&&preTop>scrollTop){
			loadMore(my_scrollbar.getValues());
		}else if(!isLoad&&loadWay=='down'&&top>0.9&&preTop<scrollTop){
			loadMore(my_scrollbar.getValues());
		}
		this.setState({
			vScrolling:false,
			hScrolling:false
		})
		onScrollStart();
	}
	checkScroll(){
		const {clientHeight,scrollHeight}=this.refs['my_scrollbar'].getValues();
		if(clientHeight<scrollHeight){
			if(!this.state.showScroll)
				this.setState({
					showScroll:true
				})
		}else{
			if(this.state.showScroll)
				this.setState({
					showScroll:false
				})
		}
	}
	componentDidMount(){
		this.checkScroll();
		window.addEventListener('resize', this.checkScroll);
	}
	componentWillReceiveProps(nextProps){
	}
	componentDidUpdate(prevProps,prevState){
		this.checkScroll()
	}
	componentWillUnmount() {
		window.removeEventListener('resize', this.checkScroll);
	}
	getRef(){
		return this.refs['my_scrollbar'];
	}
    render(){
    	const {vScrolling,hScrolling,showScroll}=this.state,self=this;
    	const {viewStyle,theme,customName}=this.props;
    	const trackStyle={position:'absolute',width:'8px',right: '0px',bottom: '2px',top: '2px',borderRadius: '3px',cursor: 'pointer'}
    	if(showScroll){
    		trackStyle['backgroundColor']='rgba(0,0,0,.05)';
    		trackStyle['filter']='alpha(opacity=40)';
    		trackStyle['msFilter']='alpha(opacity=40)';
    	}
    	const horizontalTrackStyle={backgroundColor:'rgba(0,0,0,.05)',filter:'alpha(opacity=40)',msFilter:'alpha(opacity=40)',position: 'absolute',height: '8px',right: '10px',bottom: '2px',left: '2px',borderRadius: '3px',cursor: 'pointer'}
    	return(
    	 	<Scrollbars ref='my_scrollbar' 
	        renderView={props =>{
	        		const {style={}}=props; 
	        		const myStyle=Object.assign({},style,viewStyle); 
	        		return <div {...props} style={{...myStyle,backgroundColor:'transparent'}} id={customName}  className={s.my_scrollbars}/>
	        	}
	        }
	        renderTrackHorizontal={props => {
	        		if(this.refs.my_scrollbar){
	        			const {clientWidth,scrollWidth}=this.refs.my_scrollbar.getValues();
	        			if(scrollWidth>clientWidth){
	        				return <div {...props} style={horizontalTrackStyle} />
	        			}else{
	        				return <div {...props} />
	        			}
	        		}else{
	        			return <div {...props} />
	        		}
	        	}
	    	}
			renderTrackVertical={props =>{
					return <div {...props} style={trackStyle}/>
				}
			}
			renderThumbHorizontal={props =>{
	        		const style={transition:'opacity .2s ease-in-out,background-color .2s ease-in-out',position: 'relative',display: 'block', height: '100%', cursor: 'pointer', borderRadius: 'inherit',backgroundColor:'rgba(0, 0, 0, 0.2)'};
					if(hScrolling){
						style.backgroundColor='rgba(0, 0, 0, 0.5)';
					}
					const temp=Object.assign({},props,style);
					if(this.refs.my_scrollbar){
	        			const {clientWidth,scrollWidth}=this.refs.my_scrollbar.getValues();
	        			if(scrollWidth>clientWidth){
	        				return <div style={temp} />
	        			}else{
	        				return <div {...props} />
	        			}
	        		}else{
	        			return <div {...props} />
	        		}
	        	}
	    	}
			renderThumbVertical={props => {
					const style={transition:'opacity .2s ease-in-out,background-color .2s ease-in-out',position: 'relative',display: 'block', width: '100%', cursor: 'pointer', borderRadius: 'inherit',backgroundColor:'rgba(0, 0, 0, 0.2)'};
					if(vScrolling){
						style.backgroundColor=theme=='white'?'rgba(255, 255, 255, 0.3)':'rgba(0, 0, 0, 0.5)';
					}
					return <div {...props} style={style}/>
				}
			}
			onScrollStart={this.onScrollStart}
			onScrollStop={this.onScrollStop}
			>
		        {this.props.children}
	      </Scrollbars>
		)
	}
}

export default CustomScrollbars