Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
React Flickity Component
=======================

[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)

# Introduction:
A React.js Flickity component.

Expand All @@ -16,6 +14,9 @@ npm install react-flickity-component

# Usage:

## V5 Updates
V5 supports react v19 and above.

## V4 Updates
V4 only supports react v18 and above. V4 also comes with an esmodule bundle format to support modern frontend tooling like vite.
If you are staying on react v16, please keep using v3.
Expand Down
47 changes: 28 additions & 19 deletions __tests__/component.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,60 @@ import {it, expect, vi, afterEach } from 'vitest';
import { render, cleanup, screen, waitFor } from '@testing-library/react';
import Flickity from '../src';

afterEach(() => {
afterEach(async () => {
await new Promise(resolve => setTimeout(resolve, 0));
cleanup();
});


it('Calls render and componentDidMount', () => {
const componentDidMountSpy = vi.spyOn(Flickity.prototype, 'componentDidMount');
const renderSpy = vi.spyOn(Flickity.prototype, 'render');

render(<Flickity/>);

expect(componentDidMountSpy).toHaveBeenCalledOnce();
expect(renderSpy).toHaveBeenCalled();
it('Renders component successfully', async () => {
const { container } = render(<Flickity/>);

await waitFor(() => {
expect(container.firstChild).toBeTruthy();
});

expect(container.firstChild).toBeInstanceOf(HTMLElement);
});

it('Renders children', async () => {
const { getAllByAltText } = render(
const { getAllByAltText, unmount, container } = render(
<Flickity>
<img src="/images/placeholder.png" alt="children"/>
<img src="/images/placeholder.png" alt="children"/>
<img src="/images/placeholder.png" alt="children"/>
</Flickity>
);

await waitFor(() => {
expect(container.firstChild).toBeTruthy();
});

await waitFor(() => expect(getAllByAltText('children').length).toEqual(3));
unmount();
});

it('Renders a static carousel', () => {
const { getAllByAltText, rerender } = render(
it('Renders a static carousel', async () => {
const { getAllByAltText, unmount } = render(
<Flickity static>
<img src="/images/placeholder.png" alt="children"/>
<img src="/images/placeholder.png" alt="children"/>
</Flickity>
);

expect(getAllByAltText('children').length).toEqual(2);
})
await waitFor(() => expect(getAllByAltText('children').length).toEqual(2));
unmount();
});

it('Reload carousel even it\'s static', () => {
const { getAllByAltText, rerender } = render(
it('Reload carousel even it\'s static', async () => {
const { getAllByAltText, rerender, unmount } = render(
<Flickity static reloadOnUpdate>
<img src="/images/placeholder.png" alt="children"/>
<img src="/images/placeholder.png" alt="children"/>
</Flickity>
);

expect(getAllByAltText('children').length).toEqual(2);
await waitFor(() => expect(getAllByAltText('children').length).toEqual(2));

rerender(
<Flickity static reloadOnUpdate>
Expand All @@ -58,5 +65,7 @@ it('Reload carousel even it\'s static', () => {
<img src="/images/placeholder.png" alt="children"/>
</Flickity>
);
expect(getAllByAltText('children').length).toEqual(3);
})

await waitFor(() => expect(getAllByAltText('children').length).toEqual(3));
unmount();
});
18 changes: 10 additions & 8 deletions examples/App.jsx → examples/App.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import Flickity from '../src/index.js'
import './App.css'
import React from 'react'
import Flickity from '../src/index';
import './App.css';
import React from 'react';

import Default from './Default.jsx'
import Static from './Static.jsx'
import Default from './Default';
import Static from './Static';

function App() {
return (
<>
<header>
<nav>
<a href="https://github.com/yaodingyd/react-flickity-component">Github</a>
<a href="https://github.com/yaodingyd/react-flickity-component">
Github
</a>
</nav>
<h1>React-Flickity-Component</h1>
</header>
Expand All @@ -20,7 +22,7 @@ function App() {
<Static />
</main>
</>
)
);
}

export default App
export default App;
28 changes: 0 additions & 28 deletions examples/Default.jsx

This file was deleted.

28 changes: 28 additions & 0 deletions examples/Default.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React, { useState } from 'react';
import Flickity from '../src/index';
import { images } from './images';

export default function Default() {
const [imgs, setImages] = useState(images);

function addImage() {
const newImages = [...imgs];
newImages.push('https://picsum.photos/200/300');
setImages(newImages);
}

return (
<>
<h3>Default</h3>
<pre>&lt;Flickity&gt;&#123;children&#125;&lt;&#47;Flickity&gt;</pre>
<p className="carousel">
<Flickity>
{imgs.map((image) => (
<img src={image} key={image} className="carousel-image" />
))}
</Flickity>
</p>
<button onClick={addImage}>Add image to carousel</button>
</>
);
}
28 changes: 0 additions & 28 deletions examples/Static.jsx

This file was deleted.

31 changes: 31 additions & 0 deletions examples/Static.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React, { useState } from 'react';
import Flickity from '../src/index';
import { images } from './images';

export default function Static() {
const [imgs, setImages] = useState(images);

function addImage() {
const newImages = [...imgs];
newImages.push('https://picsum.photos/200/300');
setImages(newImages);
}

return (
<>
<h3>Static</h3>
<pre>
&lt;Flickity static
reloadOnUpdate&gt;&#123;children&#125;&lt;&#47;Flickity&gt;
</pre>
<p className="carousel">
<Flickity static reloadOnUpdate>
{imgs.map((image) => (
<img src={image} key={image} className="carousel-image" />
))}
</Flickity>
</p>
<button onClick={addImage}>Add image to carousel</button>
</>
);
}
File renamed without changes.
2 changes: 1 addition & 1 deletion examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="https://unpkg.com/flickity@2/dist/flickity.min.css">
<title>React-Flickity-Component</title>
<script type="module" src="main.jsx"></script>
<script type="module" src="main.tsx"></script>
</head>
<body>
<div id="root"></div>
Expand Down
12 changes: 0 additions & 12 deletions examples/main.jsx

This file was deleted.

12 changes: 12 additions & 0 deletions examples/main.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React from 'react';
import { createRoot } from 'react-dom/client';
import App from './App';
import './global.css';

const domNode = document.getElementById('root')!;

createRoot(domNode).render(
<React.StrictMode>
<App />
</React.StrictMode>
);
41 changes: 22 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
{
"name": "react-flickity-component",
"version": "4.0.7",
"version": "5.0.0",
"description": "react flickity component",
"files": [
"dist",
"src/index.d.ts"
"dist"
],
"main": "./dist/react-flickity-component.umd.js",
"module": "./dist/react-flickity-component.es.js",
"exports": {
".": {
"types": "./src/index.d.ts",
"types": "./dist/index.d.ts",
"import": "./dist/react-flickity-component.es.js",
"require": "./dist/react-flickity-component.umd.js"
}
},
"scripts": {
"pretty": "prettier --single-quote --trailing-comma es5 --write \"src/**/*.js\" \"examples/**/*.jsx\"",
"pretty": "prettier --single-quote --trailing-comma es5 --write \"src/**/*.ts[x]\" \"examples/**/*.ts[x]\"",
"test": "vitest",
"dev": "vite serve ./examples",
"build": "vite build",
Expand All @@ -37,28 +36,32 @@
"react-component"
],
"dependencies": {
"imagesloaded": "^4.1.4",
"prop-types": "^15.7.2"
"imagesloaded": "^4.1.4"
},
"devDependencies": {
"@originjs/vite-plugin-commonjs": "^1.0.3",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
"@types/react": "^18.0.26",
"@types/react-dom": "^18.0.9",
"@testing-library/jest-dom": "^6.0.0",
"@testing-library/react": "^16.0.0",
"@types/react": "^19.0.0",
"@types/react-dom": "^19.0.0",
"@types/flickity": "^2.2.0",
"@types/imagesloaded": "^4.1.0",
"@vitejs/plugin-react": "^4.0.0",
"flickity": "^2.2.1",
"jsdom": "^20.0.3",
"prettier": "^2.8.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"jsdom": "^25.0.0",
"prettier": "^3.0.0",
"react": "^19.0.0",
"react-dom": "^19.0.0",
"typescript": "^5.0.0",
"vite": "^6.0.0",
"vitest": "^0.25.7"
"vite-plugin-dts": "^4.0.0",
"vitest": "^2.0.0"
},
"peerDependencies": {
"flickity": "^2.2.1",
"react": "^18.2.0",
"react-dom": "^18.2.0"
"react": "^19.0.0",
"react-dom": "^19.0.0"
},
"typings": "./src/index.d.ts"
"typings": "./dist/index.d.ts",
"packageManager": "pnpm@10.12.1+sha512.f0dda8580f0ee9481c5c79a1d927b9164f2c478e90992ad268bbb2465a736984391d6333d2c327913578b2804af33474ca554ba29c04a8b13060a717675ae3ac"
}
Loading