uview-pro 0.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.
Files changed (149) hide show
  1. package/changelog.md +6 -0
  2. package/components/u-action-sheet/u-action-sheet.vue +205 -0
  3. package/components/u-alert-tips/u-alert-tips.vue +241 -0
  4. package/components/u-avatar/u-avatar.vue +220 -0
  5. package/components/u-avatar-cropper/u-avatar-cropper.vue +329 -0
  6. package/components/u-avatar-cropper/weCropper.d.ts +54 -0
  7. package/components/u-avatar-cropper/weCropper.js +1267 -0
  8. package/components/u-avatar-cropper/weCropper.ts +1254 -0
  9. package/components/u-back-top/u-back-top.vue +156 -0
  10. package/components/u-badge/u-badge.vue +189 -0
  11. package/components/u-button/u-button.vue +562 -0
  12. package/components/u-calendar/u-calendar.vue +725 -0
  13. package/components/u-car-keyboard/u-car-keyboard.vue +236 -0
  14. package/components/u-card/u-card.vue +240 -0
  15. package/components/u-cell-group/u-cell-group.vue +56 -0
  16. package/components/u-cell-item/u-cell-item.vue +245 -0
  17. package/components/u-checkbox/u-checkbox.vue +310 -0
  18. package/components/u-checkbox-group/u-checkbox-group.vue +134 -0
  19. package/components/u-circle-progress/u-circle-progress.vue +210 -0
  20. package/components/u-col/u-col.vue +135 -0
  21. package/components/u-collapse/u-collapse.vue +82 -0
  22. package/components/u-collapse-item/u-collapse-item.vue +190 -0
  23. package/components/u-column-notice/u-column-notice.vue +264 -0
  24. package/components/u-count-down/u-count-down.vue +333 -0
  25. package/components/u-count-to/u-count-to.vue +297 -0
  26. package/components/u-divider/u-divider.vue +141 -0
  27. package/components/u-dropdown/u-dropdown.vue +311 -0
  28. package/components/u-dropdown-item/u-dropdown-item.vue +135 -0
  29. package/components/u-empty/u-empty.vue +111 -0
  30. package/components/u-field/u-field.vue +469 -0
  31. package/components/u-form/u-form.vue +162 -0
  32. package/components/u-form-item/u-form-item.vue +476 -0
  33. package/components/u-full-screen/u-full-screen.vue +80 -0
  34. package/components/u-gap/u-gap.vue +48 -0
  35. package/components/u-grid/u-grid.vue +101 -0
  36. package/components/u-grid-item/u-grid-item.vue +136 -0
  37. package/components/u-icon/u-icon.vue +389 -0
  38. package/components/u-image/types.ts +48 -0
  39. package/components/u-image/u-image.vue +218 -0
  40. package/components/u-index-anchor/u-index-anchor.vue +101 -0
  41. package/components/u-index-list/u-index-list.vue +376 -0
  42. package/components/u-input/u-input.vue +462 -0
  43. package/components/u-keyboard/u-keyboard.vue +188 -0
  44. package/components/u-lazy-load/u-lazy-load.vue +288 -0
  45. package/components/u-line/u-line.vue +71 -0
  46. package/components/u-line-progress/u-line-progress.vue +128 -0
  47. package/components/u-link/u-link.vue +87 -0
  48. package/components/u-loading/u-loading.vue +111 -0
  49. package/components/u-loadmore/u-loadmore.vue +205 -0
  50. package/components/u-mask/u-mask.vue +137 -0
  51. package/components/u-message-input/u-message-input.vue +315 -0
  52. package/components/u-modal/u-modal.vue +284 -0
  53. package/components/u-navbar/u-navbar.vue +314 -0
  54. package/components/u-no-network/image.ts +2 -0
  55. package/components/u-no-network/u-no-network.vue +311 -0
  56. package/components/u-notice-bar/u-notice-bar.vue +274 -0
  57. package/components/u-number-box/u-number-box.vue +344 -0
  58. package/components/u-number-keyboard/u-number-keyboard.vue +170 -0
  59. package/components/u-parse/libs/CssHandler.js +100 -0
  60. package/components/u-parse/libs/MpHtmlParser.js +580 -0
  61. package/components/u-parse/libs/config.js +80 -0
  62. package/components/u-parse/libs/handler.wxs +22 -0
  63. package/components/u-parse/libs/trees.vue +505 -0
  64. package/components/u-parse/u-parse.vue +645 -0
  65. package/components/u-picker/u-picker.vue +808 -0
  66. package/components/u-popup/u-popup.vue +404 -0
  67. package/components/u-radio/u-radio.vue +272 -0
  68. package/components/u-radio-group/u-radio-group.vue +116 -0
  69. package/components/u-rate/u-rate.vue +349 -0
  70. package/components/u-read-more/u-read-more.vue +199 -0
  71. package/components/u-row/u-row.vue +95 -0
  72. package/components/u-row-notice/u-row-notice.vue +273 -0
  73. package/components/u-search/u-search.vue +298 -0
  74. package/components/u-section/u-section.vue +175 -0
  75. package/components/u-select/u-select.vue +387 -0
  76. package/components/u-skeleton/u-skeleton.vue +230 -0
  77. package/components/u-slider/u-slider.vue +293 -0
  78. package/components/u-steps/u-steps.vue +200 -0
  79. package/components/u-sticky/u-sticky.vue +189 -0
  80. package/components/u-subsection/u-subsection.vue +388 -0
  81. package/components/u-swipe-action/u-swipe-action.vue +289 -0
  82. package/components/u-swiper/u-swiper.vue +305 -0
  83. package/components/u-switch/u-switch.vue +146 -0
  84. package/components/u-tabbar/u-tabbar.vue +347 -0
  85. package/components/u-table/u-table.vue +104 -0
  86. package/components/u-tabs/u-tabs.vue +322 -0
  87. package/components/u-tabs-swiper/u-tabs-swiper.vue +426 -0
  88. package/components/u-tag/u-tag.vue +270 -0
  89. package/components/u-td/u-td.vue +76 -0
  90. package/components/u-th/u-th.vue +70 -0
  91. package/components/u-time-line/u-time-line.vue +39 -0
  92. package/components/u-time-line-item/u-time-line-item.vue +88 -0
  93. package/components/u-toast/types.ts +4 -0
  94. package/components/u-toast/u-toast.vue +238 -0
  95. package/components/u-top-tips/u-top-tips.vue +118 -0
  96. package/components/u-tr/u-tr.vue +24 -0
  97. package/components/u-upload/u-upload.vue +600 -0
  98. package/components/u-verification-code/u-verification-code.vue +194 -0
  99. package/components/u-waterfall/u-waterfall.vue +186 -0
  100. package/iconfont.css +910 -0
  101. package/index.scss +23 -0
  102. package/index.ts +166 -0
  103. package/libs/config/config.ts +26 -0
  104. package/libs/config/zIndex.ts +37 -0
  105. package/libs/css/color.scss +155 -0
  106. package/libs/css/common.scss +176 -0
  107. package/libs/css/style.components.scss +7 -0
  108. package/libs/css/style.h5.scss +8 -0
  109. package/libs/css/style.mp.scss +72 -0
  110. package/libs/css/style.nvue.scss +3 -0
  111. package/libs/css/style.vue.scss +175 -0
  112. package/libs/function/$parent.ts +22 -0
  113. package/libs/function/addUnit.ts +13 -0
  114. package/libs/function/color.ts +37 -0
  115. package/libs/function/colorGradient.ts +123 -0
  116. package/libs/function/debounce.ts +28 -0
  117. package/libs/function/deepClone.ts +39 -0
  118. package/libs/function/deepMerge.ts +34 -0
  119. package/libs/function/getParent.ts +59 -0
  120. package/libs/function/getRect.ts +26 -0
  121. package/libs/function/guid.ts +42 -0
  122. package/libs/function/md5.ts +397 -0
  123. package/libs/function/parent.ts +21 -0
  124. package/libs/function/queryParams.ts +60 -0
  125. package/libs/function/random.ts +16 -0
  126. package/libs/function/randomArray.ts +11 -0
  127. package/libs/function/route.ts +118 -0
  128. package/libs/function/sys.ts +15 -0
  129. package/libs/function/test.ts +229 -0
  130. package/libs/function/throttle.ts +31 -0
  131. package/libs/function/timeFormat.ts +54 -0
  132. package/libs/function/timeFrom.ts +48 -0
  133. package/libs/function/toast.ts +14 -0
  134. package/libs/function/trim.ts +21 -0
  135. package/libs/function/type2icon.ts +36 -0
  136. package/libs/hooks/useEmitter.ts +77 -0
  137. package/libs/hooks/useParent.ts +29 -0
  138. package/libs/request/index.ts +237 -0
  139. package/libs/store/index.ts +88 -0
  140. package/libs/util/area.ts +1 -0
  141. package/libs/util/async-validator.js +1356 -0
  142. package/libs/util/city.ts +1 -0
  143. package/libs/util/emitter.ts +112 -0
  144. package/libs/util/mitt.ts +118 -0
  145. package/libs/util/parent.ts +20 -0
  146. package/libs/util/province.ts +1 -0
  147. package/package.json +98 -0
  148. package/readme.md +165 -0
  149. package/theme.scss +38 -0
@@ -0,0 +1,229 @@
1
+ /**
2
+ * 验证电子邮箱格式
3
+ */
4
+ function email(value: string): boolean {
5
+ return /[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/.test(value);
6
+ }
7
+
8
+ /**
9
+ * 验证手机格式
10
+ */
11
+ function mobile(value: string): boolean {
12
+ return /^1[3-9]\d{9}$/.test(value);
13
+ }
14
+
15
+ /**
16
+ * 验证URL格式
17
+ */
18
+ function url(value: string): boolean {
19
+ return /http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w-.\/?%&=]*)?/.test(value);
20
+ }
21
+
22
+ /**
23
+ * 验证日期格式
24
+ */
25
+ function date(value: string): boolean {
26
+ return !/Invalid|NaN/.test(new Date(value).toString());
27
+ }
28
+
29
+ /**
30
+ * 验证ISO类型的日期格式
31
+ */
32
+ function dateISO(value: string): boolean {
33
+ return /^\d{4}[\/\-](0?[1-9]|1[012])[\/\-](0?[1-9]|[12][0-9]|3[01])$/.test(value);
34
+ }
35
+
36
+ /**
37
+ * 验证十进制数字
38
+ */
39
+ function number(value: string): boolean {
40
+ return /^[\+-]?(\d+\.?\d*|\.\d+|\d\.\d+e\+\d+)$/.test(value);
41
+ }
42
+
43
+ /**
44
+ * 验证整数
45
+ */
46
+ function digits(value: string): boolean {
47
+ return /^\d+$/.test(value);
48
+ }
49
+
50
+ /**
51
+ * 验证身份证号码
52
+ */
53
+ function idCard(value: string): boolean {
54
+ return /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/.test(value);
55
+ }
56
+
57
+ /**
58
+ * 是否车牌号
59
+ */
60
+ function carNo(value: string): boolean {
61
+ // 新能源车牌
62
+ const xreg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([0-9]{5}[DF]$)|([DF][A-HJ-NP-Z0-9][0-9]{4}$))/;
63
+ // 旧车牌
64
+ const creg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1}$/;
65
+ if (value.length === 7) {
66
+ return creg.test(value);
67
+ } else if (value.length === 8) {
68
+ return xreg.test(value);
69
+ } else {
70
+ return false;
71
+ }
72
+ }
73
+
74
+ /**
75
+ * 金额,只允许2位小数
76
+ */
77
+ function amount(value: string): boolean {
78
+ //金额,只允许保留两位小数
79
+ return /^[1-9]\d*(,\d{3})*(\.\d{1,2})?$|^0\.\d{1,2}$/.test(value);
80
+ }
81
+
82
+ /**
83
+ * 中文
84
+ */
85
+ function chinese(value: string): boolean {
86
+ let reg = /^[\u4e00-\u9fa5]+$/gi;
87
+ return reg.test(value);
88
+ }
89
+
90
+ /**
91
+ * 只能输入字母
92
+ */
93
+ function letter(value: string): boolean {
94
+ return /^[a-zA-Z]*$/.test(value);
95
+ }
96
+
97
+ /**
98
+ * 只能是字母或者数字
99
+ */
100
+ function enOrNum(value: string): boolean {
101
+ //英文或者数字
102
+ let reg = /^[0-9a-zA-Z]*$/g;
103
+ return reg.test(value);
104
+ }
105
+
106
+ /**
107
+ * 验证是否包含某个值
108
+ */
109
+ function contains(value: string, param: string): boolean {
110
+ return value.indexOf(param) >= 0;
111
+ }
112
+
113
+ /**
114
+ * 验证一个值范围[min, max]
115
+ */
116
+ function range(value: number, param: [number, number]): boolean {
117
+ return value >= param[0] && value <= param[1];
118
+ }
119
+
120
+ /**
121
+ * 验证一个长度范围[min, max]
122
+ */
123
+ function rangeLength(value: string, param: [number, number]): boolean {
124
+ return value.length >= param[0] && value.length <= param[1];
125
+ }
126
+
127
+ /**
128
+ * 是否固定电话
129
+ */
130
+ function landline(value: string): boolean {
131
+ let reg = /^\d{3,4}-\d{7,8}(-\d{3,4})?$/;
132
+ return reg.test(value);
133
+ }
134
+
135
+ /**
136
+ * 判断是否为空
137
+ */
138
+ function empty(value: any): boolean {
139
+ switch (typeof value) {
140
+ case 'undefined':
141
+ return true;
142
+ case 'string':
143
+ if (value.replace(/(^[ \t\n\r]*)|([ \t\n\r]*$)/g, '').length == 0) return true;
144
+ break;
145
+ case 'boolean':
146
+ if (!value) return true;
147
+ break;
148
+ case 'number':
149
+ if (0 === value || isNaN(value)) return true;
150
+ break;
151
+ case 'object':
152
+ if (null === value || value.length === 0) return true;
153
+ for (var i in value) {
154
+ return false;
155
+ }
156
+ return true;
157
+ }
158
+ return false;
159
+ }
160
+
161
+ /**
162
+ * 是否json字符串
163
+ */
164
+ function jsonString(value: string): boolean {
165
+ if (typeof value == 'string') {
166
+ try {
167
+ var obj = JSON.parse(value);
168
+ if (typeof obj == 'object' && obj) {
169
+ return true;
170
+ } else {
171
+ return false;
172
+ }
173
+ } catch (e) {
174
+ return false;
175
+ }
176
+ }
177
+ return false;
178
+ }
179
+
180
+ /**
181
+ * 是否数组
182
+ */
183
+ function array(value: any): boolean {
184
+ if (typeof Array.isArray === 'function') {
185
+ return Array.isArray(value);
186
+ } else {
187
+ return Object.prototype.toString.call(value) === '[object Array]';
188
+ }
189
+ }
190
+
191
+ /**
192
+ * 是否对象
193
+ */
194
+ function object(value: any): boolean {
195
+ return Object.prototype.toString.call(value) === '[object Object]';
196
+ }
197
+
198
+ /**
199
+ * 是否短信验证码
200
+ */
201
+ function code(value: string, len: number = 6): boolean {
202
+ return new RegExp(`^\\d{${len}}$`).test(value);
203
+ }
204
+
205
+ export default {
206
+ email,
207
+ mobile,
208
+ url,
209
+ date,
210
+ dateISO,
211
+ number,
212
+ digits,
213
+ idCard,
214
+ carNo,
215
+ amount,
216
+ chinese,
217
+ letter,
218
+ enOrNum,
219
+ contains,
220
+ range,
221
+ rangeLength,
222
+ empty,
223
+ isEmpty: empty,
224
+ jsonString,
225
+ landline,
226
+ object,
227
+ array,
228
+ code
229
+ };
@@ -0,0 +1,31 @@
1
+ let timer: ReturnType<typeof setTimeout> | undefined;
2
+ let flag: boolean | undefined;
3
+ /**
4
+ * 节流原理:在一定时间内,只能触发一次
5
+ * @param func 要执行的回调函数
6
+ * @param wait 延时的时间,单位ms,默认500
7
+ * @param immediate 是否立即执行,默认true
8
+ * @returns void
9
+ */
10
+ function throttle(func: () => void, wait: number = 500, immediate: boolean = true): void {
11
+ if (immediate) {
12
+ if (!flag) {
13
+ flag = true;
14
+ // 如果是立即执行,则在wait毫秒内开始时执行
15
+ typeof func === 'function' && func();
16
+ timer = setTimeout(() => {
17
+ flag = false;
18
+ }, wait);
19
+ }
20
+ } else {
21
+ if (!flag) {
22
+ flag = true;
23
+ // 如果是非立即执行,则在wait毫秒内的结束处执行
24
+ timer = setTimeout(() => {
25
+ flag = false;
26
+ typeof func === 'function' && func();
27
+ }, wait);
28
+ }
29
+ }
30
+ }
31
+ export default throttle;
@@ -0,0 +1,54 @@
1
+ // padStart 的 polyfill,因为某些机型或情况,还无法支持es7的padStart,比如电脑版的微信小程序
2
+ // 所以这里做一个兼容polyfill的兼容处理
3
+ if (!String.prototype.padStart) {
4
+ // 为了方便表示这里 fillString 用了ES6 的默认参数,不影响理解
5
+ String.prototype.padStart = function (this: string, maxLength: number, fillString: string = ' '): string {
6
+ if (Object.prototype.toString.call(fillString) !== '[object String]') throw new TypeError('fillString must be String');
7
+ let str = this;
8
+ if (str.length >= maxLength) return String(str);
9
+ let fillLength = maxLength - str.length,
10
+ times = Math.ceil(fillLength / fillString.length);
11
+ while ((times >>= 1)) {
12
+ fillString += fillString;
13
+ if (times === 1) {
14
+ fillString += fillString;
15
+ }
16
+ }
17
+ return fillString.slice(0, fillLength) + str;
18
+ };
19
+ }
20
+
21
+ /**
22
+ * 时间格式化
23
+ * @param dateTime 时间戳、Date对象或null,默认当前时间
24
+ * @param fmt 格式化字符串,默认 'yyyy-mm-dd'
25
+ * @returns 格式化后的时间字符串
26
+ */
27
+ function timeFormat(dateTime: number | string | Date | null = null, fmt: string = 'yyyy-mm-dd'): string {
28
+ // 如果为null,则格式化当前时间
29
+ if (!dateTime) dateTime = Number(new Date());
30
+ // 如果dateTime长度为10或者13,则为秒和毫秒的时间戳,如果超过13位,则为其他的时间格式
31
+ if (typeof dateTime === 'number' || typeof dateTime === 'string') {
32
+ if (dateTime.toString().length == 10) dateTime = Number(dateTime) * 1000;
33
+ }
34
+ const date = new Date(dateTime);
35
+ let ret: RegExpExecArray | null;
36
+ const opt: Record<string, string> = {
37
+ 'y+': date.getFullYear().toString(), // 年
38
+ 'm+': (date.getMonth() + 1).toString(), // 月
39
+ 'd+': date.getDate().toString(), // 日
40
+ 'h+': date.getHours().toString(), // 时
41
+ 'M+': date.getMinutes().toString(), // 分
42
+ 's+': date.getSeconds().toString() // 秒
43
+ // 有其他格式化字符需求可以继续添加,必须转化成字符串
44
+ };
45
+ for (const k in opt) {
46
+ ret = new RegExp('(' + k + ')').exec(fmt);
47
+ if (ret) {
48
+ fmt = fmt.replace(ret[1], ret[1].length == 1 ? opt[k] : opt[k].padStart(ret[1].length, '0'));
49
+ }
50
+ }
51
+ return fmt;
52
+ }
53
+
54
+ export default timeFormat;
@@ -0,0 +1,48 @@
1
+ import timeFormat from './timeFormat';
2
+
3
+ /**
4
+ * 时间戳转为多久之前
5
+ * @param dateTime 时间戳、Date对象或null,默认当前时间
6
+ * @param format 时间格式字符串或false,超出范围时返回指定格式,否则返回多久以前
7
+ * @returns 格式化后的时间字符串
8
+ */
9
+ function timeFrom(dateTime: number | string | Date | null = null, format: string | false = 'yyyy-mm-dd'): string {
10
+ // 如果为null,则格式化当前时间
11
+ if (!dateTime) dateTime = Number(new Date());
12
+ // 如果dateTime长度为10或者13,则为秒和毫秒的时间戳,如果超过13位,则为其他的时间格式
13
+ if (typeof dateTime === 'number' || typeof dateTime === 'string') {
14
+ if (dateTime.toString().length == 10) dateTime = Number(dateTime) * 1000;
15
+ }
16
+ const timestamp = +new Date(Number(dateTime));
17
+ const timer = (Number(new Date()) - timestamp) / 1000;
18
+ // 如果小于5分钟,则返回"刚刚",其他以此类推
19
+ let tips = '';
20
+ switch (true) {
21
+ case timer < 300:
22
+ tips = '刚刚';
23
+ break;
24
+ case timer >= 300 && timer < 3600:
25
+ tips = parseInt(String(timer / 60)) + '分钟前';
26
+ break;
27
+ case timer >= 3600 && timer < 86400:
28
+ tips = parseInt(String(timer / 3600)) + '小时前';
29
+ break;
30
+ case timer >= 86400 && timer < 2592000:
31
+ tips = parseInt(String(timer / 86400)) + '天前';
32
+ break;
33
+ default:
34
+ // 如果format为false,则无论什么时间戳,都显示xx之前
35
+ if (format === false) {
36
+ if (timer >= 2592000 && timer < 365 * 86400) {
37
+ tips = parseInt(String(timer / (86400 * 30))) + '个月前';
38
+ } else {
39
+ tips = parseInt(String(timer / (86400 * 365))) + '年前';
40
+ }
41
+ } else {
42
+ tips = timeFormat(timestamp, format as string);
43
+ }
44
+ }
45
+ return tips;
46
+ }
47
+
48
+ export default timeFrom;
@@ -0,0 +1,14 @@
1
+ /**
2
+ * 显示无图标的 Toast 提示
3
+ * @param title 提示文本
4
+ * @param duration 显示时长(毫秒),默认1500
5
+ */
6
+ function toast(title: string, duration: number = 1500): void {
7
+ uni.showToast({
8
+ title: title,
9
+ icon: 'none',
10
+ duration: duration
11
+ });
12
+ }
13
+
14
+ export default toast;
@@ -0,0 +1,21 @@
1
+ /**
2
+ * 去除字符串空格
3
+ * @param str 输入字符串
4
+ * @param pos 去除位置,'both' | 'left' | 'right' | 'all',默认'both'
5
+ * @returns 去除空格后的字符串
6
+ */
7
+ function trim(str: string, pos: 'both' | 'left' | 'right' | 'all' = 'both'): string {
8
+ if (pos === 'both') {
9
+ return str.replace(/^\s+|\s+$/g, '');
10
+ } else if (pos === 'left') {
11
+ return str.replace(/^\s*/, '');
12
+ } else if (pos === 'right') {
13
+ return str.replace(/(\s*$)/g, '');
14
+ } else if (pos === 'all') {
15
+ return str.replace(/\s+/g, '');
16
+ } else {
17
+ return str;
18
+ }
19
+ }
20
+
21
+ export default trim;
@@ -0,0 +1,36 @@
1
+ /**
2
+ * 根据主题type值,获取对应的图标
3
+ * @param type 主题名称, primary|info|error|warning|success,默认success
4
+ * @param fill 是否使用fill填充实体的图标,默认false
5
+ * @returns 图标名称字符串
6
+ */
7
+ function type2icon(type: 'primary' | 'info' | 'error' | 'warning' | 'success' = 'success', fill: boolean = false): string {
8
+ // 如果非预置值,默认为success
9
+ if (!['primary', 'info', 'error', 'warning', 'success'].includes(type)) type = 'success';
10
+ let iconName = '';
11
+ // 目前(2019-12-12),info和primary使用同一个图标
12
+ switch (type) {
13
+ case 'primary':
14
+ iconName = 'info-circle';
15
+ break;
16
+ case 'info':
17
+ iconName = 'info-circle';
18
+ break;
19
+ case 'error':
20
+ iconName = 'close-circle';
21
+ break;
22
+ case 'warning':
23
+ iconName = 'error-circle';
24
+ break;
25
+ case 'success':
26
+ iconName = 'checkmark-circle';
27
+ break;
28
+ default:
29
+ iconName = 'checkmark-circle';
30
+ }
31
+ // 是否是实体类型,加上-fill,在icon组件库中,实体的类名是后面加-fill的
32
+ if (fill) iconName += '-fill';
33
+ return iconName;
34
+ }
35
+
36
+ export default type2icon;
@@ -0,0 +1,77 @@
1
+ import { type ComponentInternalInstance, getCurrentInstance } from 'vue';
2
+
3
+ /**
4
+ * 将事件名转换为驼峰格式
5
+ * @param str 需要转换的字符串
6
+ * @description 例如:on-form-change -> onFormChange
7
+ * @returns
8
+ */
9
+ function formatToCamelCase(str: string): string {
10
+ return str.replace(/-([a-z])/g, function (g) {
11
+ return g[1].toUpperCase();
12
+ });
13
+ }
14
+
15
+ export function useEmitter(name: string) {
16
+ const instance: ComponentInternalInstance | null | undefined = getCurrentInstance();
17
+
18
+ /** * 向上查找父组件并派发事件
19
+ * @param instance 当前组件实例(setup中可用getCurrentInstance())
20
+ * @param componentName 目标组件名
21
+ * @param eventName 事件名
22
+ * @param params 参数
23
+ */
24
+ function dispatch(componentName: string, eventName: string, ...params: any[]) {
25
+ let parent = instance && (instance.parent as ComponentInternalInstance | null | undefined);
26
+ while (parent) {
27
+ const name = (parent.type as any)?.name as string | undefined;
28
+ if (name === componentName) {
29
+ // 找到目标组件,派发事件
30
+ // Vue3未解决,目标组件事件监听失效,待优化,暂时使用下面的方式解决,如果你有好的方式也可以告诉我或者提PR
31
+ parent.emit && parent.emit(eventName, ...params);
32
+ // 如果有对应的方法,执行方法
33
+ // 这里可以考虑将 eventName 转换为驼峰格式
34
+ // 例如:on-form-change -> onFormChange
35
+ parent.exposed?.[formatToCamelCase(eventName)] && parent.exposed[formatToCamelCase(eventName)](...params);
36
+ break;
37
+ }
38
+ parent = parent.parent;
39
+ }
40
+ }
41
+
42
+ /**
43
+ * 向下递归查找子组件并广播事件
44
+ * @param instance 当前组件实例(setup中可用getCurrentInstance())
45
+ * @param componentName 目标组件名
46
+ * @param eventName 事件名
47
+ * @param params 参数
48
+ */
49
+ function broadcast(componentName: string, eventName: string, ...params: any[]) {
50
+ if (!instance) return;
51
+ const subTree = (instance.subTree as any)?.children || [];
52
+ const children = Array.isArray(subTree) ? subTree : [subTree];
53
+ children.forEach((vnode: any) => {
54
+ const child = vnode.component as ComponentInternalInstance | undefined;
55
+
56
+ if (child) {
57
+ const name = (child.type as any)?.name as string | undefined;
58
+ if (name === componentName) {
59
+ // 找到目标组件,广播事件
60
+ // Vue3未解决,目标组件事件监听失效,待优化,暂时使用下面的方式解决,如果你有好的方式也可以告诉我或者提PR
61
+ child.emit && child.emit(eventName, ...params);
62
+ // 如果有对应的方法,执行方法
63
+ // 这里可以考虑将 eventName 转换为驼峰格式
64
+ // 例如:on-form-change -> onFormChange
65
+ child.exposed?.[formatToCamelCase(eventName)] && child.exposed[formatToCamelCase(eventName)](...params);
66
+ } else {
67
+ broadcast.call(child, componentName, eventName, ...params);
68
+ }
69
+ }
70
+ });
71
+ }
72
+
73
+ return {
74
+ dispatch,
75
+ broadcast
76
+ };
77
+ }
@@ -0,0 +1,29 @@
1
+ import { type ComponentInternalInstance, getCurrentInstance } from 'vue'
2
+
3
+ export function useParent(name: string) {
4
+ const instance: ComponentInternalInstance | null | undefined = getCurrentInstance()
5
+
6
+ function getParent(componentName?: string) {
7
+ componentName = componentName || name
8
+ let parent = instance && (instance.parent as ComponentInternalInstance | null | undefined)
9
+
10
+ while (parent) {
11
+ const name = (parent.type as any)?.name as string | undefined
12
+ if (name === componentName) {
13
+ return parent
14
+ }
15
+ parent = parent.parent
16
+ }
17
+ return null
18
+ }
19
+ function getParentData(componentName?: string) {
20
+ componentName = componentName || name
21
+ const parent = getParent(componentName)
22
+ return parent ? parent.exposed : null
23
+ }
24
+
25
+ return {
26
+ getParent,
27
+ getParentData
28
+ }
29
+ }