-
Notifications
You must be signed in to change notification settings - Fork 257
/
Copy pathindex.js
121 lines (105 loc) · 2.66 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import Swipe from 'swipe-js-iso';
import isEqual from 'lodash.isequal';
class ReactSwipe extends Component {
static propTypes = {
swipeOptions: PropTypes.shape({
startSlide: PropTypes.number,
speed: PropTypes.number,
auto: PropTypes.number,
continuous: PropTypes.bool,
disableScroll: PropTypes.bool,
stopPropagation: PropTypes.bool,
swiping: PropTypes.func,
callback: PropTypes.func,
transitionEnd: PropTypes.func
}),
style: PropTypes.shape({
container: PropTypes.object,
wrapper: PropTypes.object,
child: PropTypes.object
}),
id: PropTypes.string,
className: PropTypes.string,
childCount: PropTypes.number
};
static defaultProps = {
swipeOptions: {},
style: {
container: {
overflow: 'hidden',
visibility: 'hidden',
position: 'relative'
},
wrapper: {
overflow: 'hidden',
position: 'relative'
},
child: {
float: 'left',
width: '100%',
position: 'relative',
transitionProperty: 'transform'
}
},
className: '',
childCount: 0
};
componentDidMount() {
this.swipe = Swipe(this.containerEl, this.props.swipeOptions);
}
componentDidUpdate(prevProps) {
const { childCount, swipeOptions } = this.props;
const shouldUpdateSwipeInstance =
prevProps.childCount !== childCount ||
!isEqual(prevProps.swipeOptions, swipeOptions);
if (shouldUpdateSwipeInstance) {
this.swipe.kill();
this.swipe = Swipe(this.containerEl, this.props.swipeOptions);
}
}
componentWillUnmount() {
this.swipe.kill();
this.swipe = void 0;
}
next() {
this.swipe.next();
}
prev() {
this.swipe.prev();
}
slide(...args) {
this.swipe.slide(...args);
}
getPos() {
return this.swipe.getPos();
}
getNumSlides() {
return this.swipe.getNumSlides();
}
render() {
const { id, className, style, children } = this.props;
return (
<div
id={id}
ref={el => (this.containerEl = el)}
className={`react-swipe-container ${className}`}
style={style.container}
>
<div style={style.wrapper}>
{React.Children.map(children, child => {
if (!child) {
return null;
}
const childStyle = child.props.style
? { ...style.child, ...child.props.style }
: style.child;
return React.cloneElement(child, { style: childStyle });
})}
</div>
</div>
);
}
}
export default ReactSwipe;