Skip to content

Commit 4407140

Browse files
committed
chore: feat update (#18)
* chore: optimize * 🦄 refactor: useSwrData * feat: useSwrData 优化 * feat: useSwrData 优化 * 🦄 feat: add use-swr-data * feat: update * chore: update * feat: update * chore: update eslint * fix: update
1 parent 9475bcb commit 4407140

File tree

16 files changed

+214
-216
lines changed

16 files changed

+214
-216
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ react-typescript 后台管理系统项目模版内置 动态路由、动态路
6262
GLOBAL_ROUTERS.APP_PAGE;
6363

6464
// 调用这方法设置你的动态路由配置
65-
const setDynamicRoutes = useRootStore((store) => store.setDynamicRoutes);
65+
const setDynamicRoutes = useRootStore(store => store.setDynamicRoutes);
6666
setDynamicRoutes(GLOBAL_ROUTERS.APP_PAGE);
6767
```
6868

eslint.config.mjs

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,44 @@
11
import antfu from "@antfu/eslint-config";
22
import tailwind from "eslint-plugin-tailwindcss";
33

4-
export default antfu({
5-
formatters: false,
6-
react: true,
7-
ignores: ["./*.md"],
8-
typescript: {
9-
tsconfigPath: "tsconfig.json",
10-
},
11-
globals: {
12-
process: true,
13-
},
14-
stylistic: {
15-
quotes: "double", // or 'double'
16-
semi: true,
17-
overrides: {
18-
"style/comma-dangle": "off",
4+
export default antfu(
5+
{
6+
formatters: false,
7+
react: true,
8+
ignores: ["**/*.md"],
9+
typescript: {
10+
tsconfigPath: "tsconfig.json",
11+
},
12+
globals: {
13+
process: true,
14+
},
15+
stylistic: {
16+
quotes: "double", // or 'double'
17+
semi: true,
18+
overrides: {
19+
"style/comma-dangle": "off",
20+
},
21+
},
22+
rules: {
23+
"no-console": "warn",
24+
"ts/no-namespace": 0,
25+
"ts/no-unsafe-call": 0,
26+
"ts/no-unsafe-return": 0,
27+
"ts/no-unsafe-argument": 0,
28+
"ts/no-misused-promises": 0,
29+
"ts/no-floating-promises": 0,
30+
"ts/no-unsafe-assignment": 0,
31+
"ts/no-unsafe-member-access": 0,
32+
"ts/strict-boolean-expressions": 0,
33+
"ts/switch-exhaustiveness-check": 0,
34+
"react-hooks-extra/no-direct-set-state-in-use-effect": 0,
35+
"n/prefer-global/process": 0, // Allow process in browser context
1936
},
2037
},
21-
rules: {
22-
"no-console": "warn",
23-
"ts/no-namespace": 0,
24-
"ts/no-unsafe-call": 0,
25-
"ts/no-unsafe-return": 0,
26-
"ts/no-unsafe-argument": 0,
27-
"ts/no-misused-promises": 0,
28-
"ts/no-floating-promises": 0,
29-
"ts/no-unsafe-assignment": 0,
30-
"ts/no-unsafe-member-access": 0,
31-
"ts/strict-boolean-expressions": 0,
32-
"ts/switch-exhaustiveness-check": 0,
33-
"react-hooks-extra/no-direct-set-state-in-use-effect": 0,
34-
"n/prefer-global/process": 0, // Allow process in browser context
35-
},
36-
}, ...tailwind.configs["flat/recommended"], {
37-
rules: {
38-
"tailwindcss/no-custom-classname": ["off"],
39-
},
40-
});
38+
...tailwind.configs["flat/recommended"],
39+
{
40+
rules: {
41+
"tailwindcss/no-custom-classname": ["off"],
42+
},
43+
}
44+
);

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@
2727
"axios": "^1.6.8",
2828
"classnames": "^2.5.1",
2929
"core-js": "^3.36.0",
30+
"lodash": "^4.17.21",
3031
"nanoid": "^4.0.2",
3132
"react": "^18.2.0",
3233
"react-dom": "^18.2.0",
3334
"react-router-dom": "^6.22.3",
3435
"swr": "^2.3.3",
36+
"use-swr-data": "^0.0.3",
3537
"zustand": "^5.0.5"
3638
},
3739
"devDependencies": {
@@ -43,6 +45,7 @@
4345
"@babel/preset-typescript": "^7.23.3",
4446
"@eslint-react/eslint-plugin": "^1.52.2",
4547
"@pmmmwh/react-refresh-webpack-plugin": "^0.6.0",
48+
"@types/lodash": "^4.17.20",
4649
"@types/mockjs": "^1.0.10",
4750
"@types/node": "^18.19.24",
4851
"@types/react": "^18.2.66",

scripts/webpack.config.cjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable no-console */
12
const ReactRefreshWebpackPlugin = require("@pmmmwh/react-refresh-webpack-plugin");
23
const webpack = require("webpack");
34
const baseConfig = require("./webpack.config.base.cjs");

src/components/LazyLoadSelect.tsx

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import type { ResponseList } from "&src/types/api";
2+
import type { SelectProps } from "antd";
3+
import type { AnyObject, PagingSwrProps } from "use-swr-data";
4+
5+
import { Select, Spin } from "antd";
6+
import debounce from "lodash/debounce";
7+
import { useEffect, useMemo, useState } from "react";
8+
import useSwrData from "use-swr-data";
9+
10+
interface LazyLoadSelectProps<P extends AnyObject, R extends ResponseList<any>> {
11+
value?: SelectProps["value"];
12+
searchKey: string;
13+
reqFunc: PagingSwrProps<R, P>["req"];
14+
fieldNames?: SelectProps["fieldNames"];
15+
reqParams?: PagingSwrProps<R, P>["params"];
16+
onChange?: SelectProps["onChange"];
17+
}
18+
19+
export default function LazyLoadSelect<P extends AnyObject, R extends ResponseList<any>>(props: LazyLoadSelectProps<P, R>) {
20+
const { value, fieldNames, searchKey = "name", reqParams, reqFunc, onChange } = props;
21+
22+
const [list, setList] = useState<any[]>([]);
23+
24+
const { data, pageInfo, onSearch, setPage } = useSwrData({
25+
reqKey: reqFunc.name,
26+
req: reqFunc,
27+
params: reqParams,
28+
paging: true,
29+
defaultPage: { pageNum: 1, pageSize: 100 },
30+
swrConfig: {
31+
revalidateOnFocus: false,
32+
revalidateIfStale: true,
33+
revalidateOnMount: true,
34+
dedupingInterval: 0,
35+
},
36+
});
37+
useEffect(() => {
38+
if (!data)
39+
return;
40+
41+
if (data?.pageNum > 1) {
42+
setList(draft => [...draft, ...data.list]);
43+
}
44+
else {
45+
setList(data.list);
46+
}
47+
}, [data]);
48+
49+
const displayList = useMemo(() => {
50+
const newList = [...list];
51+
52+
if (pageInfo.pageNum === data?.totalPage)
53+
return newList;
54+
55+
newList.push({
56+
title: "loading",
57+
label: (
58+
<div className="flex justify-center">
59+
<Spin />
60+
</div>
61+
),
62+
value: "loading",
63+
});
64+
65+
return newList;
66+
}, [list, data, pageInfo]);
67+
68+
const onSearchData = debounce((value) => {
69+
onSearch({ [searchKey]: value } as PagingSwrProps["defaultSearch"]);
70+
setList([]);
71+
}, 500);
72+
const onloadData = debounce(() => {
73+
setPage(draft => ({ ...draft, pageNum: pageInfo.pageNum + 1 }));
74+
}, 200);
75+
76+
return (
77+
<Select
78+
showSearch
79+
allowClear
80+
value={value}
81+
filterOption={false}
82+
options={displayList}
83+
fieldNames={fieldNames}
84+
onSearch={onSearchData}
85+
onPopupScroll={(e) => {
86+
const container = e.target as HTMLDivElement;
87+
const scrollTop = container?.scrollTop;
88+
const containerHeight = (container.firstChild as HTMLDivElement)?.clientHeight;
89+
90+
if (data && containerHeight - scrollTop < 300 && data.totalPage > pageInfo.pageNum)
91+
onloadData();
92+
}}
93+
onChange={(value) => {
94+
onSearch({ [searchKey]: "" } as PagingSwrProps["defaultSearch"]);
95+
onChange?.(value);
96+
}}
97+
/>
98+
);
99+
}

src/hooks/useSwrData.ts

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

src/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ interface IndexRoute {
2121
childrenList?: Route[];
2222
}
2323
type Route = PathRoute | IndexRoute;
24-
interface PageProps {
24+
interface RoutePageProps {
2525
children: React.ReactNode;
2626
title: string;
2727
}

src/pages/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Layout from "&src/layout";
22

3-
function System({ children }: PageProps) {
3+
function System({ children }: RoutePageProps) {
44
return <Layout>{children}</Layout>;
55
}
66

src/pages/system/data/report.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Card, Skeleton } from "antd";
22

3-
function Report({ title }: PageProps) {
3+
function Report({ title }: RoutePageProps) {
44
return (
55
<Card title={title}>
66
<Skeleton active />

src/pages/system/home/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Card, Skeleton } from "antd";
22

3-
function Home({ title }: PageProps) {
3+
function Home({ title }: RoutePageProps) {
44
return (
55
<div>
66
<Card title={title}>

0 commit comments

Comments
 (0)