From 9c28672d3fce1042bdad3e3283936f2ed032a644 Mon Sep 17 00:00:00 2001 From: malangfox Date: Fri, 6 Dec 2024 18:47:57 +0900 Subject: [PATCH] fix: initialize with panel with defaultIndex when display: none (#903) * fix: use defaultPanel when it equals nearestAnchor * test: add test for defaultIndex * test: fix test for initial panel when size is 0 * fix: remove autoInit at defaultIndex test --- src/Flicking.ts | 2 +- test/unit/Flicking.spec.ts | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Flicking.ts b/src/Flicking.ts index 73fca45df7..eceff4e693 100644 --- a/src/Flicking.ts +++ b/src/Flicking.ts @@ -1739,7 +1739,7 @@ class Flicking extends Component { if (!defaultPanel) return; const nearestAnchor = camera.findNearestAnchor(defaultPanel.position); - const initialPanel = (nearestAnchor && defaultPanel.index !== nearestAnchor.panel.index) ? nearestAnchor.panel : defaultPanel; + const initialPanel = (nearestAnchor && defaultPanel.position !== nearestAnchor.panel.position && defaultPanel.index !== nearestAnchor.panel.index) ? nearestAnchor.panel : defaultPanel; control.setActive(initialPanel, null, false); if (!nearestAnchor) { diff --git a/test/unit/Flicking.spec.ts b/test/unit/Flicking.spec.ts index 49ecea9e76..72fbd5ca03 100644 --- a/test/unit/Flicking.spec.ts +++ b/test/unit/Flicking.spec.ts @@ -72,7 +72,7 @@ describe("Flicking", () => { ); expect(flicking.currentPanel).not.to.be.null; - expect(flicking.currentPanel).to.equal(flicking.camera.findNearestAnchor(0).panel); + expect(flicking.currentPanel).to.equal(flicking.panels[0]); }); [15, 30, 50, 100].forEach((width) => { @@ -205,6 +205,12 @@ describe("Flicking", () => { expect(flicking.index).to.equal(0); }); + + it("should initialized with panel which has index equal to defaultIndex when panels are not visible.", async () => { + const flicking = await createFlicking(El.DEFAULT_HORIZONTAL.setWidth(0), { defaultIndex: 1 }); + + expect(flicking.index).to.equal(1); + }); }); describe("horizontal", () => {