Skip to content

Commit 3494cd1

Browse files
authored
feat: typescript rewrite and upgrade to react 19 (#157)
1 parent 52d8688 commit 3494cd1

File tree

18 files changed

+1554
-1357
lines changed

18 files changed

+1554
-1357
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
React Flickity Component
22
=======================
33

4-
[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
5-
64
# Introduction:
75
A React.js Flickity component.
86

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

1715
# Usage:
1816

17+
## V5 Updates
18+
V5 supports react v19 and above.
19+
1920
## V4 Updates
2021
V4 only supports react v18 and above. V4 also comes with an esmodule bundle format to support modern frontend tooling like vite.
2122
If you are staying on react v16, please keep using v3.

__tests__/component.spec.jsx

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,60 @@ import {it, expect, vi, afterEach } from 'vitest';
33
import { render, cleanup, screen, waitFor } from '@testing-library/react';
44
import Flickity from '../src';
55

6-
afterEach(() => {
6+
afterEach(async () => {
7+
await new Promise(resolve => setTimeout(resolve, 0));
78
cleanup();
89
});
910

1011

11-
it('Calls render and componentDidMount', () => {
12-
const componentDidMountSpy = vi.spyOn(Flickity.prototype, 'componentDidMount');
13-
const renderSpy = vi.spyOn(Flickity.prototype, 'render');
14-
15-
render(<Flickity/>);
16-
17-
expect(componentDidMountSpy).toHaveBeenCalledOnce();
18-
expect(renderSpy).toHaveBeenCalled();
12+
it('Renders component successfully', async () => {
13+
const { container } = render(<Flickity/>);
14+
15+
await waitFor(() => {
16+
expect(container.firstChild).toBeTruthy();
17+
});
18+
19+
expect(container.firstChild).toBeInstanceOf(HTMLElement);
1920
});
2021

2122
it('Renders children', async () => {
22-
const { getAllByAltText } = render(
23+
const { getAllByAltText, unmount, container } = render(
2324
<Flickity>
2425
<img src="/images/placeholder.png" alt="children"/>
2526
<img src="/images/placeholder.png" alt="children"/>
2627
<img src="/images/placeholder.png" alt="children"/>
2728
</Flickity>
2829
);
30+
31+
await waitFor(() => {
32+
expect(container.firstChild).toBeTruthy();
33+
});
2934

3035
await waitFor(() => expect(getAllByAltText('children').length).toEqual(3));
36+
unmount();
3137
});
3238

33-
it('Renders a static carousel', () => {
34-
const { getAllByAltText, rerender } = render(
39+
it('Renders a static carousel', async () => {
40+
const { getAllByAltText, unmount } = render(
3541
<Flickity static>
3642
<img src="/images/placeholder.png" alt="children"/>
3743
<img src="/images/placeholder.png" alt="children"/>
3844
</Flickity>
3945
);
4046

41-
expect(getAllByAltText('children').length).toEqual(2);
42-
})
47+
await waitFor(() => expect(getAllByAltText('children').length).toEqual(2));
48+
unmount();
49+
});
4350

44-
it('Reload carousel even it\'s static', () => {
45-
const { getAllByAltText, rerender } = render(
51+
it('Reload carousel even it\'s static', async () => {
52+
const { getAllByAltText, rerender, unmount } = render(
4653
<Flickity static reloadOnUpdate>
4754
<img src="/images/placeholder.png" alt="children"/>
4855
<img src="/images/placeholder.png" alt="children"/>
4956
</Flickity>
5057
);
5158

52-
expect(getAllByAltText('children').length).toEqual(2);
59+
await waitFor(() => expect(getAllByAltText('children').length).toEqual(2));
5360

5461
rerender(
5562
<Flickity static reloadOnUpdate>
@@ -58,5 +65,7 @@ it('Reload carousel even it\'s static', () => {
5865
<img src="/images/placeholder.png" alt="children"/>
5966
</Flickity>
6067
);
61-
expect(getAllByAltText('children').length).toEqual(3);
62-
})
68+
69+
await waitFor(() => expect(getAllByAltText('children').length).toEqual(3));
70+
unmount();
71+
});
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
import Flickity from '../src/index.js'
2-
import './App.css'
3-
import React from 'react'
1+
import Flickity from '../src/index';
2+
import './App.css';
3+
import React from 'react';
44

5-
import Default from './Default.jsx'
6-
import Static from './Static.jsx'
5+
import Default from './Default';
6+
import Static from './Static';
77

88
function App() {
99
return (
1010
<>
1111
<header>
1212
<nav>
13-
<a href="https://github.com/yaodingyd/react-flickity-component">Github</a>
13+
<a href="https://github.com/yaodingyd/react-flickity-component">
14+
Github
15+
</a>
1416
</nav>
1517
<h1>React-Flickity-Component</h1>
1618
</header>
@@ -20,7 +22,7 @@ function App() {
2022
<Static />
2123
</main>
2224
</>
23-
)
25+
);
2426
}
2527

26-
export default App
28+
export default App;

examples/Default.jsx

Lines changed: 0 additions & 28 deletions
This file was deleted.

examples/Default.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import React, { useState } from 'react';
2+
import Flickity from '../src/index';
3+
import { images } from './images';
4+
5+
export default function Default() {
6+
const [imgs, setImages] = useState(images);
7+
8+
function addImage() {
9+
const newImages = [...imgs];
10+
newImages.push('https://picsum.photos/200/300');
11+
setImages(newImages);
12+
}
13+
14+
return (
15+
<>
16+
<h3>Default</h3>
17+
<pre>&lt;Flickity&gt;&#123;children&#125;&lt;&#47;Flickity&gt;</pre>
18+
<p className="carousel">
19+
<Flickity>
20+
{imgs.map((image) => (
21+
<img src={image} key={image} className="carousel-image" />
22+
))}
23+
</Flickity>
24+
</p>
25+
<button onClick={addImage}>Add image to carousel</button>
26+
</>
27+
);
28+
}

examples/Static.jsx

Lines changed: 0 additions & 28 deletions
This file was deleted.

examples/Static.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import React, { useState } from 'react';
2+
import Flickity from '../src/index';
3+
import { images } from './images';
4+
5+
export default function Static() {
6+
const [imgs, setImages] = useState(images);
7+
8+
function addImage() {
9+
const newImages = [...imgs];
10+
newImages.push('https://picsum.photos/200/300');
11+
setImages(newImages);
12+
}
13+
14+
return (
15+
<>
16+
<h3>Static</h3>
17+
<pre>
18+
&lt;Flickity static
19+
reloadOnUpdate&gt;&#123;children&#125;&lt;&#47;Flickity&gt;
20+
</pre>
21+
<p className="carousel">
22+
<Flickity static reloadOnUpdate>
23+
{imgs.map((image) => (
24+
<img src={image} key={image} className="carousel-image" />
25+
))}
26+
</Flickity>
27+
</p>
28+
<button onClick={addImage}>Add image to carousel</button>
29+
</>
30+
);
31+
}
File renamed without changes.

examples/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
66
<link rel="stylesheet" href="https://unpkg.com/flickity@2/dist/flickity.min.css">
77
<title>React-Flickity-Component</title>
8-
<script type="module" src="main.jsx"></script>
8+
<script type="module" src="main.tsx"></script>
99
</head>
1010
<body>
1111
<div id="root"></div>

examples/main.jsx

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)