uview-pro 0.2.4 → 0.3.0

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.
Files changed (162) hide show
  1. package/changelog.md +29 -0
  2. package/components/u-action-sheet/types.ts +2 -0
  3. package/components/u-action-sheet/u-action-sheet.vue +15 -4
  4. package/components/u-alert-tips/types.ts +2 -0
  5. package/components/u-alert-tips/u-alert-tips.vue +24 -9
  6. package/components/u-avatar/types.ts +2 -0
  7. package/components/u-avatar/u-avatar.vue +15 -5
  8. package/components/u-avatar-cropper/u-avatar-cropper.vue +13 -2
  9. package/components/u-back-top/types.ts +2 -5
  10. package/components/u-back-top/u-back-top.vue +27 -15
  11. package/components/u-badge/types.ts +2 -0
  12. package/components/u-badge/u-badge.vue +33 -15
  13. package/components/u-button/types.ts +2 -2
  14. package/components/u-button/u-button.vue +23 -10
  15. package/components/u-car-keyboard/types.ts +2 -0
  16. package/components/u-car-keyboard/u-car-keyboard.vue +14 -5
  17. package/components/u-card/types.ts +2 -0
  18. package/components/u-card/u-card.vue +25 -10
  19. package/components/u-cell-group/types.ts +2 -0
  20. package/components/u-cell-group/u-cell-group.vue +15 -5
  21. package/components/u-cell-item/types.ts +2 -0
  22. package/components/u-cell-item/u-cell-item.vue +24 -11
  23. package/components/u-checkbox/types.ts +2 -0
  24. package/components/u-checkbox/u-checkbox.vue +12 -13
  25. package/components/u-checkbox-group/types.ts +2 -0
  26. package/components/u-checkbox-group/u-checkbox-group.vue +1 -1
  27. package/components/u-circle-progress/types.ts +2 -0
  28. package/components/u-circle-progress/u-circle-progress.vue +24 -9
  29. package/components/u-city-select/types.ts +2 -0
  30. package/components/u-city-select/u-city-select.vue +16 -4
  31. package/components/u-col/types.ts +2 -0
  32. package/components/u-col/u-col.vue +27 -8
  33. package/components/u-collapse/u-collapse.vue +8 -4
  34. package/components/u-collapse-item/u-collapse-item.vue +32 -27
  35. package/components/u-column-notice/types.ts +2 -0
  36. package/components/u-column-notice/u-column-notice.vue +16 -4
  37. package/components/u-count-down/types.ts +2 -0
  38. package/components/u-count-down/u-count-down.vue +20 -8
  39. package/components/u-count-to/types.ts +2 -0
  40. package/components/u-count-to/u-count-to.vue +25 -7
  41. package/components/u-divider/types.ts +2 -0
  42. package/components/u-divider/u-divider.vue +26 -10
  43. package/components/u-dropdown/types.ts +2 -0
  44. package/components/u-dropdown/u-dropdown.vue +48 -20
  45. package/components/u-dropdown-item/types.ts +2 -0
  46. package/components/u-dropdown-item/u-dropdown-item.vue +33 -41
  47. package/components/u-empty/types.ts +2 -0
  48. package/components/u-empty/u-empty.vue +20 -5
  49. package/components/u-field/types.ts +2 -0
  50. package/components/u-field/u-field.vue +18 -3
  51. package/components/u-form/types.ts +2 -0
  52. package/components/u-form/u-form.vue +20 -26
  53. package/components/u-form-item/types.ts +2 -0
  54. package/components/u-form-item/u-form-item.vue +67 -67
  55. package/components/u-full-screen/types.ts +2 -0
  56. package/components/u-full-screen/u-full-screen.vue +16 -2
  57. package/components/u-gap/types.ts +2 -0
  58. package/components/u-gap/u-gap.vue +15 -5
  59. package/components/u-grid/types.ts +2 -0
  60. package/components/u-grid/u-grid.vue +23 -25
  61. package/components/u-grid-item/types.ts +3 -3
  62. package/components/u-grid-item/u-grid-item.vue +43 -47
  63. package/components/u-icon/types.ts +0 -1
  64. package/components/u-icon/u-icon.vue +5 -3
  65. package/components/u-image/u-image.vue +13 -4
  66. package/components/u-index-anchor/types.ts +3 -3
  67. package/components/u-index-anchor/u-index-anchor.vue +18 -10
  68. package/components/u-index-list/u-index-list.vue +9 -12
  69. package/components/u-input/types.ts +2 -5
  70. package/components/u-input/u-input.vue +15 -6
  71. package/components/u-keyboard/u-keyboard.vue +13 -4
  72. package/components/u-lazy-load/u-lazy-load.vue +13 -2
  73. package/components/u-line/u-line.vue +13 -4
  74. package/components/u-line-progress/u-line-progress.vue +13 -4
  75. package/components/u-link/u-link.vue +13 -4
  76. package/components/u-loading/u-loading.vue +13 -4
  77. package/components/u-loading-popup/u-loading-popup.vue +14 -0
  78. package/components/u-loadmore/u-loadmore.vue +13 -2
  79. package/components/u-mask/types.ts +2 -5
  80. package/components/u-mask/u-mask.vue +21 -14
  81. package/components/u-message-input/u-message-input.vue +13 -2
  82. package/components/u-modal/u-modal.vue +13 -4
  83. package/components/u-navbar/u-navbar.vue +13 -2
  84. package/components/u-no-network/u-no-network.vue +13 -2
  85. package/components/u-notice-bar/u-notice-bar.vue +13 -3
  86. package/components/u-number-box/u-number-box.vue +13 -4
  87. package/components/u-number-keyboard/u-number-keyboard.vue +13 -4
  88. package/components/u-picker/u-picker.vue +13 -4
  89. package/components/u-popup/types.ts +2 -2
  90. package/components/u-popup/u-popup.vue +15 -5
  91. package/components/u-radio/types.ts +2 -0
  92. package/components/u-radio/u-radio.vue +26 -19
  93. package/components/u-radio-group/types.ts +2 -0
  94. package/components/u-radio-group/u-radio-group.vue +20 -32
  95. package/components/u-rate/types.ts +2 -0
  96. package/components/u-rate/u-rate.vue +20 -5
  97. package/components/u-read-more/types.ts +2 -0
  98. package/components/u-read-more/u-read-more.vue +23 -7
  99. package/components/u-root-portal/u-root-portal.vue +3 -1
  100. package/components/u-row/types.ts +2 -0
  101. package/components/u-row/u-row.vue +29 -11
  102. package/components/u-row-notice/types.ts +2 -0
  103. package/components/u-row-notice/u-row-notice.vue +16 -4
  104. package/components/u-safe-bottom/u-safe-bottom.vue +13 -2
  105. package/components/u-search/types.ts +2 -0
  106. package/components/u-search/u-search.vue +16 -5
  107. package/components/u-section/types.ts +2 -0
  108. package/components/u-section/u-section.vue +31 -12
  109. package/components/u-select/types.ts +2 -0
  110. package/components/u-select/u-select.vue +15 -5
  111. package/components/u-skeleton/types.ts +2 -0
  112. package/components/u-skeleton/u-skeleton.vue +14 -3
  113. package/components/u-slider/types.ts +2 -0
  114. package/components/u-slider/u-slider.vue +27 -19
  115. package/components/u-status-bar/u-status-bar.vue +13 -4
  116. package/components/u-steps/types.ts +2 -0
  117. package/components/u-steps/u-steps.vue +15 -3
  118. package/components/u-sticky/types.ts +2 -0
  119. package/components/u-sticky/u-sticky.vue +23 -7
  120. package/components/u-subsection/types.ts +2 -0
  121. package/components/u-subsection/u-subsection.vue +14 -3
  122. package/components/u-swipe-action/types.ts +2 -0
  123. package/components/u-swipe-action/u-swipe-action.vue +19 -3
  124. package/components/u-swiper/types.ts +2 -0
  125. package/components/u-swiper/u-swiper.vue +19 -2
  126. package/components/u-switch/types.ts +2 -0
  127. package/components/u-switch/u-switch.vue +15 -6
  128. package/components/u-tabbar/types.ts +2 -0
  129. package/components/u-tabbar/u-tabbar.vue +20 -3
  130. package/components/u-table/types.ts +2 -0
  131. package/components/u-table/u-table.vue +15 -3
  132. package/components/u-tabs/types.ts +2 -0
  133. package/components/u-tabs/u-tabs.vue +14 -3
  134. package/components/u-tabs-swiper/types.ts +2 -0
  135. package/components/u-tabs-swiper/u-tabs-swiper.vue +15 -3
  136. package/components/u-tag/types.ts +2 -0
  137. package/components/u-tag/u-tag.vue +22 -6
  138. package/components/u-td/types.ts +2 -0
  139. package/components/u-td/u-td.vue +14 -3
  140. package/components/u-text/u-text.vue +4 -2
  141. package/components/u-th/types.ts +2 -0
  142. package/components/u-th/u-th.vue +14 -3
  143. package/components/u-time-line/u-time-line.vue +17 -3
  144. package/components/u-time-line-item/types.ts +2 -0
  145. package/components/u-time-line-item/u-time-line-item.vue +15 -3
  146. package/components/u-toast/types.ts +2 -0
  147. package/components/u-toast/u-toast.vue +15 -8
  148. package/components/u-top-tips/types.ts +2 -0
  149. package/components/u-top-tips/u-top-tips.vue +20 -3
  150. package/components/u-tr/types.ts +4 -1
  151. package/components/u-tr/u-tr.vue +17 -2
  152. package/components/u-upload/types.ts +2 -0
  153. package/components/u-upload/u-upload.vue +14 -5
  154. package/components/u-verification-code/types.ts +2 -0
  155. package/components/u-verification-code/u-verification-code.vue +15 -3
  156. package/components/u-waterfall/types.ts +2 -0
  157. package/components/u-waterfall/u-waterfall.vue +15 -3
  158. package/libs/hooks/index.ts +2 -1
  159. package/libs/hooks/useCompRelation.ts +364 -0
  160. package/libs/index.ts +56 -39
  161. package/package.json +1 -1
  162. package/readme.md +6 -6
@@ -1,10 +1,12 @@
1
1
  import type { ExtractPropTypes, PropType } from 'vue';
2
+ import { baseProps } from '../common/props';
2
3
 
3
4
  /**
4
5
  * WaterfallProps waterfall props 类型定义
5
6
  * @description 瀑布流组件,支持数据、间隔、idKey
6
7
  */
7
8
  export const WaterfallProps = {
9
+ ...baseProps,
8
10
  /** 瀑布流数据数组,必填 */
9
11
  modelValue: { type: Array as PropType<any[]>, required: true, default: () => [] },
10
12
  /** 新增数据的动画间隔,单位ms */
@@ -1,5 +1,5 @@
1
1
  <template>
2
- <view class="u-waterfall">
2
+ <view class="u-waterfall" :class="customClass" :style="$u.toStyle(customStyle)">
3
3
  <view id="u-left-column" class="u-column">
4
4
  <slot name="left" :leftList="leftList"></slot>
5
5
  </view>
@@ -8,13 +8,25 @@
8
8
  </view>
9
9
  </view>
10
10
  </template>
11
+
12
+ <script lang="ts">
13
+ export default {
14
+ name: 'u-waterfall',
15
+ options: {
16
+ addGlobalClass: true,
17
+ // #ifndef MP-TOUTIAO
18
+ virtualHost: true,
19
+ // #endif
20
+ styleIsolation: 'shared'
21
+ }
22
+ };
23
+ </script>
24
+
11
25
  <script setup lang="ts">
12
26
  import { ref, computed, watch, onMounted, getCurrentInstance } from 'vue';
13
27
  import { $u } from '../..';
14
28
  import { WaterfallProps } from './types';
15
29
 
16
- defineOptions({ name: 'u-waterfall' });
17
-
18
30
  /**
19
31
  * waterfall 瀑布流
20
32
  * @description 这是一个瀑布流形式的组件,内容分为左右两列,结合uView的懒加载组件效果更佳。相较于某些只是奇偶数左右分别,或者没有利用vue作用域插槽的做法,uView的瀑布流实现了真正的 组件化,搭配LazyLoad 懒加载和loadMore 加载更多组件,让您开箱即用,眼前一亮。
@@ -1,3 +1,4 @@
1
- export * from './useComponent';
1
+ // export * from './useComponent';
2
2
  export * from './useEmitter';
3
3
  export * from './useRect';
4
+ export * from './useCompRelation';
@@ -0,0 +1,364 @@
1
+ // utils/useComponent.ts
2
+ import { ref, reactive, getCurrentInstance, onUnmounted, nextTick, computed, watch, onMounted } from 'vue';
3
+ import { logger } from '../util/logger';
4
+
5
+ // 类型定义
6
+ interface ParentContext {
7
+ name: string;
8
+ addChild: (child: ChildContext) => void;
9
+ removeChild: (childId: string) => void;
10
+ broadcast: (event: string, data?: any) => void;
11
+ getChildren: () => ChildContext[];
12
+ getExposed: () => Record<string, any>;
13
+ getChildExposed: (childId: string) => Record<string, any>;
14
+ getChildrenExposed: () => Array<{ id: string; name: string; exposed: Record<string, any> }>;
15
+ }
16
+
17
+ interface ChildContext {
18
+ id: string;
19
+ name: string;
20
+ emitToParent: (event: string, data?: any) => void;
21
+ getParentExposed: () => Record<string, any>;
22
+ getInstance: () => any;
23
+ getExposed: () => Record<string, any>;
24
+ }
25
+
26
+ // 符号定义
27
+ const PARENT_CONTEXT_SYMBOL = Symbol('parent_context');
28
+ const CHILDREN_CONTEXT_SYMBOL = Symbol('children_context');
29
+
30
+ /**
31
+ * 生成实例唯一ID
32
+ */
33
+ function generateInstanceId(componentName: string): string {
34
+ return `${componentName}_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
35
+ }
36
+
37
+ /**
38
+ * 查找父组件实例
39
+ */
40
+ function findParentInstance(name: string, instance: any): any {
41
+ if (!instance) return null;
42
+
43
+ let parent = instance.parent;
44
+ while (parent) {
45
+ const parentName = parent.type?.name || parent.type?.__name;
46
+ if (parentName === name) {
47
+ return parent;
48
+ }
49
+ parent = parent.parent;
50
+ }
51
+ return null;
52
+ }
53
+
54
+ /**
55
+ * 获取父组件上下文
56
+ */
57
+ function getParentContext(name: string, instance: any): ParentContext | null {
58
+ const parentInstance = findParentInstance(name, instance);
59
+ if (parentInstance) {
60
+ return parentInstance.proxy?.[PARENT_CONTEXT_SYMBOL] || null;
61
+ }
62
+ return null;
63
+ }
64
+
65
+ /**
66
+ * 父组件 Hook
67
+ */
68
+ export function useParent(componentName?: string) {
69
+ const instance = getCurrentInstance();
70
+ if (!instance) {
71
+ throw new Error('useParent must be called within setup function');
72
+ }
73
+
74
+ // 使用组件名称作为默认名称
75
+ const name = componentName || instance.type.name || instance.type.__name;
76
+ if (!name) {
77
+ throw new Error('Component name is required for useParent. Either provide a name or set component name.');
78
+ }
79
+
80
+ const children = reactive<ChildContext[]>([]);
81
+ const childrenMap = new Map<string, ChildContext>();
82
+
83
+ // 父组件上下文
84
+ const parentContext: ParentContext = {
85
+ name,
86
+
87
+ addChild(child: ChildContext) {
88
+ if (!childrenMap.has(child.id)) {
89
+ childrenMap.set(child.id, child);
90
+ children.push(child);
91
+ logger.log(`Parent ${name} added child: ${child.name} (${child.id})`);
92
+ }
93
+ },
94
+
95
+ removeChild(childId: string) {
96
+ if (childrenMap.has(childId)) {
97
+ const child = childrenMap.get(childId)!;
98
+ childrenMap.delete(childId);
99
+ const index = children.findIndex(c => c.id === childId);
100
+ if (index > -1) {
101
+ children.splice(index, 1);
102
+ }
103
+ logger.log(`Parent ${name} removed child: ${childId}`);
104
+ }
105
+ },
106
+
107
+ broadcast(event: string, data?: any) {
108
+ logger.log(`Parent ${name} broadcasting event: ${event} to ${childrenMap.size} children`);
109
+ childrenMap.forEach(child => {
110
+ const exposed = child.getExposed();
111
+ if (exposed && typeof exposed[event] === 'function') {
112
+ try {
113
+ exposed[event](data);
114
+ } catch (error) {
115
+ logger.warn(`Error calling child method ${event}:`, error);
116
+ }
117
+ }
118
+ });
119
+ },
120
+
121
+ getChildren() {
122
+ return Array.from(childrenMap.values());
123
+ },
124
+
125
+ getExposed() {
126
+ return instance.exposed || {};
127
+ },
128
+
129
+ getChildExposed(childId: string) {
130
+ const child = childrenMap.get(childId);
131
+ if (child && child.getExposed) {
132
+ return child.getExposed();
133
+ }
134
+ logger.warn(`Child ${childId} not found or does not have getExposed method`);
135
+ return {};
136
+ },
137
+
138
+ getChildrenExposed() {
139
+ return Array.from(childrenMap.values())
140
+ .filter(child => child.getExposed)
141
+ .map(child => {
142
+ const exposed = child.getExposed();
143
+ return {
144
+ id: child.id,
145
+ name: child.name,
146
+ exposed: exposed
147
+ };
148
+ })
149
+ .filter(item => Object.keys(item.exposed).length > 0);
150
+ }
151
+ };
152
+
153
+ // 在组件实例上存储父组件上下文
154
+ if (instance.proxy) {
155
+ instance.proxy[PARENT_CONTEXT_SYMBOL] = parentContext;
156
+ }
157
+
158
+ // 组件卸载时清理
159
+ onUnmounted(() => {
160
+ // 清理所有子组件引用
161
+ childrenMap.forEach((child, childId) => {
162
+ parentContext.removeChild(childId);
163
+ });
164
+ if (instance.proxy) {
165
+ delete instance.proxy[PARENT_CONTEXT_SYMBOL];
166
+ }
167
+ logger.log(`Parent ${name} unmounted and cleaned up`);
168
+ });
169
+
170
+ return {
171
+ parentName: name,
172
+ children,
173
+ broadcast: parentContext.broadcast,
174
+ getChildren: parentContext.getChildren,
175
+ getChildExposed: parentContext.getChildExposed,
176
+ getChildrenExposed: parentContext.getChildrenExposed,
177
+ getExposed: parentContext.getExposed
178
+ };
179
+ }
180
+
181
+ /**
182
+ * 子组件 Hook
183
+ */
184
+ export function useChildren(componentName?: string, parentName?: string) {
185
+ const instance = getCurrentInstance();
186
+ if (!instance) {
187
+ throw new Error('useChildren must be called within setup function');
188
+ }
189
+
190
+ // 使用组件名称作为默认名称
191
+ const name = componentName || instance.type.name || instance.type.__name;
192
+ if (!name) {
193
+ throw new Error('Component name is required for useChildren. Either provide a name or set component name.');
194
+ }
195
+
196
+ const instanceId = generateInstanceId(name);
197
+ const parentRef = ref<ParentContext | null>(null);
198
+ const parentExposed = ref<Record<string, any>>({});
199
+
200
+ // 获取父组件暴露内容
201
+ const getParentExposed = (): Record<string, any> => {
202
+ if (parentRef.value) {
203
+ const exposed = parentRef.value.getExposed();
204
+ parentExposed.value = exposed;
205
+ return exposed;
206
+ }
207
+ return {};
208
+ };
209
+
210
+ // 获取子组件exposed内容
211
+ const getExposed = (): Record<string, any> => {
212
+ return instance.exposed || {};
213
+ };
214
+
215
+ // 查找父组件
216
+ const findParent = (): ParentContext | null => {
217
+ // 如果指定了父组件名称,使用精确查找
218
+ if (parentName) {
219
+ return getParentContext(parentName, instance);
220
+ }
221
+
222
+ // 否则查找最近的父组件上下文
223
+ let current = instance.parent;
224
+ while (current) {
225
+ const context = current.proxy?.[PARENT_CONTEXT_SYMBOL];
226
+ if (context) {
227
+ return context;
228
+ }
229
+ current = current.parent;
230
+ }
231
+ return null;
232
+ };
233
+
234
+ // 链接到父组件
235
+ const linkParent = (): boolean => {
236
+ const parent = findParent();
237
+ if (parent) {
238
+ parentRef.value = parent;
239
+ parent.addChild(childContext);
240
+ getParentExposed();
241
+ logger.log(`Child ${name} linked to parent ${parent.name}`);
242
+ return true;
243
+ }
244
+ return false;
245
+ };
246
+
247
+ // 向父组件发送事件
248
+ const emitToParent = (event: string, data?: any) => {
249
+ if (parentRef.value) {
250
+ const exposed = parentRef.value.getExposed();
251
+ if (exposed && typeof exposed[event] === 'function') {
252
+ try {
253
+ exposed[event](data, instanceId, name);
254
+ } catch (error) {
255
+ logger.warn(`Error calling parent method ${event}:`, error);
256
+ }
257
+ } else {
258
+ logger.warn(`Parent method ${event} not found or not a function`);
259
+ }
260
+ } else {
261
+ logger.warn(`No parent found to emit event: ${event}`);
262
+ }
263
+ };
264
+
265
+ // 子组件上下文
266
+ const childContext: ChildContext = {
267
+ id: instanceId,
268
+ name,
269
+ emitToParent,
270
+ getParentExposed,
271
+ getInstance() {
272
+ return instance;
273
+ },
274
+ getExposed
275
+ };
276
+
277
+ logger.log(`Child ${name} registered, looking for parent`);
278
+
279
+ onMounted(() => {
280
+ // 立即尝试连接父组件
281
+ let connected = linkParent();
282
+ nextTick(() => {
283
+ // 如果未连接成功,500ms后重试一次
284
+ if (!connected) {
285
+ setTimeout(() => {
286
+ linkParent();
287
+ }, 500);
288
+ }
289
+ });
290
+ });
291
+
292
+ // 组件卸载时清理
293
+ onUnmounted(() => {
294
+ if (parentRef.value) {
295
+ parentRef.value.removeChild(instanceId);
296
+ }
297
+ logger.log(`Child ${name} unmounted`);
298
+ });
299
+
300
+ return {
301
+ childId: instanceId,
302
+ childName: name,
303
+ parent: parentRef,
304
+ emitToParent,
305
+ getParentExposed,
306
+ parentExposed: computed(() => parentExposed.value),
307
+ getExposed
308
+ };
309
+ }
310
+
311
+ /**
312
+ * 检查父组件是否存在
313
+ */
314
+ export function hasParent(parentName?: string): boolean {
315
+ const instance = getCurrentInstance();
316
+ if (!instance) return false;
317
+
318
+ if (parentName) {
319
+ return getParentContext(parentName, instance) !== null;
320
+ }
321
+
322
+ // 查找最近的父组件上下文
323
+ let current = instance.parent;
324
+ while (current) {
325
+ const context = current.proxy?.[PARENT_CONTEXT_SYMBOL];
326
+ if (context) {
327
+ return true;
328
+ }
329
+ current = current.parent;
330
+ }
331
+ return false;
332
+ }
333
+
334
+ /**
335
+ * 获取父组件上下文
336
+ */
337
+ export function getParentContextByName(parentName: string): ParentContext | null {
338
+ const instance = getCurrentInstance();
339
+ if (!instance) return null;
340
+ return getParentContext(parentName, instance);
341
+ }
342
+
343
+ /**
344
+ * 热更新清理函数
345
+ */
346
+ export function cleanupComponentRelations(): void {
347
+ logger.log('Cleaning up component relations for hot reload');
348
+ // 由于使用组件实例存储,热更新时会自动重新建立关系
349
+ }
350
+
351
+ // 热更新处理
352
+ if (import.meta.hot) {
353
+ import.meta.hot.accept(() => {
354
+ logger.log('Hot reload detected, relations will be automatically reconnected');
355
+ });
356
+ }
357
+
358
+ export default {
359
+ useParent,
360
+ useChildren,
361
+ hasParent,
362
+ getParentContextByName,
363
+ cleanupComponentRelations
364
+ };
package/libs/index.ts CHANGED
@@ -165,49 +165,66 @@ export function addStyle(
165
165
  * @param {object | object[]} styles 外部传入的样式对象或数组
166
166
  * @returns {string} 格式化后的 CSS 样式字符串
167
167
  */
168
- export function toStyle(styles: Record<string, any> | Record<string, any>[] | string): string {
169
- // 如果 styles 是字符串类型
170
- if (test.string(styles)) {
171
- // 如果是字符串且不为空,确保末尾有分号
172
- return styles ? (styles.endsWith(';') ? styles : styles + ';') : '';
168
+ export function toStyle(...styles: Array<Record<string, any> | string | null | undefined>): string {
169
+ // 支持多参数:每个参数可以是 object 或 string,后面的参数优先级更高,会覆盖同名样式
170
+ // 如果传入单个数组(兼容旧调用),解构展开
171
+ if (styles.length === 1 && Array.isArray(styles[0])) {
172
+ styles = (styles[0] as any[]).slice();
173
173
  }
174
- // 如果 styles 是数组类型
175
- if (test.array(styles)) {
176
- // 使用过滤函数去除空值和 null 值的元素
177
- // 对每个非空元素递归调用 objToStyle,然后通过分号连接
178
- const result = styles
179
- .filter(function (item) {
180
- return item != null && item !== '';
181
- })
182
- .map(function (item) {
183
- return toStyle(item);
184
- })
185
- .join(';');
186
174
 
187
- // 如果结果不为空,确保末尾有分号
188
- return result ? (result.endsWith(';') ? result : result + ';') : '';
189
- }
190
- // 如果 styles 是对象类型
191
- if (test.object(styles)) {
192
- // 使用 Object.keys 获取所有属性名
193
- // 使用过滤函数去除值为 null 或空字符串的属性
194
- // 对每个属性名和属性值进行格式化,通过分号连接
195
- const result = Object.keys(styles)
196
- .filter(function (key) {
197
- return styles[key] != null && styles[key] !== '';
198
- })
199
- .map(function (key) {
200
- // 使用 kebabCase 函数将属性名转换为 kebab-case 格式
201
- // 将属性名和属性值格式化为 CSS 样式的键值对
202
- return [kebabCase(key), styles[key]].join(':');
203
- })
204
- .join(';');
175
+ // 用于合并样式的 Map,key 使用 kebab-case
176
+ const map = new Map<string, any>();
177
+
178
+ const processString = (str: string) => {
179
+ if (!str) return;
180
+ // 移除可能的末尾分号,再按分号分割
181
+ const parts = str.split(';');
182
+ for (let part of parts) {
183
+ part = part.trim();
184
+ if (!part) continue;
185
+ const idx = part.indexOf(':');
186
+ if (idx === -1) continue;
187
+ const key = trim(part.slice(0, idx));
188
+ const val = trim(part.slice(idx + 1));
189
+ if (key === '' || val === '') continue;
190
+ const k = kebabCase(key);
191
+ map.set(k, val);
192
+ }
193
+ };
194
+
195
+ const processObject = (obj: Record<string, any>) => {
196
+ if (!obj) return;
197
+ Object.keys(obj).forEach(key => {
198
+ const val = obj[key];
199
+ if (val == null || val === '') return;
200
+ const k = kebabCase(key);
201
+ map.set(k, val);
202
+ });
203
+ };
205
204
 
206
- // 如果结果不为空,确保末尾有分号
207
- return result ? (result.endsWith(';') ? result : result + ';') : '';
205
+ for (const item of styles) {
206
+ if (item == null || item === '') continue;
207
+ if (test.string(item)) {
208
+ processString(item as string);
209
+ } else if (test.array(item)) {
210
+ // 若传入数组作为参数,递归处理数组元素
211
+ (item as any[]).forEach(el => {
212
+ if (test.string(el)) processString(el as string);
213
+ else if (test.object(el)) processObject(el as Record<string, any>);
214
+ });
215
+ } else if (test.object(item)) {
216
+ processObject(item as Record<string, any>);
217
+ }
208
218
  }
209
- // 如果 styles 不是对象也不是数组,则直接返回
210
- return '';
219
+
220
+ if (map.size === 0) return '';
221
+
222
+ // 按插入顺序构造样式字符串,值转成字符串
223
+ const result = Array.from(map.entries())
224
+ .map(([k, v]) => `${k}:${String(v)}`)
225
+ .join(';');
226
+
227
+ return result ? (result.endsWith(';') ? result : result + ';') : '';
211
228
  }
212
229
 
213
230
  /**
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "id": "uview-pro",
3
3
  "name": "uview-pro",
4
4
  "displayName": "【Vue3重构版】uView Pro|基于Vue3+TS全面重构的70+精选UI组件库",
5
- "version": "0.2.4",
5
+ "version": "0.3.0",
6
6
  "description": "uView Pro,是全面支持Vue3的uni-app生态框架,70+精选组件已使用TypeScript重构,已全面支持uni-app Vue3.0",
7
7
  "main": "index.ts",
8
8
  "module": "index.ts",
package/readme.md CHANGED
@@ -38,10 +38,10 @@
38
38
 
39
39
  <table class="table">
40
40
  <tr>
41
- <td><img src="https://ik.imagekit.io/anyup/images/social/qr_uview_pro_wx.jpg" width="200" height="200" ></td>
42
- <td><img src="https://ik.imagekit.io/anyup/images/social/qr_uview_pro_alipay.png" width="200" height="200" ></td>
43
- <td><img src="https://ik.imagekit.io/anyup/images/social/qr_uview_pro_h5.png" width="200" height="200" ></td>
44
- <td><img src="https://ik.imagekit.io/anyup/images/social/qr_uview_pro_android.png" width="200" height="200" ></td>
41
+ <td><img src="https://ik.imagekit.io/anyup/images/social/qr_uview_pro_wx.jpg" width="150" height="150" ></td>
42
+ <td><img src="https://ik.imagekit.io/anyup/images/social/qr_uview_pro_alipay.png" width="150" height="150" ></td>
43
+ <td><img src="https://ik.imagekit.io/anyup/images/social/qr_uview_pro_h5.png" width="150" height="150" ></td>
44
+ <td><img src="https://ik.imagekit.io/anyup/images/social/qr_uview_pro_android.png" width="150" height="150" ></td>
45
45
  </tr>
46
46
  <tr>
47
47
  <td align="center"><strong>微信小程序</strong><br>(微信扫码)</td>
@@ -73,7 +73,7 @@ pnpm dev
73
73
 
74
74
  <table class="table">
75
75
  <tr>
76
- <td><img src="https://ik.imagekit.io/anyup/images/social/weixin-chat.png?updatedAt=1759853787761" width="250" height="345" ></td>
76
+ <td><img src="https://ik.imagekit.io/anyup/images/social/weixin-chat.png?updatedAt=1760233880982" width="250" height="345" ></td>
77
77
  <td><img src="https://ik.imagekit.io/anyup/images/social/qq-chat.png" width="250" height="345" ></td>
78
78
  </tr>
79
79
  <tr>
@@ -212,7 +212,7 @@ export function createApp() {
212
212
 
213
213
  <table class="table">
214
214
  <tr>
215
- <td><img src="https://ik.imagekit.io/anyup/images/social/weixin-pay.png" width="250" height="345" ></td>
215
+ <td><img src="https://ik.imagekit.io/anyup/images/social/wechat-pay.png" width="250" height="345" ></td>
216
216
  <td><img src="https://ik.imagekit.io/anyup/images/social/ali-pay.png" width="250" height="345" ></td>
217
217
  </tr>
218
218
  <tr>