Skip to content

Commit

Permalink
fix: initialize with panel with defaultIndex when display: none (#903)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
malangfox authored Dec 6, 2024
1 parent 1eaa5b0 commit 9c28672
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Flicking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1739,7 +1739,7 @@ class Flicking extends Component<FlickingEvents> {
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) {
Expand Down
8 changes: 7 additions & 1 deletion test/unit/Flicking.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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", () => {
Expand Down

0 comments on commit 9c28672

Please sign in to comment.