-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
114 lines (100 loc) · 2.36 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
/** @jsx dom */
import condenseKeys from 'condense-keys';
import dom from 'magic-virtual-element';
import Swiper from 'swiper';
const propTypes = {
arrows: {
type: 'boolean'
},
class: {
type: 'string'
},
duration: {
type: 'number'
},
history: {
type: 'boolean'
},
loop: {
type: 'boolean'
},
onInit: {
type: 'function'
},
options: {
type: 'object'
},
pagination: {
type: 'boolean'
},
play: {
type: 'boolean'
},
speed: {
type: 'boolean'
},
vertical: {
type: 'boolean'
}
};
const defaultProps = {
duration: 3000,
loop: false,
onInit: () => {},
play: false,
speed: 300
};
const initialState = () => ({
active: 0
});
const getPagination = pagination => pagination && <div class='swiper-pagination'/>;
const init = ({props}, el) => {
const {children} = props;
const container = el.querySelector('.swiper-container');
if (container.swiper) {
container.swiper.destroy(true, true);
container.swiper = null;
}
if (children.length < 2) {
return;
}
const {arrows, duration, loop, onInit, options, pagination, play, speed, vertical} = props;
const swiper = new Swiper(container, condenseKeys(Object.assign({
autoplay: play && duration,
autoplayDisableOnInteraction: false,
direction: vertical ? 'vertical' : 'horizontal',
loop,
paginationClickable: true,
speed
}, options, {
nextButton: arrows ? '.swiper-button-next' : null,
pagination: pagination ? '.swiper-pagination' : null,
prevButton: arrows ? '.swiper-button-prev' : null,
slideClass: 'swiper-slide',
wrapperClass: 'swiper-wrapper'
})));
onInit(swiper);
};
const getArrows = arrows => arrows && (
<div class='Swiper-controls'>
<div class='swiper-button-prev'/>
<div class='swiper-button-next'/>
</div>
);
const afterRender = ({props}, el) => requestAnimationFrame(() => init({props}, el));
const render = ({props}) => {
const {arrows, children, history, pagination} = props;
const items = children.map((x, i) => <div key={i} class='swiper-slide' {...history ? {'data-history': `slide-${i + 1}`} : {}}>{x}</div>); // eslint-disable-line react/no-array-index-key
return (
<div class={['Swiper', props.class]}>
<div class='swiper-container'>
<div class='swiper-wrapper'>
{items}
</div>
{getArrows(arrows)}
{getPagination(pagination)}
</div>
</div>
);
};
export default {afterRender, defaultProps, initialState, propTypes, render};