py-test-component 1.0.11 → 2.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/package.json +2 -7
- package/src/react/index.js +28 -119
- package/src/vue/index.js +8 -2
- package/dist/313f7dacf2076822059d.woff +0 -0
- package/dist/4520188144a17fb24a6a.ttf +0 -0
- package/dist/py-component.esm.js +0 -2
- package/dist/py-component.esm.js.LICENSE.txt +0 -37
- package/dist/py-component.js +0 -2
- package/dist/py-component.js.LICENSE.txt +0 -37
package/package.json
CHANGED
|
@@ -1,23 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "py-test-component",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"description": "Vue2 + ElementUI 组件库,支持 React 使用",
|
|
5
5
|
"main": "dist/py-component.js",
|
|
6
6
|
"module": "dist/py-component.esm.js",
|
|
7
|
-
"types": "index.d.ts",
|
|
8
7
|
"exports": {
|
|
9
8
|
".": {
|
|
10
|
-
"types": "./index.d.ts",
|
|
11
9
|
"import": "./dist/py-component.esm.js",
|
|
12
10
|
"require": "./dist/py-component.js"
|
|
13
11
|
},
|
|
14
12
|
"./vue": {
|
|
15
|
-
"types": "./index.d.ts",
|
|
16
13
|
"import": "./src/vue/index.js",
|
|
17
14
|
"require": "./src/vue/index.js"
|
|
18
15
|
},
|
|
19
16
|
"./react": {
|
|
20
|
-
"types": "./index.d.ts",
|
|
21
17
|
"import": "./src/react/index.js",
|
|
22
18
|
"require": "./src/react/index.js"
|
|
23
19
|
}
|
|
@@ -25,8 +21,7 @@
|
|
|
25
21
|
"files": [
|
|
26
22
|
"dist",
|
|
27
23
|
"src/react",
|
|
28
|
-
"src/vue"
|
|
29
|
-
"index.d.ts"
|
|
24
|
+
"src/vue"
|
|
30
25
|
],
|
|
31
26
|
"scripts": {
|
|
32
27
|
"build": "webpack --mode production",
|
package/src/react/index.js
CHANGED
|
@@ -1,146 +1,55 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useEffect, useRef, useState, forwardRef } from 'react';
|
|
2
2
|
|
|
3
3
|
// 注入 ElementUI CSS(只执行一次)
|
|
4
4
|
let cssInjected = false;
|
|
5
5
|
function injectElementUICSS() {
|
|
6
6
|
if (cssInjected || typeof document === 'undefined') return;
|
|
7
7
|
cssInjected = true;
|
|
8
|
-
|
|
9
|
-
// 检查是否已存在
|
|
10
|
-
if (document.querySelector('link[data-py-component-css]')) return;
|
|
11
|
-
|
|
12
8
|
const link = document.createElement('link');
|
|
13
9
|
link.rel = 'stylesheet';
|
|
14
10
|
link.href = 'https://unpkg.com/element-ui/lib/theme-chalk/index.css';
|
|
15
|
-
link.dataset.pyComponentCss = 'true';
|
|
16
11
|
document.head.appendChild(link);
|
|
17
12
|
}
|
|
18
13
|
|
|
19
|
-
//
|
|
20
|
-
let loadPromise = null;
|
|
21
|
-
let isLoaded = false;
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* 加载组件库核心逻辑
|
|
25
|
-
* 动态加载 UMD 构建产物,避免循环依赖
|
|
26
|
-
*/
|
|
27
|
-
async function doLoad() {
|
|
28
|
-
if (typeof window === 'undefined') return null;
|
|
29
|
-
if (window.PyComponent) return window.PyComponent;
|
|
30
|
-
|
|
31
|
-
// 使用相对路径加载构建产物,支持 Webpack/Vite 等构建工具解析
|
|
32
|
-
const module = await import('../../dist/py-component.esm.js');
|
|
33
|
-
const PyComponent = module.default || module;
|
|
34
|
-
|
|
35
|
-
if (PyComponent && typeof window !== 'undefined') {
|
|
36
|
-
window.PyComponent = PyComponent;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
return PyComponent;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* 获取或创建加载 Promise
|
|
44
|
-
*/
|
|
45
|
-
function loadPyComponent() {
|
|
46
|
-
if (loadPromise) return loadPromise;
|
|
47
|
-
if (isLoaded && window.PyComponent) {
|
|
48
|
-
return Promise.resolve(window.PyComponent);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
loadPromise = doLoad().finally(() => {
|
|
52
|
-
isLoaded = true;
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
return loadPromise;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* 使用组件库的 Hook
|
|
60
|
-
*/
|
|
14
|
+
// 加载组件库
|
|
61
15
|
function usePyComponent() {
|
|
62
|
-
const [loaded, setLoaded] = useState(
|
|
63
|
-
const [error, setError] = useState(null);
|
|
64
|
-
|
|
16
|
+
const [loaded, setLoaded] = useState(false);
|
|
65
17
|
useEffect(() => {
|
|
66
|
-
// 自动注入 CSS
|
|
67
18
|
injectElementUICSS();
|
|
68
|
-
|
|
69
|
-
if (isLoaded) {
|
|
70
|
-
setLoaded(true);
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
loadPyComponent()
|
|
75
|
-
.then(() => setLoaded(true))
|
|
76
|
-
.catch((err) => {
|
|
77
|
-
console.error('[py-test-component] 加载失败:', err);
|
|
78
|
-
setError(err);
|
|
79
|
-
});
|
|
19
|
+
import('../../dist/py-component.esm.js').then(() => setLoaded(true));
|
|
80
20
|
}, []);
|
|
81
|
-
|
|
82
|
-
return { loaded, error, PyComponent: typeof window !== 'undefined' ? window.PyComponent : null };
|
|
21
|
+
return loaded;
|
|
83
22
|
}
|
|
84
23
|
|
|
85
|
-
//
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
24
|
+
// 通用包装函数:动态创建 React 组件
|
|
25
|
+
function wrapVueComponent(tagName, dataProp = null) {
|
|
26
|
+
return forwardRef(function WrappedComponent({ propData, loading, ...props }, ref) {
|
|
27
|
+
const elRef = useRef(null);
|
|
28
|
+
const loaded = usePyComponent();
|
|
89
29
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
30
|
+
useEffect(() => {
|
|
31
|
+
if (elRef.current && propData !== undefined && dataProp) {
|
|
32
|
+
elRef.current[dataProp] = propData;
|
|
33
|
+
}
|
|
34
|
+
}, [propData]);
|
|
95
35
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
// 直接设置 property,避免 JSON 序列化/反序列化开销
|
|
99
|
-
elRef.current.tableData = data;
|
|
36
|
+
if (!loaded && loading) {
|
|
37
|
+
return <div style={{ padding: '20px', textAlign: 'center' }}>{loading}</div>;
|
|
100
38
|
}
|
|
101
|
-
}, [data, loaded]);
|
|
102
|
-
|
|
103
|
-
if (!loaded && propsLoading) {
|
|
104
|
-
return createElement('div', { style: { padding: '20px', textAlign: 'center' } }, propsLoading);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
return createElement('py-table', { ref: elRef, ...props });
|
|
108
|
-
});
|
|
109
39
|
|
|
110
|
-
|
|
111
|
-
export const PyWeather = forwardRef(function PyWeather({ loading: propsLoading, ...props }, ref) {
|
|
112
|
-
const elRef = useRef(null);
|
|
113
|
-
const { loaded, error } = usePyComponent();
|
|
114
|
-
|
|
115
|
-
useImperativeHandle(ref, () => ({
|
|
116
|
-
getElement: () => elRef.current,
|
|
117
|
-
loaded,
|
|
118
|
-
error
|
|
119
|
-
}));
|
|
120
|
-
|
|
121
|
-
if (!loaded && propsLoading) {
|
|
122
|
-
return createElement('div', { style: { padding: '20px', textAlign: 'center' } }, propsLoading);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
return createElement('py-weather', { ref: elRef, ...props });
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
// 初始化函数
|
|
129
|
-
export function initPyComponent(config) {
|
|
130
|
-
return loadPyComponent().then((PyComponent) => {
|
|
131
|
-
if (PyComponent?.init) {
|
|
132
|
-
PyComponent.init(config);
|
|
133
|
-
}
|
|
134
|
-
return PyComponent;
|
|
40
|
+
return <tagName ref={ref || elRef} {...props} />;
|
|
135
41
|
});
|
|
136
42
|
}
|
|
137
43
|
|
|
138
|
-
//
|
|
139
|
-
export
|
|
140
|
-
|
|
141
|
-
}
|
|
44
|
+
// 一键注册所有组件
|
|
45
|
+
export const PyTable = wrapVueComponent('py-table', 'propData');
|
|
46
|
+
export const PyWeather = wrapVueComponent('py-weather', 'propData');
|
|
142
47
|
|
|
143
|
-
//
|
|
144
|
-
export function
|
|
145
|
-
return
|
|
48
|
+
// 初始化 store(唯一暴露给外部的 store 方法)
|
|
49
|
+
export function initStore(config) {
|
|
50
|
+
return import('../../dist/py-component.esm.js').then((m) => {
|
|
51
|
+
const PyComponent = m.default || m;
|
|
52
|
+
PyComponent?.store?.set?.(config);
|
|
53
|
+
return PyComponent?.store;
|
|
54
|
+
});
|
|
146
55
|
}
|
package/src/vue/index.js
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
// Vue 项目专用入口 - 直接导出 Vue 组件
|
|
2
2
|
import PyTable from '../components/PyTable.vue';
|
|
3
3
|
import PyWeather from '../components/PyWeather.vue';
|
|
4
|
+
import store from '../store';
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
|
|
6
|
+
// 初始化 store(统一的方法)
|
|
7
|
+
function initStore(config) {
|
|
8
|
+
store.set(config);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export { PyTable, PyWeather, initStore };
|
|
12
|
+
export default { PyTable, PyWeather, initStore };
|
|
Binary file
|
|
Binary file
|