@taicode/common-web 1.1.20 → 3.0.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.
- package/output/catalyst/alert.jsx +55 -0
- package/output/catalyst/auth-layout.jsx +7 -0
- package/output/catalyst/avatar.jsx +45 -0
- package/output/catalyst/badge.jsx +53 -0
- package/output/catalyst/button.jsx +187 -0
- package/output/catalyst/checkbox.jsx +105 -0
- package/output/catalyst/combobox.jsx +120 -0
- package/output/catalyst/description-list.jsx +24 -0
- package/output/catalyst/dialog.jsx +55 -0
- package/output/catalyst/divider.jsx +16 -0
- package/output/catalyst/dropdown.jsx +102 -0
- package/output/catalyst/fieldset.jsx +41 -0
- package/output/catalyst/heading.jsx +22 -0
- package/output/catalyst/input.jsx +73 -0
- package/output/catalyst/link.jsx +14 -0
- package/output/catalyst/listbox.jsx +120 -0
- package/output/catalyst/navbar.jsx +67 -0
- package/output/catalyst/pagination.jsx +52 -0
- package/output/catalyst/radio.jsx +103 -0
- package/output/catalyst/select.jsx +59 -0
- package/output/catalyst/sidebar-layout.jsx +58 -0
- package/output/catalyst/sidebar.jsx +85 -0
- package/output/catalyst/stacked-layout.jsx +55 -0
- package/output/catalyst/switch.jsx +161 -0
- package/output/catalyst/table.jsx +68 -0
- package/output/catalyst/text.jsx +29 -0
- package/output/catalyst/textarea.jsx +49 -0
- package/output/helpers/cache-api/cache-api.d.ts +13 -0
- package/output/helpers/cache-api/cache-api.d.ts.map +1 -0
- package/output/helpers/cache-api/cache-api.js +114 -0
- package/output/helpers/cache-api/cache-api.test.d.ts +2 -0
- package/output/helpers/cache-api/cache-api.test.d.ts.map +1 -0
- package/output/helpers/cache-api/cache-api.test.js +348 -0
- package/output/helpers/cache-api/index.d.ts +2 -0
- package/output/helpers/cache-api/index.d.ts.map +1 -0
- package/output/helpers/cache-api/index.js +1 -0
- package/output/helpers/service/index.d.ts +1 -0
- package/output/helpers/service/index.d.ts.map +1 -0
- package/output/helpers/service/index.js +1 -0
- package/output/helpers/service/service.d.ts +5 -0
- package/output/helpers/service/service.d.ts.map +1 -0
- package/output/helpers/service/service.js +2 -0
- package/output/helpers/side-cache/index.d.ts +2 -0
- package/output/helpers/side-cache/index.d.ts.map +1 -0
- package/output/helpers/side-cache/index.js +1 -0
- package/output/helpers/side-cache/side-cache.d.ts +10 -0
- package/output/helpers/side-cache/side-cache.d.ts.map +1 -0
- package/output/helpers/side-cache/side-cache.js +137 -0
- package/output/helpers/side-cache/side-cache.test.d.ts +2 -0
- package/output/helpers/side-cache/side-cache.test.d.ts.map +1 -0
- package/output/helpers/side-cache/side-cache.test.js +179 -0
- package/output/helpers/use-observer/index.d.ts +2 -0
- package/output/helpers/use-observer/index.d.ts.map +1 -0
- package/output/helpers/use-observer/index.js +1 -0
- package/output/helpers/use-observer/use-observer.d.ts +3 -0
- package/output/helpers/use-observer/use-observer.d.ts.map +1 -0
- package/output/helpers/use-observer/use-observer.js +16 -0
- package/output/helpers/use-observer/use-observer.test.d.ts +2 -0
- package/output/helpers/use-observer/use-observer.test.d.ts.map +1 -0
- package/output/helpers/use-observer/use-observer.test.jsx +134 -0
- package/output/loading-button/index.d.ts +31 -13
- package/output/loading-button/index.d.ts.map +1 -1
- package/output/loading-button/index.js +50 -26
- package/output/loading-button/index.test.d.ts +1 -1
- package/output/loading-button/index.test.d.ts.map +1 -1
- package/output/loading-button/index.test.js +188 -114
- package/output/service/service.test.jsx +367 -0
- package/output/signin/index.d.ts +2 -2
- package/output/signin/index.d.ts.map +1 -1
- package/output/signin/index.js +28 -10
- package/output/signin/service.d.ts.map +1 -1
- package/output/signin/service.js +6 -4
- package/output/signin/service.test.js +6 -2
- package/output/toaster/index.d.ts +1 -3
- package/output/toaster/index.d.ts.map +1 -1
- package/output/toaster/index.js +6 -13
- package/output/use-observer/use-observer.test.jsx +134 -0
- package/package.json +20 -14
- package/output/loading-button/LoadingButtonDemo.d.ts +0 -5
- package/output/loading-button/LoadingButtonDemo.d.ts.map +0 -1
- package/output/loading-button/LoadingButtonDemo.js +0 -33
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { renderHook, act } from '@testing-library/react';
|
|
3
|
+
import { observable, action } from 'mobx';
|
|
4
|
+
import { useObserver } from './use-observer';
|
|
5
|
+
describe('useObserver', () => {
|
|
6
|
+
it('应该返回初始值当没有selector时', () => {
|
|
7
|
+
const initialValue = '初始值';
|
|
8
|
+
const { result } = renderHook(() => useObserver(initialValue));
|
|
9
|
+
expect(result.current).toBe(initialValue);
|
|
10
|
+
});
|
|
11
|
+
it('应该使用selector转换值', () => {
|
|
12
|
+
const initialValue = { count: 5 };
|
|
13
|
+
const selector = (value) => value.count * 2;
|
|
14
|
+
const { result } = renderHook(() => useObserver(initialValue, selector));
|
|
15
|
+
expect(result.current).toBe(10);
|
|
16
|
+
});
|
|
17
|
+
it('应该在观察对象变化时重新渲染', async () => {
|
|
18
|
+
const observableStore = observable({
|
|
19
|
+
count: 0,
|
|
20
|
+
increment: action(() => {
|
|
21
|
+
observableStore.count++;
|
|
22
|
+
})
|
|
23
|
+
});
|
|
24
|
+
const { result } = renderHook(() => useObserver(observableStore, (store) => store.count));
|
|
25
|
+
expect(result.current).toBe(0);
|
|
26
|
+
// 改变observable的值
|
|
27
|
+
act(() => {
|
|
28
|
+
observableStore.increment();
|
|
29
|
+
});
|
|
30
|
+
// 等待React更新
|
|
31
|
+
await new Promise(resolve => setTimeout(resolve, 0));
|
|
32
|
+
expect(result.current).toBe(1);
|
|
33
|
+
});
|
|
34
|
+
it('应该处理复杂的selector函数', () => {
|
|
35
|
+
const store = observable({
|
|
36
|
+
users: [
|
|
37
|
+
{ id: 1, name: '张三', active: true },
|
|
38
|
+
{ id: 2, name: '李四', active: false },
|
|
39
|
+
{ id: 3, name: '王五', active: true }
|
|
40
|
+
]
|
|
41
|
+
});
|
|
42
|
+
const selector = (store) => store.users.filter((user) => user.active).map((user) => user.name);
|
|
43
|
+
const { result } = renderHook(() => useObserver(store, selector));
|
|
44
|
+
expect(result.current).toEqual(['张三', '王五']);
|
|
45
|
+
});
|
|
46
|
+
it('应该在observable数组变化时重新渲染', async () => {
|
|
47
|
+
const store = observable({
|
|
48
|
+
items: ['项目1', '项目2'],
|
|
49
|
+
addItem: action((item) => {
|
|
50
|
+
store.items.push(item);
|
|
51
|
+
})
|
|
52
|
+
});
|
|
53
|
+
const { result } = renderHook(() => useObserver(store, (store) => store.items.length));
|
|
54
|
+
expect(result.current).toBe(2);
|
|
55
|
+
act(() => {
|
|
56
|
+
store.addItem('项目3');
|
|
57
|
+
});
|
|
58
|
+
await new Promise(resolve => setTimeout(resolve, 0));
|
|
59
|
+
expect(result.current).toBe(3);
|
|
60
|
+
});
|
|
61
|
+
it('应该处理null值的初始值', () => {
|
|
62
|
+
const { result } = renderHook(() => useObserver(null));
|
|
63
|
+
expect(result.current).toBeNull();
|
|
64
|
+
});
|
|
65
|
+
it('应该处理undefined值的初始值', () => {
|
|
66
|
+
const { result } = renderHook(() => useObserver(undefined));
|
|
67
|
+
expect(result.current).toBeUndefined();
|
|
68
|
+
});
|
|
69
|
+
it('应该在selector为null时返回原值', () => {
|
|
70
|
+
const initialValue = { data: '测试数据' };
|
|
71
|
+
const { result } = renderHook(() => useObserver(initialValue, null));
|
|
72
|
+
expect(result.current).toBe(initialValue);
|
|
73
|
+
});
|
|
74
|
+
it('应该处理嵌套observable对象', async () => {
|
|
75
|
+
const store = observable({
|
|
76
|
+
user: {
|
|
77
|
+
profile: {
|
|
78
|
+
name: '测试用户',
|
|
79
|
+
email: 'test@example.com'
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
updateName: action((name) => {
|
|
83
|
+
store.user.profile.name = name;
|
|
84
|
+
})
|
|
85
|
+
});
|
|
86
|
+
const { result } = renderHook(() => useObserver(store, (store) => store.user.profile.name));
|
|
87
|
+
expect(result.current).toBe('测试用户');
|
|
88
|
+
act(() => {
|
|
89
|
+
store.updateName('新用户名');
|
|
90
|
+
});
|
|
91
|
+
await new Promise(resolve => setTimeout(resolve, 0));
|
|
92
|
+
expect(result.current).toBe('新用户名');
|
|
93
|
+
});
|
|
94
|
+
it('应该处理多个observable属性的变化', async () => {
|
|
95
|
+
const store = observable({
|
|
96
|
+
firstName: '张',
|
|
97
|
+
lastName: '三',
|
|
98
|
+
updateFirstName: action((name) => {
|
|
99
|
+
store.firstName = name;
|
|
100
|
+
}),
|
|
101
|
+
updateLastName: action((name) => {
|
|
102
|
+
store.lastName = name;
|
|
103
|
+
})
|
|
104
|
+
});
|
|
105
|
+
const { result } = renderHook(() => useObserver(store, (store) => `${store.firstName}${store.lastName}`));
|
|
106
|
+
expect(result.current).toBe('张三');
|
|
107
|
+
act(() => {
|
|
108
|
+
store.updateFirstName('李');
|
|
109
|
+
});
|
|
110
|
+
await new Promise(resolve => setTimeout(resolve, 0));
|
|
111
|
+
expect(result.current).toBe('李三');
|
|
112
|
+
act(() => {
|
|
113
|
+
store.updateLastName('四');
|
|
114
|
+
});
|
|
115
|
+
await new Promise(resolve => setTimeout(resolve, 0));
|
|
116
|
+
expect(result.current).toBe('李四');
|
|
117
|
+
});
|
|
118
|
+
it('应该在组件卸载时清理reaction', () => {
|
|
119
|
+
const store = observable({ count: 0 });
|
|
120
|
+
const { unmount } = renderHook(() => useObserver(store, (store) => store.count));
|
|
121
|
+
// 卸载组件应该不会抛出错误
|
|
122
|
+
expect(() => unmount()).not.toThrow();
|
|
123
|
+
});
|
|
124
|
+
it('应该处理boolean类型的返回值', () => {
|
|
125
|
+
const store = observable({ isActive: true });
|
|
126
|
+
const { result } = renderHook(() => useObserver(store, (store) => store.isActive));
|
|
127
|
+
expect(result.current).toBe(true);
|
|
128
|
+
});
|
|
129
|
+
it('应该处理数字类型的返回值', () => {
|
|
130
|
+
const store = observable({ count: 42 });
|
|
131
|
+
const { result } = renderHook(() => useObserver(store, (store) => store.count));
|
|
132
|
+
expect(result.current).toBe(42);
|
|
133
|
+
});
|
|
134
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@taicode/common-web",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.1",
|
|
4
4
|
"author": "Alain",
|
|
5
5
|
"license": "ISC",
|
|
6
6
|
"description": "",
|
|
@@ -28,31 +28,37 @@
|
|
|
28
28
|
"dev": "tsc -p tsconfig.json --watch"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
31
|
-
"@taicode/common-base": ">=1.1.0",
|
|
32
|
-
"@needle-di/core": ">=1.0.0",
|
|
33
31
|
"@heroicons/react": "^2.2.0",
|
|
34
|
-
"
|
|
32
|
+
"@needle-di/core": ">=1.0.0",
|
|
33
|
+
"@taicode/common-base": "^3.0.1",
|
|
35
34
|
"@types/react": ">=18",
|
|
36
35
|
"mobx": ">=6",
|
|
36
|
+
"mobx-react-lite": "^4.1.1",
|
|
37
37
|
"react": ">=18"
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"react-toastify": "^11.0.5",
|
|
41
|
-
"@heroicons/react": "^2.2.0",
|
|
42
40
|
"@headlessui/react": "^2.2.4",
|
|
41
|
+
"@heroicons/react": "^2.2.0",
|
|
42
|
+
"@radix-ui/react-dialog": "^1.1.15",
|
|
43
|
+
"@radix-ui/react-slot": "^1.2.4",
|
|
44
|
+
"class-variance-authority": "^0.7.1",
|
|
45
|
+
"clsx": "^2.1.1",
|
|
43
46
|
"framer-motion": "^12.19.2",
|
|
44
|
-
"
|
|
47
|
+
"sonner": "^1.7.1",
|
|
48
|
+
"tailwind-merge": "^3.4.0"
|
|
45
49
|
},
|
|
46
50
|
"devDependencies": {
|
|
47
|
-
"@
|
|
51
|
+
"@needle-di/core": "^1.0.0",
|
|
48
52
|
"@testing-library/jest-dom": "^6.1.0",
|
|
49
|
-
"
|
|
50
|
-
"vitest": "^1.0.0",
|
|
51
|
-
"@vitest/ui": "^1.0.0",
|
|
53
|
+
"@testing-library/react": "^14.0.0",
|
|
52
54
|
"@types/react": "^18.0.0",
|
|
53
|
-
"@
|
|
54
|
-
"
|
|
55
|
+
"@vitest/ui": "^1.0.0",
|
|
56
|
+
"execa": "^8.0.1",
|
|
57
|
+
"jsdom": "^23.0.0",
|
|
55
58
|
"mobx": "^6.0.0",
|
|
56
|
-
"react": "^
|
|
59
|
+
"mobx-react-lite": "^4.1.1",
|
|
60
|
+
"react": "^18.0.0",
|
|
61
|
+
"type-detect": "^4.1.0",
|
|
62
|
+
"vitest": "^1.0.0"
|
|
57
63
|
}
|
|
58
64
|
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"LoadingButtonDemo.d.ts","sourceRoot":"","sources":["../../source/loading-button/LoadingButtonDemo.tsx"],"names":[],"mappings":"AAGA;;GAEG;AACH,wBAAgB,iBAAiB,4CAsKhC"}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
-
import { useState } from 'react';
|
|
3
|
-
import { LoadingButton } from '../loading-button';
|
|
4
|
-
/**
|
|
5
|
-
* LoadingButton 组件演示
|
|
6
|
-
*/
|
|
7
|
-
export function LoadingButtonDemo() {
|
|
8
|
-
const [result, setResult] = useState('');
|
|
9
|
-
// 模拟异步保存操作
|
|
10
|
-
const handleSave = async () => {
|
|
11
|
-
setResult('正在保存...');
|
|
12
|
-
await new Promise(resolve => setTimeout(resolve, 2000));
|
|
13
|
-
setResult('保存成功!');
|
|
14
|
-
};
|
|
15
|
-
// 模拟可能失败的操作
|
|
16
|
-
const handleDelete = async () => {
|
|
17
|
-
setResult('正在删除...');
|
|
18
|
-
await new Promise(resolve => setTimeout(resolve, 1500));
|
|
19
|
-
// 模拟随机失败
|
|
20
|
-
if (Math.random() > 0.5) {
|
|
21
|
-
setResult('删除成功!');
|
|
22
|
-
}
|
|
23
|
-
else {
|
|
24
|
-
setResult('删除失败!');
|
|
25
|
-
throw new Error('删除失败');
|
|
26
|
-
}
|
|
27
|
-
};
|
|
28
|
-
// 同步操作
|
|
29
|
-
const handleClick = () => {
|
|
30
|
-
setResult('立即点击!');
|
|
31
|
-
};
|
|
32
|
-
return (_jsxs("div", { className: "space-y-8 p-8", children: [_jsxs("div", { children: [_jsx("h2", { className: "text-2xl font-bold mb-4", children: "LoadingButton \u7EC4\u4EF6\u6F14\u793A" }), _jsx("p", { className: "text-gray-600 mb-4", children: "\u6F14\u793A\u5982\u4F55\u4F7F\u7528 LoadingButton \u7EC4\u4EF6\u5904\u7406\u5F02\u6B65\u64CD\u4F5C" })] }), result && (_jsx("div", { className: "p-4 bg-gray-100 dark:bg-gray-800 rounded-lg", children: _jsx("p", { className: "text-sm font-medium", children: result }) })), _jsxs("section", { className: "space-y-4", children: [_jsx("h3", { className: "text-lg font-semibold", children: "\u57FA\u7840\u7528\u6CD5" }), _jsxs("div", { className: "flex gap-3 flex-wrap", children: [_jsx(LoadingButton, { color: "blue", onClick: handleSave, children: "\u4FDD\u5B58\u6570\u636E" }), _jsx(LoadingButton, { color: "red", onClick: handleDelete, children: "\u5220\u9664\u6570\u636E" }), _jsx(LoadingButton, { color: "green", onClick: handleClick, children: "\u540C\u6B65\u64CD\u4F5C" })] })] }), _jsxs("section", { className: "space-y-4", children: [_jsx("h3", { className: "text-lg font-semibold", children: "\u4E0D\u540C\u6837\u5F0F" }), _jsxs("div", { className: "flex gap-3 flex-wrap", children: [_jsx(LoadingButton, { color: "emerald", onClick: handleSave, children: "Solid \u6837\u5F0F" }), _jsx(LoadingButton, { outline: true, onClick: handleSave, children: "Outline \u6837\u5F0F" }), _jsx(LoadingButton, { plain: true, onClick: handleSave, children: "Plain \u6837\u5F0F" })] })] }), _jsxs("section", { className: "space-y-4", children: [_jsx("h3", { className: "text-lg font-semibold", children: "\u81EA\u5B9A\u4E49\u52A0\u8F7D\u6587\u672C" }), _jsxs("div", { className: "flex gap-3 flex-wrap", children: [_jsx(LoadingButton, { color: "blue", onClick: handleSave, loadingText: "\u6B63\u5728\u4FDD\u5B58\u6570\u636E...", children: "\u81EA\u5B9A\u4E49\u6587\u672C" }), _jsx(LoadingButton, { color: "purple", onClick: handleSave, loadingText: _jsxs("span", { className: "flex items-center gap-2", children: [_jsxs("svg", { className: "animate-spin h-4 w-4", viewBox: "0 0 24 24", children: [_jsx("circle", { className: "opacity-25", cx: "12", cy: "12", r: "10", stroke: "currentColor", strokeWidth: "4", fill: "none" }), _jsx("path", { className: "opacity-75", fill: "currentColor", d: "M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z" })] }), "\u5904\u7406\u4E2D"] }), children: "\u81EA\u5B9A\u4E49\u56FE\u6807" })] })] }), _jsxs("section", { className: "space-y-4", children: [_jsx("h3", { className: "text-lg font-semibold", children: "\u989C\u8272\u9009\u9879" }), _jsxs("div", { className: "flex gap-3 flex-wrap", children: [_jsx(LoadingButton, { color: "red", onClick: handleSave, children: "Red" }), _jsx(LoadingButton, { color: "orange", onClick: handleSave, children: "Orange" }), _jsx(LoadingButton, { color: "amber", onClick: handleSave, children: "Amber" }), _jsx(LoadingButton, { color: "yellow", onClick: handleSave, children: "Yellow" }), _jsx(LoadingButton, { color: "lime", onClick: handleSave, children: "Lime" }), _jsx(LoadingButton, { color: "green", onClick: handleSave, children: "Green" }), _jsx(LoadingButton, { color: "emerald", onClick: handleSave, children: "Emerald" }), _jsx(LoadingButton, { color: "teal", onClick: handleSave, children: "Teal" }), _jsx(LoadingButton, { color: "cyan", onClick: handleSave, children: "Cyan" }), _jsx(LoadingButton, { color: "sky", onClick: handleSave, children: "Sky" }), _jsx(LoadingButton, { color: "blue", onClick: handleSave, children: "Blue" }), _jsx(LoadingButton, { color: "indigo", onClick: handleSave, children: "Indigo" }), _jsx(LoadingButton, { color: "violet", onClick: handleSave, children: "Violet" }), _jsx(LoadingButton, { color: "purple", onClick: handleSave, children: "Purple" }), _jsx(LoadingButton, { color: "fuchsia", onClick: handleSave, children: "Fuchsia" }), _jsx(LoadingButton, { color: "pink", onClick: handleSave, children: "Pink" }), _jsx(LoadingButton, { color: "rose", onClick: handleSave, children: "Rose" })] })] }), _jsxs("section", { className: "space-y-4", children: [_jsx("h3", { className: "text-lg font-semibold", children: "\u7981\u7528\u72B6\u6001" }), _jsxs("div", { className: "flex gap-3 flex-wrap", children: [_jsx(LoadingButton, { color: "blue", disabled: true, onClick: handleSave, children: "\u7981\u7528\u6309\u94AE" }), _jsx(LoadingButton, { color: "green", onClick: handleSave, disableOnLoading: false, children: "\u52A0\u8F7D\u65F6\u4E0D\u7981\u7528" })] })] })] }));
|
|
33
|
-
}
|