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,274 @@
1
+ <template>
2
+ <view class="u-notice-bar-wrap" v-if="isShow" :style="{ borderRadius: props.borderRadius + 'rpx' }">
3
+ <block v-if="props.mode === 'horizontal' && props.isCircular">
4
+ <u-row-notice
5
+ :type="props.type"
6
+ :color="props.color"
7
+ :bgColor="props.bgColor"
8
+ :list="props.list"
9
+ :volumeIcon="props.volumeIcon"
10
+ :moreIcon="props.moreIcon"
11
+ :volumeSize="props.volumeSize"
12
+ :closeIcon="props.closeIcon"
13
+ :mode="props.mode"
14
+ :fontSize="props.fontSize"
15
+ :speed="props.speed"
16
+ :playState="props.playState"
17
+ :padding="props.padding"
18
+ @getMore="getMore"
19
+ @close="close"
20
+ @click="click"
21
+ ></u-row-notice>
22
+ </block>
23
+ <block v-if="props.mode === 'vertical' || (props.mode === 'horizontal' && !props.isCircular)">
24
+ <u-column-notice
25
+ :type="props.type"
26
+ :color="props.color"
27
+ :bgColor="props.bgColor"
28
+ :list="props.list"
29
+ :volumeIcon="props.volumeIcon"
30
+ :moreIcon="props.moreIcon"
31
+ :closeIcon="props.closeIcon"
32
+ :mode="props.mode"
33
+ :volumeSize="props.volumeSize"
34
+ :disable-touch="props.disableTouch"
35
+ :fontSize="props.fontSize"
36
+ :duration="props.duration"
37
+ :playState="props.playState"
38
+ :padding="props.padding"
39
+ @getMore="getMore"
40
+ @close="close"
41
+ @click="click"
42
+ @end="end"
43
+ ></u-column-notice>
44
+ </block>
45
+ </view>
46
+ </template>
47
+
48
+ <script setup lang="ts">
49
+ import { computed } from 'vue';
50
+
51
+ // 组件名称声明
52
+ defineOptions({ name: 'u-notice-bar' });
53
+
54
+ /**
55
+ * noticeBar 滚动通知
56
+ * @description 该组件用于滚动通告场景,有多种模式可供选择
57
+ * @tutorial https://www.uviewui.com/components/noticeBar.html
58
+ * @property {Array} list 滚动内容,数组形式,见上方说明
59
+ * @property {String} type 显示的主题(默认warning)
60
+ * @property {Boolean} volumeIcon 是否显示小喇叭图标(默认true)
61
+ * @property {Boolean} moreIcon 是否显示右边的向右箭头(默认false)
62
+ * @property {Boolean} closeIcon 是否显示关闭图标(默认false)
63
+ * @property {Boolean} autoplay 是否自动播放(默认true)
64
+ * @property {String} color 文字颜色
65
+ * @property {String|Number} bgColor 背景颜色
66
+ * @property {String} mode 滚动模式(默认horizontal)
67
+ * @property {Boolean} show 是否显示(默认true)
68
+ * @property {String|Number} fontSize 字体大小,单位rpx(默认28)
69
+ * @property {String|Number} volumeSize 左边喇叭的大小(默认34)
70
+ * @property {String|Number} duration 滚动周期时长,只对步进模式有效,横向衔接模式无效,单位ms(默认2000)
71
+ * @property {String|Number} speed 水平滚动时的滚动速度,即每秒移动多少距离,只对水平衔接方式有效,单位rpx(默认160)
72
+ * @property {Boolean} isCircular mode为horizontal时,指明是否水平衔接滚动(默认true)
73
+ * @property {String} playState 播放状态,play - 播放,paused - 暂停(默认play)
74
+ * @property {String|Number} borderRadius 通知栏圆角(默认为0)
75
+ * @property {String|Number} padding 内边距,字符串,与普通的内边距css写法一致(默认"18rpx 24rpx")
76
+ * @property {Boolean} noListHidden 列表为空时,是否显示组件(默认false)
77
+ * @property {Boolean} disableTouch 是否禁止通过手动滑动切换通知,只有mode = vertical,或者mode = horizontal且isCircular = false时有效(默认true)
78
+ * @event {Function} click 点击通告文字触发,只有mode = vertical,或者mode = horizontal且isCircular = false时有效
79
+ * @event {Function} close 点击右侧关闭图标触发
80
+ * @event {Function} getMore 点击右侧向右图标触发
81
+ * @event {Function} end 列表的消息每次被播放一个周期时触发,只有mode = vertical,或者mode = horizontal且isCircular = false时有效
82
+ * @example <u-notice-bar :more-icon="true" :list="list"></u-notice-bar>
83
+ */
84
+
85
+ const props = defineProps({
86
+ /** 滚动内容,数组形式 */
87
+ list: {
88
+ type: Array as () => string[],
89
+ default: () => []
90
+ },
91
+ /** 显示的主题,success|error|primary|info|warning */
92
+ type: {
93
+ type: String,
94
+ default: 'warning'
95
+ },
96
+ /** 是否显示左侧的音量图标 */
97
+ volumeIcon: {
98
+ type: Boolean,
99
+ default: true
100
+ },
101
+ /** 音量喇叭的大小 */
102
+ volumeSize: {
103
+ type: [Number, String],
104
+ default: 34
105
+ },
106
+ /** 是否显示右侧的右箭头图标 */
107
+ moreIcon: {
108
+ type: Boolean,
109
+ default: false
110
+ },
111
+ /** 是否显示右侧的关闭图标 */
112
+ closeIcon: {
113
+ type: Boolean,
114
+ default: false
115
+ },
116
+ /** 是否自动播放 */
117
+ autoplay: {
118
+ type: Boolean,
119
+ default: true
120
+ },
121
+ /** 文字颜色,各图标也会使用文字颜色 */
122
+ color: {
123
+ type: String,
124
+ default: ''
125
+ },
126
+ /** 背景颜色 */
127
+ bgColor: {
128
+ type: String,
129
+ default: ''
130
+ },
131
+ /** 滚动方向,horizontal-水平滚动,vertical-垂直滚动 */
132
+ mode: {
133
+ type: String,
134
+ default: 'horizontal'
135
+ },
136
+ /** 是否显示 */
137
+ show: {
138
+ type: Boolean,
139
+ default: true
140
+ },
141
+ /** 字体大小,单位rpx */
142
+ fontSize: {
143
+ type: [Number, String],
144
+ default: 28
145
+ },
146
+ /** 滚动一个周期的时间长,单位ms */
147
+ duration: {
148
+ type: [Number, String],
149
+ default: 2000
150
+ },
151
+ /** 水平滚动时的滚动速度,即每秒滚动多少rpx */
152
+ speed: {
153
+ type: [Number, String],
154
+ default: 160
155
+ },
156
+ /** 水平滚动时,是否采用衔接形式滚动 */
157
+ isCircular: {
158
+ type: Boolean,
159
+ default: true
160
+ },
161
+ /** 播放状态,play-播放,paused-暂停 */
162
+ playState: {
163
+ type: String,
164
+ default: 'play'
165
+ },
166
+ /** 是否禁止用手滑动切换 */
167
+ disableTouch: {
168
+ type: Boolean,
169
+ default: true
170
+ },
171
+ /** 滚动通知设置圆角 */
172
+ borderRadius: {
173
+ type: [Number, String],
174
+ default: 0
175
+ },
176
+ /** 通知的边距 */
177
+ padding: {
178
+ type: [Number, String],
179
+ default: '18rpx 24rpx'
180
+ },
181
+ /** list列表为空时,是否显示组件 */
182
+ noListHidden: {
183
+ type: Boolean,
184
+ default: true
185
+ }
186
+ });
187
+
188
+ const emit = defineEmits<{ (e: 'click', index: number): void; (e: 'close'): void; (e: 'getMore'): void; (e: 'end'): void }>();
189
+
190
+ /**
191
+ * 是否显示组件
192
+ * 如果设置show为false,或者设置了noListHidden为true,且list长度又为零的话,隐藏组件
193
+ */
194
+ const isShow = computed(() => {
195
+ if (!props.show || (props.noListHidden && props.list.length === 0)) return false;
196
+ return true;
197
+ });
198
+
199
+ /**
200
+ * 点击通告栏
201
+ */
202
+ function click(index: number) {
203
+ emit('click', index);
204
+ }
205
+
206
+ /**
207
+ * 点击关闭按钮
208
+ */
209
+ function close() {
210
+ emit('close');
211
+ }
212
+
213
+ /**
214
+ * 点击更多箭头按钮
215
+ */
216
+ function getMore() {
217
+ emit('getMore');
218
+ }
219
+
220
+ /**
221
+ * 滚动一个周期结束,只对垂直,或者水平步进形式有效
222
+ */
223
+ function end() {
224
+ emit('end');
225
+ }
226
+ </script>
227
+
228
+ <style lang="scss" scoped>
229
+ @import '../../libs/css/style.components.scss';
230
+ .u-notice-bar-wrap {
231
+ overflow: hidden;
232
+ }
233
+ .u-notice-bar {
234
+ padding: 18rpx 24rpx;
235
+ overflow: hidden;
236
+ }
237
+ .u-direction-row {
238
+ @include vue-flex;
239
+ align-items: center;
240
+ justify-content: space-between;
241
+ }
242
+ .u-left-icon {
243
+ @include vue-flex;
244
+ align-items: center;
245
+ }
246
+ .u-notice-box {
247
+ flex: 1;
248
+ @include vue-flex;
249
+ overflow: hidden;
250
+ margin-left: 12rpx;
251
+ }
252
+ .u-right-icon {
253
+ margin-left: 12rpx;
254
+ @include vue-flex;
255
+ align-items: center;
256
+ }
257
+ .u-notice-content {
258
+ line-height: 1;
259
+ white-space: nowrap;
260
+ font-size: 26rpx;
261
+ animation: u-loop-animation 10s linear infinite both;
262
+ text-align: right;
263
+ // 这一句很重要,为了能让滚动左右连接起来
264
+ padding-left: 100%;
265
+ }
266
+ @keyframes u-loop-animation {
267
+ 0% {
268
+ transform: translate3d(0, 0, 0);
269
+ }
270
+ 100% {
271
+ transform: translate3d(-100%, 0, 0);
272
+ }
273
+ }
274
+ </style>
@@ -0,0 +1,344 @@
1
+ <template>
2
+ <view class="u-numberbox">
3
+ <view
4
+ class="u-icon-minus"
5
+ @touchstart.stop.prevent="btnTouchStart('minus')"
6
+ @touchend.stop.prevent="clearTimer"
7
+ :class="{ 'u-icon-disabled': disabled || inputVal <= min }"
8
+ :style="{
9
+ background: bgColor,
10
+ height: inputHeight + 'rpx',
11
+ color: color
12
+ }"
13
+ >
14
+ <u-icon name="minus" :size="size"></u-icon>
15
+ </view>
16
+ <input
17
+ :disabled="disabledInput || disabled"
18
+ :cursor-spacing="getCursorSpacing"
19
+ :class="{ 'u-input-disabled': disabled }"
20
+ v-model="inputVal"
21
+ class="u-number-input"
22
+ @blur="onBlur"
23
+ @focus="onFocus"
24
+ type="digit"
25
+ :style="{
26
+ color: color,
27
+ fontSize: size + 'rpx',
28
+ background: bgColor,
29
+ height: inputHeight + 'rpx',
30
+ width: inputWidth + 'rpx'
31
+ }"
32
+ />
33
+ <view
34
+ class="u-icon-plus"
35
+ @touchstart.stop.prevent="btnTouchStart('plus')"
36
+ @touchend.stop.prevent="clearTimer"
37
+ :class="{ 'u-icon-disabled': disabled || inputVal >= max }"
38
+ :style="{
39
+ background: bgColor,
40
+ height: inputHeight + 'rpx',
41
+ color: color
42
+ }"
43
+ >
44
+ <u-icon name="plus" :size="size"></u-icon>
45
+ </view>
46
+ </view>
47
+ </template>
48
+
49
+ <script setup lang="ts">
50
+ import { ref, watch, computed, nextTick } from 'vue';
51
+
52
+ defineOptions({
53
+ name: 'u-number-box'
54
+ });
55
+
56
+ /**
57
+ * numberBox 步进器
58
+ * @description 该组件一般用于商城购物选择物品数量的场景。注意:该输入框只能输入大于或等于0的整数,不支持小数输入
59
+ * @tutorial https://www.uviewui.com/components/numberBox.html
60
+ * @property {Number} modelValue 输入框初始值(默认1)
61
+ * @property {String} bg-color 输入框和按钮的背景颜色(默认#F2F3F5)
62
+ * @property {Number} min 用户可输入的最小值(默认0)
63
+ * @property {Number} max 用户可输入的最大值(默认99999)
64
+ * @property {Number} step 步长,每次加或减的值(默认1)
65
+ * @property {Boolean} disabled 是否禁用操作,禁用后无法加减或手动修改输入框的值(默认false)
66
+ * @property {Boolean} disabled-input 是否禁止输入框手动输入值(默认false)
67
+ * @property {Boolean} positive-integer 是否只能输入正整数(默认true)
68
+ * @property {String | Number} size 输入框文字和按钮字体大小,单位rpx(默认26)
69
+ * @property {String} color 输入框文字和加减按钮图标的颜色(默认#323233)
70
+ * @property {String | Number} input-width 输入框宽度,单位rpx(默认80)
71
+ * @property {String | Number} input-height 输入框和按钮的高度,单位rpx(默认50)
72
+ * @property {String | Number} index 事件回调时用以区分当前发生变化的是哪个输入框
73
+ * @property {Boolean} long-press 是否开启长按连续递增或递减(默认true)
74
+ * @property {String | Number} press-time 开启长按触发后,每触发一次需要多久,单位ms(默认250)
75
+ * @property {String | Number} cursor-spacing 指定光标于键盘的距离,避免键盘遮挡输入框,单位rpx(默认200)
76
+ * @event {Function} change 输入框内容发生变化时触发,对象形式
77
+ * @event {Function} blur 输入框失去焦点时触发,对象形式
78
+ * @event {Function} minus 点击减少按钮时触发(按钮可点击情况下),对象形式
79
+ * @event {Function} plus 点击增加按钮时触发(按钮可点击情况下),对象形式
80
+ * @example <u-number-box :min="1" :max="100"></u-number-box>
81
+ */
82
+ const props = defineProps({
83
+ /** 输入框初始值(默认1) */
84
+ modelValue: { type: Number, default: 1 },
85
+ /** 输入框和按钮的背景颜色(默认#F2F3F5) */
86
+ bgColor: { type: String, default: '#F2F3F5' },
87
+ /** 用户可输入的最小值(默认0) */
88
+ min: { type: Number, default: 0 },
89
+ /** 用户可输入的最大值(默认99999) */
90
+ max: { type: Number, default: 99999 },
91
+ /** 步长,每次加或减的值(默认1) */
92
+ step: { type: Number, default: 1 },
93
+ /** 是否禁用操作,禁用后无法加减或手动修改输入框的值(默认false) */
94
+ disabled: { type: Boolean, default: false },
95
+ /** 输入框文字和按钮字体大小,单位rpx(默认26) */
96
+ size: { type: [Number, String], default: 26 },
97
+ /** 输入框文字和加减按钮图标的颜色(默认#323233) */
98
+ color: { type: String, default: '#323233' },
99
+ /** 输入框宽度,单位rpx(默认80) */
100
+ inputWidth: { type: [Number, String], default: 80 },
101
+ /** 输入框和按钮的高度,单位rpx(默认50) */
102
+ inputHeight: { type: [Number, String], default: 50 },
103
+ /** 事件回调时用以区分当前发生变化的是哪个输入框 */
104
+ index: { type: [Number, String], default: '' },
105
+ /** 是否禁止输入框手动输入值(默认false) */
106
+ disabledInput: { type: Boolean, default: false },
107
+ /** 指定光标于键盘的距离,避免键盘遮挡输入框,单位rpx(默认100) */
108
+ cursorSpacing: { type: [Number, String], default: 100 },
109
+ /** 是否开启长按连续递增或递减(默认true) */
110
+ longPress: { type: Boolean, default: true },
111
+ /** 开启长按触发后,每触发一次需要多久,单位ms(默认250) */
112
+ pressTime: { type: [Number, String], default: 250 },
113
+ /** 是否只能输入大于或等于0的整数(正整数)(默认true) */
114
+ positiveInteger: { type: Boolean, default: true }
115
+ });
116
+ const emit = defineEmits(['update:modelValue', 'change', 'blur', 'focus', 'minus', 'plus']);
117
+
118
+ // 响应式变量
119
+ const inputVal = ref<number>(props.modelValue); // 输入框中的值,不能直接使用props中的value,因为应该改变props的状态
120
+ const timer = ref<any>(null); // 用作长按的定时器
121
+ const changeFromInner = ref(false); // 值发生变化,是来自内部还是外部
122
+ const innerChangeTimer = ref<any>(null); // 内部定时器
123
+
124
+ // 计算属性
125
+ const getCursorSpacing = computed(() => {
126
+ const spacing = typeof props.cursorSpacing === 'string' ? Number(props.cursorSpacing) : props.cursorSpacing;
127
+ return Number(uni.upx2px(spacing));
128
+ });
129
+
130
+ // 监听外部 value 变化
131
+ watch(
132
+ () => props.modelValue,
133
+ (v1, v2) => {
134
+ // 只有value的改变是来自外部的时候,才去同步inputVal的值,否则会造成循环错误
135
+ if (!changeFromInner.value) {
136
+ inputVal.value = v1;
137
+ // 延时保证 changeFromInner 在运行周期最后处重置
138
+ // 因为inputVal变化后,会触发this.handleChange(),在其中changeFromInner会再次被设置为true,
139
+ // 造成外面修改值,也导致被认为是内部修改的混乱,这里进行this.$nextTick延时,保证在运行周期的最后处
140
+ // 将changeFromInner设置为false
141
+ nextTick(() => {
142
+ changeFromInner.value = false;
143
+ });
144
+ }
145
+ }
146
+ );
147
+
148
+ // 监听 inputVal 变化
149
+ watch(inputVal, (v1, v2) => {
150
+ // 为了让用户能够删除所有输入值,重新输入内容,删除所有值后,内容为空字符串
151
+ if (typeof v1 !== 'number' || isNaN(v1)) return;
152
+ let value = 0;
153
+ // 首先判断是否数值,并且在min和max之间,如果不是,使用原来值
154
+ let tmp = typeof v1 === 'number' && !isNaN(v1);
155
+ if (tmp && v1 >= props.min && v1 <= props.max) value = Number(v1);
156
+ else value = Number(v2);
157
+ // 判断是否只能输入大于等于0的整数
158
+ if (props.positiveInteger) {
159
+ // 小于0,或者带有小数点,
160
+ if (value < 0 || String(v1).indexOf('.') !== -1) {
161
+ value = Number(v2);
162
+ // 双向绑定input的值,必须要使用$nextTick修改显示的值
163
+ nextTick(() => {
164
+ inputVal.value = value;
165
+ });
166
+ }
167
+ }
168
+ // 发出change事件
169
+ handleChange(value, 'change');
170
+ });
171
+
172
+ function btnTouchStart(callback: 'minus' | 'plus') {
173
+ // 先执行一遍方法,否则会造成松开手时,就执行了clearTimer,导致无法实现功能
174
+ if (callback === 'minus') minus();
175
+ else plus();
176
+ // 如果没开启长按功能,直接返回
177
+ if (!props.longPress) return;
178
+ // 再次清空定时器,防止重复注册定时器
179
+ clearInterval(timer.value);
180
+ timer.value = setInterval(() => {
181
+ // 执行加或减函数
182
+ if (callback === 'minus') minus();
183
+ else plus();
184
+ }, Number(props.pressTime));
185
+ }
186
+
187
+ function clearTimer() {
188
+ // 清除长按定时器
189
+ nextTick(() => {
190
+ clearInterval(timer.value);
191
+ timer.value = null;
192
+ });
193
+ }
194
+
195
+ function minus() {
196
+ // 执行减法操作
197
+ computeVal('minus');
198
+ }
199
+ function plus() {
200
+ // 执行加法操作
201
+ computeVal('plus');
202
+ }
203
+
204
+ // 为了保证小数相加减出现精度溢出的问题
205
+ function calcPlus(num1: number, num2: number) {
206
+ let baseNum, baseNum1, baseNum2;
207
+ try {
208
+ baseNum1 = num1.toString().split('.')[1].length;
209
+ } catch (e) {
210
+ baseNum1 = 0;
211
+ }
212
+ try {
213
+ baseNum2 = num2.toString().split('.')[1].length;
214
+ } catch (e) {
215
+ baseNum2 = 0;
216
+ }
217
+ baseNum = Math.pow(10, Math.max(baseNum1, baseNum2));
218
+ let precision = baseNum1 >= baseNum2 ? baseNum1 : baseNum2; //精度
219
+ return Number(((num1 * baseNum + num2 * baseNum) / baseNum).toFixed(precision));
220
+ }
221
+
222
+ // 为了保证小数相加减出现精度溢出的问题
223
+ function calcMinus(num1: number, num2: number) {
224
+ let baseNum, baseNum1, baseNum2;
225
+ try {
226
+ baseNum1 = num1.toString().split('.')[1].length;
227
+ } catch (e) {
228
+ baseNum1 = 0;
229
+ }
230
+ try {
231
+ baseNum2 = num2.toString().split('.')[1].length;
232
+ } catch (e) {
233
+ baseNum2 = 0;
234
+ }
235
+ baseNum = Math.pow(10, Math.max(baseNum1, baseNum2));
236
+ let precision = baseNum1 >= baseNum2 ? baseNum1 : baseNum2;
237
+ return Number(((num1 * baseNum - num2 * baseNum) / baseNum).toFixed(precision));
238
+ }
239
+
240
+ // 处理加减按钮点击逻辑
241
+ function computeVal(type: 'minus' | 'plus') {
242
+ uni.hideKeyboard();
243
+ if (props.disabled) return;
244
+ let value = 0;
245
+ // 减
246
+ if (type === 'minus') {
247
+ value = calcMinus(Number(inputVal.value), props.step);
248
+ } else if (type === 'plus') {
249
+ value = calcPlus(Number(inputVal.value), props.step);
250
+ }
251
+ // 判断是否小于最小值和大于最大值
252
+ if (value < props.min || value > props.max) return;
253
+ inputVal.value = value;
254
+ handleChange(value, type);
255
+ }
256
+
257
+ // 处理用户手动输入的情况
258
+ function onBlur(event: any) {
259
+ let val = 0;
260
+ let value = event.detail.value;
261
+ // 如果为非0-9数字组成,或者其第一位数值为0,直接让其等于min值
262
+ // 这里不直接判断是否正整数,是因为用户传递的props min值可能为0
263
+ if (!/(^\d+$)/.test(value) || value[0] == 0) val = props.min;
264
+ else val = +value;
265
+ if (val > props.max) val = props.max;
266
+ else if (val < props.min) val = props.min;
267
+ nextTick(() => {
268
+ inputVal.value = val;
269
+ });
270
+ handleChange(val, 'blur');
271
+ }
272
+
273
+ // 输入框获得焦点事件
274
+ function onFocus() {
275
+ emit('focus');
276
+ }
277
+
278
+ // 处理值变化的统一逻辑
279
+ function handleChange(value: number, type: 'update:modelValue' | 'change' | 'blur' | 'focus' | 'minus' | 'plus') {
280
+ if (props.disabled) return;
281
+ // 清除定时器,避免造成混乱
282
+ if (innerChangeTimer.value) {
283
+ clearTimeout(innerChangeTimer.value);
284
+ innerChangeTimer.value = null;
285
+ }
286
+ // 发出input事件,修改通过v-model绑定的值,达到双向绑定的效果
287
+ changeFromInner.value = true;
288
+ // 一定时间内,清除changeFromInner标记,否则内部值改变后外部通过程序修改value值,将会无效
289
+ innerChangeTimer.value = setTimeout(() => {
290
+ changeFromInner.value = false;
291
+ }, 150);
292
+ emit('update:modelValue', Number(value));
293
+ emit(type, {
294
+ // 转为Number类型
295
+ value: Number(value),
296
+ index: props.index
297
+ });
298
+ }
299
+ </script>
300
+
301
+ <style lang="scss" scoped>
302
+ @import '../../libs/css/style.components.scss';
303
+
304
+ .u-numberbox {
305
+ display: inline-flex;
306
+ align-items: center;
307
+ }
308
+
309
+ .u-number-input {
310
+ position: relative;
311
+ text-align: center;
312
+ padding: 0;
313
+ margin: 0 6rpx;
314
+ @include vue-flex;
315
+ align-items: center;
316
+ justify-content: center;
317
+ }
318
+
319
+ .u-icon-plus,
320
+ .u-icon-minus {
321
+ width: 60rpx;
322
+ @include vue-flex;
323
+ justify-content: center;
324
+ align-items: center;
325
+ }
326
+
327
+ .u-icon-plus {
328
+ border-radius: 0 8rpx 8rpx 0;
329
+ }
330
+
331
+ .u-icon-minus {
332
+ border-radius: 8rpx 0 0 8rpx;
333
+ }
334
+
335
+ .u-icon-disabled {
336
+ color: #c8c9cc !important;
337
+ background: #f7f8fa !important;
338
+ }
339
+
340
+ .u-input-disabled {
341
+ color: #c8c9cc !important;
342
+ background-color: #f2f3f5 !important;
343
+ }
344
+ </style>