hy-app 0.5.9 → 0.5.11

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 (290) hide show
  1. package/components/hy-action-sheet/hy-action-sheet.vue +200 -200
  2. package/components/hy-action-sheet/props.ts +71 -71
  3. package/components/hy-action-sheet/typing.d.ts +64 -64
  4. package/components/hy-address-picker/hy-address-picker.vue +1 -1
  5. package/components/hy-address-picker/props.ts +100 -100
  6. package/components/hy-address-picker/typing.d.ts +16 -16
  7. package/components/hy-avatar/hy-avatar.vue +163 -163
  8. package/components/hy-avatar/props.ts +78 -78
  9. package/components/hy-avatar/typing.d.ts +4 -4
  10. package/components/hy-back-top/hy-back-top.vue +90 -90
  11. package/components/hy-back-top/props.ts +60 -60
  12. package/components/hy-back-top/typing.d.ts +4 -4
  13. package/components/hy-badge/hy-badge.vue +97 -97
  14. package/components/hy-badge/props.ts +82 -82
  15. package/components/hy-badge/typing.d.ts +9 -9
  16. package/components/hy-button/hy-button.vue +275 -275
  17. package/components/hy-button/props.ts +135 -135
  18. package/components/hy-button/typing.d.ts +30 -30
  19. package/components/hy-calendar/header.vue +60 -60
  20. package/components/hy-calendar/hy-calendar.vue +362 -362
  21. package/components/hy-calendar/month.vue +537 -537
  22. package/components/hy-calendar/props.ts +159 -159
  23. package/components/hy-calendar/typing.d.ts +6 -6
  24. package/components/hy-card/hy-card.vue +161 -161
  25. package/components/hy-card/props.ts +122 -122
  26. package/components/hy-card/typing.d.ts +12 -12
  27. package/components/hy-cell/hy-cell.vue +33 -33
  28. package/components/hy-cell/props.ts +54 -54
  29. package/components/hy-cell/typing.d.ts +4 -4
  30. package/components/hy-cell-item/hy-cell-item.vue +161 -161
  31. package/components/hy-cell-item/props.ts +66 -66
  32. package/components/hy-cell-item/typing.d.ts +7 -7
  33. package/components/hy-check-button/hy-check-button.vue +96 -96
  34. package/components/hy-check-button/props.ts +74 -74
  35. package/components/hy-check-button/typing.d.ts +44 -44
  36. package/components/hy-checkbox/hy-checkbox.vue +227 -227
  37. package/components/hy-checkbox/props.ts +96 -96
  38. package/components/hy-checkbox/typing.d.ts +8 -8
  39. package/components/hy-checkbox-group/hy-checkbox-group.vue +45 -45
  40. package/components/hy-checkbox-group/props.ts +80 -80
  41. package/components/hy-checkbox-group/typing.d.ts +6 -6
  42. package/components/hy-checkbox-item/hy-checkbox-item.vue +199 -199
  43. package/components/hy-checkbox-item/props.ts +24 -24
  44. package/components/hy-checkbox-item/typing.d.ts +7 -7
  45. package/components/hy-code-input/hy-code-input.vue +231 -231
  46. package/components/hy-code-input/props.ts +88 -88
  47. package/components/hy-code-input/typing.d.ts +8 -8
  48. package/components/hy-config-provider/hy-config-provider.vue +53 -53
  49. package/components/hy-config-provider/props.ts +28 -28
  50. package/components/hy-count-down/hy-count-down.vue +170 -170
  51. package/components/hy-count-down/index.ts +52 -52
  52. package/components/hy-count-down/props.ts +32 -32
  53. package/components/hy-count-down/typing.d.ts +14 -14
  54. package/components/hy-count-to/hy-count-to.vue +218 -218
  55. package/components/hy-count-to/props.ts +62 -62
  56. package/components/hy-count-to/typing.d.ts +4 -4
  57. package/components/hy-coupon/hy-coupon.vue +172 -172
  58. package/components/hy-coupon/index.scss +171 -171
  59. package/components/hy-coupon/props.ts +103 -103
  60. package/components/hy-coupon/typing.d.ts +14 -14
  61. package/components/hy-datetime-picker/hy-datetime-picker.vue +521 -521
  62. package/components/hy-datetime-picker/props.ts +142 -142
  63. package/components/hy-datetime-picker/typing.d.ts +20 -20
  64. package/components/hy-divider/hy-divider.vue +132 -132
  65. package/components/hy-divider/props.ts +80 -80
  66. package/components/hy-dropdown/hy-dropdown.vue +60 -60
  67. package/components/hy-dropdown/props.ts +40 -40
  68. package/components/hy-dropdown-item/hy-dropdown-item.vue +206 -206
  69. package/components/hy-dropdown-item/props.ts +21 -21
  70. package/components/hy-dropdown-item/typing.d.ts +17 -17
  71. package/components/hy-empty/hy-empty.vue +116 -116
  72. package/components/hy-empty/icon.ts +72 -72
  73. package/components/hy-empty/props.ts +60 -60
  74. package/components/hy-empty/typing.d.ts +38 -38
  75. package/components/hy-flex/hy-flex.vue +53 -53
  76. package/components/hy-flex/index.scss +8 -8
  77. package/components/hy-flex/props.ts +58 -58
  78. package/components/hy-flex/typing.d.ts +21 -21
  79. package/components/hy-float-button/hy-float-button.vue +378 -378
  80. package/components/hy-float-button/props.ts +111 -111
  81. package/components/hy-float-button/typing.d.ts +35 -35
  82. package/components/hy-folding-panel/hy-folding-panel.vue +109 -109
  83. package/components/hy-folding-panel/props.ts +42 -42
  84. package/components/hy-folding-panel/typing.d.ts +19 -19
  85. package/components/hy-folding-panel-item/hy-folding-panel-item.vue +183 -183
  86. package/components/hy-folding-panel-item/props.ts +81 -81
  87. package/components/hy-folding-panel-item/typing.d.ts +37 -37
  88. package/components/hy-form/hy-form.vue +220 -220
  89. package/components/hy-form/props.ts +37 -37
  90. package/components/hy-form/typing.d.ts +41 -41
  91. package/components/hy-form-group/hy-form-group.vue +333 -333
  92. package/components/hy-form-group/props.ts +105 -105
  93. package/components/hy-form-item/hy-form-item.vue +176 -176
  94. package/components/hy-form-item/index.scss +0 -1
  95. package/components/hy-form-item/props.ts +25 -25
  96. package/components/hy-form-item/typing.d.ts +30 -30
  97. package/components/hy-grid/hy-grid.vue +109 -109
  98. package/components/hy-grid/props.ts +60 -60
  99. package/components/hy-grid/typing.d.ts +35 -35
  100. package/components/hy-icon/hy-icon.vue +112 -112
  101. package/components/hy-icon/index.scss +0 -3
  102. package/components/hy-icon/props.ts +79 -79
  103. package/components/hy-icon/typing.d.ts +9 -9
  104. package/components/hy-image/hy-image.vue +192 -192
  105. package/components/hy-image/props.ts +107 -107
  106. package/components/hy-image/typing.d.ts +10 -10
  107. package/components/hy-input/hy-input.vue +333 -333
  108. package/components/hy-input/index.scss +5 -0
  109. package/components/hy-input/props.ts +186 -186
  110. package/components/hy-input/typing.d.ts +31 -31
  111. package/components/hy-line/hy-line.vue +55 -55
  112. package/components/hy-line/props.ts +43 -43
  113. package/components/hy-line-progress/hy-line-progress.vue +102 -102
  114. package/components/hy-line-progress/index.scss +1 -0
  115. package/components/hy-line-progress/props.ts +33 -33
  116. package/components/hy-list/hy-list.vue +226 -226
  117. package/components/hy-list/props.ts +69 -69
  118. package/components/hy-list/typing.d.ts +6 -6
  119. package/components/hy-loading/hy-loading.vue +107 -107
  120. package/components/hy-loading/props.ts +65 -65
  121. package/components/hy-menu/hy-menu.vue +159 -159
  122. package/components/hy-menu/props.ts +44 -44
  123. package/components/hy-menu/typing.d.ts +34 -34
  124. package/components/hy-modal/hy-modal.vue +173 -173
  125. package/components/hy-modal/props.ts +90 -90
  126. package/components/hy-modal/typing.d.ts +11 -11
  127. package/components/hy-navbar/hy-navbar.vue +144 -144
  128. package/components/hy-navbar/props.ts +78 -78
  129. package/components/hy-navbar/typing.d.ts +6 -6
  130. package/components/hy-notice-bar/hy-column-notice.vue +94 -94
  131. package/components/hy-notice-bar/hy-notice-bar.vue +96 -96
  132. package/components/hy-notice-bar/hy-row-notice.vue +121 -121
  133. package/components/hy-notice-bar/props.ts +85 -85
  134. package/components/hy-notice-bar/typing.d.ts +8 -8
  135. package/components/hy-notify/hy-notify.vue +174 -174
  136. package/components/hy-notify/props.ts +51 -51
  137. package/components/hy-number-step/hy-number-step.vue +367 -367
  138. package/components/hy-number-step/props.ts +112 -112
  139. package/components/hy-number-step/typing.d.ts +16 -16
  140. package/components/hy-overlay/hy-overlay.vue +60 -60
  141. package/components/hy-overlay/props.ts +33 -33
  142. package/components/hy-overlay/typing.d.ts +4 -4
  143. package/components/hy-pagination/hy-pagination.vue +135 -135
  144. package/components/hy-pagination/props.ts +55 -55
  145. package/components/hy-pagination/typing.d.ts +10 -10
  146. package/components/hy-picker/hy-picker.vue +7 -5
  147. package/components/hy-picker/props.ts +7 -2
  148. package/components/hy-picker/typing.d.ts +9 -5
  149. package/components/hy-popover/hy-popover.vue +251 -251
  150. package/components/hy-popover/props.ts +51 -51
  151. package/components/hy-popover/typing.d.ts +39 -39
  152. package/components/hy-popup/hy-popup.vue +197 -197
  153. package/components/hy-popup/props.ts +85 -85
  154. package/components/hy-popup/typing.d.ts +10 -10
  155. package/components/hy-price/hy-price.vue +79 -79
  156. package/components/hy-price/props.ts +54 -54
  157. package/components/hy-price/typing.d.ts +4 -4
  158. package/components/hy-qrcode/hy-qrcode.vue +216 -216
  159. package/components/hy-qrcode/props.ts +70 -70
  160. package/components/hy-qrcode/qrcode.js +1304 -1304
  161. package/components/hy-qrcode/typing.d.ts +8 -8
  162. package/components/hy-radio/hy-radio.vue +226 -226
  163. package/components/hy-radio/props.ts +1 -1
  164. package/components/hy-radio/typing.d.ts +8 -8
  165. package/components/hy-rate/hy-rate.vue +239 -239
  166. package/components/hy-rate/props.ts +77 -77
  167. package/components/hy-rate/typing.d.ts +6 -6
  168. package/components/hy-read-more/hy-read-more.vue +130 -130
  169. package/components/hy-read-more/props.ts +45 -45
  170. package/components/hy-read-more/typing.d.ts +6 -6
  171. package/components/hy-rolling-num/hy-rolling-num.vue +188 -188
  172. package/components/hy-rolling-num/props.ts +68 -68
  173. package/components/hy-scroll-list/hy-scroll-list.vue +123 -123
  174. package/components/hy-scroll-list/props.ts +22 -22
  175. package/components/hy-scroll-list/typing.d.ts +6 -6
  176. package/components/hy-search/hy-search.vue +221 -221
  177. package/components/hy-search/props.ts +131 -131
  178. package/components/hy-search/typing.d.ts +22 -22
  179. package/components/hy-signature/hy-signature.vue +640 -640
  180. package/components/hy-signature/props.ts +118 -118
  181. package/components/hy-signature/typing.d.ts +93 -93
  182. package/components/hy-slider/hy-slider.vue +444 -444
  183. package/components/hy-slider/props.ts +77 -77
  184. package/components/hy-slider/typing.d.ts +10 -10
  185. package/components/hy-status-bar/hy-status-bar.vue +41 -41
  186. package/components/hy-status-bar/props.ts +8 -8
  187. package/components/hy-status-bar/typing.d.ts +12 -12
  188. package/components/hy-steps/hy-steps.vue +267 -267
  189. package/components/hy-steps/props.ts +49 -49
  190. package/components/hy-steps/typing.d.ts +21 -21
  191. package/components/hy-sticky/hy-sticky.vue +226 -226
  192. package/components/hy-sticky/props.ts +24 -24
  193. package/components/hy-sticky/typing.d.ts +4 -4
  194. package/components/hy-submit-bar/hy-submit-bar.vue +189 -189
  195. package/components/hy-submit-bar/props.ts +91 -91
  196. package/components/hy-submit-bar/typing.d.ts +24 -24
  197. package/components/hy-subsection/hy-subsection.vue +207 -207
  198. package/components/hy-subsection/props.ts +52 -52
  199. package/components/hy-subsection/typing.d.ts +13 -13
  200. package/components/hy-swipe-action/hy-swipe-action.vue +323 -323
  201. package/components/hy-swipe-action/index.ts +25 -25
  202. package/components/hy-swipe-action/props.ts +47 -47
  203. package/components/hy-swipe-action/typing.d.ts +25 -25
  204. package/components/hy-swiper/hy-swiper-indicator.vue +75 -75
  205. package/components/hy-swiper/hy-swiper.vue +224 -224
  206. package/components/hy-swiper/props.ts +128 -128
  207. package/components/hy-swiper/typing.d.ts +26 -26
  208. package/components/hy-switch/hy-switch.vue +173 -173
  209. package/components/hy-switch/props.ts +61 -61
  210. package/components/hy-switch/typing.d.ts +8 -8
  211. package/components/hy-tabbar/hy-tabbar.vue +136 -136
  212. package/components/hy-tabbar/props.ts +59 -59
  213. package/components/hy-tabbar/typing.d.ts +21 -21
  214. package/components/hy-tabbar-group/hy-tabbar-group.vue +87 -87
  215. package/components/hy-tabbar-group/props.ts +78 -78
  216. package/components/hy-tabbar-group/typing.d.ts +16 -16
  217. package/components/hy-tabbar-item/hy-tabbar-item.vue +103 -103
  218. package/components/hy-tabbar-item/typing.d.ts +10 -10
  219. package/components/hy-table/hy-table.vue +358 -358
  220. package/components/hy-table/props.ts +47 -47
  221. package/components/hy-table/typing.d.ts +34 -34
  222. package/components/hy-tabs/hy-tabs.vue +335 -335
  223. package/components/hy-tabs/props.ts +77 -77
  224. package/components/hy-tabs/typing.d.ts +33 -33
  225. package/components/hy-tag/hy-tag.vue +174 -174
  226. package/components/hy-tag/props.ts +89 -89
  227. package/components/hy-tag/typing.d.ts +13 -13
  228. package/components/hy-text/hy-text.vue +237 -237
  229. package/components/hy-text/props.ts +115 -115
  230. package/components/hy-text/typing.d.ts +6 -6
  231. package/components/hy-textarea/hy-textarea.vue +197 -197
  232. package/components/hy-textarea/index.scss +5 -0
  233. package/components/hy-textarea/props.ts +116 -116
  234. package/components/hy-textarea/typing.d.ts +22 -22
  235. package/components/hy-toast/hy-toast.vue +190 -190
  236. package/components/hy-toast/typing.d.ts +38 -38
  237. package/components/hy-tooltip/hy-tooltip.vue +277 -277
  238. package/components/hy-tooltip/props.ts +78 -78
  239. package/components/hy-tooltip/typing.d.ts +4 -4
  240. package/components/hy-transition/hy-transition.vue +157 -157
  241. package/components/hy-transition/props.ts +32 -32
  242. package/components/hy-transition/typing.d.ts +16 -16
  243. package/components/hy-upload/hy-upload.vue +385 -385
  244. package/components/hy-upload/props.ts +132 -132
  245. package/components/hy-upload/typing.d.ts +65 -65
  246. package/components/hy-warn/hy-warn.vue +115 -115
  247. package/components/hy-warn/props.ts +49 -49
  248. package/components/hy-warn/typing.d.ts +6 -6
  249. package/components/hy-waterfall/hy-waterfall.vue +191 -191
  250. package/components/hy-waterfall/props.ts +21 -21
  251. package/components/hy-watermark/hy-watermark.vue +978 -978
  252. package/components/hy-watermark/props.ts +104 -104
  253. package/components/index.ts +183 -183
  254. package/global.d.ts +91 -91
  255. package/index.ts +1 -1
  256. package/libs/api/http.ts +140 -140
  257. package/libs/api/index.ts +1 -1
  258. package/libs/common/index.ts +2 -2
  259. package/libs/common/queue.ts +28 -28
  260. package/libs/composables/index.ts +6 -6
  261. package/libs/composables/usePopover.ts +241 -241
  262. package/libs/composables/useQueue.ts +53 -53
  263. package/libs/composables/useShakeService.ts +64 -64
  264. package/libs/composables/useShare.ts +42 -42
  265. package/libs/composables/useToast.ts +45 -45
  266. package/libs/composables/useTouch.ts +51 -51
  267. package/libs/config/color.ts +7 -7
  268. package/libs/config/icon.ts +430 -430
  269. package/libs/config/index.ts +2 -2
  270. package/libs/css/iconfont.css +443 -443
  271. package/libs/css/theme.scss +1 -1
  272. package/libs/global/index.ts +6 -6
  273. package/libs/global/register-properties.ts +37 -37
  274. package/libs/index.ts +7 -7
  275. package/libs/typing/index.ts +4 -4
  276. package/libs/typing/modules/common.d.ts +139 -139
  277. package/libs/typing/modules/enum.ts +67 -67
  278. package/libs/typing/modules/form.ts +5 -1
  279. package/libs/typing/modules/http.ts +17 -17
  280. package/libs/typing/modules/icon.d.ts +366 -366
  281. package/libs/typing/modules/rect.ts +10 -10
  282. package/libs/utils/base64.ts +119 -119
  283. package/libs/utils/calendar.js +1021 -1021
  284. package/libs/utils/colorGradient.ts +112 -112
  285. package/libs/utils/index.ts +5 -5
  286. package/libs/utils/inside.ts +350 -361
  287. package/libs/utils/inspect.ts +171 -171
  288. package/libs/utils/utils.ts +521 -521
  289. package/package.json +18 -18
  290. package/web-types.json +1 -1
@@ -1,220 +1,220 @@
1
- <template>
2
- <view class="hy-form">
3
- <slot></slot>
4
- </view>
5
- </template>
6
-
7
- <script lang="ts">
8
- export default {
9
- name: 'hy-form',
10
- options: {
11
- addGlobalClass: true,
12
- virtualHost: true,
13
- styleIsolation: 'shared'
14
- }
15
- }
16
- </script>
17
-
18
- <script setup lang="ts">
19
- import { provide, reactive, ref, toRefs } from 'vue'
20
- import { clearVal, isArray } from '../../libs'
21
- import formProps from './props'
22
- import type { IFormEmits } from './typing'
23
-
24
- /**
25
- * 表单组件父组件,需要搭配hy-form-item
26
- * @displayName hy-form
27
- */
28
- defineOptions({})
29
-
30
- const props = defineProps(formProps)
31
- const emit = defineEmits<IFormEmits>()
32
-
33
- // 表单数据
34
- const formData = reactive(props.model || {})
35
- const formItems = ref<any[]>([])
36
- const errors = reactive<Record<string, string>>({})
37
-
38
- // 表单上下文
39
- const formContext = {
40
- ...toRefs(props),
41
- formData,
42
- errors,
43
- addFormItem: (item: any) => {
44
- formItems.value.push(item)
45
- },
46
- removeFormItem: (item: any) => {
47
- const index = formItems.value.indexOf(item)
48
- if (index > -1) {
49
- formItems.value.splice(index, 1)
50
- }
51
- },
52
- validateField: (field: string, value: any, trigger?: 'blur' | 'change') => {
53
- const fieldRules = props.rules?.[field]
54
- if (!fieldRules) return true
55
-
56
- const rules = isArray(fieldRules) ? fieldRules : [fieldRules]
57
- let isValid = true
58
- let errorMessage = ''
59
-
60
- for (const rule of rules) {
61
- // 检查触发时机
62
- if (
63
- (trigger && !rule.trigger) ||
64
- (trigger && rule.trigger && !rule.trigger.includes(trigger))
65
- ) {
66
- continue
67
- }
68
-
69
- // 必填校验
70
- if (rule.required && (!value || value === '')) {
71
- errorMessage = rule.message || `${field} 是必填项`
72
- isValid = false
73
- break
74
- }
75
-
76
- // 长度校验
77
- if (rule.min && String(value).length < rule.min) {
78
- errorMessage = rule.message || `${field} 长度不能少于 ${rule.min} 个字符`
79
- isValid = false
80
- break
81
- }
82
-
83
- if (rule.max && String(value).length > rule.max) {
84
- errorMessage = rule.message || `${field} 长度不能超过 ${rule.max} 个字符`
85
- isValid = false
86
- break
87
- }
88
-
89
- // 类型校验
90
- if (rule.type === 'phone') {
91
- const phoneRegex = /^1[3-9]\d{9}$/
92
- if (!phoneRegex.test(String(value))) {
93
- errorMessage = rule.message || '请输入正确的手机号'
94
- isValid = false
95
- break
96
- }
97
- }
98
-
99
- if (rule.type === 'email') {
100
- const emailRegex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/
101
- if (!emailRegex.test(String(value))) {
102
- errorMessage = rule.message || '请输入正确的邮箱格式'
103
- isValid = false
104
- break
105
- }
106
- }
107
-
108
- if (rule.type === 'password') {
109
- const passwordRegex =
110
- /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]).{8,}$/
111
- if (!passwordRegex.test(String(value))) {
112
- errorMessage = rule.message || '密码至少8个字符,包含大小写字母、数字和特殊符号'
113
- isValid = false
114
- break
115
- }
116
- }
117
-
118
- // 自定义校验
119
- if (rule.validator) {
120
- const result = rule.validator(value)
121
- if (result === false || typeof result === 'string') {
122
- errorMessage =
123
- typeof result === 'string' ? result : rule.message || `${field} 校验失败`
124
- isValid = false
125
- break
126
- }
127
- }
128
- }
129
-
130
- if (isValid) {
131
- delete errors[field]
132
- } else {
133
- errors[field] = errorMessage
134
- }
135
-
136
- return isValid
137
- },
138
- setFieldValue: (field: string, value: any) => {
139
- formData[field] = value
140
- },
141
- getFieldValue: (field: string) => {
142
- return formData[field]
143
- }
144
- }
145
-
146
- // 提供表单上下文给子组件
147
- provide('formContext', formContext)
148
-
149
- // 验证所有字段
150
- const validate = () => {
151
- return new Promise((resolve, reject) => {
152
- let isValid = true
153
- const allErrors: Record<string, string> = {}
154
-
155
- formItems.value.forEach((item) => {
156
- const field = item.props?.prop
157
- if (field) {
158
- const value = formData[field]
159
- const fieldValid = formContext.validateField(field, value)
160
- if (!fieldValid) {
161
- isValid = false
162
- allErrors[field] = errors[field]
163
- }
164
- }
165
- })
166
-
167
- emit('validate', isValid, allErrors)
168
- if (isValid) {
169
- resolve(isValid)
170
- } else {
171
- reject(allErrors)
172
- }
173
- })
174
- }
175
-
176
- // 重置表单
177
- const resetFields = () => {
178
- clearVal(formData)
179
- Object.keys(errors).forEach((key) => {
180
- delete errors[key]
181
- })
182
- }
183
-
184
- // 清除验证
185
- const clearValidate = (fields?: string[]) => {
186
- if (fields) {
187
- fields.forEach((field) => {
188
- delete errors[field]
189
- })
190
- } else {
191
- Object.keys(errors).forEach((key) => {
192
- delete errors[key]
193
- })
194
- }
195
- }
196
-
197
- // 提交表单
198
- const submit = async () => {
199
- if (await validate()) {
200
- emit('submit', { ...formData })
201
- return formData
202
- }
203
- return false
204
- }
205
-
206
- defineExpose({
207
- validate,
208
- resetFields,
209
- clearValidate,
210
- submit,
211
- formData,
212
- errors
213
- })
214
- </script>
215
-
216
- <style lang="scss" scoped>
217
- .hy-form {
218
- width: 100%;
219
- }
220
- </style>
1
+ <template>
2
+ <view class="hy-form">
3
+ <slot></slot>
4
+ </view>
5
+ </template>
6
+
7
+ <script lang="ts">
8
+ export default {
9
+ name: 'hy-form',
10
+ options: {
11
+ addGlobalClass: true,
12
+ virtualHost: true,
13
+ styleIsolation: 'shared'
14
+ }
15
+ }
16
+ </script>
17
+
18
+ <script setup lang="ts">
19
+ import { provide, reactive, ref, toRefs } from 'vue'
20
+ import { clearVal, isArray } from '../../libs'
21
+ import formProps from './props'
22
+ import type { IFormEmits } from './typing'
23
+
24
+ /**
25
+ * 表单组件父组件,需要搭配hy-form-item
26
+ * @displayName hy-form
27
+ */
28
+ defineOptions({})
29
+
30
+ const props = defineProps(formProps)
31
+ const emit = defineEmits<IFormEmits>()
32
+
33
+ // 表单数据
34
+ const formData = reactive(props.model || {})
35
+ const formItems = ref<any[]>([])
36
+ const errors = reactive<Record<string, string>>({})
37
+
38
+ // 表单上下文
39
+ const formContext = {
40
+ ...toRefs(props),
41
+ formData,
42
+ errors,
43
+ addFormItem: (item: any) => {
44
+ formItems.value.push(item)
45
+ },
46
+ removeFormItem: (item: any) => {
47
+ const index = formItems.value.indexOf(item)
48
+ if (index > -1) {
49
+ formItems.value.splice(index, 1)
50
+ }
51
+ },
52
+ validateField: (field: string, value: any, trigger?: 'blur' | 'change') => {
53
+ const fieldRules = props.rules?.[field]
54
+ if (!fieldRules) return true
55
+
56
+ const rules = isArray(fieldRules) ? fieldRules : [fieldRules]
57
+ let isValid = true
58
+ let errorMessage = ''
59
+
60
+ for (const rule of rules) {
61
+ // 检查触发时机
62
+ if (
63
+ (trigger && !rule.trigger) ||
64
+ (trigger && rule.trigger && !rule.trigger.includes(trigger))
65
+ ) {
66
+ continue
67
+ }
68
+
69
+ // 必填校验
70
+ if (rule.required && (!value || value === '')) {
71
+ errorMessage = rule.message || `${field} 是必填项`
72
+ isValid = false
73
+ break
74
+ }
75
+
76
+ // 长度校验
77
+ if (rule.min && String(value).length < rule.min) {
78
+ errorMessage = rule.message || `${field} 长度不能少于 ${rule.min} 个字符`
79
+ isValid = false
80
+ break
81
+ }
82
+
83
+ if (rule.max && String(value).length > rule.max) {
84
+ errorMessage = rule.message || `${field} 长度不能超过 ${rule.max} 个字符`
85
+ isValid = false
86
+ break
87
+ }
88
+
89
+ // 类型校验
90
+ if (rule.type === 'phone') {
91
+ const phoneRegex = /^1[3-9]\d{9}$/
92
+ if (!phoneRegex.test(String(value))) {
93
+ errorMessage = rule.message || '请输入正确的手机号'
94
+ isValid = false
95
+ break
96
+ }
97
+ }
98
+
99
+ if (rule.type === 'email') {
100
+ const emailRegex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/
101
+ if (!emailRegex.test(String(value))) {
102
+ errorMessage = rule.message || '请输入正确的邮箱格式'
103
+ isValid = false
104
+ break
105
+ }
106
+ }
107
+
108
+ if (rule.type === 'password') {
109
+ const passwordRegex =
110
+ /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]).{8,}$/
111
+ if (!passwordRegex.test(String(value))) {
112
+ errorMessage = rule.message || '密码至少8个字符,包含大小写字母、数字和特殊符号'
113
+ isValid = false
114
+ break
115
+ }
116
+ }
117
+
118
+ // 自定义校验
119
+ if (rule.validator) {
120
+ const result = rule.validator(value)
121
+ if (result === false || typeof result === 'string') {
122
+ errorMessage =
123
+ typeof result === 'string' ? result : rule.message || `${field} 校验失败`
124
+ isValid = false
125
+ break
126
+ }
127
+ }
128
+ }
129
+
130
+ if (isValid) {
131
+ delete errors[field]
132
+ } else {
133
+ errors[field] = errorMessage
134
+ }
135
+
136
+ return isValid
137
+ },
138
+ setFieldValue: (field: string, value: any) => {
139
+ formData[field] = value
140
+ },
141
+ getFieldValue: (field: string) => {
142
+ return formData[field]
143
+ }
144
+ }
145
+
146
+ // 提供表单上下文给子组件
147
+ provide('formContext', formContext)
148
+
149
+ // 验证所有字段
150
+ const validate = () => {
151
+ return new Promise((resolve, reject) => {
152
+ let isValid = true
153
+ const allErrors: Record<string, string> = {}
154
+
155
+ formItems.value.forEach((item) => {
156
+ const field = item.props?.prop
157
+ if (field) {
158
+ const value = formData[field]
159
+ const fieldValid = formContext.validateField(field, value)
160
+ if (!fieldValid) {
161
+ isValid = false
162
+ allErrors[field] = errors[field]
163
+ }
164
+ }
165
+ })
166
+
167
+ emit('validate', isValid, allErrors)
168
+ if (isValid) {
169
+ resolve(isValid)
170
+ } else {
171
+ reject(allErrors)
172
+ }
173
+ })
174
+ }
175
+
176
+ // 重置表单
177
+ const resetFields = () => {
178
+ clearVal(formData)
179
+ Object.keys(errors).forEach((key) => {
180
+ delete errors[key]
181
+ })
182
+ }
183
+
184
+ // 清除验证
185
+ const clearValidate = (fields?: string[]) => {
186
+ if (fields) {
187
+ fields.forEach((field) => {
188
+ delete errors[field]
189
+ })
190
+ } else {
191
+ Object.keys(errors).forEach((key) => {
192
+ delete errors[key]
193
+ })
194
+ }
195
+ }
196
+
197
+ // 提交表单
198
+ const submit = async () => {
199
+ if (await validate()) {
200
+ emit('submit', { ...formData })
201
+ return formData
202
+ }
203
+ return false
204
+ }
205
+
206
+ defineExpose({
207
+ validate,
208
+ resetFields,
209
+ clearValidate,
210
+ submit,
211
+ formData,
212
+ errors
213
+ })
214
+ </script>
215
+
216
+ <style lang="scss" scoped>
217
+ .hy-form {
218
+ width: 100%;
219
+ }
220
+ </style>
@@ -1,37 +1,37 @@
1
- import type { PropType } from 'vue'
2
- import type { FormItemRule } from './typing'
3
-
4
- const formProps = {
5
- /** 表单数据对象 */
6
- model: Object as PropType<AnyObject>,
7
- /** 表单校验规则 */
8
- rules: Object as PropType<FormItemRule>,
9
- /** 表单底部边框 */
10
- border: {
11
- type: Boolean,
12
- default: false
13
- },
14
- /** label标签的宽度,单位rpx */
15
- labelWidth: {
16
- type: String,
17
- default: 'auto'
18
- },
19
- /**
20
- * 标签位置
21
- * @values left,top
22
- * */
23
- labelPosition: {
24
- type: String,
25
- default: 'left'
26
- },
27
- /**
28
- * 标签位置
29
- * @values left,center,right
30
- * */
31
- labelAlign: {
32
- type: String,
33
- default: 'left'
34
- }
35
- }
36
-
37
- export default formProps
1
+ import type { PropType } from 'vue'
2
+ import type { FormItemRule } from './typing'
3
+
4
+ const formProps = {
5
+ /** 表单数据对象 */
6
+ model: Object as PropType<AnyObject>,
7
+ /** 表单校验规则 */
8
+ rules: Object as PropType<FormItemRule>,
9
+ /** 表单底部边框 */
10
+ border: {
11
+ type: Boolean,
12
+ default: false
13
+ },
14
+ /** label标签的宽度,单位rpx */
15
+ labelWidth: {
16
+ type: String,
17
+ default: 'auto'
18
+ },
19
+ /**
20
+ * 标签位置
21
+ * @values left,top
22
+ * */
23
+ labelPosition: {
24
+ type: String,
25
+ default: 'left'
26
+ },
27
+ /**
28
+ * 标签位置
29
+ * @values left,center,right
30
+ * */
31
+ labelAlign: {
32
+ type: String,
33
+ default: 'left'
34
+ }
35
+ }
36
+
37
+ export default formProps
@@ -1,41 +1,41 @@
1
- export interface FormRule {
2
- /**
3
- * 是否必填
4
- */
5
- required?: boolean
6
- /**
7
- * 验证失败时的提示信息
8
- */
9
- message?: string
10
- /**
11
- * 自定义验证函数
12
- */
13
- validator?: (value: any) => boolean | string
14
- /**
15
- * 触发验证的时机
16
- */
17
- trigger?: ('blur' | 'change')[]
18
- /**
19
- * 最小长度
20
- */
21
- min?: number
22
- /**
23
- * 最大长度
24
- */
25
- max?: number
26
- /**
27
- * 验证类型
28
- */
29
- type?: 'phone' | 'email' | 'password'
30
- }
31
-
32
- export interface FormItemRule {
33
- [key: string]: FormRule | FormRule[]
34
- }
35
-
36
- export interface IFormEmits {
37
- /** 提交表单 */
38
- (e: 'submit', data: FormRule): void
39
- /** 表单校验 */
40
- (e: 'validate', valid: boolean, errors: AnyObject): void
41
- }
1
+ export interface FormRule {
2
+ /**
3
+ * 是否必填
4
+ */
5
+ required?: boolean
6
+ /**
7
+ * 验证失败时的提示信息
8
+ */
9
+ message?: string
10
+ /**
11
+ * 自定义验证函数
12
+ */
13
+ validator?: (value: any) => boolean | string
14
+ /**
15
+ * 触发验证的时机
16
+ */
17
+ trigger?: ('blur' | 'change')[]
18
+ /**
19
+ * 最小长度
20
+ */
21
+ min?: number
22
+ /**
23
+ * 最大长度
24
+ */
25
+ max?: number
26
+ /**
27
+ * 验证类型
28
+ */
29
+ type?: 'phone' | 'email' | 'password'
30
+ }
31
+
32
+ export interface FormItemRule {
33
+ [key: string]: FormRule | FormRule[]
34
+ }
35
+
36
+ export interface IFormEmits {
37
+ /** 提交表单 */
38
+ (e: 'submit', data: FormRule): void
39
+ /** 表单校验 */
40
+ (e: 'validate', valid: boolean, errors: AnyObject): void
41
+ }