@tsingroc/tsingroc-components 3.14.0 → 3.14.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import { ConfigProvider, theme } from "antd";
3
3
  import dayjs from "dayjs";
4
- import zhCN from "antd/locale/zh_CN";
4
+ import zhCN from "antd/es/locale/zh_CN";
5
5
  import "dayjs/locale/zh-cn";
6
6
  /**
7
7
  * 清鹏智能的公共 Ant Design 主题,会影响内部的所有 Ant Design 组件的样式。
@@ -1 +1 @@
1
- {"version":3,"file":"TsingrocTheme.js","sourceRoot":"","sources":["../../src/components/TsingrocTheme.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,cAAc,EAAE,KAAK,EAAE,MAAM,MAAM,CAAC;AAC7C,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B,OAAO,IAAI,MAAM,mBAAmB,CAAC;AACrC,OAAO,oBAAoB,CAAC;AAS5B;;;GAGG;AACH,SAAS,aAAa,CAAC,KAAyB;IAC9C,sBAAsB;IACtB,kDAAkD;IAClD,YAAY;IACZ,oeAAoe;IACpe,KAAK,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IACtB,OAAO,CACL,KAAC,cAAc,IACb,MAAM,EAAE,IAAI,EACZ,KAAK,EAAE;YACL,SAAS,EAAE,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,aAAa,CAAC,CAAC,CAAC,KAAK,CAAC,gBAAgB;YACpE,KAAK,EAAE,KAAK,CAAC,IAAI;gBACf,CAAC,CAAC;oBACE,YAAY,EAAE,SAAS;oBACvB,SAAS,EAAE,SAAS;oBACpB,YAAY,EAAE,SAAS;iBACxB;gBACH,CAAC,CAAC;oBACE,YAAY,EAAE,SAAS;oBACvB,SAAS,EAAE,SAAS;oBACpB,YAAY,EAAE,SAAS;iBACxB;SACN,YAEA,KAAK,CAAC,QAAQ,GACA,CAClB,CAAC;AACJ,CAAC;AAED,eAAe,aAAa,CAAC"}
1
+ {"version":3,"file":"TsingrocTheme.js","sourceRoot":"","sources":["../../src/components/TsingrocTheme.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,cAAc,EAAE,KAAK,EAAE,MAAM,MAAM,CAAC;AAC7C,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B,OAAO,IAAI,MAAM,sBAAsB,CAAC;AACxC,OAAO,oBAAoB,CAAC;AAS5B;;;GAGG;AACH,SAAS,aAAa,CAAC,KAAyB;IAC9C,sBAAsB;IACtB,kDAAkD;IAClD,YAAY;IACZ,oeAAoe;IACpe,KAAK,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IACtB,OAAO,CACL,KAAC,cAAc,IACb,MAAM,EAAE,IAAI,EACZ,KAAK,EAAE;YACL,SAAS,EAAE,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,aAAa,CAAC,CAAC,CAAC,KAAK,CAAC,gBAAgB;YACpE,KAAK,EAAE,KAAK,CAAC,IAAI;gBACf,CAAC,CAAC;oBACE,YAAY,EAAE,SAAS;oBACvB,SAAS,EAAE,SAAS;oBACpB,YAAY,EAAE,SAAS;iBACxB;gBACH,CAAC,CAAC;oBACE,YAAY,EAAE,SAAS;oBACvB,SAAS,EAAE,SAAS;oBACpB,YAAY,EAAE,SAAS;iBACxB;SACN,YAEA,KAAK,CAAC,QAAQ,GACA,CAClB,CAAC;AACJ,CAAC;AAED,eAAe,aAAa,CAAC"}
package/package.json CHANGED
@@ -1,13 +1,12 @@
1
1
  {
2
2
  "name": "@tsingroc/tsingroc-components",
3
- "version": "3.14.0",
3
+ "version": "3.14.1",
4
4
  "author": "",
5
5
  "license": "ISC",
6
6
  "description": "",
7
7
  "keywords": [],
8
8
  "type": "module",
9
9
  "main": "dist/index.js",
10
- "types": "dist/index.d.ts",
11
10
  "sideEffects": [
12
11
  "**/*.css"
13
12
  ],
@@ -17,7 +16,8 @@
17
16
  "./echarts/gl": "./dist/echarts/gl.js"
18
17
  },
19
18
  "files": [
20
- "dist"
19
+ "dist",
20
+ "src"
21
21
  ],
22
22
  "scripts": {
23
23
  "test": "echo \"Error: no test specified\" && exit 1",
@@ -0,0 +1,389 @@
1
+ import {
2
+ createContext,
3
+ type ReactNode,
4
+ useContext,
5
+ useEffect,
6
+ useMemo,
7
+ useRef,
8
+ useState,
9
+ } from "react";
10
+ import { jwtDecode, type JwtPayload } from "jwt-decode";
11
+ import Sdk from "casdoor-js-sdk";
12
+ import type { SdkConfig } from "casdoor-js-sdk/lib/esm/sdk";
13
+
14
+ export interface AccessTokenPayload extends JwtPayload {
15
+ /**
16
+ * Token 的发布者。见 [JWT 标准][1]。
17
+ *
18
+ * [1]: https://datatracker.ietf.org/doc/html/rfc7519#section-4.1
19
+ */
20
+ iss: string;
21
+ /**
22
+ * Token 的主题。见 [JWT 标准][1]。
23
+ *
24
+ * [1]: https://datatracker.ietf.org/doc/html/rfc7519#section-4.1
25
+ */
26
+ sub: string;
27
+ /**
28
+ * Token 的目标应用 ID。见 [JWT 标准][1]。
29
+ *
30
+ * [1]: https://datatracker.ietf.org/doc/html/rfc7519#section-4.1
31
+ */
32
+ aud: string[];
33
+ /**
34
+ * 使用 Token 的应用 ID。见 [JWT 标准][1]。
35
+ *
36
+ * [1]: https://datatracker.ietf.org/doc/html/rfc7519#section-4.1
37
+ */
38
+ azp: string;
39
+ /**
40
+ * Token 的唯一标识符。见 [JWT 标准][1]。
41
+ *
42
+ * [1]: https://datatracker.ietf.org/doc/html/rfc7519#section-4.1
43
+ */
44
+ jti: string;
45
+ /** Token 的过期时间(Unix 时间戳)。*/
46
+ exp: number;
47
+ /** Token 的生效时间(Unix 时间戳)。*/
48
+ nbf: number;
49
+ /** Token 的签发时间(Unix 时间戳)。*/
50
+ iat: number;
51
+
52
+ /** 用户的登录名。*/
53
+ name: string;
54
+ /** 用户的 UUID。*/
55
+ id: string;
56
+ /** 用户的显示名。*/
57
+ displayName: string;
58
+ /** 用户的头像 URL。*/
59
+ avatar: string;
60
+ /** 用户的邮件地址。*/
61
+ email: string;
62
+ /** 用户的手机号。*/
63
+ phone: string;
64
+
65
+ owner: "tsingroc";
66
+ tokenType: "access-token";
67
+ scope: "profile";
68
+ }
69
+
70
+ export interface UserInfo {
71
+ /**
72
+ * Token 的发布者。见 [JWT 标准][1]。
73
+ *
74
+ * [1]: https://datatracker.ietf.org/doc/html/rfc7519#section-4.1
75
+ */
76
+ iss: string;
77
+ /**
78
+ * Token 的主题。见 [JWT 标准][1]。
79
+ *
80
+ * [1]: https://datatracker.ietf.org/doc/html/rfc7519#section-4.1
81
+ */
82
+ sub: string;
83
+ /**
84
+ * Token 的目标应用 ID。见 [JWT 标准][1]。
85
+ *
86
+ * [1]: https://datatracker.ietf.org/doc/html/rfc7519#section-4.1
87
+ */
88
+ aud: string;
89
+ /** 用户的全名(显示名)。*/
90
+ name: string;
91
+ /** 短用户名(登录名)。*/
92
+ prefered_username?: string;
93
+ /** 用户的邮件地址。*/
94
+ email?: string;
95
+ /** 用户的邮件地址是否已确认。*/
96
+ email_verified?: boolean;
97
+ /** 用户的手机号。*/
98
+ phone?: string;
99
+ /** 用户的头像 URL。*/
100
+ picture: string;
101
+ /** 用户的地址。*/
102
+ address?: string;
103
+ /** 用户所属的用户组。*/
104
+ groups: string[];
105
+ /** 用户的角色。*/
106
+ roles: string[];
107
+ }
108
+
109
+ interface User {
110
+ /** 用户的个人信息,未经过 {@linkcode AuthCheck} 验证时为空。*/
111
+ userInfo?: UserInfo;
112
+ /** Access Token,未登录时为空。*/
113
+ accessToken?: string;
114
+ /** 解码后的 Access Token,未登录时为空。这里也会有一些用户的个人信息。*/
115
+ decodedAccessToken?: AccessTokenPayload;
116
+ }
117
+
118
+ export interface Auth extends User {
119
+ /** 全局的 Casdoor SDK 对象。*/
120
+ sdk: Sdk;
121
+ /**
122
+ * 跳转到登录。
123
+ *
124
+ * 参数 `redirect` 可以填入登录完成后要跳转到的地址。如果省略,那么跳转目标将由
125
+ * {@linkcode AuthCallback} 的 {@linkcode AuthCallbackProps.onRedirect | onRedirect}
126
+ * 属性决定,默认为跳转到主页。
127
+ */
128
+ login(redirect?: string): void;
129
+ /** 从登录页面返回时应该调用的回调。当登录状态无效时会**抛出异常**。*/
130
+ callback(): Promise<void>;
131
+ /** 在线检查用户是否登录。用户未登录时会**抛出异常**。*/
132
+ validate(): Promise<void>;
133
+ /**
134
+ * 登出。
135
+ *
136
+ * 参数 `redirect` 可以填入登出后跳转到的地址。如果省略,默认为**不做任何跳转**。
137
+ * 你也可以在登出后手动加一行跳转操作,比如使用 React Router 的 `navigate`。
138
+ */
139
+ logout(redirect?: string): void;
140
+ /**
141
+ * 以当前用户的 access token 作为 bearer token 发起请求。
142
+ * 用法与正常的 {@linkcode fetch} 完全一致。未登录时,返回的 Promise 会立即失败。
143
+ */
144
+ fetchWithAuth(
145
+ resource: RequestInfo | URL,
146
+ options?: RequestInit,
147
+ ): Promise<Response>;
148
+ }
149
+
150
+ const AuthContext = createContext<Auth | undefined>(undefined);
151
+
152
+ /**
153
+ * 获取登录鉴权相关的工具函数和全局状态。必须在 {@linkcode AuthProvider} 内部才能使用。
154
+ *
155
+ * 参数 `user` 仅供组件库内部使用,编写项目代码时可以忽略。
156
+ */
157
+ export function useAuth(user: string = "useAuth") {
158
+ const auth = useContext(AuthContext);
159
+ if (auth === undefined) {
160
+ throw new Error(user + " 必须在 AuthProvider 内部使用!");
161
+ }
162
+ return auth;
163
+ }
164
+
165
+ export interface AuthProviderProps {
166
+ /**
167
+ * 用于初始化 Casdoor SDK 的配置对象。
168
+ * {@linkcode SdkConfig.redirectPath | sdkConfig.redirectPath} 应该指向一个包含
169
+ * {@linkcode AuthCallback} 组件的路径。
170
+ */
171
+ sdkConfig: SdkConfig;
172
+ /**
173
+ * 登录 API 的源点,末尾不带 `/`。
174
+ * @default location.origin
175
+ */
176
+ signinOrigin?: string;
177
+ /**
178
+ * 登陆 API 的路径。
179
+ * @default "/api/signin"
180
+ */
181
+ signinPath?: string;
182
+ /** 需要管理登录状态的内容。*/
183
+ children: ReactNode;
184
+ }
185
+
186
+ /**
187
+ * 为包裹的内容提供登陆状态管理服务,通常应该包裹在整个 App 组件外部。
188
+ *
189
+ * 使用时至少需要提供 {@linkcode AuthProviderProps.sdkConfig | sdkConfig} 和
190
+ * {@linkcode AuthProviderProps.signinOrigin | signinOrigin} 两个属性。
191
+ * 其中,{@linkcode SdkConfig.redirectPath | sdkConfig.redirectPath} 应该指向一个包含
192
+ * {@linkcode AuthCallback} 组件的路径。
193
+ *
194
+ * 在 {@linkcode AuthProvider} 的内部,
195
+ * 可以使用 {@linkcode useAuth} 来获取登录鉴权相关的工具函数和全局状态,
196
+ * 也可以使用 {@linkcode AuthCheck} 来确保某个页面或内容只有已登录用户可以访问。
197
+ */
198
+ export function AuthProvider(props: AuthProviderProps): ReactNode {
199
+ const [user, setUser] = useState<User>(() => {
200
+ const token = localStorage.getItem("access_token");
201
+ if (!token) {
202
+ return {};
203
+ } else {
204
+ return {
205
+ accessToken: token,
206
+ get decodedAccessToken() {
207
+ return jwtDecode<AccessTokenPayload>(token);
208
+ },
209
+ };
210
+ }
211
+ });
212
+ const auth = useMemo<Auth>(() => {
213
+ const sdk = new Sdk(props.sdkConfig);
214
+ return {
215
+ sdk,
216
+ login(redirect) {
217
+ location.href = sdk.getSigninUrl();
218
+ if (redirect) {
219
+ sessionStorage.setItem("login_redirect", redirect);
220
+ }
221
+ },
222
+ async callback() {
223
+ // sdk 的类型声明有误
224
+ const resp = (await sdk.signin(
225
+ props.signinOrigin ?? location.origin,
226
+ props.signinPath,
227
+ )) as unknown as { token?: string };
228
+ const token = resp.token;
229
+ if (!token) {
230
+ throw new Error("登录 API 返回了异常的结果:" + JSON.stringify(resp));
231
+ }
232
+ localStorage.setItem("access_token", token);
233
+ setUser({
234
+ accessToken: token,
235
+ get decodedAccessToken() {
236
+ return jwtDecode<AccessTokenPayload>(token);
237
+ },
238
+ });
239
+ },
240
+ async validate() {
241
+ const token = user.accessToken;
242
+ if (!token) {
243
+ throw new Error("未登录!");
244
+ }
245
+ // sdk 的类型声明有误
246
+ const resp = (await sdk.getUserInfo(token)) as unknown as
247
+ | UserInfo
248
+ | { status: "error" };
249
+ if ("status" in resp) {
250
+ localStorage.removeItem("access_token");
251
+ setUser({});
252
+ throw new Error("登陆状态无效!");
253
+ } else {
254
+ setUser({ ...user, userInfo: resp });
255
+ }
256
+ },
257
+ logout(redirect) {
258
+ localStorage.removeItem("access_token");
259
+ setUser({});
260
+ if (redirect) {
261
+ location.href = redirect;
262
+ }
263
+ },
264
+ async fetchWithAuth(resource, options) {
265
+ const token = user.accessToken;
266
+ if (!token) {
267
+ throw new Error("未登录时无法使用 fetchWithAuth");
268
+ }
269
+ return await fetch(resource, {
270
+ ...options,
271
+ headers: {
272
+ Authorization: `Bearer ${token}`,
273
+ ...options?.headers,
274
+ },
275
+ });
276
+ },
277
+ ...user,
278
+ };
279
+ }, [props.sdkConfig, props.signinOrigin, props.signinPath, user]);
280
+ return (
281
+ <AuthContext.Provider value={auth}>{props.children}</AuthContext.Provider>
282
+ );
283
+ }
284
+
285
+ export interface AuthCheckProps {
286
+ /**
287
+ * 用户必须拥有的角色。用户必须拥有这里列出的所有角色才能访问被保护的内容。
288
+ * 如果省略,则只需要登录即可访问。
289
+ */
290
+ roles?: string[];
291
+ /**
292
+ * 在检查过程中时向用户显示的加载界面。
293
+ * @default "正在登录……"
294
+ */
295
+ loading?: ReactNode;
296
+ /**
297
+ * 当用户未登录或登录状态验证失败时的回调。默认会跳转到登录。
298
+ * @default () => auth.login(location.href) // auth = useAuth()
299
+ */
300
+ onFail?: (err: unknown) => void;
301
+ /**
302
+ * 用户已登录,但不满足权限要求时显示的内容。
303
+ * @default "您无权访问此页面!"
304
+ */
305
+ forbidden?: ReactNode;
306
+ /** 用户登录后显示的内容。*/
307
+ children: ReactNode;
308
+ }
309
+
310
+ /**
311
+ * 在线检查用户是否登录、是否满足权限要求。
312
+ * 未登录的用户会被跳转到登录,不满足权限要求的用户会见到警告信息,只有检验通过的用户才能访问内部的内容。
313
+ *
314
+ * 该组件必须在 {@linkcode AuthProvider} 内部才能使用。
315
+ */
316
+ export function AuthCheck(props: AuthCheckProps): ReactNode {
317
+ const auth = useAuth("AuthCheck");
318
+ const userInfo = auth.userInfo;
319
+ // 用于确保在 Strict Mode 下副作用也只执行一次
320
+ const effectDone = useRef(false);
321
+ useEffect(() => {
322
+ if (effectDone.current) return;
323
+ effectDone.current = true;
324
+ if (!auth.userInfo) {
325
+ auth.validate().catch(props.onFail ?? (() => auth.login(location.href)));
326
+ }
327
+ // 这个副作用只在首次渲染时执行
328
+ // eslint-disable-next-line react-hooks/exhaustive-deps
329
+ }, []);
330
+ if (userInfo) {
331
+ if ((props.roles ?? []).every((role) => userInfo.roles.includes(role))) {
332
+ return props.children;
333
+ } else {
334
+ return props.forbidden ?? "您无权访问此页面!";
335
+ }
336
+ } else {
337
+ return props.loading ?? "正在登录……";
338
+ }
339
+ }
340
+
341
+ export interface AuthCallbackProps {
342
+ /**
343
+ * 跳转时的回调。传入的 `redirect` 参数就是 {@linkcode Auth.login | login} 函数的参数。
344
+ * @default (redirect) => { location.href = redirect ?? "/"; }
345
+ */
346
+ onRedirect?: (redirect?: string) => void;
347
+ /**
348
+ * 发生错误时显示的内容。
349
+ * @default (err) => err.toString()
350
+ */
351
+ error?: (err: unknown) => ReactNode;
352
+ /**
353
+ * 跳转前显示的内容。
354
+ * @default "正在跳转……"
355
+ */
356
+ children?: ReactNode;
357
+ }
358
+
359
+ /**
360
+ * 从登录页面返回到的回调页面。
361
+ *
362
+ * 该组件必须在 {@linkcode AuthProvider} 内部才能使用。
363
+ */
364
+ export function AuthCallback(props: AuthCallbackProps): ReactNode {
365
+ const auth = useAuth("AuthCallback");
366
+ const [error, setError] = useState<unknown>();
367
+ // 用于确保在 Strict Mode 下副作用也只执行一次
368
+ const effectDone = useRef(false);
369
+ useEffect(() => {
370
+ if (effectDone.current) return;
371
+ effectDone.current = true;
372
+ const redirect = sessionStorage.getItem("login_redirect");
373
+ sessionStorage.removeItem("login_redirect");
374
+ auth.callback().then(() => {
375
+ if (props.onRedirect) {
376
+ props.onRedirect(redirect ?? undefined);
377
+ } else {
378
+ location.href = redirect ?? "/";
379
+ }
380
+ }, setError);
381
+ // 这个副作用只在首次渲染时执行
382
+ // eslint-disable-next-line react-hooks/exhaustive-deps
383
+ }, []);
384
+ if (error) {
385
+ return props.error ? props.error(error) : (error as object).toString();
386
+ } else {
387
+ return props.children ?? "正在跳转……";
388
+ }
389
+ }
@@ -0,0 +1,182 @@
1
+ import dayjs, { type Dayjs } from "dayjs";
2
+ import { createStyles } from "antd-style";
3
+ import localeData from "dayjs/plugin/localeData";
4
+ import { useState } from "react";
5
+
6
+ export interface CalendarProps
7
+ extends Omit<React.HTMLAttributes<HTMLTableElement>, "onChange"> {
8
+ /**
9
+ * 当前显示的月份。
10
+ *
11
+ * 注意:传入的月份的时区会决定整个日历所用的时区。
12
+ */
13
+ month: Dayjs;
14
+ /** 当前选中的日期。*/
15
+ selected?: Dayjs;
16
+ /** 选中日期更改时的回调函数。*/
17
+ onChange?: (selected: Dayjs) => void;
18
+ /**
19
+ * 无论一个月需要占用多少周,都总是显示 6 周。
20
+ * @default false
21
+ */
22
+ alwaysSixWeeks?: boolean;
23
+ /**
24
+ * 淡化第一周和最后几周不属于本月的日期。
25
+ * @default true
26
+ */
27
+ fadeAdjacentDays?: boolean;
28
+ /**
29
+ * 表头单元格的属性,详见 [MDN][1]。可以通过设置这里的 `children` 来自定义表头内容。
30
+ *
31
+ * [1]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/th
32
+ */
33
+ thProps?: (
34
+ /**
35
+ * 范围 0-6,表示一周的第几天
36
+ *
37
+ * 注意:这个数字的含义会受到地区设置的影响!请使用 `dayjs.weekdays()` 获取每一天的名称。
38
+ */
39
+ weekday: number,
40
+ ) => React.HTMLAttributes<HTMLTableCellElement>;
41
+ /**
42
+ * 日期单元格的属性,详见 [MDN][1]。可以通过设置这里的 `children` 来自定义单元格内容。
43
+ *
44
+ * [1]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/td
45
+ */
46
+ tdProps?: (
47
+ date: Dayjs,
48
+ selected: boolean,
49
+ ) => React.HTMLAttributes<HTMLTableCellElement>;
50
+ }
51
+
52
+ dayjs.extend(localeData);
53
+
54
+ const useStyles = createStyles(({ token, css, cx }) => {
55
+ const calendarFade = css`
56
+ color: ${token.colorTextDisabled};
57
+ `;
58
+ const calendarSelected = css`
59
+ background: ${token.colorPrimaryBg};
60
+ color: ${token.colorPrimary};
61
+ `;
62
+ const calendar = css`
63
+ box-sizing: border-box;
64
+ width: 100%;
65
+ height: 100%;
66
+ border-collapse: collapse;
67
+ font-family: ${token.fontFamily};
68
+ font-size: ${token.fontSize}px;
69
+ line-height: ${token.lineHeight};
70
+ user-select: none;
71
+
72
+ > thead > tr > th {
73
+ box-sizing: border-box;
74
+ height: ${token.fontSize * token.lineHeight + token.paddingXXS * 2}px;
75
+ padding: ${token.paddingXXS}px;
76
+ font-weight: normal;
77
+ }
78
+
79
+ > tbody > tr > td {
80
+ box-sizing: border-box;
81
+ width: calc(100% / 7);
82
+ border: 1px solid ${token.colorBorder};
83
+ padding: ${token.paddingXXS}px;
84
+ vertical-align: bottom;
85
+ transition:
86
+ background ease ${token.motionDurationFast},
87
+ color ease ${token.motionDurationFast};
88
+
89
+ &:hover:not(.${cx(calendarSelected)}) {
90
+ background: ${token.colorBgTextHover};
91
+ }
92
+ }
93
+ `;
94
+ return {
95
+ calendar,
96
+ calendarFade,
97
+ calendarSelected,
98
+ };
99
+ });
100
+
101
+ /**
102
+ * 一个日历组件。
103
+ *
104
+ * 除了文档列出的属性之外,也接受所有 [HTML Table 元素][1] 接受的属性。
105
+ *
106
+ * [1]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/table
107
+ */
108
+ function Calendar(props: CalendarProps) {
109
+ const {
110
+ month,
111
+ selected: propsSelected,
112
+ onChange,
113
+ alwaysSixWeeks,
114
+ fadeAdjacentDays = true,
115
+ thProps,
116
+ tdProps,
117
+ className,
118
+ ...rest
119
+ } = props;
120
+ const { cx, styles } = useStyles();
121
+
122
+ const [selected, setSelected] =
123
+ propsSelected === undefined
124
+ ? // eslint-disable-next-line react-hooks/rules-of-hooks
125
+ useState<Dayjs | undefined>()
126
+ : [propsSelected, () => {}];
127
+
128
+ const monthStart = month.startOf("month");
129
+ const calendarStart = monthStart.startOf("week");
130
+ const monthEnd = month.endOf("month");
131
+ const minWeeks = Math.ceil(monthEnd.diff(calendarStart, "week", true));
132
+ const weekdays = [...dayjs.weekdaysShort()];
133
+ // 按地区设置调整一周的第一天
134
+ weekdays.splice(0, 0, ...weekdays.splice(calendarStart.day()));
135
+ return (
136
+ <table {...rest} className={cx(styles.calendar, className)}>
137
+ <thead>
138
+ <tr>
139
+ {weekdays.map((name, weekday) => (
140
+ <th key={weekday} children={name} {...thProps?.(weekday)} />
141
+ ))}
142
+ </tr>
143
+ </thead>
144
+ <tbody>
145
+ {Array.from({ length: alwaysSixWeeks ? 6 : minWeeks }, (_, week) => (
146
+ <tr key={week}>
147
+ {Array(7)
148
+ .fill(null)
149
+ .map((_, weekday) => {
150
+ const date = calendarStart.add(week * 7 + weekday, "day");
151
+ const fade =
152
+ fadeAdjacentDays && date.month() != monthStart.month();
153
+ const isSelected =
154
+ Math.floor(selected?.diff(date, "day", true) ?? NaN) === 0;
155
+ const { className, ...props } = tdProps?.(date, false) ?? {};
156
+ return (
157
+ <td
158
+ key={weekday}
159
+ children={date.date()}
160
+ onClick={() => {
161
+ setSelected(date);
162
+ onChange?.(date);
163
+ }}
164
+ {...props}
165
+ className={cx(
166
+ {
167
+ [styles.calendarFade]: fade,
168
+ [styles.calendarSelected]: isSelected,
169
+ },
170
+ className,
171
+ )}
172
+ />
173
+ );
174
+ })}
175
+ </tr>
176
+ ))}
177
+ </tbody>
178
+ </table>
179
+ );
180
+ }
181
+
182
+ export default Calendar;
@@ -0,0 +1,38 @@
1
+ import { Progress, type ProgressProps } from "antd";
2
+ import type { ProgressGradient } from "antd/es/progress/progress";
3
+
4
+ export interface CircularProgressProps
5
+ extends ProgressProps,
6
+ React.RefAttributes<HTMLDivElement> {
7
+ /** 进度百分比,范围是 0-100。*/
8
+ progress?: number;
9
+ /**
10
+ * 进度条已完成部分的颜色。
11
+ * @default "#34A9D4"
12
+ */
13
+ color?: string | string[] | ProgressGradient;
14
+ }
15
+
16
+ /**
17
+ * 环形进度条。
18
+ *
19
+ * 本组件基于 [Ant Design 的 `Progress`][1] 自定义而来。
20
+ * 因此,除了文档中列出的属性以外,也兼容 `Progress` 的所有属性。
21
+ *
22
+ * [1]: https://ant-design.antgroup.com/components/progress-cn
23
+ */
24
+ function CircularProgress(props: CircularProgressProps) {
25
+ return (
26
+ <Progress
27
+ type="circle"
28
+ percent={props.progress}
29
+ strokeLinecap="butt"
30
+ strokeWidth={9}
31
+ strokeColor={props.color ?? "#34A9D4"}
32
+ trailColor="#22405A"
33
+ {...props}
34
+ />
35
+ );
36
+ }
37
+
38
+ export default CircularProgress;