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,95 @@
1
+ <template>
2
+ <view
3
+ class="u-row"
4
+ :style="{
5
+ alignItems: uAlignItem,
6
+ justifyContent: uJustify
7
+ }"
8
+ @tap="onClick"
9
+ >
10
+ <slot />
11
+ </view>
12
+ </template>
13
+
14
+ <script setup lang="ts">
15
+ import { computed, provide } from 'vue';
16
+
17
+ defineOptions({
18
+ name: 'u-row'
19
+ });
20
+
21
+ /**
22
+ * row 行布局
23
+ * @description 通过基础的 12 分栏,迅速简便地创建布局。
24
+ * @tutorial https://www.uviewui.com/components/layout.html#row-props
25
+ * @property {String|Number} gutter 栅格间隔,左右各为此值的一半,单位rpx(默认0)
26
+ * @property {String} justify 水平排列方式(微信小程序暂不支持)默认(start(或flex-start))
27
+ * @property {String} align 垂直排列方式(默认center)
28
+ * @example <u-row gutter="16"></u-row>
29
+ */
30
+
31
+ const emit = defineEmits<{ (e: 'click'): void }>();
32
+
33
+ const props = defineProps({
34
+ /** 给col添加间距,左右边距各占一半 */
35
+ gutter: { type: [String, Number], default: 20 },
36
+ /** 水平排列方式,可选值为`start`(或`flex-start`)、`end`(或`flex-end`)、`center`、`around`(或`space-around`)、`between`(或`space-between`) */
37
+ justify: { type: String, default: 'start' },
38
+ /** 垂直对齐方式,可选值为top、center、bottom */
39
+ align: { type: String, default: 'center' },
40
+ /** 是否阻止事件传播 */
41
+ stop: { type: Boolean, default: true }
42
+ });
43
+
44
+ // 提供 gutter 给子组件 u-col 使用
45
+ provide('u-row-gutter', props.gutter);
46
+
47
+ /**
48
+ * 计算水平排列方式
49
+ * @returns {string}
50
+ */
51
+ const uJustify = computed(() => {
52
+ if (props.justify === 'end' || props.justify === 'start') return 'flex-' + props.justify;
53
+ else if (props.justify === 'around' || props.justify === 'between') return 'space-' + props.justify;
54
+ else return props.justify;
55
+ });
56
+
57
+ /**
58
+ * 计算垂直对齐方式
59
+ * @returns {string}
60
+ */
61
+ const uAlignItem = computed(() => {
62
+ if (props.align === 'top') return 'flex-start';
63
+ if (props.align === 'bottom') return 'flex-end';
64
+ else return props.align;
65
+ });
66
+
67
+ /**
68
+ * 点击事件
69
+ * @param e 事件对象
70
+ */
71
+ function onClick(e: Event) {
72
+ // 触发 click 事件
73
+ emit('click');
74
+ }
75
+ </script>
76
+
77
+ <style lang="scss">
78
+ @import '../../libs/css/style.components.scss';
79
+
80
+ .u-row {
81
+ // 由于微信小程序编译后奇怪的页面结构,只能使用float布局实现,flex无法实现
82
+ /* #ifndef MP-WEIXIN || MP-QQ || MP-TOUTIAO */
83
+ @include vue-flex;
84
+ /* #endif */
85
+ flex-wrap: wrap;
86
+ }
87
+
88
+ .u-row:after {
89
+ /* #ifdef MP-WEIXIN || MP-QQ || MP-TOUTIAO */
90
+ display: table;
91
+ clear: both;
92
+ content: '';
93
+ /* #endif */
94
+ }
95
+ </style>
@@ -0,0 +1,273 @@
1
+ <template>
2
+ <view v-if="props.show" class="u-notice-bar" :style="{ background: computeBgColor, padding: props.padding }" :class="[props.type ? `u-type-${props.type}-light-bg` : '']">
3
+ <view class="u-direction-row">
4
+ <view class="u-icon-wrap">
5
+ <u-icon class="u-left-icon" v-if="props.volumeIcon" name="volume-fill" :size="props.volumeSize" :color="computeColor"></u-icon>
6
+ </view>
7
+ <view class="u-notice-box" id="u-notice-box">
8
+ <view class="u-notice-content" id="u-notice-content" :style="{ animationDuration: animationDuration, animationPlayState: animationPlayState }">
9
+ <text class="u-notice-text" @tap="click" :style="textStyle" :class="['u-type-' + props.type]">{{ showText }}</text>
10
+ </view>
11
+ </view>
12
+ <view class="u-icon-wrap">
13
+ <u-icon @click="getMore" class="u-right-icon" v-if="props.moreIcon" name="arrow-right" :size="26" :color="computeColor"></u-icon>
14
+ <u-icon @click="close" class="u-right-icon" v-if="props.closeIcon" name="close" :size="24" :color="computeColor"></u-icon>
15
+ </view>
16
+ </view>
17
+ </view>
18
+ </template>
19
+
20
+ <script setup lang="ts">
21
+ import { ref, computed, watch, onMounted, nextTick, getCurrentInstance } from 'vue';
22
+
23
+ defineOptions({ name: 'u-row-notice' });
24
+
25
+ /**
26
+ * u-row-notice 水平滚动通告栏
27
+ * @property {Array} list 显示的内容,数组
28
+ * @property {String} type 显示的主题,success|error|primary|info|warning|none
29
+ * @property {Boolean} volumeIcon 是否显示左侧的音量图标
30
+ * @property {Boolean} moreIcon 是否显示右侧的右箭头图标
31
+ * @property {Boolean} closeIcon 是否显示右侧的关闭图标
32
+ * @property {Boolean} autoplay 是否自动播放
33
+ * @property {String} color 文字颜色,各图标也会使用文字颜色
34
+ * @property {String} bgColor 背景颜色
35
+ * @property {Boolean} show 是否显示
36
+ * @property {String|Number} fontSize 字体大小,单位rpx
37
+ * @property {String|Number} volumeSize 音量喇叭的大小
38
+ * @property {String|Number} speed 水平滚动时的滚动速度,即每秒滚动多少rpx
39
+ * @property {String} playState 播放状态,play-播放,paused-暂停
40
+ * @property {String|Number} padding 通知的边距
41
+ */
42
+
43
+ const props = defineProps({
44
+ /** 显示的内容,数组 */
45
+ list: {
46
+ type: Array as () => string[],
47
+ default: () => []
48
+ },
49
+ /** 显示的主题,success|error|primary|info|warning|none */
50
+ type: {
51
+ type: String,
52
+ default: 'warning'
53
+ },
54
+ /** 是否显示左侧的音量图标 */
55
+ volumeIcon: {
56
+ type: Boolean,
57
+ default: true
58
+ },
59
+ /** 是否显示右侧的右箭头图标 */
60
+ moreIcon: {
61
+ type: Boolean,
62
+ default: false
63
+ },
64
+ /** 是否显示右侧的关闭图标 */
65
+ closeIcon: {
66
+ type: Boolean,
67
+ default: false
68
+ },
69
+ /** 是否自动播放 */
70
+ autoplay: {
71
+ type: Boolean,
72
+ default: true
73
+ },
74
+ /** 文字颜色,各图标也会使用文字颜色 */
75
+ color: {
76
+ type: String,
77
+ default: ''
78
+ },
79
+ /** 背景颜色 */
80
+ bgColor: {
81
+ type: String,
82
+ default: ''
83
+ },
84
+ /** 是否显示 */
85
+ show: {
86
+ type: Boolean,
87
+ default: true
88
+ },
89
+ /** 字体大小,单位rpx */
90
+ fontSize: {
91
+ type: [Number, String],
92
+ default: 26
93
+ },
94
+ /** 音量喇叭的大小 */
95
+ volumeSize: {
96
+ type: [Number, String],
97
+ default: 34
98
+ },
99
+ /** 水平滚动时的滚动速度,即每秒滚动多少rpx */
100
+ speed: {
101
+ type: [Number, String],
102
+ default: 160
103
+ },
104
+ /** 播放状态,play-播放,paused-暂停 */
105
+ playState: {
106
+ type: String,
107
+ default: 'play'
108
+ },
109
+ /** 通知的边距 */
110
+ padding: {
111
+ type: [Number, String],
112
+ default: '18rpx 24rpx'
113
+ }
114
+ });
115
+
116
+ const emit = defineEmits<{ (e: 'click'): void; (e: 'close'): void; (e: 'getMore'): void }>();
117
+ const instance = getCurrentInstance();
118
+ const textWidth = ref(0); // 滚动的文字宽度
119
+ const animationDuration = ref('10s'); // 动画执行时间
120
+ const animationPlayState = ref('paused'); // 动画的开始和结束执行
121
+ const showText = ref(''); // 显示的文本
122
+
123
+ /**
124
+ * 计算字体颜色,如果没有自定义的,就用uview主题颜色
125
+ */
126
+ const computeColor = computed(() => {
127
+ if (props.color) return props.color;
128
+ // 如果是无主题,就默认使用content-color
129
+ else if (props.type === 'none') return '#606266';
130
+ else return props.type;
131
+ });
132
+
133
+ /**
134
+ * 文字内容的样式
135
+ */
136
+ const textStyle = computed(() => {
137
+ const style: Record<string, any> = {};
138
+ if (props.color) style.color = props.color;
139
+ else if (props.type === 'none') style.color = '#606266';
140
+ style.fontSize = props.fontSize + 'rpx';
141
+ return style;
142
+ });
143
+
144
+ /**
145
+ * 计算背景颜色
146
+ */
147
+ const computeBgColor = computed(() => {
148
+ if (props.bgColor) return props.bgColor;
149
+ else if (props.type === 'none') return 'transparent';
150
+ });
151
+
152
+ /**
153
+ * 初始化滚动宽度和动画
154
+ */
155
+ function initSize() {
156
+ nextTick(() => {
157
+ uni.createSelectorQuery()
158
+ .in(instance?.proxy)
159
+ .select('#u-notice-content')
160
+ .boundingClientRect()
161
+ .exec(ret => {
162
+ textWidth.value = ret[0]?.width || 0;
163
+ // 根据t=s/v(时间=路程/速度),这里为何不需要加上#u-notice-box的宽度,因为中设置了.u-notice-content样式中设置了padding-left: 100%
164
+ // 恰巧计算出来的结果中已经包含了#u-notice-box的宽度
165
+ animationDuration.value = `${textWidth.value / uni.upx2px(Number(props.speed))}s`;
166
+ // 这里必须这样开始动画,否则在APP上动画速度不会改变(HX版本2.4.6,IOS13)
167
+ animationPlayState.value = 'paused';
168
+ setTimeout(() => {
169
+ if (props.playState === 'play' && props.autoplay) animationPlayState.value = 'running';
170
+ }, 10);
171
+ });
172
+ });
173
+ }
174
+
175
+ watch(
176
+ () => props.list,
177
+ val => {
178
+ showText.value = val.join(',');
179
+ initSize();
180
+ },
181
+ { immediate: true }
182
+ );
183
+
184
+ watch(
185
+ () => props.playState,
186
+ val => {
187
+ animationPlayState.value = val === 'play' ? 'running' : 'paused';
188
+ }
189
+ );
190
+
191
+ watch(
192
+ () => props.speed,
193
+ () => {
194
+ initSize();
195
+ }
196
+ );
197
+
198
+ onMounted(() => {
199
+ initSize();
200
+ });
201
+
202
+ /**
203
+ * 点击通告栏
204
+ */
205
+ function click() {
206
+ emit('click');
207
+ }
208
+
209
+ /**
210
+ * 点击关闭按钮
211
+ */
212
+ function close() {
213
+ emit('close');
214
+ }
215
+
216
+ /**
217
+ * 点击更多箭头按钮
218
+ */
219
+ function getMore() {
220
+ emit('getMore');
221
+ }
222
+ </script>
223
+
224
+ <style lang="scss" scoped>
225
+ @import '../../libs/css/style.components.scss';
226
+ .u-notice-bar {
227
+ padding: 18rpx 24rpx;
228
+ overflow: hidden;
229
+ }
230
+ .u-direction-row {
231
+ @include vue-flex;
232
+ align-items: center;
233
+ justify-content: space-between;
234
+ }
235
+ .u-left-icon {
236
+ /* #ifndef APP-NVUE */
237
+ display: inline-flex;
238
+ /* #endif */
239
+ align-items: center;
240
+ }
241
+ .u-notice-box {
242
+ flex: 1;
243
+ @include vue-flex;
244
+ overflow: hidden;
245
+ margin-left: 12rpx;
246
+ }
247
+ .u-right-icon {
248
+ margin-left: 12rpx;
249
+ display: inline-flex;
250
+ align-items: center;
251
+ }
252
+ .u-notice-content {
253
+ animation: u-loop-animation 10s linear infinite both;
254
+ text-align: right;
255
+ // 这一句很重要,为了能让滚动左右连接起来
256
+ padding-left: 100%;
257
+ @include vue-flex;
258
+ flex-wrap: nowrap;
259
+ }
260
+ .u-notice-text {
261
+ font-size: 26rpx;
262
+ word-break: keep-all;
263
+ white-space: nowrap;
264
+ }
265
+ @keyframes u-loop-animation {
266
+ 0% {
267
+ transform: translate3d(0, 0, 0);
268
+ }
269
+ 100% {
270
+ transform: translate3d(-100%, 0, 0);
271
+ }
272
+ }
273
+ </style>
@@ -0,0 +1,298 @@
1
+ <template>
2
+ <view
3
+ class="u-search"
4
+ @tap="clickHandler"
5
+ :style="{
6
+ margin: margin
7
+ }"
8
+ >
9
+ <view
10
+ class="u-content"
11
+ :style="{
12
+ backgroundColor: bgColor,
13
+ borderRadius: shape == 'round' ? '100rpx' : '10rpx',
14
+ border: borderStyle,
15
+ height: height + 'rpx'
16
+ }"
17
+ >
18
+ <view class="u-icon-wrap">
19
+ <u-icon class="u-clear-icon" :size="30" :name="searchIcon" :color="searchIconColor ? searchIconColor : color"></u-icon>
20
+ </view>
21
+ <input
22
+ confirm-type="search"
23
+ @blur="blur"
24
+ :value="modelValue"
25
+ @confirm="search"
26
+ @input="inputChange"
27
+ :disabled="disabled"
28
+ @focus="getFocus"
29
+ :focus="focus"
30
+ :maxlength="maxlength"
31
+ placeholder-class="u-placeholder-class"
32
+ :placeholder="placeholder"
33
+ :placeholder-style="`color: ${placeholderColor}`"
34
+ class="u-input"
35
+ type="text"
36
+ :style="[
37
+ {
38
+ textAlign: inputAlign,
39
+ color: color,
40
+ backgroundColor: bgColor
41
+ },
42
+ inputStyle
43
+ ]"
44
+ />
45
+ <view class="u-close-wrap" v-if="keyword && clearabled && focused" @tap="clear">
46
+ <u-icon class="u-clear-icon" name="close-circle-fill" size="34" color="#c0c4cc"></u-icon>
47
+ </view>
48
+ </view>
49
+ <view :style="[actionStyle]" class="u-action" :class="[showActionBtn || show ? 'u-action-active' : '']" @tap.stop.prevent="custom">{{ actionText }}</view>
50
+ </view>
51
+ </template>
52
+
53
+ <script setup lang="ts">
54
+ import { ref, computed, watch, nextTick } from 'vue';
55
+
56
+ defineOptions({ name: 'u-search' });
57
+
58
+ /**
59
+ * search 搜索框
60
+ * @description 搜索组件,集成了常见搜索框所需功能,用户可以一键引入,开箱即用。
61
+ * @tutorial https://www.uviewui.com/components/search.html
62
+ * @property {String} shape 搜索框形状,round-圆形,square-方形(默认round)
63
+ * @property {String} bg-color 搜索框背景颜色(默认#f2f2f2)
64
+ * @property {String} border-color 边框颜色,配置了颜色,才会有边框
65
+ * @property {String} placeholder 占位文字内容(默认“请输入关键字”)
66
+ * @property {Boolean} clearabled 是否启用清除控件(默认true)
67
+ * @property {Boolean} focus 是否自动获得焦点(默认false)
68
+ * @property {Boolean} show-action 是否显示右侧控件(默认true)
69
+ * @property {String} action-text 右侧控件文字(默认“搜索”)
70
+ * @property {Object} action-style 右侧控件的样式,对象形式
71
+ * @property {String} input-align 输入框内容水平对齐方式(默认left)
72
+ * @property {Object} input-style 自定义输入框样式,对象形式
73
+ * @property {Boolean} disabled 是否启用输入框(默认false)
74
+ * @property {String} search-icon-color 搜索图标的颜色,默认同输入框字体颜色
75
+ * @property {String} color 输入框字体颜色(默认#606266)
76
+ * @property {String} placeholder-color placeholder的颜色(默认#909399)
77
+ * @property {String} search-icon 输入框左边的图标,可以为uView图标名称或图片路径
78
+ * @property {String} margin 组件与其他上下左右元素之间的距离,带单位的字符串形式,如"30rpx"
79
+ * @property {Boolean} animation 是否开启动画,见上方说明(默认false)
80
+ * @property {String} value 输入框初始值
81
+ * @property {String | Number} maxlength 输入框最大能输入的长度,-1为不限制长度
82
+ * @property {Boolean} input-style input输入框的样式,可以定义文字颜色,大小等,对象形式
83
+ * @property {String | Number} height 输入框高度,单位rpx(默认64)
84
+ * @event {Function} change 输入框内容发生变化时触发
85
+ * @event {Function} search 用户确定搜索时触发,用户按回车键,或者手机键盘右下角的"搜索"键时触发
86
+ * @event {Function} custom 用户点击右侧控件时触发
87
+ * @event {Function} clear 用户点击清除按钮时触发
88
+ * @example <u-search placeholder="日照香炉生紫烟" v-model="keyword"></u-search>
89
+ */
90
+
91
+ const props = defineProps({
92
+ /** 搜索框形状,round-圆形,square-方形 */
93
+ shape: { type: String, default: 'round' },
94
+ /** 搜索框背景色,默认值#f2f2f2 */
95
+ bgColor: { type: String, default: '#f2f2f2' },
96
+ /** 占位提示文字 */
97
+ placeholder: { type: String, default: '请输入关键字' },
98
+ /** 是否启用清除控件 */
99
+ clearabled: { type: Boolean, default: true },
100
+ /** 是否自动聚焦 */
101
+ focus: { type: Boolean, default: false },
102
+ /** 是否在搜索框右侧显示取消按钮 */
103
+ showAction: { type: Boolean, default: true },
104
+ /** 右边控件的样式 */
105
+ actionStyle: { type: Object, default: () => ({}) },
106
+ /** 取消按钮文字 */
107
+ actionText: { type: String, default: '搜索' },
108
+ /** 输入框内容对齐方式,可选值为 left|center|right */
109
+ inputAlign: { type: String, default: 'left' },
110
+ /** 是否启用输入框 */
111
+ disabled: { type: Boolean, default: false },
112
+ /** 开启showAction时,是否在input获取焦点时才显示 */
113
+ animation: { type: Boolean, default: false },
114
+ /** 边框颜色,只要配置了颜色,才会有边框 */
115
+ borderColor: { type: String, default: 'none' },
116
+ /** 输入框的初始化内容 */
117
+ modelValue: { type: String, default: '' },
118
+ /** 搜索框高度,单位rpx */
119
+ height: { type: [Number, String], default: 64 },
120
+ /** input输入框的样式,可以定义文字颜色,大小等,对象形式 */
121
+ inputStyle: { type: Object, default: () => ({}) },
122
+ /** 输入框最大能输入的长度,-1为不限制长度(来自uniapp文档) */
123
+ maxlength: { type: [Number, String], default: '-1' },
124
+ /** 搜索图标的颜色,默认同输入框字体颜色 */
125
+ searchIconColor: { type: String, default: '' },
126
+ /** 输入框字体颜色 */
127
+ color: { type: String, default: '#606266' },
128
+ /** placeholder的颜色 */
129
+ placeholderColor: { type: String, default: '#909399' },
130
+ /** 组件与其他上下左右元素之间的距离,带单位的字符串形式,如"30rpx"、"30rpx 20rpx"等写法 */
131
+ margin: { type: String, default: '0' },
132
+ /** 左边输入框的图标,可以为uView图标名称或图片路径 */
133
+ searchIcon: { type: String, default: 'search' }
134
+ });
135
+
136
+ const emit = defineEmits(['update:modelValue', 'change', 'search', 'custom', 'clear', 'focus', 'blur', 'click']);
137
+
138
+ // 绑定输入框的值
139
+ const keyword = ref(props.modelValue);
140
+ // 是否显示右边的清除图标
141
+ const showClear = ref(false);
142
+ // 控制右侧动画展开
143
+ const show = ref(false);
144
+ // 标记input当前状态是否处于聚焦中,如果是,才会显示右侧的清除控件
145
+ const focused = ref(props.focus);
146
+
147
+ // 监听v-model和props.value变化
148
+ watch(
149
+ () => props.modelValue,
150
+ nVal => {
151
+ keyword.value = nVal;
152
+ }
153
+ );
154
+ watch(keyword, nVal => {
155
+ emit('update:modelValue', nVal);
156
+ emit('change', nVal);
157
+ });
158
+
159
+ /**
160
+ * 是否显示右侧action按钮
161
+ */
162
+ const showActionBtn = computed(() => {
163
+ if (!props.animation && props.showAction) return true;
164
+ else return false;
165
+ });
166
+ /**
167
+ * 边框样式
168
+ */
169
+ const borderStyle = computed(() => {
170
+ if (props.borderColor) return `1px solid ${props.borderColor}`;
171
+ else return 'none';
172
+ });
173
+
174
+ /**
175
+ * 监听input事件获取输入框内容的变化
176
+ */
177
+ function inputChange(e: any) {
178
+ keyword.value = e.detail.value;
179
+ }
180
+ /**
181
+ * 清空输入
182
+ * 也可以作为用户通过ref形式调用清空输入框内容
183
+ */
184
+ function clear() {
185
+ keyword.value = '';
186
+ // 延后发出事件,避免在父组件监听clear事件时,value为更新前的值(不为空)
187
+ nextTick(() => {
188
+ emit('clear');
189
+ });
190
+ }
191
+ /**
192
+ * 确定搜索
193
+ */
194
+ function search(e: any) {
195
+ emit('search', e.detail.value);
196
+ try {
197
+ // #ifdef H5 || MP
198
+ uni.hideKeyboard();
199
+ // #endif
200
+ } catch (e) {}
201
+ }
202
+ /**
203
+ * 点击右边自定义按钮的事件
204
+ */
205
+ function custom() {
206
+ emit('custom', keyword.value);
207
+ try {
208
+ // #ifdef H5 || MP
209
+ uni.hideKeyboard();
210
+ // #endif
211
+ } catch (e) {}
212
+ }
213
+ /**
214
+ * 获取焦点
215
+ */
216
+ function getFocus() {
217
+ focused.value = true;
218
+ // 开启右侧搜索按钮展开的动画效果
219
+ if (props.animation && props.showAction) show.value = true;
220
+ emit('focus', keyword.value);
221
+ }
222
+ /**
223
+ * 失去焦点
224
+ */
225
+ function blur() {
226
+ // 最开始使用的是监听图标@touchstart事件,自从hx2.8.4后,此方法在微信小程序出错
227
+ // 这里改为监听点击事件,手点击清除图标时,同时也发生了@blur事件,导致图标消失而无法点击,这里做一个延时
228
+ setTimeout(() => {
229
+ focused.value = false;
230
+ }, 100);
231
+ show.value = false;
232
+ emit('blur', keyword.value);
233
+ }
234
+ /**
235
+ * 点击搜索框,只有disabled=true时才发出事件,因为禁止了输入,意味着是想跳转真正的搜索页
236
+ */
237
+ function clickHandler() {
238
+ if (props.disabled) emit('click');
239
+ }
240
+ </script>
241
+
242
+ <style lang="scss" scoped>
243
+ @import '../../libs/css/style.components.scss';
244
+
245
+ .u-search {
246
+ @include vue-flex;
247
+ align-items: center;
248
+ flex: 1;
249
+ }
250
+
251
+ .u-content {
252
+ @include vue-flex;
253
+ align-items: center;
254
+ padding: 0 18rpx;
255
+ flex: 1;
256
+ }
257
+
258
+ .u-clear-icon {
259
+ @include vue-flex;
260
+ align-items: center;
261
+ }
262
+
263
+ .u-input {
264
+ flex: 1;
265
+ font-size: 28rpx;
266
+ line-height: 1;
267
+ margin: 0 10rpx;
268
+ color: $u-tips-color;
269
+ }
270
+
271
+ .u-close-wrap {
272
+ width: 40rpx;
273
+ height: 100%;
274
+ @include vue-flex;
275
+ align-items: center;
276
+ justify-content: center;
277
+ border-radius: 50%;
278
+ }
279
+
280
+ .u-placeholder-class {
281
+ color: $u-tips-color;
282
+ }
283
+
284
+ .u-action {
285
+ font-size: 28rpx;
286
+ color: $u-main-color;
287
+ width: 0;
288
+ overflow: hidden;
289
+ transition: all 0.3s;
290
+ white-space: nowrap;
291
+ text-align: center;
292
+ }
293
+
294
+ .u-action-active {
295
+ width: 80rpx;
296
+ margin-left: 10rpx;
297
+ }
298
+ </style>