1 | import React from 'react';
|
2 | import ZyngaScroller from './ZyngaScroller';
|
3 | import TouchableArea from './TouchableArea';
|
4 | import PropTypes from 'prop-types';
|
5 |
|
6 | export default class TouchWrapper extends React.Component {
|
7 | static propTypes = {
|
8 | tableWidth: PropTypes.number.isRequired,
|
9 | tableHeight: PropTypes.number.isRequired,
|
10 | contentWidth: PropTypes.number,
|
11 | contentHeight: PropTypes.number,
|
12 | onScroll: PropTypes.func.isRequired,
|
13 | children: PropTypes.any,
|
14 | }
|
15 |
|
16 | state = {
|
17 | left: 0,
|
18 | top: 0,
|
19 | initialized: false,
|
20 | }
|
21 |
|
22 | constructor(props) {
|
23 | super(props);
|
24 | }
|
25 |
|
26 | componentDidMount() {
|
27 | this.scroller = new ZyngaScroller(this.onScroll);
|
28 | this.scroller.setDimensions(
|
29 | this.props.tableWidth,
|
30 | this.props.tableHeight,
|
31 | this.props.contentWidth,
|
32 | this.props.contentHeight
|
33 | );
|
34 | }
|
35 |
|
36 | componentWillReceiveProps(nextProps) {
|
37 | const diffTableWidth = nextProps.tableWidth !== this.props.tableWidth;
|
38 | const diffTableHeight = nextProps.tableHeight !== this.props.tableHeight;
|
39 | const diffContentWidth = nextProps.contentWidth !== this.props.contentWidth;
|
40 | const diffContentHeight = nextProps.contentHeight !== this.props.contentHeight;
|
41 |
|
42 | if(this.scroller && (diffTableWidth || diffTableHeight || diffContentWidth || diffContentHeight)) {
|
43 | this.scroller.setDimensions(nextProps.tableWidth, nextProps.tableHeight, nextProps.contentWidth, nextProps.contentHeight);
|
44 | }
|
45 | }
|
46 |
|
47 | onScroll = (scrollLeft, scrollTop) => {
|
48 | if(this.state.initialized) {
|
49 | this.props.onScroll(scrollLeft, scrollTop);
|
50 | } else {
|
51 | this.setState({
|
52 | initialized: true,
|
53 | });
|
54 | }
|
55 | }
|
56 |
|
57 | render() {
|
58 | return (
|
59 | <TouchableArea scroller={this.scroller}>
|
60 | {this.props.children}
|
61 | </TouchableArea>
|
62 | );
|
63 | }
|
64 | }
|