@egjs/react-flicking 4.2.4 → 4.4.0

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.
@@ -4,11 +4,12 @@ name: @egjs/react-flicking
4
4
  license: MIT
5
5
  author: NAVER Corp.
6
6
  repository: https://github.com/naver/egjs-flicking/tree/master/packages/react-flicking
7
- version: 4.2.4
7
+ version: 4.4.0
8
8
  */
9
9
  'use strict';
10
10
 
11
11
  var React = require('react');
12
+ var Component = require('@egjs/component');
12
13
  var ListDiffer = require('@egjs/list-differ');
13
14
  var VanillaFlicking = require('@egjs/flicking');
14
15
  var reactIs = require('react-is');
@@ -227,7 +228,6 @@ var DEFAULT_PROPS = {
227
228
  plugins: [],
228
229
  useFindDOMNode: false,
229
230
  hideBeforeInit: false,
230
- firstPanelSize: undefined,
231
231
  onReady: function (e) {},
232
232
  onBeforeResize: function (e) {},
233
233
  onAfterResize: function (e) {},
@@ -247,41 +247,9 @@ var DEFAULT_PROPS = {
247
247
  onPanelChange: function (e) {}
248
248
  };
249
249
 
250
- var ReactPanel = function (_super) {
251
- __extends(ReactPanel, _super);
252
-
253
- function ReactPanel() {
254
- return _super !== null && _super.apply(this, arguments) || this;
255
- }
256
-
257
- var __proto = ReactPanel.prototype;
258
- Object.defineProperty(__proto, "element", {
259
- get: function () {
260
- return this._externalComponent.element;
261
- },
262
- enumerable: false,
263
- configurable: true
264
- });
265
- Object.defineProperty(__proto, "rendered", {
266
- get: function () {
267
- return !this._externalComponent.hide;
268
- },
269
- enumerable: false,
270
- configurable: true
271
- });
272
-
273
- __proto.markForShow = function () {
274
- this._externalComponent.hide = false;
275
- };
276
-
277
- __proto.markForHide = function () {
278
- this._externalComponent.hide = true;
279
- };
280
-
281
- return ReactPanel;
282
- }(VanillaFlicking.ExternalPanel);
283
-
284
- var ReactRenderer = function (_super) {
250
+ var ReactRenderer =
251
+ /*#__PURE__*/
252
+ function (_super) {
285
253
  __extends(ReactRenderer, _super);
286
254
 
287
255
  function ReactRenderer(options) {
@@ -296,21 +264,18 @@ var ReactRenderer = function (_super) {
296
264
 
297
265
  __proto.render = function () {
298
266
  return __awaiter(this, void 0, void 0, function () {
299
- var flicking, reactFlicking;
267
+ var flicking, reactFlicking, strategy;
300
268
  return __generator(this, function (_a) {
301
- flicking = this._flicking;
269
+ flicking = VanillaFlicking.getFlickingAttached(this._flicking);
302
270
  reactFlicking = this._reactFlicking;
303
- if (!flicking || !reactFlicking.mounted) return [2
304
- /*return*/
305
- ];
306
-
307
- this._updateRenderingPanels();
308
-
271
+ strategy = this._strategy;
272
+ strategy.updateRenderingPanels(flicking);
273
+ strategy.renderPanels(flicking);
309
274
  return [2
310
275
  /*return*/
311
276
  , new Promise(function (resolve) {
312
- reactFlicking.setRenderCallback(resolve);
313
- reactFlicking.setState({});
277
+ reactFlicking.renderEmitter.once("render", resolve);
278
+ reactFlicking.forceUpdate();
314
279
  })];
315
280
  });
316
281
  });
@@ -320,164 +285,226 @@ var ReactRenderer = function (_super) {
320
285
  return __awaiter(this, void 0, void 0, function () {
321
286
  var reactFlicking;
322
287
  return __generator(this, function (_a) {
323
- reactFlicking = this._reactFlicking;
324
- if (!reactFlicking.mounted) return [2
325
- /*return*/
326
- ];
327
-
328
- this._panels.forEach(function (panel) {
329
- return panel.markForShow();
330
- });
331
-
332
- return [2
333
- /*return*/
334
- , new Promise(function (resolve) {
335
- reactFlicking.setRenderCallback(resolve);
336
- reactFlicking.setState({});
337
- })];
288
+ switch (_a.label) {
289
+ case 0:
290
+ reactFlicking = this._reactFlicking;
291
+ return [4
292
+ /*yield*/
293
+ , _super.prototype.forceRenderAllPanels.call(this)];
294
+
295
+ case 1:
296
+ _a.sent();
297
+
298
+ return [2
299
+ /*return*/
300
+ , new Promise(function (resolve) {
301
+ reactFlicking.renderEmitter.once("render", resolve);
302
+ reactFlicking.forceUpdate();
303
+ })];
304
+ }
338
305
  });
339
306
  });
340
307
  };
341
308
 
342
309
  __proto._collectPanels = function () {
343
- var _this = this;
344
-
345
- var align = this._getPanelAlign();
346
-
347
- var children = this._reactFlicking.reactPanels;
348
- this._panels = children.map(function (panelComponent, index) {
349
- return new ReactPanel({
350
- flicking: _this._flicking,
351
- index: index,
352
- align: align,
353
- externalComponent: panelComponent
354
- });
355
- });
310
+ var flicking = VanillaFlicking.getFlickingAttached(this._flicking);
311
+ var reactFlicking = this._reactFlicking;
312
+ var reactPanels = reactFlicking.reactPanels;
313
+ this._panels = this._strategy.collectPanels(flicking, reactPanels);
356
314
  };
357
315
 
358
316
  __proto._createPanel = function (externalComponent, options) {
359
- return new ReactPanel(__assign({
360
- externalComponent: externalComponent
361
- }, options));
317
+ return this._strategy.createPanel(externalComponent, options);
362
318
  };
363
319
 
364
320
  return ReactRenderer;
365
321
  }(VanillaFlicking.ExternalRenderer);
366
322
 
367
- var NonStrictPanelComponent = function (_super) {
368
- __extends(NonStrictPanelComponent, _super);
323
+ var StrictPanel =
324
+ /*#__PURE__*/
325
+ function (_super) {
326
+ __extends(StrictPanel, _super);
369
327
 
370
- function NonStrictPanelComponent() {
328
+ function StrictPanel() {
371
329
  var _this = _super !== null && _super.apply(this, arguments) || this;
372
330
 
373
- _this.hide = false;
331
+ _this._hide = false;
332
+ _this._elRef = React.createRef();
374
333
  return _this;
375
334
  }
376
335
 
377
- var __proto = NonStrictPanelComponent.prototype;
378
- Object.defineProperty(__proto, "element", {
336
+ var __proto = StrictPanel.prototype;
337
+ Object.defineProperty(__proto, "nativeElement", {
379
338
  get: function () {
380
- return reactDom.findDOMNode(this);
339
+ return this._elRef.current;
340
+ },
341
+ enumerable: false,
342
+ configurable: true
343
+ });
344
+ Object.defineProperty(__proto, "rendered", {
345
+ get: function () {
346
+ return !this._hide;
381
347
  },
382
348
  enumerable: false,
383
349
  configurable: true
384
350
  });
385
351
 
386
352
  __proto.render = function () {
387
- return this.hide ? React.createElement(React.Fragment, null) : this.props.children;
353
+ return this._hide ? React.createElement(React.Fragment, null) : this._getElement();
388
354
  };
389
355
 
390
- return NonStrictPanelComponent;
356
+ __proto.show = function () {
357
+ this._hide = false;
358
+ };
359
+
360
+ __proto.hide = function () {
361
+ this._hide = true;
362
+ };
363
+
364
+ __proto._getElement = function () {
365
+ return React.cloneElement(React.Children.only(this.props.children), {
366
+ ref: this._elRef
367
+ });
368
+ };
369
+
370
+ return StrictPanel;
391
371
  }(React.Component);
392
372
 
393
- var StrictPanelComponent = function (_super) {
394
- __extends(StrictPanelComponent, _super);
373
+ var NonStrictPanel =
374
+ /*#__PURE__*/
375
+ function (_super) {
376
+ __extends(NonStrictPanel, _super);
395
377
 
396
- function StrictPanelComponent() {
378
+ function NonStrictPanel() {
397
379
  var _this = _super !== null && _super.apply(this, arguments) || this;
398
380
 
399
- _this.hide = false;
400
- _this._elRef = React.createRef();
381
+ _this._hide = false;
401
382
  return _this;
402
383
  }
403
384
 
404
- var __proto = StrictPanelComponent.prototype;
405
- Object.defineProperty(__proto, "element", {
385
+ var __proto = NonStrictPanel.prototype;
386
+ Object.defineProperty(__proto, "nativeElement", {
406
387
  get: function () {
407
- return this._elRef.current;
388
+ return reactDom.findDOMNode(this);
389
+ },
390
+ enumerable: false,
391
+ configurable: true
392
+ });
393
+ Object.defineProperty(__proto, "rendered", {
394
+ get: function () {
395
+ return !this._hide;
408
396
  },
409
397
  enumerable: false,
410
398
  configurable: true
411
399
  });
412
400
 
413
401
  __proto.render = function () {
414
- return this.hide ? React.createElement(React.Fragment, null) : this._getElement();
402
+ return this._hide ? React.createElement(React.Fragment, null) : this.props.children;
415
403
  };
416
404
 
417
- __proto._getElement = function () {
418
- return React.cloneElement(React.Children.only(this.props.children), {
419
- ref: this._elRef
420
- });
405
+ __proto.show = function () {
406
+ this._hide = false;
421
407
  };
422
408
 
423
- return StrictPanelComponent;
409
+ __proto.hide = function () {
410
+ this._hide = true;
411
+ };
412
+
413
+ return NonStrictPanel;
424
414
  }(React.Component);
425
415
 
416
+ /*
417
+ * Copyright (c) 2015 NAVER Corp.
418
+ * egjs projects are licensed under the MIT license
419
+ */
420
+
426
421
  var ViewportSlot = function (_a) {
427
422
  var children = _a.children;
428
423
  return React.createElement(React.Fragment, null, children);
429
424
  };
430
425
 
431
- var Flicking = function (_super) {
426
+ var ReactElementProvider =
427
+ /*#__PURE__*/
428
+ function () {
429
+ function ReactElementProvider(el) {
430
+ this._el = el;
431
+ }
432
+
433
+ var __proto = ReactElementProvider.prototype;
434
+ Object.defineProperty(__proto, "element", {
435
+ get: function () {
436
+ return this._el.nativeElement;
437
+ },
438
+ enumerable: false,
439
+ configurable: true
440
+ });
441
+ Object.defineProperty(__proto, "rendered", {
442
+ get: function () {
443
+ return this._el.rendered;
444
+ },
445
+ enumerable: false,
446
+ configurable: true
447
+ });
448
+
449
+ __proto.show = function () {
450
+ this._el.show();
451
+ };
452
+
453
+ __proto.hide = function () {
454
+ this._el.hide();
455
+ };
456
+
457
+ return ReactElementProvider;
458
+ }();
459
+
460
+ var Flicking =
461
+ /*#__PURE__*/
462
+ function (_super) {
432
463
  __extends(Flicking, _super);
433
464
 
434
- function Flicking(props) {
435
- var _this = _super.call(this, props) || this;
465
+ function Flicking() {
466
+ var _this = _super !== null && _super.apply(this, arguments) || this;
436
467
 
437
468
  _this._panels = [];
438
- _this._panels = _this._getChildren().map(function () {
439
- return React.createRef();
440
- });
469
+ _this._renderEmitter = new Component();
441
470
  return _this;
442
471
  }
443
472
 
444
473
  var __proto = Flicking.prototype;
445
- Object.defineProperty(__proto, "mounted", {
474
+ Object.defineProperty(__proto, "reactPanels", {
446
475
  get: function () {
447
- return this._mounted;
476
+ return this._panels.map(function (panel) {
477
+ return panel.current;
478
+ });
448
479
  },
449
480
  enumerable: false,
450
481
  configurable: true
451
482
  });
452
- Object.defineProperty(__proto, "reactPanels", {
483
+ Object.defineProperty(__proto, "renderEmitter", {
453
484
  get: function () {
454
- return this._panels.map(function (panel) {
455
- return panel.current;
456
- });
485
+ return this._renderEmitter;
457
486
  },
458
487
  enumerable: false,
459
488
  configurable: true
460
489
  });
461
490
 
462
- __proto.setRenderCallback = function (callback) {
463
- this._renderCallback = callback;
464
- };
465
-
466
491
  __proto.componentDidMount = function () {
467
- this._mounted = true;
468
492
  var props = this.props;
493
+ var rendererOptions = {
494
+ reactFlicking: this,
495
+ strategy: props.virtual && props.panelsPerView > 0 ? new VanillaFlicking.VirtualRenderingStrategy() : new VanillaFlicking.NormalRenderingStrategy({
496
+ providerCtor: ReactElementProvider,
497
+ panelCtor: VanillaFlicking.ExternalPanel
498
+ })
499
+ };
469
500
  var flicking = new VanillaFlicking(this._viewportElement, __assign(__assign({}, props), {
470
501
  renderExternal: {
471
502
  renderer: ReactRenderer,
472
- rendererOptions: {
473
- reactFlicking: this
474
- }
503
+ rendererOptions: rendererOptions
475
504
  }
476
505
  }));
477
506
  this._vanillaFlicking = flicking;
478
507
 
479
- this._bindEvents();
480
-
481
508
  var children = this._getChildren();
482
509
 
483
510
  this._jsxDiffer = new ListDiffer(children, function (panel) {
@@ -485,25 +512,19 @@ var Flicking = function (_super) {
485
512
  });
486
513
  this._pluginsDiffer = new ListDiffer();
487
514
 
488
- this._checkPlugins();
489
- };
515
+ this._bindEvents();
490
516
 
491
- __proto.componentWillUnmount = function () {
492
- this._mounted = false;
517
+ this._checkPlugins();
493
518
 
494
- this._vanillaFlicking.destroy();
519
+ if (props.status) {
520
+ flicking.setStatus(props.status);
521
+ }
495
522
  };
496
523
 
497
- __proto.shouldComponentUpdate = function (nextProps) {
498
- var children = this._getChildren(nextProps.children);
499
-
500
- var diffResult = this._jsxDiffer.update(children);
524
+ __proto.componentWillUnmount = function () {
525
+ var _a;
501
526
 
502
- this._panels = children.map(function () {
503
- return React.createRef();
504
- });
505
- this._diffResult = diffResult;
506
- return true;
527
+ (_a = this._vanillaFlicking) === null || _a === void 0 ? void 0 : _a.destroy();
507
528
  };
508
529
 
509
530
  __proto.componentDidUpdate = function () {
@@ -512,12 +533,13 @@ var Flicking = function (_super) {
512
533
 
513
534
  this._checkPlugins();
514
535
 
515
- this._renderCallback && this._renderCallback();
536
+ this._renderEmitter.trigger("render");
537
+
516
538
  if (!diffResult || !flicking.initialized) return;
517
539
  VanillaFlicking.sync(flicking, diffResult, this.reactPanels);
518
540
 
519
541
  if (diffResult.added.length > 0 || diffResult.removed.length > 0) {
520
- this.setState({});
542
+ this.forceUpdate();
521
543
  }
522
544
 
523
545
  this._diffResult = null;
@@ -526,14 +548,19 @@ var Flicking = function (_super) {
526
548
  __proto.render = function () {
527
549
  var _this = this;
528
550
 
529
- var _a;
551
+ var _a, _b, _c, _d;
530
552
 
531
553
  var props = this.props;
532
554
  var Viewport = props.viewportTag;
533
555
  var Camera = props.cameraTag;
534
556
  var attributes = {};
535
557
  var flicking = this._vanillaFlicking;
536
- var initialized = this._diffResult && flicking && flicking.initialized;
558
+
559
+ var children = this._getChildren(props.children);
560
+
561
+ var diffResult = (_b = (_a = this._jsxDiffer) === null || _a === void 0 ? void 0 : _a.update(children)) !== null && _b !== void 0 ? _b : null;
562
+ this._panels = this._createPanelRefs(props, children);
563
+ this._diffResult = diffResult;
537
564
 
538
565
  for (var name in props) {
539
566
  if (!(name in DEFAULT_PROPS) && !(name in VanillaFlicking.prototype)) {
@@ -541,8 +568,9 @@ var Flicking = function (_super) {
541
568
  }
542
569
  }
543
570
 
571
+ var initialized = !!this._diffResult && flicking && flicking.initialized;
544
572
  var viewportClasses = ["flicking-viewport"];
545
- var isHorizontal = flicking ? flicking.horizontal : (_a = props.horizontal) !== null && _a !== void 0 ? _a : true;
573
+ var isHorizontal = flicking ? flicking.horizontal : (_c = props.horizontal) !== null && _c !== void 0 ? _c : true;
546
574
 
547
575
  if (!isHorizontal) {
548
576
  viewportClasses.push("vertical");
@@ -561,18 +589,7 @@ var Flicking = function (_super) {
561
589
  transform: VanillaFlicking.getDefaultCameraTransform(this.props.align, this.props.horizontal, this.props.firstPanelSize)
562
590
  }
563
591
  } : {};
564
- var children = initialized ? VanillaFlicking.getRenderingPanels(flicking, this._diffResult) : this._getChildren();
565
- var panels = this.props.useFindDOMNode ? children.map(function (child, idx) {
566
- return React.createElement(NonStrictPanelComponent, {
567
- key: child.key,
568
- ref: _this._panels[idx]
569
- }, child);
570
- }) : children.map(function (child, idx) {
571
- return React.createElement(StrictPanelComponent, {
572
- key: child.key,
573
- ref: _this._panels[idx]
574
- }, child);
575
- });
592
+ var panels = !!props.virtual && ((_d = props.panelsPerView) !== null && _d !== void 0 ? _d : -1) > 0 ? this._getVirtualPanels(initialized) : this._getPanels(initialized);
576
593
  return React.createElement(Viewport, __assign({}, attributes, {
577
594
  className: viewportClasses.join(" "),
578
595
  ref: function (e) {
@@ -583,6 +600,17 @@ var Flicking = function (_super) {
583
600
  }, cameraProps), panels), this._getViewportSlot());
584
601
  };
585
602
 
603
+ __proto._createPanelRefs = function (props, children) {
604
+ var _a;
605
+
606
+ var panelsPerView = (_a = props.panelsPerView) !== null && _a !== void 0 ? _a : -1;
607
+ return panelsPerView > 0 && !!props.virtual ? VanillaFlicking.range(panelsPerView + 1).map(function () {
608
+ return React.createRef();
609
+ }) : children.map(function () {
610
+ return React.createRef();
611
+ });
612
+ };
613
+
586
614
  __proto._bindEvents = function () {
587
615
  var _this = this;
588
616
 
@@ -602,19 +630,18 @@ var Flicking = function (_super) {
602
630
  };
603
631
 
604
632
  __proto._checkPlugins = function () {
605
- var _a, _b;
633
+ var flicking = this._vanillaFlicking;
606
634
 
607
- var _c = this._pluginsDiffer.update(this.props.plugins),
608
- list = _c.list,
609
- added = _c.added,
610
- removed = _c.removed,
611
- prevList = _c.prevList;
635
+ var _a = this._pluginsDiffer.update(this.props.plugins),
636
+ list = _a.list,
637
+ added = _a.added,
638
+ removed = _a.removed,
639
+ prevList = _a.prevList;
612
640
 
613
- (_a = this._vanillaFlicking).addPlugins.apply(_a, added.map(function (index) {
641
+ flicking.addPlugins.apply(flicking, added.map(function (index) {
614
642
  return list[index];
615
643
  }));
616
-
617
- (_b = this._vanillaFlicking).removePlugins.apply(_b, removed.map(function (index) {
644
+ flicking.removePlugins.apply(flicking, removed.map(function (index) {
618
645
  return prevList[index];
619
646
  }));
620
647
  };
@@ -629,7 +656,7 @@ var Flicking = function (_super) {
629
656
  return React.Children.toArray(children).filter(function (child) {
630
657
  return child.type !== ViewportSlot;
631
658
  }).reduce(function (all, child) {
632
- return __spreadArray(__spreadArray([], all), _this._unpackFragment(child));
659
+ return __spreadArray(__spreadArray([], all, true), _this._unpackFragment(child), true);
633
660
  }, []);
634
661
  };
635
662
 
@@ -643,17 +670,63 @@ var Flicking = function (_super) {
643
670
  var _this = this;
644
671
 
645
672
  return reactIs.isFragment(child) ? React.Children.toArray(child.props.children).reduce(function (allChilds, fragChild) {
646
- return __spreadArray(__spreadArray([], allChilds), _this._unpackFragment(fragChild));
673
+ return __spreadArray(__spreadArray([], allChilds, true), _this._unpackFragment(fragChild), true);
647
674
  }, []) : [child];
648
675
  };
649
676
 
677
+ __proto._getVirtualPanels = function (initialized) {
678
+ var _this = this;
679
+
680
+ var _a = this.props.virtual.panelClass,
681
+ panelClass = _a === void 0 ? "flicking-panel" : _a;
682
+ var panelsPerView = this.props.panelsPerView;
683
+ var flicking = this._vanillaFlicking;
684
+ var renderingIndexes = initialized ? flicking.renderer.strategy.getRenderingIndexesByOrder(flicking) : VanillaFlicking.range(panelsPerView + 1);
685
+ var firstPanel = flicking && flicking.panels[0];
686
+ var size = firstPanel ? flicking.horizontal ? {
687
+ width: firstPanel.size
688
+ } : {
689
+ height: firstPanel.size
690
+ } : {};
691
+ return renderingIndexes.map(function (idx) {
692
+ return React.createElement("div", {
693
+ key: idx,
694
+ "data-element-index": idx,
695
+ ref: _this._panels[idx],
696
+ className: panelClass,
697
+ style: size
698
+ });
699
+ });
700
+ };
701
+
702
+ __proto._getPanels = function (initialized) {
703
+ var _this = this;
704
+
705
+ var children = initialized ? VanillaFlicking.getRenderingPanels(this._vanillaFlicking, this._diffResult) : this._getChildren();
706
+ return this.props.useFindDOMNode ? children.map(function (child, idx) {
707
+ return React.createElement(NonStrictPanel, {
708
+ key: child.key,
709
+ ref: _this._panels[idx]
710
+ }, child);
711
+ }) : children.map(function (child, idx) {
712
+ return React.createElement(StrictPanel, {
713
+ key: child.key,
714
+ ref: _this._panels[idx]
715
+ }, child);
716
+ });
717
+ };
718
+
650
719
  Flicking.defaultProps = DEFAULT_PROPS;
651
720
 
652
721
  __decorate([VanillaFlicking.withFlickingMethods], Flicking.prototype, "_vanillaFlicking", void 0);
653
722
 
654
723
  return Flicking;
655
- }(React.Component);
724
+ }(React.PureComponent);
656
725
 
726
+ /*
727
+ * Copyright (c) 2015 NAVER Corp.
728
+ * egjs projects are licensed under the MIT license
729
+ */
657
730
  Flicking.ViewportSlot = ViewportSlot;
658
731
 
659
732
  module.exports = Flicking;