|
1 | 1 | import _ from 'lodash';
|
2 |
| -import React from 'react'; |
3 | 2 | import PropTypes from 'prop-types';
|
| 3 | +import React from 'react'; |
4 | 4 | import {ScrollView, StyleSheet} from 'react-native';
|
5 | 5 | import {Constants} from '../../helpers';
|
6 | 6 | import {BaseComponent} from '../../commons';
|
| 7 | +import View from '../view'; |
7 | 8 | import * as presenter from './CarouselPresenter';
|
8 | 9 |
|
9 | 10 |
|
@@ -42,26 +43,41 @@ export default class Carousel extends BaseComponent {
|
42 | 43 | };
|
43 | 44 |
|
44 | 45 | static defaultProps = {
|
45 |
| - initialPage: 0, |
46 |
| - pageWidth: Constants.screenWidth |
| 46 | + initialPage: 0 |
47 | 47 | };
|
48 | 48 |
|
49 | 49 | constructor(props) {
|
50 | 50 | super(props);
|
51 | 51 |
|
| 52 | + const defaultPageWidth = props.pageWidth || Constants.screenWidth; |
| 53 | + |
52 | 54 | this.state = {
|
53 | 55 | currentPage: props.initialPage,
|
54 | 56 | currentStandingPage: props.initialPage,
|
55 |
| - initialOffset: {x: presenter.calcOffset(props, {currentPage: props.initialPage})} |
| 57 | + pageWidth: defaultPageWidth, |
| 58 | + initialOffset: {x: presenter.calcOffset(props, {currentPage: props.initialPage, pageWidth: defaultPageWidth})} |
56 | 59 | };
|
57 | 60 | }
|
58 | 61 |
|
59 |
| - generateStyles() { |
60 |
| - this.styles = createStyles(this.props); |
| 62 | + componentDidMount() { |
| 63 | + Constants.addDimensionsEventListener(this.onOrientationChanged); |
61 | 64 | }
|
62 | 65 |
|
63 |
| - get pageWidth() { |
64 |
| - return Math.floor(this.props.pageWidth); |
| 66 | + componentWillUnmount() { |
| 67 | + Constants.removeDimensionsEventListener(this.onOrientationChanged); |
| 68 | + } |
| 69 | + |
| 70 | + onOrientationChanged = () => { |
| 71 | + if (!this.props.pageWidth) { |
| 72 | + this.setState({ |
| 73 | + pageWidth: Constants.screenWidth, |
| 74 | + initialOffset: {x: presenter.calcOffset(this.props, {currentPage: this.state.currentPage, pageWidth: Constants.screenWidth})} |
| 75 | + }); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + generateStyles() { |
| 80 | + this.styles = createStyles(this.props); |
65 | 81 | }
|
66 | 82 |
|
67 | 83 | updateOffset = (animated = false) => {
|
@@ -104,40 +120,42 @@ export default class Carousel extends BaseComponent {
|
104 | 120 | }
|
105 | 121 |
|
106 | 122 | const {loop} = this.props;
|
107 |
| - const offsetX = presenter.getDirectionOffset(event.nativeEvent.contentOffset.x, this.props); |
| 123 | + const {pageWidth} = this.state; |
| 124 | + const offsetX = presenter.getDirectionOffset(event.nativeEvent.contentOffset.x, this.props, pageWidth); |
108 | 125 |
|
109 | 126 | if (offsetX >= 0) {
|
110 |
| - const newPage = presenter.calcPageIndex(offsetX, this.props); |
111 |
| - |
| 127 | + const newPage = presenter.calcPageIndex(offsetX, this.props, pageWidth); |
112 | 128 | this.setState({currentPage: newPage});
|
113 | 129 | }
|
114 | 130 |
|
115 |
| - if (loop && presenter.isOutOfBounds(offsetX, this.props)) { |
| 131 | + if (loop && presenter.isOutOfBounds(offsetX, this.props, pageWidth)) { |
116 | 132 | this.updateOffset();
|
117 | 133 | }
|
118 | 134 |
|
119 | 135 | _.invoke(this.props, 'onScroll', event);
|
120 | 136 | }
|
121 | 137 |
|
122 |
| - cloneChild(child) { |
123 |
| - if (!child.key) { |
124 |
| - return child; |
125 |
| - } |
126 |
| - |
127 |
| - return React.cloneElement(child, { |
128 |
| - key: `${child.key}-clone`, |
129 |
| - }); |
| 138 | + renderChild = (child, key) => { |
| 139 | + return ( |
| 140 | + <View style={{width: this.state.pageWidth}} key={key}> |
| 141 | + {child} |
| 142 | + </View> |
| 143 | + ); |
130 | 144 | }
|
131 | 145 |
|
132 | 146 | renderChildren() {
|
133 | 147 | const {children, loop} = this.props;
|
134 | 148 | const length = presenter.getChildrenLength(this.props);
|
135 |
| - const childrenArray = React.Children.toArray(children); |
| 149 | + |
| 150 | + const childrenArray = React.Children.map(children, (child, index) => { |
| 151 | + return this.renderChild(child, `${index}`); |
| 152 | + }); |
136 | 153 |
|
137 | 154 | if (loop) {
|
138 |
| - childrenArray.unshift(this.cloneChild(children[length - 1])); |
139 |
| - childrenArray.push(this.cloneChild(children[0])); |
| 155 | + childrenArray.unshift(this.renderChild(children[length - 1], `${length - 1}-clone`)); |
| 156 | + childrenArray.push(this.renderChild(children[0], `${0}-clone`)); |
140 | 157 | }
|
| 158 | + |
141 | 159 | return childrenArray;
|
142 | 160 | }
|
143 | 161 |
|
|
0 commit comments