py-test-component 2.0.6 → 2.0.8

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.
@@ -0,0 +1,7 @@
1
+ /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */
2
+
3
+ /**
4
+ * vue-custom-element v3.3.0
5
+ * (c) 2021 Karol Fabjańczuk
6
+ * @license MIT
7
+ */
package/package.json CHANGED
@@ -1,59 +1,61 @@
1
- {
2
- "name": "py-test-component",
3
- "version": "2.0.6",
4
- "description": "Vue2 + ElementUI 组件库,支持 React 使用",
5
- "main": "dist/py-component.js",
6
- "module": "dist/py-component.esm.js",
7
- "exports": {
8
- ".": {
9
- "import": "./dist/py-component.esm.js",
10
- "require": "./dist/py-component.js"
11
- },
12
- "./vue": {
13
- "import": "./src/vue/index.js",
14
- "require": "./src/vue/index.js"
15
- },
16
- "./react": {
17
- "import": "./src/react/index.js",
18
- "require": "./src/react/index.js"
19
- }
20
- },
21
- "files": [
22
- "dist",
23
- "src",
24
- "README.md"
25
- ],
26
- "scripts": {
27
- "build": "webpack --mode production",
28
- "dev": "webpack --mode development --watch"
29
- },
30
- "dependencies": {
31
- "element-ui": "^2.15.0",
32
- "vue": "^2.7.0",
33
- "vue-custom-element": "^3.3.0"
34
- },
35
- "devDependencies": {
36
- "@babel/core": "^7.22.0",
37
- "@babel/preset-env": "^7.22.0",
38
- "@vue/web-component-wrapper": "^1.3.0",
39
- "babel-loader": "^9.1.2",
40
- "css-loader": "^6.8.1",
41
- "style-loader": "^1.3.0",
42
- "vue-loader": "^15.10.1",
43
- "vue-template-compiler": "^2.7.14",
44
- "webpack": "^5.88.0",
45
- "webpack-cli": "^5.1.0"
46
- },
47
- "peerDependencies": {
48
- "element-ui": "^2.15.0",
49
- "vue": "^2.7.0"
50
- },
51
- "peerDependenciesMeta": {
52
- "vue": {
53
- "optional": true
54
- },
55
- "element-ui": {
56
- "optional": true
57
- }
58
- }
59
- }
1
+ {
2
+ "name": "py-test-component",
3
+ "version": "2.0.8",
4
+ "description": "Vue2 + ElementUI 组件库,支持 React 使用",
5
+ "main": "dist/py-component.js",
6
+ "module": "dist/py-component.esm.js",
7
+ "exports": {
8
+ ".": {
9
+ "import": "./dist/py-component.esm.js",
10
+ "require": "./dist/py-component.js"
11
+ },
12
+ "./vue": {
13
+ "import": "./dist/vue.esm.js",
14
+ "require": "./dist/vue.js"
15
+ },
16
+ "./react": {
17
+ "import": "./dist/react.esm.js",
18
+ "require": "./dist/react.js"
19
+ }
20
+ },
21
+ "files": [
22
+ "dist",
23
+ "README.md",
24
+ "USAGE.md",
25
+ "DEVELOPER.md"
26
+ ],
27
+ "scripts": {
28
+ "build": "webpack --mode production",
29
+ "dev": "webpack --mode development --watch"
30
+ },
31
+ "dependencies": {
32
+ "element-ui": "^2.15.0",
33
+ "vue": "^2.7.0",
34
+ "vue-custom-element": "^3.3.0"
35
+ },
36
+ "devDependencies": {
37
+ "@babel/core": "^7.22.0",
38
+ "@babel/preset-env": "^7.22.0",
39
+ "@babel/preset-react": "^7.28.5",
40
+ "@vue/web-component-wrapper": "^1.3.0",
41
+ "babel-loader": "^9.1.2",
42
+ "css-loader": "^6.8.1",
43
+ "style-loader": "^1.3.0",
44
+ "vue-loader": "^15.10.1",
45
+ "vue-template-compiler": "^2.7.14",
46
+ "webpack": "^5.88.0",
47
+ "webpack-cli": "^5.1.0"
48
+ },
49
+ "peerDependencies": {
50
+ "element-ui": "^2.15.0",
51
+ "vue": "^2.7.0"
52
+ },
53
+ "peerDependenciesMeta": {
54
+ "vue": {
55
+ "optional": true
56
+ },
57
+ "element-ui": {
58
+ "optional": true
59
+ }
60
+ }
61
+ }
@@ -1,43 +0,0 @@
1
- <template>
2
- <el-table
3
- :data="tableData"
4
- style="width: 100%">
5
- <el-table-column
6
- prop="date"
7
- label="日期"
8
- width="180">
9
- </el-table-column>
10
- <el-table-column
11
- prop="name"
12
- label="姓名"
13
- width="180">
14
- </el-table-column>
15
- <el-table-column
16
- prop="address"
17
- label="地址">
18
- </el-table-column>
19
- </el-table>
20
- </template>
21
-
22
- <script>
23
- export default {
24
- name: 'PyTable',
25
- props: {
26
- propData: {
27
- default: null
28
- }
29
- },
30
- computed: {
31
- tableData() {
32
- // el-table 需要数组,如果不是数组则返回空数组
33
- return Array.isArray(this.propData) ? this.propData : []
34
- }
35
- }
36
- };
37
- </script>
38
-
39
- <style scoped>
40
- .py-table-wrapper {
41
- padding: 10px;
42
- }
43
- </style>
@@ -1,109 +0,0 @@
1
- <template>
2
- <div class="py-weather">
3
- <div class="search-box">
4
- <el-input
5
- v-model="city"
6
- placeholder="请输入城市名称"
7
- @keyup.enter.native="handleSearch"
8
- clearable
9
- />
10
- <el-button type="primary" @click="handleSearch" :loading="loading">
11
- 查询
12
- </el-button>
13
- </div>
14
-
15
- <div v-if="weatherData" class="weather-info">
16
- <h3>{{ weatherData.location.name }}</h3>
17
- <div class="weather-detail">
18
- <span class="temperature">{{ weatherData.now.temperature }}°C</span>
19
- <span class="weather-text">{{ weatherData.now.text }}</span>
20
- </div>
21
- </div>
22
-
23
- <div v-if="error" class="error-message">
24
- {{ error }}
25
- </div>
26
- </div>
27
- </template>
28
-
29
- <script>
30
- import { queryWeather } from '../utils/api';
31
-
32
- export default {
33
- name: 'PyWeather',
34
- data() {
35
- return {
36
- city: '',
37
- loading: false,
38
- weatherData: null,
39
- error: null
40
- };
41
- },
42
- methods: {
43
- async handleSearch() {
44
- if (!this.city.trim()) {
45
- this.$message?.warning?.('请输入城市名称') || alert('请输入城市名称');
46
- return;
47
- }
48
-
49
- this.loading = true;
50
- this.error = null;
51
-
52
- try {
53
- const response = await queryWeather(this.city.trim());
54
- if (response.results && response.results[0]) {
55
- this.weatherData = response.results[0];
56
- } else {
57
- this.error = '未找到该城市的天气信息';
58
- this.weatherData = null;
59
- }
60
- } catch (err) {
61
- this.error = '查询失败,请稍后重试';
62
- this.weatherData = null;
63
- } finally {
64
- this.loading = false;
65
- }
66
- }
67
- }
68
- };
69
- </script>
70
-
71
- <style scoped>
72
- .py-weather {
73
- padding: 20px;
74
- max-width: 400px;
75
- }
76
- .search-box {
77
- display: flex;
78
- gap: 10px;
79
- }
80
- .weather-info {
81
- margin-top: 20px;
82
- padding: 20px;
83
- background: #f5f7fa;
84
- border-radius: 8px;
85
- }
86
- .weather-info h3 {
87
- margin: 0 0 15px 0;
88
- color: #303133;
89
- }
90
- .weather-detail {
91
- display: flex;
92
- align-items: center;
93
- gap: 15px;
94
- }
95
- .temperature {
96
- font-size: 36px;
97
- font-weight: bold;
98
- color: #409EFF;
99
- }
100
- .weather-text {
101
- font-size: 18px;
102
- color: #606266;
103
- }
104
- .error-message {
105
- margin-top: 15px;
106
- color: #f56c6c;
107
- font-size: 14px;
108
- }
109
- </style>
package/src/index.js DELETED
@@ -1,38 +0,0 @@
1
- import Vue from 'vue';
2
- import ElementUI from 'element-ui';
3
- import 'element-ui/lib/theme-chalk/index.css';
4
- import vueCustomElement from 'vue-custom-element';
5
-
6
- import PyTable from './components/PyTable.vue';
7
- import PyWeather from './components/PyWeather.vue';
8
- import store from './store';
9
-
10
- Vue.use(ElementUI);
11
- Vue.use(vueCustomElement);
12
-
13
- // 使用 vue-custom-element 创建 Web Components(禁用 Shadow DOM)
14
- Vue.customElement('py-table', PyTable, {
15
- shadow: false,
16
- props: {
17
- propData: {
18
- default: null
19
- }
20
- }
21
- });
22
-
23
- Vue.customElement('py-weather', PyWeather, {
24
- shadow: false
25
- });
26
-
27
- // 初始化 store(唯一暴露给外部的 store 方法)
28
- function initStore(config = {}) {
29
- store.set(config);
30
- }
31
-
32
- // 导出
33
- export { PyTable, PyWeather, initStore };
34
- export default { initStore };
35
-
36
- if (typeof window !== 'undefined') {
37
- window.PyComponent = { initStore };
38
- }
@@ -1,55 +0,0 @@
1
- import React, { useEffect, useRef, useState, forwardRef } from 'react';
2
-
3
- // 注入 ElementUI CSS(只执行一次)
4
- let cssInjected = false;
5
- function injectElementUICSS() {
6
- if (cssInjected || typeof document === 'undefined') return;
7
- cssInjected = true;
8
- const link = document.createElement('link');
9
- link.rel = 'stylesheet';
10
- link.href = 'https://unpkg.com/element-ui/lib/theme-chalk/index.css';
11
- document.head.appendChild(link);
12
- }
13
-
14
- // 加载组件库
15
- function usePyComponent() {
16
- const [loaded, setLoaded] = useState(false);
17
- useEffect(() => {
18
- injectElementUICSS();
19
- import('py-test-component').then(() => setLoaded(true));
20
- }, []);
21
- return loaded;
22
- }
23
-
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();
29
-
30
- useEffect(() => {
31
- if (elRef.current && propData !== undefined && dataProp) {
32
- elRef.current[dataProp] = propData;
33
- }
34
- }, [propData]);
35
-
36
- if (!loaded && loading) {
37
- return <div style={{ padding: '20px', textAlign: 'center' }}>{loading}</div>;
38
- }
39
-
40
- // 使用 createElement 避免 JSX 标签名大小写警告
41
- return React.createElement(tagName, { ref: ref || elRef, ...props });
42
- });
43
- }
44
-
45
- // 一键注册所有组件
46
- export const PyTable = wrapVueComponent('py-table', 'propData');
47
- export const PyWeather = wrapVueComponent('py-weather', 'propData');
48
-
49
- // 初始化 store(仅设置,不暴露 store 给外部)
50
- export function initStore(config) {
51
- return import('py-test-component').then((m) => {
52
- const PyComponent = m.default || m;
53
- PyComponent?.initStore?.(config);
54
- });
55
- }
@@ -1,75 +0,0 @@
1
- import Vue from 'vue';
2
-
3
- // 使用 Vue.observable 创建响应式全局状态
4
- const state = Vue.observable({
5
- config: {}
6
- });
7
-
8
- // 订阅者列表(用于非 Vue 环境,如 React)
9
- const subscribers = new Set();
10
-
11
- function notifySubscribers(key, value) {
12
- subscribers.forEach(fn => fn(key, value, state.config));
13
- }
14
-
15
- const store = {
16
- /**
17
- * 获取 store 中的值
18
- * @param {string} key - 键名,支持点号路径如 'user.name'
19
- * @returns {any} 值
20
- */
21
- get(key) {
22
- if (!key) return state.config;
23
- const keys = key.split('.');
24
- let value = state.config;
25
- for (const k of keys) {
26
- if (value === undefined || value === null) return undefined;
27
- value = value[k];
28
- }
29
- return value;
30
- },
31
-
32
- /**
33
- * 设置 store 中的值
34
- * @param {string} key - 键名
35
- * @param {any} value - 值
36
- */
37
- set(key, value) {
38
- if (typeof key === 'object') {
39
- Object.assign(state.config, key);
40
- notifySubscribers(null, state.config);
41
- } else {
42
- const keys = key.split('.');
43
- let target = state.config;
44
- for (let i = 0; i < keys.length - 1; i++) {
45
- const k = keys[i];
46
- if (!(k in target) || typeof target[k] !== 'object') {
47
- target[k] = {};
48
- }
49
- target = target[k];
50
- }
51
- target[keys[keys.length - 1]] = value;
52
- notifySubscribers(key, value);
53
- }
54
- },
55
-
56
- /**
57
- * 订阅 store 变化(供 React 等使用)
58
- * @param {function} callback - (key, value, config) => void
59
- * @returns {function} 取消订阅函数
60
- */
61
- subscribe(callback) {
62
- subscribers.add(callback);
63
- return () => subscribers.delete(callback);
64
- },
65
-
66
- /**
67
- * 获取整个状态对象(供内部组件使用)
68
- * @returns {object}
69
- */
70
- getState() {
71
- return state;
72
- }
73
- };
74
-
75
- export default store;
package/src/utils/api.js DELETED
@@ -1,35 +0,0 @@
1
- import { get, post } from './request';
2
-
3
- /**
4
- * 查询天气
5
- * @param {string} city - 城市名称
6
- * @returns {Promise<object>}
7
- */
8
- export function queryWeather(city) {
9
- // 使用免费的天气 API(心知天气或 OpenWeatherMap 等)
10
- // 这里使用一个示例 API,实际使用时可以替换为真实的天气 API
11
- const apiKey = 'demo'; // 实际使用时替换为真实的 API Key
12
- const url = `https://api.seniverse.com/v3/weather/now.json`;
13
-
14
- // 如果没有真实 API,返回模拟数据
15
- if (apiKey === 'demo') {
16
- return new Promise((resolve) => {
17
- setTimeout(() => {
18
- resolve({
19
- results: [{
20
- location: { name: city },
21
- now: {
22
- temperature: Math.floor(Math.random() * 30) + 5,
23
- text: ['晴', '多云', '阴', '小雨', '大雨'][Math.floor(Math.random() * 5)]
24
- }
25
- }]
26
- });
27
- }, 500);
28
- });
29
- }
30
-
31
- return get(url, {
32
- key: apiKey,
33
- location: city
34
- });
35
- }
@@ -1,68 +0,0 @@
1
- /**
2
- * 封装 fetch POST 请求
3
- * @param {string} url - 请求地址
4
- * @param {object} data - 请求数据
5
- * @param {object} options - 其他配置
6
- * @returns {Promise}
7
- */
8
- export function post(url, data = {}, options = {}) {
9
- const defaultOptions = {
10
- method: 'POST',
11
- headers: {
12
- 'Content-Type': 'application/json'
13
- },
14
- ...options
15
- };
16
-
17
- // 如果 data 是 FormData,不设置 Content-Type
18
- if (data instanceof FormData) {
19
- delete defaultOptions.headers['Content-Type'];
20
- } else {
21
- defaultOptions.body = JSON.stringify(data);
22
- }
23
-
24
- return fetch(url, defaultOptions)
25
- .then(response => {
26
- if (!response.ok) {
27
- throw new Error(`HTTP error! status: ${response.status}`);
28
- }
29
- return response.json();
30
- })
31
- .catch(error => {
32
- console.error('Request failed:', error);
33
- throw error;
34
- });
35
- }
36
-
37
- /**
38
- * 封装 fetch GET 请求
39
- * @param {string} url - 请求地址
40
- * @param {object} params - URL 参数
41
- * @param {object} options - 其他配置
42
- * @returns {Promise}
43
- */
44
- export function get(url, params = {}, options = {}) {
45
- const queryString = Object.keys(params)
46
- .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`)
47
- .join('&');
48
-
49
- const fullUrl = queryString ? `${url}?${queryString}` : url;
50
-
51
- return fetch(fullUrl, {
52
- method: 'GET',
53
- headers: {
54
- 'Content-Type': 'application/json'
55
- },
56
- ...options
57
- })
58
- .then(response => {
59
- if (!response.ok) {
60
- throw new Error(`HTTP error! status: ${response.status}`);
61
- }
62
- return response.json();
63
- })
64
- .catch(error => {
65
- console.error('Request failed:', error);
66
- throw error;
67
- });
68
- }
package/src/vue/index.js DELETED
@@ -1,12 +0,0 @@
1
- // Vue 项目专用入口 - 直接导出 Vue 组件
2
- import PyTable from '../components/PyTable.vue';
3
- import PyWeather from '../components/PyWeather.vue';
4
- import store from '../store';
5
-
6
- // 初始化 store(统一的方法)
7
- function initStore(config) {
8
- store.set(config);
9
- }
10
-
11
- export { PyTable, PyWeather, initStore };
12
- export default { PyTable, PyWeather, initStore };