Skip to content

Commit aef5284

Browse files
authored
Chore: 支持react 18 (#71)
* chore(rc-cli): 更新react相关依赖 * refactor: 新增render工具函数支持老版本 * test: 修改单元测试
1 parent 04ad62a commit aef5284

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+4701
-3951
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@
2828
## 快速开始
2929

3030
```jsx
31-
import ReactDOM from 'react-dom';
31+
import ReactDOM from 'react-dom/client';
3232
import { Button } from 'rc-ui-lib';
3333

3434
function App() {
3535
return <Button>Default Button</Button>;
3636
}
3737

38-
ReactDOM.render(<App />, mountNode);
38+
ReactDOM.createRoot(mountNode).render(<App />);
3939
```
4040

4141
## 浏览器支持

packages/rc-cli/cjs/jest.config.cjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ const JEST_STYLE_MOCK_FILE = join(__dirname, 'jest.style-mock.cjs');
1010
const DEFAULT_CONFIG = {
1111
testEnvironment: 'jsdom',
1212
setupFiles: [JEST_SETUP_FILE],
13+
setupFilesAfterEnv: [
14+
'@testing-library/jest-dom/extend-expect',
15+
'<rootDir>/tests/setupAfterEnv.ts',
16+
],
1317
moduleNameMapper: {
1418
'\\.(css|less|scss)$': JEST_STYLE_MOCK_FILE,
1519
'\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':

packages/rc-cli/package.json

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"@popperjs/core": "^2.11.6",
3939
"@rollup/pluginutils": "^5.0.2",
4040
"@testing-library/jest-dom": "^5.15.1",
41-
"@testing-library/react": "^12.1.2",
41+
"@testing-library/react": "^13.4.0",
4242
"@vant/touch-emulator": "^1.3.2",
4343
"@vitejs/plugin-react": "^2.0.1",
4444
"address": "^1.1.2",
@@ -57,14 +57,16 @@
5757
"front-matter": "^4.0.2",
5858
"fs-extra": "^10.1.0",
5959
"highlight.js": "^11.6.0",
60-
"jest": "^26.6.3",
60+
"jest": "^28.1.3",
61+
"jest-axe": "^6.0.1",
62+
"jest-environment-jsdom": "^28.1.3",
6163
"less": "^3.12.2",
6264
"markdown-it": "^12.0.0",
6365
"markdown-it-anchor": "^6.0.0",
6466
"ora": "^5.1.0",
6567
"postcss": "^8.1.7",
6668
"postcss-load-config": "^3.1.4",
67-
"react-router-dom": "^5.2.0",
69+
"react-router-dom": "^6.15.0",
6870
"release-it": "^15.4.1",
6971
"rehype-highlight": "^6.0.0",
7072
"signale": "^1.4.0",
@@ -85,13 +87,13 @@
8587
"@types/markdown-it": "^12.2.3",
8688
"@types/node": "^14.11.8",
8789
"@types/postcss-load-config": "^3.0.1",
88-
"@types/react": "^17.0.3",
89-
"@types/react-router-dom": "^5.1.7",
90+
"@types/react": "^18.2.20",
91+
"@types/react-router-dom": "^5.3.3",
9092
"@types/signale": "^1.4.1",
9193
"@types/through2": "^2.0.36",
9294
"@types/vinyl-fs": "^2.4.11",
93-
"react": "^17.0.2",
94-
"react-dom": "^17.0.2"
95+
"react": "^18.2.0",
96+
"react-dom": "^18.2.0"
9597
},
9698
"release-it": {
9799
"git": {

packages/rc-cli/site/desktop/App.jsx

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { useMemo, useEffect, Suspense } from 'react';
2-
import { Switch, Route, Redirect, useLocation, useHistory } from 'react-router-dom';
2+
import { Routes, Route, useLocation, useNavigate, Navigate } from 'react-router-dom';
33
import { config, packageVersion } from 'site-desktop-shared';
44

55
import { isMobile } from '../common';
@@ -10,23 +10,24 @@ import './index.less';
1010

1111
const App = () => {
1212
const { pathname } = useLocation();
13-
const history = useHistory();
13+
const navigate = useNavigate();
14+
15+
const handleMessage = (event) => {
16+
if (event.data.pathname && pathname !== event.data.pathname) {
17+
navigate(event.data.pathname);
18+
}
19+
};
1420

1521
useEffect(() => {
1622
if (isMobile) {
1723
window.location.replace(`mobile.html${window.location.hash}`);
1824
}
25+
return () => {
26+
window.removeEventListener('message', handleMessage);
27+
};
1928
}, []);
2029

21-
window.addEventListener(
22-
'message',
23-
(event) => {
24-
if (event.data.pathname && pathname !== event.data.pathname) {
25-
history.push(event.data.pathname);
26-
}
27-
},
28-
false,
29-
);
30+
window.addEventListener('message', handleMessage, false);
3031

3132
const path = window.location.pathname.replace(/\/index(\.html)?/, '/');
3233
const simulator = `${path}mobile.html${window.location.hash}`;
@@ -95,20 +96,27 @@ const App = () => {
9596
currentComponentName={currentComponentName}
9697
>
9798
<Suspense fallback={<div style={{ height: '110vh' }}></div>}>
98-
<Switch>
99+
<Routes>
99100
{routes.map((route) =>
100101
route.redirect ? (
101-
<Redirect key={route.path} to={route.redirect(pathname)} />
102+
<Route
103+
key={route.path}
104+
path={route.path}
105+
element={<Navigate to={route.redirect(pathname)} replace />}
106+
/>
102107
) : (
103108
<Route
104109
key={route.path}
105110
exact={route.exact}
106111
path={route.path}
107-
render={(props) => <route.component {...props} routes={route.routes} />}
112+
element={route.component}
113+
// render={(props) => (
114+
// <route.component {...props} element={route.component} routes={route.routes} />
115+
// )}
108116
/>
109117
),
110118
)}
111-
</Switch>
119+
</Routes>
112120
</Suspense>
113121
</Doc>
114122
);
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import React from 'react';
2-
import ReactDOM from 'react-dom';
2+
import ReactDOM from 'react-dom/client';
33
import { HashRouter } from 'react-router-dom';
44

55
import App from './App';
66

7-
ReactDOM.render(
7+
ReactDOM.createRoot(document.getElementById('app')).render(
88
<HashRouter>
99
<App />
1010
</HashRouter>,
11-
document.getElementById('app'),
1211
);

packages/rc-cli/site/desktop/routes.js renamed to packages/rc-cli/site/desktop/routes.jsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ export function getLangFromRoute(pathname) {
3333
}
3434

3535
const getRoutes = () => {
36-
console.log(documents);
3736
const routes = [];
3837
const names = Object.keys(documents);
3938

@@ -42,7 +41,7 @@ const getRoutes = () => {
4241
name: lang,
4342
exact: true,
4443
path: `/${lang || ''}`,
45-
component: Home,
44+
component: <Home />,
4645
state: { lang },
4746
});
4847
}
@@ -55,20 +54,22 @@ const getRoutes = () => {
5554
}
5655

5756
if (lang) {
57+
const Component = lazy(documents[name]);
5858
routes.push({
5959
name: `${lang}/${component}`,
6060
path: `/${lang}/${component}`,
61-
component: lazy(documents[name]),
61+
component: <Component />,
6262
state: {
6363
lang,
6464
name: component,
6565
},
6666
});
6767
} else {
68+
const Component = lazy(documents[name]);
6869
routes.push({
6970
name: `${component}`,
7071
path: `/${component}`,
71-
component: lazy(documents[name]),
72+
component: <Component />,
7273
meta: {
7374
name: component,
7475
},
@@ -87,6 +88,7 @@ const getRoutes = () => {
8788
redirect: () => '/',
8889
});
8990
}
91+
9092
return routes;
9193
};
9294

packages/rc-cli/site/mobile/App.jsx

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { useMemo, Suspense } from 'react';
2-
import { Switch, Route, Redirect, useLocation } from 'react-router-dom';
2+
import { Routes, Route, Navigate, useLocation } from 'react-router-dom';
33

44
import ScrollTop from './components/ScrollTop';
55
import DemoNav from './components/DemoNav';
@@ -20,22 +20,27 @@ const App = () => {
2020
<DemoNav title={title} />
2121
<ScrollTop />
2222
<Suspense fallback={<div></div>}>
23-
<Switch>
23+
<Routes>
2424
{routes.map((route) => {
2525
return route.redirect ? (
26-
<Redirect key={route.path} to={route.redirect(pathname)} />
26+
<Route
27+
key={route.path}
28+
path={route.path}
29+
element={<Navigate to={route.redirect(pathname)} replace />}
30+
/>
2731
) : (
2832
<Route
2933
key={route.path}
3034
exact={route.exact}
3135
path={route.path}
32-
render={(props) => {
33-
return <route.component {...props} meta={route.meta} routes={route.routes} />;
34-
}}
36+
element={route.component}
37+
// render={(props) => {
38+
// return <route.component {...props} meta={route.meta} routes={route.routes} />;
39+
// }}
3540
/>
3641
);
3742
})}
38-
</Switch>
43+
</Routes>
3944
</Suspense>
4045
</div>
4146
);

packages/rc-cli/site/mobile/components/DemoHome/index.jsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,10 @@ const DemoHome = (props) => {
3636
);
3737
};
3838

39+
DemoHome.defaultProps = {
40+
meta: {
41+
lang: 'zh-CN'
42+
}
43+
}
44+
3945
export default DemoHome;

packages/rc-cli/site/mobile/components/DemoHomeNav/index.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import React, { useMemo } from 'react';
2-
import { useHistory } from 'react-router-dom';
2+
import { useNavigate } from 'react-router-dom';
33
import { isMobile } from '../../../common/index.js';
44
import ArrowRight from '../ArrowRight';
55
import './index.less';
66

77
const DemoHomeNav = (props) => {
88
const { lang, group } = props;
99

10-
const history = useHistory();
10+
const navigate = useNavigate();
1111

1212
const base = useMemo(() => {
1313
return lang ? `/${lang}` : '';
@@ -22,7 +22,7 @@ const DemoHomeNav = (props) => {
2222
className="demo-home-nav__block"
2323
key={navItem.path}
2424
onClick={() => {
25-
history.push(`${base}/${navItem.path}`);
25+
navigate(`${base}/${navItem.path}`);
2626
if (!isMobile && window !== window.top) {
2727
window.top.postMessage({ pathname: `${base}/${navItem.path}` }, window.top);
2828
}

packages/rc-cli/site/mobile/components/DemoNav/index.jsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import React from 'react';
2-
import { useHistory } from 'react-router-dom';
2+
import { useNavigate } from 'react-router-dom';
33

44
import './index.less';
55

66
const DemoNav = (props) => {
7-
const history = useHistory();
7+
const navigate = useNavigate();
88

9-
const path =
10-
'M296.114 508.035c-3.22-13.597.473-28.499 11.079-39.105l333.912-333.912c16.271-16.272 42.653-16.272 58.925 0s16.272 42.654 0 58.926L395.504 498.47l304.574 304.574c16.272 16.272 16.272 42.654 0 58.926s-42.654 16.272-58.926 0L307.241 528.058a41.472 41.472 0 0 1-11.127-20.023z';
119
const onBack = () => {
12-
if (history.length > 1) {
13-
history.goBack();
10+
if (navigate.length > 1) {
11+
navigate(-1);
1412
} else {
15-
history.replace('/');
13+
navigate('/');
1614
}
1715
};
16+
const path =
17+
'M296.114 508.035c-3.22-13.597.473-28.499 11.079-39.105l333.912-333.912c16.271-16.272 42.653-16.272 58.925 0s16.272 42.654 0 58.926L395.504 498.47l304.574 304.574c16.272 16.272 16.272 42.654 0 58.926s-42.654 16.272-58.926 0L307.241 528.058a41.472 41.472 0 0 1-11.127-20.023z';
1818

1919
return props.title ? (
2020
<div className="demo-nav">

0 commit comments

Comments
 (0)