@tarojs/components-react 4.0.4 → 4.0.5-alpha.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,13 +1,16 @@
1
- import { __rest } from 'tslib';
2
- import 'swiper/swiper-bundle.min.css';
1
+ import { __rest, __classPrivateFieldGet, __classPrivateFieldSet } from 'tslib';
2
+ import 'swiper/swiper-bundle.css';
3
3
  import './style/index.css.js';
4
4
  import classNames from 'classnames';
5
5
  import React__default from 'react';
6
- import Swipers from 'swiper/swiper-bundle.esm.js';
6
+ import Swipers from 'swiper/bundle';
7
7
  import { debounce, createForwardRefComponent } from '../../utils/index.js';
8
8
  import { jsx, jsxs } from 'react/jsx-runtime';
9
9
 
10
+ var _SwiperInner_source, _SwiperInner_swiperResetting, _SwiperInner_lastSwiperActiveIndex, _SwiperInner_domChangeByOutSide;
10
11
  let INSTANCE_ID = 0;
12
+ const ONE_ADDITIONAL_SLIDES_THRESHOLD = 5;
13
+ const TWO_ADDITIONAL_SLIDES_THRESHOLD = 7;
11
14
  const createEvent = type => {
12
15
  let e;
13
16
  try {
@@ -45,181 +48,329 @@ class SwiperItemInner extends React__default.Component {
45
48
  }
46
49
  }
47
50
  class SwiperInner extends React__default.Component {
48
- constructor() {
49
- super(...arguments);
51
+ constructor(props) {
52
+ var _this;
53
+ super(props);
54
+ _this = this;
50
55
  this._id = 1 + INSTANCE_ID++;
51
- this._$current = 0;
52
- this._$width = 0;
53
- this._$height = 0;
54
- this.handleSwiperLoopListen = () => {
55
- var _a, _b, _c, _d;
56
- (_b = (_a = this.observerFirst) === null || _a === void 0 ? void 0 : _a.disconnect) === null || _b === void 0 ? void 0 : _b.call(_a);
57
- (_d = (_c = this.observerLast) === null || _c === void 0 ? void 0 : _c.disconnect) === null || _d === void 0 ? void 0 : _d.call(_c);
58
- this.observerFirst = new MutationObserver(this.handleSwiperLoop);
59
- this.observerLast = new MutationObserver(this.handleSwiperLoop);
60
- const wrapper = this.mySwiper.$wrapperEl[0];
61
- const list = wrapper.querySelectorAll('taro-swiper-item-core:not(.swiper-slide-duplicate)');
62
- if (list.length >= 1) {
63
- this.observerFirst.observe(list[0], {
64
- characterData: true
65
- });
66
- } else if (list.length >= 2) {
67
- this.observerLast.observe(list[list.length - 1], {
68
- characterData: true
69
- });
56
+ _SwiperInner_source.set(this, 'autoplay');
57
+ _SwiperInner_swiperResetting.set(this, false);
58
+ _SwiperInner_lastSwiperActiveIndex.set(this, 0
59
+ // dom 变化是否由外部引起,因为 swiper 的循环模式也会引起 dom 的变化。如果不是由外部引起的 dom 变化,就不需要重新初始化 swiper
60
+ );
61
+ // dom 变化是否由外部引起,因为 swiper 的循环模式也会引起 dom 的变化。如果不是由外部引起的 dom 变化,就不需要重新初始化 swiper
62
+ _SwiperInner_domChangeByOutSide.set(this, false);
63
+ this.handleInit = function () {
64
+ let reset = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
65
+ const {
66
+ autoplay = false,
67
+ circular = true,
68
+ current = 0,
69
+ displayMultipleItems = 1,
70
+ duration = 500,
71
+ interval = 5000,
72
+ currentItemId,
73
+ effectsProps = {},
74
+ vertical
75
+ } = _this.props;
76
+ let initialSlide = parseInt(String(current), 10);
77
+ if (reset) {
78
+ initialSlide = __classPrivateFieldGet(_this, _SwiperInner_lastSwiperActiveIndex, "f");
79
+ } else {
80
+ if (currentItemId) {
81
+ let itemIdIndex = 0;
82
+ _this.getSlidersList().forEach((swiperItem, index) => {
83
+ // @ts-ignore
84
+ if (swiperItem.itemId && swiperItem.itemId === currentItemId) {
85
+ itemIdIndex = index;
86
+ }
87
+ });
88
+ initialSlide = itemIdIndex;
89
+ }
70
90
  }
71
- };
72
- this.handleSwiperLoop = debounce(() => {
73
- if (this.mySwiper && this.mySwiper.$wrapperEl && this.props.circular) {
91
+ const loopAdditionalSlides = _this.getLoopAdditionalSlides();
92
+ const centeredSlides = parseFloat(String(displayMultipleItems)) === 1;
93
+ const slidesPerView = parseFloat(String(displayMultipleItems)) === 1 ? 'auto' : displayMultipleItems;
94
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
95
+ const that = _this;
96
+ const opt = Object.assign(Object.assign({
97
+ // 指示器
98
+ pagination: {
99
+ el: `.taro-swiper-${_this._id} > .swiper-container > .swiper-pagination`
100
+ },
101
+ direction: vertical ? 'vertical' : 'horizontal',
102
+ loop: circular,
103
+ slidesPerView: slidesPerView,
104
+ initialSlide,
105
+ speed: parseInt(String(duration), 10),
106
+ observer: true,
107
+ observeParents: true,
108
+ loopAdditionalSlides,
109
+ centeredSlides
110
+ }, effectsProps), {
111
+ on: {
112
+ init(_swiper) {
113
+ that.getNeedFixLoop() && _swiper.loopFix();
114
+ that.props.autoplay && _swiper.autoplay.start();
115
+ },
116
+ transitionEnd(_swiper) {
117
+ if (__classPrivateFieldGet(that, _SwiperInner_swiperResetting, "f") || __classPrivateFieldGet(that, _SwiperInner_lastSwiperActiveIndex, "f") === _swiper.realIndex) return;
118
+ __classPrivateFieldSet(that, _SwiperInner_lastSwiperActiveIndex, _swiper.realIndex, "f");
119
+ that.getNeedFixLoop() && _swiper.loopFix();
120
+ that.props.autoplay && _swiper.autoplay.start();
121
+ const e = createEvent('touchend');
122
+ try {
123
+ const currentId = that.getCurrentId(_swiper);
124
+ Object.defineProperty(e, 'detail', {
125
+ enumerable: true,
126
+ value: {
127
+ source: __classPrivateFieldGet(that, _SwiperInner_source, "f"),
128
+ current: this.realIndex,
129
+ currentId: currentId
130
+ }
131
+ });
132
+ } catch (err) {} // eslint-disable-line no-empty
133
+ that.handleOnAnimationFinish(e);
134
+ __classPrivateFieldSet(that, _SwiperInner_source, 'autoplay', "f");
135
+ },
136
+ touchMove() {
137
+ __classPrivateFieldSet(that, _SwiperInner_source, 'touch', "f");
138
+ },
139
+ touchEnd: _swiper => {
140
+ __classPrivateFieldSet(that, _SwiperInner_source, 'touch', "f");
141
+ that.props.autoplay && _swiper.autoplay.start();
142
+ },
143
+ touchStart: _swiper => {
144
+ __classPrivateFieldSet(that, _SwiperInner_source, 'touch', "f");
145
+ that.props.autoplay && _swiper.autoplay.pause();
146
+ },
147
+ slideChange(_swiper) {
148
+ if (__classPrivateFieldGet(that, _SwiperInner_swiperResetting, "f") || __classPrivateFieldGet(that, _SwiperInner_lastSwiperActiveIndex, "f") === _swiper.realIndex) return;
149
+ const e = createEvent('touchend');
150
+ try {
151
+ const currentId = that.getCurrentId(_swiper);
152
+ Object.defineProperty(e, 'detail', {
153
+ enumerable: true,
154
+ value: {
155
+ current: this.realIndex,
156
+ source: __classPrivateFieldGet(that, _SwiperInner_source, "f"),
157
+ currentId
158
+ }
159
+ });
160
+ } catch (err) {} // eslint-disable-line no-empty
161
+ that.handleOnChange(e);
162
+ },
163
+ autoplay(_swiper) {
164
+ // Note: 修复 autoplay 时,切换到其他页面再切回来,autoplay 会停止的问题
165
+ _swiper.animating = false;
166
+ __classPrivateFieldSet(that, _SwiperInner_source, 'autoplay', "f");
167
+ }
168
+ }
169
+ });
170
+ // 自动播放
171
+ if (autoplay) {
172
+ opt.autoplay = {
173
+ delay: parseInt(String(interval), 10),
174
+ disableOnInteraction: false
175
+ };
176
+ }
177
+ _this.swiper = new Swipers(_this.$el, opt);
178
+ // Note: 这里是拦截了 swiper 的 minTranslate 和 maxTranslate 方法,手动修复了 loop 模式下的 margin 问题
179
+ // 因为这两个属性会影响滑动到哪个位置进行 fixloop
180
+ // 可参考源码:https://github.com/nolimits4web/swiper/blob/v11.1.0/src/core/events/onTouchMove.mjs
181
+ // https://github.com/nolimits4web/swiper/blob/v11.1.0/src/core/loop/loopFix.mjs
182
+ if (_this.getNeedFixLoop()) {
74
183
  // @ts-ignore
75
- this.mySwiper.loopDestroy();
184
+ const minTranslate = _this.swiper.minTranslate.bind(_this.swiper);
76
185
  // @ts-ignore
77
- this.mySwiper.loopCreate();
186
+ const maxTranslate = _this.swiper.maxTranslate.bind(_this.swiper);
187
+ if (centeredSlides && _this.getSlidersList().length < 4) {
188
+ // @ts-ignore
189
+ _this.swiper.minTranslate = () => minTranslate() + _this.parseMargin()[1];
190
+ // @ts-ignore
191
+ _this.swiper.maxTranslate = () => maxTranslate() - _this.parseMargin()[0];
192
+ } else {
193
+ // @ts-ignore
194
+ _this.swiper.minTranslate = () => minTranslate() - _this.parseMargin()[0];
195
+ // @ts-ignore
196
+ _this.swiper.maxTranslate = () => maxTranslate() + _this.parseMargin()[1];
197
+ }
198
+ }
199
+ _this.setState({
200
+ swiperWrapper: _this.swiper.wrapperEl
201
+ });
202
+ };
203
+ this.handleSwiperSizeDebounce = debounce(() => {
204
+ if (!this.swiper) return;
205
+ if (this.props.circular) {
206
+ if (__classPrivateFieldGet(this, _SwiperInner_domChangeByOutSide, "f")) {
207
+ this.reset();
208
+ __classPrivateFieldSet(this, _SwiperInner_domChangeByOutSide, false, "f");
209
+ }
210
+ } else {
211
+ this.swiper.update();
212
+ __classPrivateFieldSet(this, _SwiperInner_swiperResetting, false, "f");
78
213
  }
79
- }, 500);
214
+ }, 50);
215
+ this.reset = () => {
216
+ if (!this.swiper) return;
217
+ __classPrivateFieldSet(this, _SwiperInner_swiperResetting, true, "f");
218
+ __classPrivateFieldSet(this, _SwiperInner_lastSwiperActiveIndex, this.swiper.realIndex, "f");
219
+ this.swiper.destroy();
220
+ this.handleInit(true);
221
+ __classPrivateFieldSet(this, _SwiperInner_swiperResetting, false, "f");
222
+ };
223
+ // 以下为方法函数
224
+ this.getSlidersList = () => {
225
+ var _a, _b;
226
+ return ((_b = (_a = this.$el) === null || _a === void 0 ? void 0 : _a.querySelectorAll) === null || _b === void 0 ? void 0 : _b.call(_a, '.swiper-slide')) || [];
227
+ };
228
+ // 获取是否需要手动修复 loop 的条件
229
+ this.getNeedFixLoop = () => {
230
+ const margins = this.parseMargin();
231
+ const hasMargin = margins.filter(Boolean).length > 0;
232
+ return this.props.circular && hasMargin;
233
+ };
234
+ this.parseMargin = () => {
235
+ const {
236
+ previousMargin = '0px',
237
+ nextMargin = '0px'
238
+ } = this.props;
239
+ const [, pM] = /^(\d+)px/.exec(previousMargin) || [];
240
+ const [, nN] = /^(\d+)px/.exec(nextMargin) || [];
241
+ return [parseInt(pM) || 0, parseInt(nN) || 0];
242
+ };
243
+ this.state = {
244
+ swiperWrapper: null
245
+ };
80
246
  }
81
247
  componentDidMount() {
82
- const {
83
- autoplay = false,
84
- circular = true,
85
- current = 0,
86
- displayMultipleItems = 1,
87
- duration = 500,
88
- interval = 5000,
89
- spaceBetween,
90
- vertical
91
- } = this.props;
92
- // eslint-disable-next-line @typescript-eslint/no-this-alias
93
- const that = this;
94
- const opt = {
95
- // 指示器
96
- pagination: {
97
- el: `.taro-swiper-${this._id} > .swiper-container > .swiper-pagination`
98
- },
99
- direction: vertical ? 'vertical' : 'horizontal',
100
- loop: circular,
101
- slidesPerView: parseFloat(String(displayMultipleItems)),
102
- initialSlide: parseInt(String(current), 10),
103
- speed: parseInt(String(duration), 10),
104
- observer: true,
105
- observeParents: true,
106
- on: {
107
- slideChange() {
108
- const e = createEvent('touchend');
109
- try {
110
- Object.defineProperty(e, 'detail', {
111
- enumerable: true,
112
- value: {
113
- current: this.realIndex
114
- }
115
- });
116
- } catch (err) {} // eslint-disable-line no-empty
117
- that._$current = this.realIndex;
118
- that.handleOnChange(e);
119
- },
120
- transitionEnd() {
121
- const e = createEvent('touchend');
122
- try {
123
- Object.defineProperty(e, 'detail', {
124
- enumerable: true,
125
- value: {
126
- current: this.mySwiper.realIndex
127
- }
128
- });
129
- if (this.mySwiper.isBeginning) {
130
- this.mySwiper.slideToLoop(this.props.children.length - 1, 0);
131
- } else if (this.mySwiper.isEnd) {
132
- this.mySwiper.slideToLoop(0, 0);
133
- }
134
- } catch (err) {} // eslint-disable-line no-empty
135
- that.handleOnAnimationFinish(e);
136
- },
137
- observerUpdate(_swiper, e) {
138
- const target = e.target;
139
- const className = target && typeof target.className === 'string' ? target.className : '';
140
- if (className.includes('taro_page') && target.style.display !== 'none') {
141
- if (that.props.autoplay && target.contains(_swiper.$el[0])) {
142
- if (that.props.circular) {
143
- _swiper.slideToLoop(this.realIndex, 0); // 更新下标
144
- } else {
145
- _swiper.slideTo(this.realIndex);
146
- }
147
- }
148
- }
149
- }
248
+ this.handleInit();
249
+ }
250
+ hackSwiperWrapDomAction() {
251
+ if (!this.state.swiperWrapper || !this.swiper) return;
252
+ const appendChild = this.state.swiperWrapper.appendChild.bind(this.state.swiperWrapper);
253
+ const removeChild = this.state.swiperWrapper.removeChild.bind(this.state.swiperWrapper);
254
+ const replaceChild = this.state.swiperWrapper.replaceChild.bind(this.state.swiperWrapper);
255
+ const insertBefore = this.state.swiperWrapper.insertBefore.bind(this.state.swiperWrapper);
256
+ const beforeAction = () => {
257
+ if (!this.state.swiperWrapper || !this.swiper) return;
258
+ __classPrivateFieldSet(this, _SwiperInner_swiperResetting, true, "f");
259
+ if (!__classPrivateFieldGet(this, _SwiperInner_domChangeByOutSide, "f") && this.props.circular) {
260
+ // 如果是由于外部子节点的变化引起的 dom 变化,需要重新初始化 swiper。
261
+ // 在初dom操作之前,需要调用 loopDestroy,把子节点的顺序恢复
262
+ __classPrivateFieldSet(this, _SwiperInner_domChangeByOutSide, true, "f");
263
+ this.swiper.loopDestroy();
264
+ this.swiper.params.loop = false;
150
265
  }
151
266
  };
152
- // 自动播放
153
- if (autoplay) {
154
- opt.autoplay = {
155
- delay: parseInt(String(interval), 10),
156
- disableOnInteraction: false
157
- };
267
+ // eslint-disable-next-line react/no-direct-mutation-state
268
+ this.state.swiperWrapper.appendChild = function () {
269
+ beforeAction();
270
+ return appendChild(...arguments);
271
+ };
272
+ // eslint-disable-next-line react/no-direct-mutation-state
273
+ this.state.swiperWrapper.removeChild = function () {
274
+ beforeAction();
275
+ return removeChild(...arguments);
276
+ };
277
+ // eslint-disable-next-line react/no-direct-mutation-state
278
+ this.state.swiperWrapper.replaceChild = function () {
279
+ beforeAction();
280
+ return replaceChild(...arguments);
281
+ };
282
+ // eslint-disable-next-line react/no-direct-mutation-state
283
+ this.state.swiperWrapper.insertBefore = function () {
284
+ beforeAction();
285
+ return insertBefore(...arguments);
286
+ };
287
+ }
288
+ componentDidUpdate(prevProps, pervState) {
289
+ if (!this.swiper || !this.state.swiperWrapper) return;
290
+ if (pervState.swiperWrapper !== this.state.swiperWrapper && this.state.swiperWrapper) {
291
+ this.observer && this.observer.disconnect();
292
+ this.observer = new MutationObserver(this.handleSwiperSizeDebounce);
293
+ this.observer.observe(this.state.swiperWrapper, {
294
+ childList: true
295
+ });
296
+ this.hackSwiperWrapDomAction();
158
297
  }
159
- // 两端距离
160
- if (spaceBetween) {
161
- opt.spaceBetween = spaceBetween;
298
+ if (prevProps.circular !== this.props.circular || prevProps.displayMultipleItems !== this.props.displayMultipleItems) {
299
+ this.reset();
162
300
  }
163
- this.mySwiper = new Swipers(this.$el, opt);
164
- setTimeout(() => {
165
- this.mySwiper.update();
166
- }, 500);
167
- if (!this.mySwiper || !this.props.circular) return;
168
- const wrapper = this.mySwiper.$wrapperEl[0];
169
- this.observer = new MutationObserver(this.handleSwiperLoopListen);
170
- this.observer.observe(wrapper, {
171
- childList: true
172
- });
173
- }
174
- UNSAFE_componentWillReceiveProps(nextProps) {
175
- if (this.mySwiper) {
176
- const nextCurrent = typeof nextProps.current === 'number' ? nextProps.current : this._$current || 0;
177
- this.handleSwiperLoop();
178
- // 是否衔接滚动模式
179
- if (nextProps.circular) {
180
- if (!this.mySwiper.isBeginning && !this.mySwiper.isEnd) {
181
- this.mySwiper.slideToLoop(parseInt(nextCurrent, 10)); // 更新下标
182
- }
301
+ if (prevProps.interval !== this.props.interval) {
302
+ if (typeof this.swiper.params.autoplay === 'object') {
303
+ this.swiper.params.autoplay.delay = this.props.interval;
304
+ }
305
+ }
306
+ if (prevProps.duration !== this.props.duration) {
307
+ this.swiper.params.speed = this.props.duration;
308
+ }
309
+ if (prevProps.current !== this.props.current && !this.props.currentItemId) {
310
+ const n = parseInt(String(this.props.current), 10);
311
+ if (isNaN(n) || n === this.swiper.realIndex) return;
312
+ __classPrivateFieldSet(this, _SwiperInner_source, '', "f");
313
+ if (this.props.circular) {
314
+ this.swiper.slideToLoop(n); // 更新下标
315
+ this.props.autoplay && this.swiper.autoplay.pause();
316
+ // @ts-ignore
317
+ this.swiper.loopFix();
318
+ this.props.autoplay && this.swiper.autoplay.start();
183
319
  } else {
184
- this.mySwiper.slideTo(parseInt(nextCurrent, 10) + 1); // 更新下标
320
+ this.swiper.slideTo(n); // 更新下标
185
321
  }
186
- const autoplay = this.mySwiper.autoplay;
187
- // 判断是否需要停止或开始自动轮播
188
- if (autoplay.running !== nextProps.autoplay) {
189
- if (nextProps.autoplay) {
190
- if (typeof this.mySwiper.params.autoplay === 'object') {
191
- this.mySwiper.params.autoplay.disableOnInteraction = false;
192
- this.mySwiper.params.autoplay.delay = parseInt(String(this.props.interval) || '3000', 10);
322
+ }
323
+ if (prevProps.autoplay !== this.props.autoplay) {
324
+ const swiperAutoplay = this.swiper.autoplay;
325
+ if (swiperAutoplay) {
326
+ if (swiperAutoplay.running === this.props.autoplay) return;
327
+ if (this.props.autoplay) {
328
+ if (this.swiper.params && typeof this.swiper.params.autoplay === 'object') {
329
+ if (this.swiper.params.autoplay.disableOnInteraction === true) {
330
+ this.swiper.params.autoplay.disableOnInteraction = false;
331
+ }
332
+ this.swiper.params.autoplay.delay = this.props.interval;
193
333
  }
194
- autoplay.start();
334
+ swiperAutoplay.start();
195
335
  } else {
196
- autoplay.stop();
336
+ swiperAutoplay.stop();
197
337
  }
198
338
  }
199
- this.mySwiper.update(); // 更新子元素
200
- }
201
- }
202
- componentDidUpdate(preProps) {
203
- if (preProps.children.length === 0 && this.props.children.length > 0) {
204
- this.mySwiper.loopDestroy();
205
- this.mySwiper.loopCreate();
206
339
  }
207
- if (!this.mySwiper) return;
208
- if (this.props.autoplay) {
209
- if (this._$width !== this.mySwiper.width || this._$height !== this.mySwiper.height) {
210
- this.mySwiper.autoplay.start();
340
+ if (prevProps.currentItemId !== this.props.currentItemId) {
341
+ let itemIdIndex = 0;
342
+ this.getSlidersList().forEach((swiperItem, index) => {
343
+ const itemId = swiperItem.getAttribute('item-id');
344
+ // @ts-ignore
345
+ if (itemId === this.props.currentItemId) {
346
+ if (this.props.circular) {
347
+ itemIdIndex = Number(swiperItem.getAttribute('data-swiper-slide-index'));
348
+ } else {
349
+ itemIdIndex = index;
350
+ }
351
+ }
352
+ });
353
+ if (isNaN(itemIdIndex) || itemIdIndex === this.swiper.realIndex) return;
354
+ __classPrivateFieldSet(this, _SwiperInner_source, '', "f");
355
+ if (this.props.circular) {
356
+ this.swiper.slideToLoop(itemIdIndex); // 更新下标
357
+ this.props.autoplay && this.swiper.autoplay.pause();
358
+ // @ts-ignore
359
+ this.swiper.loopFix();
360
+ this.props.autoplay && this.swiper.autoplay.start();
361
+ } else {
362
+ this.swiper.slideTo(itemIdIndex); // 更新下标
211
363
  }
212
364
  }
213
- this._$width = this.mySwiper.width;
214
- this._$height = this.mySwiper.height;
215
365
  }
216
366
  componentWillUnmount() {
217
- var _a, _b, _c, _d, _e, _f;
367
+ var _a, _b, _c, _d;
218
368
  this.$el = null;
219
- if (this.mySwiper) this.mySwiper.destroy();
220
- (_b = (_a = this.observer) === null || _a === void 0 ? void 0 : _a.disconnect) === null || _b === void 0 ? void 0 : _b.call(_a);
221
- (_d = (_c = this.observerFirst) === null || _c === void 0 ? void 0 : _c.disconnect) === null || _d === void 0 ? void 0 : _d.call(_c);
222
- (_f = (_e = this.observerLast) === null || _e === void 0 ? void 0 : _e.disconnect) === null || _f === void 0 ? void 0 : _f.call(_e);
369
+ (_b = (_a = this.swiper) === null || _a === void 0 ? void 0 : _a.destroy) === null || _b === void 0 ? void 0 : _b.call(_a);
370
+ (_d = (_c = this.observer) === null || _c === void 0 ? void 0 : _c.disconnect) === null || _d === void 0 ? void 0 : _d.call(_c);
371
+ this.setState({
372
+ swiperWrapper: null
373
+ });
223
374
  }
224
375
  handleOnChange(e) {
225
376
  const func = this.props.onChange;
@@ -229,35 +380,43 @@ class SwiperInner extends React__default.Component {
229
380
  const func = this.props.onAnimationFinish;
230
381
  typeof func === 'function' && func(e);
231
382
  }
232
- parsePX() {
233
- let s = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '0px';
234
- return parseFloat(s.replace(/r*px/i, ''));
383
+ // Note: loop 的时候添加 additionalSlides 可以避免循环的时候由于 loopFix 不及时,出现空白的问题。但是并不是 additionalSlides 越多越好,因为 additionalSlides 越多,如果 swiper-item 的数量不够,会导致出现 bug。
384
+ // 目前的策略是 swiper-item 的数量小于等于 5 时,不添加 additionalSlides,大于 5 小于等于 7 时,添加 1 additionalSlides,大于 7 时,添加 2 个 additionalSlides。
385
+ getLoopAdditionalSlides() {
386
+ const slidersLength = this.getSlidersList().length;
387
+ if (!this.$el || !this.getNeedFixLoop() || slidersLength < ONE_ADDITIONAL_SLIDES_THRESHOLD) return 0;
388
+ if (slidersLength <= TWO_ADDITIONAL_SLIDES_THRESHOLD) return 1;
389
+ return 2;
390
+ }
391
+ getCurrentId(swiper) {
392
+ const slides = swiper.slides;
393
+ const activeIndex = swiper.activeIndex;
394
+ const currentSlide = slides[activeIndex];
395
+ return currentSlide.getAttribute('item-id');
235
396
  }
236
397
  render() {
237
398
  const {
238
399
  className,
239
400
  style,
240
401
  vertical,
241
- previousMargin,
242
- nextMargin,
243
402
  indicatorColor,
244
403
  indicatorActiveColor,
245
404
  forwardedRef
246
405
  } = this.props;
247
406
  const defaultIndicatorColor = indicatorColor || 'rgba(0, 0, 0, .3)';
248
407
  const defaultIndicatorActiveColor = indicatorActiveColor || '#000';
408
+ const [pM, nM] = this.parseMargin();
249
409
  const cls = classNames(`taro-swiper-${this._id}`, className);
250
- const sty = Object.assign({
251
- paddingTop: vertical ? this.parsePX(previousMargin) : 0,
252
- paddingRight: vertical ? 0 : this.parsePX(nextMargin),
253
- paddingBottom: vertical ? this.parsePX(nextMargin) : 0,
254
- paddingLeft: vertical ? 0 : this.parsePX(previousMargin),
255
- overflow: 'hidden'
256
- }, style);
257
- const paginationCls = classNames('swiper-pagination', {
258
- 'swiper-pagination-hidden': !this.props.indicatorDots,
259
- 'swiper-pagination-bullets': this.props.indicatorDots
260
- });
410
+ const sty = Object.assign({}, style);
411
+ const hasMargin = pM || nM;
412
+ if (hasMargin) {
413
+ sty.overflow = 'hidden';
414
+ }
415
+ if (this.props.full) {
416
+ sty.height = '100%';
417
+ }
418
+ const swiperContainerStyleList = ['overflow: visible;', vertical ? `margin-top: ${pM}px; margin-bottom: ${nM}px;` : `margin-right: ${nM}px; margin-left: ${pM}px;`, this.props.full ? 'height: 100%;' : ''];
419
+ const swiperPaginationStyleList = [this.props.indicatorDots ? 'opacity: 1;' : 'display: none;', 'font-size: 0;'];
261
420
  return /*#__PURE__*/jsx("div", {
262
421
  className: `swiper-container-wrapper ${cls}`,
263
422
  style: sty,
@@ -279,18 +438,21 @@ class SwiperInner extends React__default.Component {
279
438
  __html: `<style type='text/css'>
280
439
  .taro-swiper-${this._id} > .swiper-container > .swiper-pagination > .swiper-pagination-bullet { background: ${defaultIndicatorColor} }
281
440
  .taro-swiper-${this._id} > .swiper-container > .swiper-pagination > .swiper-pagination-bullet-active { background: ${defaultIndicatorActiveColor} }
441
+ .taro-swiper-${this._id} > .swiper-container { ${swiperContainerStyleList.join('')} }
442
+ .taro-swiper-${this._id} > .swiper-container > .swiper-pagination { ${swiperPaginationStyleList.join('')} }
282
443
  </style>`
283
444
  }
284
445
  }), /*#__PURE__*/jsx("div", {
285
446
  className: "swiper-wrapper",
286
447
  children: this.props.children
287
448
  }), /*#__PURE__*/jsx("div", {
288
- className: paginationCls
449
+ className: "swiper-pagination"
289
450
  })]
290
451
  })
291
452
  });
292
453
  }
293
454
  }
455
+ _SwiperInner_source = new WeakMap(), _SwiperInner_swiperResetting = new WeakMap(), _SwiperInner_lastSwiperActiveIndex = new WeakMap(), _SwiperInner_domChangeByOutSide = new WeakMap();
294
456
  const Swiper = createForwardRefComponent(SwiperInner);
295
457
  const SwiperItem = createForwardRefComponent(SwiperItemInner);
296
458