uview-pro 0.2.4 → 0.3.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 (249) hide show
  1. package/changelog.md +584 -545
  2. package/components/common/props.ts +22 -22
  3. package/components/u-action-sheet/types.ts +37 -35
  4. package/components/u-action-sheet/u-action-sheet.vue +178 -167
  5. package/components/u-alert-tips/types.ts +41 -39
  6. package/components/u-alert-tips/u-alert-tips.vue +238 -223
  7. package/components/u-avatar/types.ts +36 -34
  8. package/components/u-avatar/u-avatar.vue +217 -207
  9. package/components/u-avatar-cropper/types.ts +23 -23
  10. package/components/u-avatar-cropper/u-avatar-cropper.vue +297 -286
  11. package/components/u-avatar-cropper/weCropper.d.ts +62 -62
  12. package/components/u-avatar-cropper/weCropper.js +1281 -1281
  13. package/components/u-avatar-cropper/weCropper.ts +1276 -1276
  14. package/components/u-back-top/types.ts +36 -39
  15. package/components/u-back-top/u-back-top.vue +140 -128
  16. package/components/u-badge/types.ts +38 -36
  17. package/components/u-badge/u-badge.vue +183 -165
  18. package/components/u-button/types.ts +66 -66
  19. package/components/u-button/u-button.vue +579 -566
  20. package/components/u-calendar/types.ts +75 -75
  21. package/components/u-calendar/u-calendar.vue +793 -793
  22. package/components/u-car-keyboard/types.ts +14 -12
  23. package/components/u-car-keyboard/u-car-keyboard.vue +262 -253
  24. package/components/u-card/types.ts +61 -59
  25. package/components/u-card/u-card.vue +209 -194
  26. package/components/u-cell-group/types.ts +19 -17
  27. package/components/u-cell-group/u-cell-group.vue +60 -50
  28. package/components/u-cell-item/types.ts +56 -54
  29. package/components/u-cell-item/u-cell-item.vue +226 -213
  30. package/components/u-checkbox/types.ts +33 -31
  31. package/components/u-checkbox/u-checkbox.vue +282 -283
  32. package/components/u-checkbox-group/types.ts +34 -32
  33. package/components/u-checkbox-group/u-checkbox-group.vue +130 -130
  34. package/components/u-circle-progress/types.ts +54 -52
  35. package/components/u-circle-progress/u-circle-progress.vue +206 -191
  36. package/components/u-city-select/types.ts +22 -20
  37. package/components/u-city-select/u-city-select.vue +276 -264
  38. package/components/u-col/types.ts +32 -30
  39. package/components/u-col/u-col.vue +142 -123
  40. package/components/u-collapse/types.ts +33 -33
  41. package/components/u-collapse/u-collapse.vue +190 -186
  42. package/components/u-collapse-item/types.ts +27 -27
  43. package/components/u-collapse-item/u-collapse-item.vue +290 -285
  44. package/components/u-column-notice/types.ts +50 -48
  45. package/components/u-column-notice/u-column-notice.vue +222 -210
  46. package/components/u-count-down/types.ts +44 -42
  47. package/components/u-count-down/u-count-down.vue +286 -274
  48. package/components/u-count-to/types.ts +34 -32
  49. package/components/u-count-to/u-count-to.vue +266 -248
  50. package/components/u-divider/types.ts +33 -31
  51. package/components/u-divider/u-divider.vue +145 -129
  52. package/components/u-dropdown/types.ts +34 -32
  53. package/components/u-dropdown/u-dropdown.vue +330 -302
  54. package/components/u-dropdown-item/types.ts +29 -27
  55. package/components/u-dropdown-item/u-dropdown-item.vue +120 -128
  56. package/components/u-empty/types.ts +38 -36
  57. package/components/u-empty/u-empty.vue +103 -88
  58. package/components/u-field/types.ts +71 -69
  59. package/components/u-field/u-field.vue +388 -373
  60. package/components/u-form/types.ts +29 -27
  61. package/components/u-form/u-form.vue +130 -136
  62. package/components/u-form-item/types.ts +72 -70
  63. package/components/u-form-item/u-form-item.vue +447 -447
  64. package/components/u-full-screen/types.ts +16 -14
  65. package/components/u-full-screen/u-full-screen.vue +103 -89
  66. package/components/u-gap/types.ts +20 -18
  67. package/components/u-gap/u-gap.vue +50 -40
  68. package/components/u-grid/types.ts +21 -19
  69. package/components/u-grid/u-grid.vue +91 -93
  70. package/components/u-grid-item/types.ts +16 -16
  71. package/components/u-grid-item/u-grid-item.vue +130 -134
  72. package/components/u-icon/types.ts +61 -62
  73. package/components/u-icon/u-icon.vue +296 -294
  74. package/components/u-image/types.ts +51 -51
  75. package/components/u-image/u-image.vue +239 -230
  76. package/components/u-index-anchor/types.ts +16 -16
  77. package/components/u-index-anchor/u-index-anchor.vue +94 -86
  78. package/components/u-index-list/types.ts +43 -43
  79. package/components/u-index-list/u-index-list.vue +352 -355
  80. package/components/u-input/types.ts +137 -140
  81. package/components/u-input/u-input.vue +288 -279
  82. package/components/u-keyboard/types.ts +40 -40
  83. package/components/u-keyboard/u-keyboard.vue +178 -169
  84. package/components/u-lazy-load/types.ts +37 -37
  85. package/components/u-lazy-load/u-lazy-load.vue +246 -235
  86. package/components/u-line/types.ts +44 -44
  87. package/components/u-line/u-line.vue +68 -59
  88. package/components/u-line-progress/types.ts +58 -58
  89. package/components/u-line-progress/u-line-progress.vue +126 -117
  90. package/components/u-link/types.ts +43 -43
  91. package/components/u-link/u-link.vue +84 -75
  92. package/components/u-loading/types.ts +35 -35
  93. package/components/u-loading/u-loading.vue +105 -96
  94. package/components/u-loading-popup/types.ts +26 -26
  95. package/components/u-loading-popup/u-loading-popup.vue +253 -239
  96. package/components/u-loadmore/types.ts +79 -79
  97. package/components/u-loadmore/u-loadmore.vue +156 -145
  98. package/components/u-mask/types.ts +40 -43
  99. package/components/u-mask/u-mask.vue +113 -106
  100. package/components/u-message-input/types.ts +74 -74
  101. package/components/u-message-input/u-message-input.vue +281 -270
  102. package/components/u-modal/types.ts +118 -118
  103. package/components/u-modal/u-modal.vue +220 -211
  104. package/components/u-navbar/types.ts +103 -103
  105. package/components/u-navbar/u-navbar.vue +251 -240
  106. package/components/u-no-network/image.ts +2 -2
  107. package/components/u-no-network/types.ts +28 -28
  108. package/components/u-no-network/u-no-network.vue +303 -292
  109. package/components/u-notice-bar/types.ts +111 -111
  110. package/components/u-notice-bar/u-notice-bar.vue +189 -179
  111. package/components/u-number-box/types.ts +42 -42
  112. package/components/u-number-box/u-number-box.vue +321 -312
  113. package/components/u-number-keyboard/types.ts +26 -26
  114. package/components/u-number-keyboard/u-number-keyboard.vue +188 -179
  115. package/components/u-picker/types.ts +123 -123
  116. package/components/u-picker/u-picker.vue +685 -676
  117. package/components/u-popup/types.ts +59 -59
  118. package/components/u-popup/u-popup.vue +385 -375
  119. package/components/u-radio/types.ts +27 -25
  120. package/components/u-radio/u-radio.vue +279 -272
  121. package/components/u-radio-group/types.ts +31 -29
  122. package/components/u-radio-group/u-radio-group.vue +96 -108
  123. package/components/u-rate/types.ts +42 -40
  124. package/components/u-rate/u-rate.vue +249 -234
  125. package/components/u-read-more/types.ts +37 -35
  126. package/components/u-read-more/u-read-more.vue +172 -156
  127. package/components/u-root-portal/u-root-portal.vue +56 -54
  128. package/components/u-row/types.ts +22 -20
  129. package/components/u-row/u-row.vue +105 -87
  130. package/components/u-row-notice/types.ts +41 -39
  131. package/components/u-row-notice/u-row-notice.vue +256 -244
  132. package/components/u-safe-bottom/u-safe-bottom.vue +57 -46
  133. package/components/u-search/types.ts +55 -53
  134. package/components/u-search/u-search.vue +279 -268
  135. package/components/u-section/types.ts +34 -32
  136. package/components/u-section/u-section.vue +150 -131
  137. package/components/u-select/types.ts +45 -43
  138. package/components/u-select/u-select.vue +388 -378
  139. package/components/u-skeleton/types.ts +22 -20
  140. package/components/u-skeleton/u-skeleton.vue +231 -220
  141. package/components/u-slider/types.ts +34 -32
  142. package/components/u-slider/u-slider.vue +255 -247
  143. package/components/u-status-bar/u-status-bar.vue +74 -65
  144. package/components/u-steps/types.ts +30 -28
  145. package/components/u-steps/u-steps.vue +181 -169
  146. package/components/u-sticky/types.ts +24 -22
  147. package/components/u-sticky/u-sticky.vue +178 -162
  148. package/components/u-subsection/types.ts +38 -36
  149. package/components/u-subsection/u-subsection.vue +339 -328
  150. package/components/u-swipe-action/types.ts +52 -50
  151. package/components/u-swipe-action/u-swipe-action.vue +276 -260
  152. package/components/u-swiper/types.ts +49 -47
  153. package/components/u-swiper/u-swiper.vue +308 -291
  154. package/components/u-switch/types.ts +30 -28
  155. package/components/u-switch/u-switch.vue +150 -141
  156. package/components/u-tabbar/types.ts +38 -36
  157. package/components/u-tabbar/u-tabbar.vue +315 -298
  158. package/components/u-table/types.ts +27 -25
  159. package/components/u-table/u-table.vue +67 -55
  160. package/components/u-tabs/types.ts +53 -51
  161. package/components/u-tabs/u-tabs.vue +302 -291
  162. package/components/u-tabs-swiper/types.ts +55 -53
  163. package/components/u-tabs-swiper/u-tabs-swiper.vue +409 -397
  164. package/components/u-tag/types.ts +39 -37
  165. package/components/u-tag/u-tag.vue +268 -252
  166. package/components/u-td/types.ts +14 -12
  167. package/components/u-td/u-td.vue +98 -87
  168. package/components/u-text/types.ts +72 -72
  169. package/components/u-text/u-text.vue +343 -341
  170. package/components/u-th/types.ts +14 -12
  171. package/components/u-th/u-th.vue +92 -81
  172. package/components/u-time-line/u-time-line.vue +53 -39
  173. package/components/u-time-line-item/types.ts +16 -14
  174. package/components/u-time-line-item/u-time-line-item.vue +90 -78
  175. package/components/u-toast/types.ts +38 -36
  176. package/components/u-toast/u-toast.vue +240 -233
  177. package/components/u-top-tips/types.ts +16 -14
  178. package/components/u-top-tips/u-top-tips.vue +130 -113
  179. package/components/u-tr/types.ts +11 -8
  180. package/components/u-tr/u-tr.vue +39 -24
  181. package/components/u-upload/types.ts +82 -80
  182. package/components/u-upload/u-upload.vue +568 -559
  183. package/components/u-verification-code/types.ts +24 -22
  184. package/components/u-verification-code/u-verification-code.vue +176 -164
  185. package/components/u-waterfall/types.ts +18 -16
  186. package/components/u-waterfall/u-waterfall.vue +187 -175
  187. package/iconfont.css +913 -913
  188. package/index.scss +25 -25
  189. package/index.ts +38 -38
  190. package/libs/config/config.ts +26 -26
  191. package/libs/config/zIndex.ts +37 -37
  192. package/libs/css/color.scss +155 -155
  193. package/libs/css/common.scss +178 -178
  194. package/libs/css/style.components.scss +16 -16
  195. package/libs/css/style.h5.scss +8 -8
  196. package/libs/css/style.mp.scss +72 -72
  197. package/libs/css/style.nvue.scss +15 -15
  198. package/libs/css/style.vue.scss +188 -188
  199. package/libs/function/$parent.ts +24 -24
  200. package/libs/function/addUnit.ts +13 -13
  201. package/libs/function/color.ts +37 -37
  202. package/libs/function/colorGradient.ts +139 -139
  203. package/libs/function/debounce.ts +28 -28
  204. package/libs/function/deepClone.ts +39 -39
  205. package/libs/function/deepMerge.ts +35 -35
  206. package/libs/function/getParent.ts +63 -63
  207. package/libs/function/getRect.ts +26 -26
  208. package/libs/function/guid.ts +42 -42
  209. package/libs/function/md5.ts +403 -403
  210. package/libs/function/parent.ts +21 -21
  211. package/libs/function/queryParams.ts +64 -64
  212. package/libs/function/random.ts +16 -16
  213. package/libs/function/randomArray.ts +11 -11
  214. package/libs/function/route.ts +118 -118
  215. package/libs/function/styleUtils.ts +83 -83
  216. package/libs/function/sys.ts +15 -15
  217. package/libs/function/test.ts +289 -289
  218. package/libs/function/throttle.ts +31 -31
  219. package/libs/function/timeFormat.ts +55 -55
  220. package/libs/function/timeFrom.ts +48 -48
  221. package/libs/function/toast.ts +14 -14
  222. package/libs/function/trim.ts +21 -21
  223. package/libs/function/type2icon.ts +39 -39
  224. package/libs/hooks/index.ts +4 -3
  225. package/libs/hooks/useCompRelation.ts +364 -0
  226. package/libs/hooks/useComponent.ts +759 -759
  227. package/libs/hooks/useEmitter.ts +79 -79
  228. package/libs/hooks/useParent.ts +33 -33
  229. package/libs/hooks/useRect.ts +33 -33
  230. package/libs/index.ts +337 -320
  231. package/libs/request/auto-http.ts +76 -76
  232. package/libs/request/index.ts +242 -242
  233. package/libs/store/index.ts +88 -88
  234. package/libs/util/async-validator.d.ts +75 -75
  235. package/libs/util/async-validator.js +1 -1
  236. package/libs/util/calendar.d.ts +57 -57
  237. package/libs/util/emitter.ts +112 -112
  238. package/libs/util/eventBus.ts +86 -86
  239. package/libs/util/logger.ts +364 -364
  240. package/libs/util/mitt.ts +118 -118
  241. package/libs/util/parent.ts +20 -20
  242. package/package.json +4 -4
  243. package/readme.md +241 -241
  244. package/theme.scss +38 -38
  245. package/types/components.d.ts +97 -97
  246. package/types/global.d.ts +331 -331
  247. package/types/ignore-errors.d.ts +30 -30
  248. package/types/index.d.ts +19 -19
  249. package/types/uni-app.d.ts +63 -63
@@ -1,378 +1,388 @@
1
- <template>
2
- <view class="u-select">
3
- <!-- <view class="u-select__action" :class="{
4
- 'u-select--border': border
5
- }" @tap.stop="selectHandler">
6
- <view class="u-select__action__icon" :class="{
7
- 'u-select__action__icon--reverse': value == true
8
- }">
9
- <u-icon name="arrow-down-fill" size="26" color="#c0c4cc"></u-icon>
10
- </view>
11
- </view> -->
12
- <u-popup
13
- :maskCloseAble="maskCloseAble"
14
- mode="bottom"
15
- :popup="false"
16
- v-model="popupValue"
17
- length="auto"
18
- :safeAreaInsetBottom="safeAreaInsetBottom"
19
- @close="close"
20
- :z-index="uZIndex"
21
- >
22
- <view class="u-select">
23
- <view class="u-select__header" @touchmove.stop.prevent="">
24
- <view
25
- class="u-select__header__cancel u-select__header__btn"
26
- :style="{ color: cancelColor }"
27
- hover-class="u-hover-class"
28
- :hover-stay-time="150"
29
- @tap="getResult('cancel')"
30
- >
31
- {{ cancelText }}
32
- </view>
33
- <view class="u-select__header__title">
34
- {{ title }}
35
- </view>
36
- <view
37
- class="u-select__header__confirm u-select__header__btn"
38
- :style="{ color: moving ? cancelColor : confirmColor }"
39
- hover-class="u-hover-class"
40
- :hover-stay-time="150"
41
- @touchmove.stop=""
42
- @tap.stop="getResult('confirm')"
43
- >
44
- {{ confirmText }}
45
- </view>
46
- </view>
47
- <view class="u-select__body">
48
- <picker-view
49
- @change="columnChange"
50
- class="u-select__body__picker-view"
51
- :value="defaultSelector"
52
- @pickstart="pickstart"
53
- @pickend="pickend"
54
- v-if="modelValue"
55
- >
56
- <picker-view-column v-for="(item, index) in columnData" :key="index">
57
- <view
58
- class="u-select__body__picker-view__item"
59
- v-for="(item1, index1) in item"
60
- :key="index1"
61
- >
62
- <view class="u-line-1">{{ item1[labelName] }}</view>
63
- </view>
64
- </picker-view-column>
65
- </picker-view>
66
- </view>
67
- </view>
68
- </u-popup>
69
- </view>
70
- </template>
71
-
72
- <script setup lang="ts">
73
- import { ref, computed, watch } from 'vue';
74
- import { SelectProps } from './types';
75
- import type { SelectListItem } from '../../types/global';
76
-
77
- defineOptions({
78
- name: 'u-select'
79
- });
80
-
81
- /**
82
- * select 列选择器
83
- * @description 此选择器用于单列,多列,多列联动的选择场景。(从1.3.0版本起,不建议使用Picker组件的单列和多列模式,Select组件是专门为列选择而构造的组件,更简单易用。)
84
- * @tutorial https://uviewpro.cn/zh/components/select.html
85
- * @property {String} mode 模式选择,"single-column"-单列模式,"mutil-column"-多列模式,"mutil-column-auto"-多列联动模式
86
- * @property {Array} list 列数据,数组形式,见官网说明
87
- * @property {Boolean} v-model 布尔值变量,用于控制选择器的弹出与收起
88
- * @property {Boolean} safe-area-inset-bottom 是否开启底部安全区适配(默认false)
89
- * @property {String} cancel-color 取消按钮的颜色(默认#606266)
90
- * @property {String} confirm-color 确认按钮的颜色(默认#2979ff)
91
- * @property {String} confirm-text 确认按钮的文字
92
- * @property {String} cancel-text 取消按钮的文字
93
- * @property {String} default-value 提供的默认选中的下标,见官网说明
94
- * @property {Boolean} mask-close-able 是否允许通过点击遮罩关闭Picker(默认true)
95
- * @property {String Number} z-index 弹出时的z-index值(默认10075)
96
- * @property {String} value-name 自定义list数据的value属性名 1.3.6
97
- * @property {String} label-name 自定义list数据的label属性名 1.3.6
98
- * @property {String} child-name 自定义list数据的children属性名,只对多列联动模式有效 1.3.7
99
- * @event {Function} confirm 点击确定按钮,返回当前选择的值
100
- * @example <u-select v-model="show" :list="list"></u-select>
101
- */
102
-
103
- const props = defineProps(SelectProps);
104
- const emit = defineEmits(['update:modelValue', 'confirm', 'cancel', 'click']);
105
- // 用于列改变时,保存当前的索引,下一次变化时比较得出是哪一列发生了变化
106
-
107
- const defaultSelector = ref<number[]>([0]);
108
- // picker-view的数据
109
- const columnData = ref<SelectListItem[][]>([]);
110
- // 每次队列发生变化时,保存选择的结果
111
- const selectValue = ref<SelectListItem[]>([]);
112
- // 上一次列变化时的index
113
- const lastSelectIndex = ref<number[]>([]);
114
- // 列数
115
- const columnNum = ref(0);
116
- // 列是否还在滑动中,微信小程序如果在滑动中就点确定,结果可能不准确
117
- const moving = ref(false);
118
- // 如果用户有传递z-index值,优先使用
119
- const uZIndex = computed(() => (props.zIndex ? props.zIndex : 10075));
120
-
121
- const popupValue = computed({
122
- get: () => props.modelValue,
123
- set: (val: boolean) => emit('update:modelValue', val)
124
- });
125
-
126
- watch(
127
- () => props.modelValue,
128
- val => {
129
- if (val) setTimeout(() => init(), 10);
130
- },
131
- { immediate: true }
132
- );
133
- // 标识滑动开始,只有微信小程序才有这样的事件
134
- function pickstart() {
135
- // #ifdef MP-WEIXIN
136
- moving.value = true;
137
- // #endif
138
- }
139
- function pickend() {
140
- // #ifdef MP-WEIXIN
141
- moving.value = false;
142
- // #endif
143
- }
144
-
145
- function init() {
146
- setColumnNum();
147
- setDefaultSelector();
148
- setColumnData();
149
- setSelectValue();
150
- }
151
-
152
- // 获取默认选中列下标
153
- function setDefaultSelector() {
154
- // 如果没有传入默认选中的值,生成长度为columnNum,用0填充的数组
155
- defaultSelector.value =
156
- props.defaultValue.length == columnNum.value ? props.defaultValue : Array(columnNum.value).fill(0);
157
- lastSelectIndex.value = [...defaultSelector.value];
158
- }
159
- // 计算列数
160
- function setColumnNum() {
161
- // 单列的列数为1
162
- if (props.mode == 'single-column') columnNum.value = 1;
163
- // 多列时,this.list数组长度就是列数
164
- else if (props.mode == 'mutil-column') columnNum.value = props.list.length;
165
- // 多列联动时,通过历遍this.list的第一个元素,得出有多少列
166
- else if (props.mode == 'mutil-column-auto') {
167
- let num = 1;
168
- let column: any = props.list;
169
- // 只要有元素并且第一个元素有children属性,继续遍历
170
- while (Array.isArray(column) && column[0] && typeof column[0] === 'object' && props.childName in column[0]) {
171
- column = column[0][props.childName];
172
- num++;
173
- }
174
- columnNum.value = num;
175
- }
176
- }
177
-
178
- // 获取需要展示在picker中的列数据
179
- function setColumnData() {
180
- let data: SelectListItem[][] = [];
181
- selectValue.value = [];
182
- if (props.mode == 'mutil-column-auto') {
183
- // 获得所有数据中的第一个元素
184
- let column: any = props.list[defaultSelector.value.length ? defaultSelector.value[0] : 0];
185
- // 通过循环所有的列数,再根据设定列的数组,得出当前需要渲染的整个列数组
186
- for (let i = 0; i < columnNum.value; i++) {
187
- // 第一列默认为整个list数组
188
- if (i == 0) {
189
- data[i] = is2DList(props.list) ? props.list[i] || [] : (props.list as SelectListItem[]);
190
- column = column && typeof column === 'object' ? column[props.childName] : [];
191
- } else {
192
- // 大于第一列时,判断是否有默认选中的,如果没有就用该列的第一项
193
- data[i] = Array.isArray(column) ? column : [];
194
- column =
195
- Array.isArray(column) &&
196
- column[defaultSelector.value[i]] &&
197
- typeof column[defaultSelector.value[i]] === 'object'
198
- ? column[defaultSelector.value[i]][props.childName]
199
- : [];
200
- }
201
- }
202
- } else if (props.mode == 'single-column') {
203
- data[0] = Array.isArray(props.list) && !is2DList(props.list) ? (props.list as SelectListItem[]) : [];
204
- } else if (props.mode == 'mutil-column') {
205
- data = is2DList(props.list) ? props.list : [props.list as SelectListItem[]];
206
- }
207
- columnData.value = data;
208
- }
209
-
210
- // 获取默认选中的值,如果没有设置defaultValue,就默认选中每列的第一个
211
- function setSelectValue() {
212
- for (let i = 0; i < columnNum.value; i++) {
213
- const tmp = columnData.value[i][defaultSelector.value[i]];
214
- let data: any = {
215
- value: tmp ? tmp[props.valueName] : null,
216
- label: tmp ? tmp[props.labelName] : null
217
- };
218
-
219
- // 判断是否存在额外的参数,如果存在,就返回
220
- if (tmp && tmp.extra !== undefined) data.extra = tmp.extra;
221
- selectValue.value.push(data);
222
- }
223
- }
224
-
225
- // 列选项
226
- function columnChange(e: any) {
227
- let index: number = -1;
228
- const columnIndex = e.detail.value;
229
- // 由于后面是需要push进数组的,所以需要先清空数组
230
- selectValue.value = [];
231
- defaultSelector.value = columnIndex;
232
- if (props.mode == 'mutil-column-auto') {
233
- // 对比前后两个数组,寻找变更的是哪一列,如果某一个元素不同,即可判定该列发生了变化
234
- lastSelectIndex.value.map((val, idx) => {
235
- if (val != columnIndex[idx]) index = idx;
236
- });
237
- for (let i = index + 1; i < columnNum.value; i++) {
238
- // 当前变化列的下一列的数据,需要获取上一列的数据,同时需要指定是上一列的第几个的children,再往后的
239
- // 默认是队列的第一个为默认选项
240
- const prevCol = columnData.value[i - 1];
241
- const prevIdx = i - 1 == index ? columnIndex[index] : 0;
242
- columnData.value[i] =
243
- Array.isArray(prevCol) && prevCol[prevIdx] && typeof prevCol[prevIdx] === 'object'
244
- ? prevCol[prevIdx][props.childName]
245
- : [];
246
- // 改变的列之后的所有列,默认选中第一个
247
- defaultSelector.value[i] = 0;
248
- }
249
- // 在历遍的过程中,可能由于上一步修改this.columnData,导致产生连锁反应,程序触发columnChange,会有多次调用
250
- // 只有在最后一次数据稳定后的结果是正确的,此前的历遍中,可能会产生undefined,故需要判断
251
- columnIndex.map((item: any, idx: number) => {
252
- let data = columnData.value[idx][columnIndex[idx]];
253
- let tmp: any = {
254
- value: data ? data[props.valueName] : null,
255
- label: data ? data[props.labelName] : null
256
- };
257
- // 判断是否有需要额外携带的参数
258
- if (data && data.extra !== undefined) tmp.extra = data.extra;
259
- selectValue.value.push(tmp);
260
- });
261
- // 保存这一次的结果,用于下次列发生变化时作比较
262
- lastSelectIndex.value = [...columnIndex];
263
- } else if (props.mode == 'single-column') {
264
- let data = columnData.value[0][columnIndex[0]];
265
- // 初始默认选中值
266
- let tmp: any = {
267
- value: data ? data[props.valueName] : null,
268
- label: data ? data[props.labelName] : null
269
- };
270
- // 判断是否有需要额外携带的参数
271
- if (data && data.extra !== undefined) tmp.extra = data.extra;
272
- selectValue.value.push(tmp);
273
- } else if (props.mode == 'mutil-column') {
274
- // 初始默认选中值
275
- columnIndex.map((item: any, idx: number) => {
276
- let data = columnData.value[idx][columnIndex[idx]];
277
- // 初始默认选中值
278
- let tmp: any = {
279
- value: data ? data[props.valueName] : null,
280
- label: data ? data[props.labelName] : null
281
- };
282
- // 判断是否有需要额外携带的参数
283
- if (data && data.extra !== undefined) tmp.extra = data.extra;
284
- selectValue.value.push(tmp);
285
- });
286
- }
287
- }
288
-
289
- function close() {
290
- emit('update:modelValue', false);
291
- // 重置default-value默认值
292
- defaultSelector.value = [0];
293
- }
294
- // 点击确定或者取消
295
- function getResult(event: 'update:modelValue' | 'confirm' | 'cancel' | 'click' | null = null) {
296
- // #ifdef MP-WEIXIN
297
- if (moving.value) return;
298
- // #endif
299
-
300
- if (event) emit(event, selectValue.value);
301
- close();
302
- }
303
-
304
- function selectHandler() {
305
- emit('click');
306
- }
307
-
308
- // 类型守卫提前声明
309
- function is2DList(list: SelectListItem[] | SelectListItem[][]): list is SelectListItem[][] {
310
- return Array.isArray(list) && list.length > 0 && Array.isArray(list[0]);
311
- }
312
- </script>
313
-
314
- <style scoped lang="scss">
315
- @import '../../libs/css/style.components.scss';
316
-
317
- .u-select {
318
- &__action {
319
- position: relative;
320
- line-height: $u-form-item-height;
321
- height: $u-form-item-height;
322
-
323
- &__icon {
324
- position: absolute;
325
- right: 20rpx;
326
- top: 50%;
327
- transition: transform 0.4s;
328
- transform: translateY(-50%);
329
- z-index: 1;
330
-
331
- &--reverse {
332
- transform: rotate(-180deg) translateY(50%);
333
- }
334
- }
335
- }
336
-
337
- &__hader {
338
- &__title {
339
- color: $u-content-color;
340
- }
341
- }
342
-
343
- &--border {
344
- border-radius: 6rpx;
345
- border-radius: 4px;
346
- border: 1px solid $u-form-item-border-color;
347
- }
348
-
349
- &__header {
350
- @include vue-flex;
351
- align-items: center;
352
- justify-content: space-between;
353
- height: 80rpx;
354
- padding: 0 40rpx;
355
- }
356
-
357
- &__body {
358
- width: 100%;
359
- height: 500rpx;
360
- overflow: hidden;
361
- background-color: #fff;
362
-
363
- &__picker-view {
364
- height: 100%;
365
- box-sizing: border-box;
366
-
367
- &__item {
368
- @include vue-flex;
369
- align-items: center;
370
- justify-content: center;
371
- font-size: 32rpx;
372
- color: $u-main-color;
373
- padding: 0 8rpx;
374
- }
375
- }
376
- }
377
- }
378
- </style>
1
+ <template>
2
+ <view class="u-select" :class="customClass" :style="$u.toStyle(customStyle)">
3
+ <!-- <view class="u-select__action" :class="{
4
+ 'u-select--border': border
5
+ }" @tap.stop="selectHandler">
6
+ <view class="u-select__action__icon" :class="{
7
+ 'u-select__action__icon--reverse': value == true
8
+ }">
9
+ <u-icon name="arrow-down-fill" size="26" color="#c0c4cc"></u-icon>
10
+ </view>
11
+ </view> -->
12
+ <u-popup
13
+ :maskCloseAble="maskCloseAble"
14
+ mode="bottom"
15
+ :popup="false"
16
+ v-model="popupValue"
17
+ length="auto"
18
+ :safeAreaInsetBottom="safeAreaInsetBottom"
19
+ @close="close"
20
+ :z-index="uZIndex"
21
+ >
22
+ <view class="u-select">
23
+ <view class="u-select__header" @touchmove.stop.prevent="">
24
+ <view
25
+ class="u-select__header__cancel u-select__header__btn"
26
+ :style="{ color: cancelColor }"
27
+ hover-class="u-hover-class"
28
+ :hover-stay-time="150"
29
+ @tap="getResult('cancel')"
30
+ >
31
+ {{ cancelText }}
32
+ </view>
33
+ <view class="u-select__header__title">
34
+ {{ title }}
35
+ </view>
36
+ <view
37
+ class="u-select__header__confirm u-select__header__btn"
38
+ :style="{ color: moving ? cancelColor : confirmColor }"
39
+ hover-class="u-hover-class"
40
+ :hover-stay-time="150"
41
+ @touchmove.stop=""
42
+ @tap.stop="getResult('confirm')"
43
+ >
44
+ {{ confirmText }}
45
+ </view>
46
+ </view>
47
+ <view class="u-select__body">
48
+ <picker-view
49
+ @change="columnChange"
50
+ class="u-select__body__picker-view"
51
+ :value="defaultSelector"
52
+ @pickstart="pickstart"
53
+ @pickend="pickend"
54
+ v-if="modelValue"
55
+ >
56
+ <picker-view-column v-for="(item, index) in columnData" :key="index">
57
+ <view
58
+ class="u-select__body__picker-view__item"
59
+ v-for="(item1, index1) in item"
60
+ :key="index1"
61
+ >
62
+ <view class="u-line-1">{{ item1[labelName] }}</view>
63
+ </view>
64
+ </picker-view-column>
65
+ </picker-view>
66
+ </view>
67
+ </view>
68
+ </u-popup>
69
+ </view>
70
+ </template>
71
+
72
+ <script lang="ts">
73
+ export default {
74
+ name: 'u-select',
75
+ options: {
76
+ addGlobalClass: true,
77
+ // #ifndef MP-TOUTIAO
78
+ virtualHost: true,
79
+ // #endif
80
+ styleIsolation: 'shared'
81
+ }
82
+ };
83
+ </script>
84
+
85
+ <script setup lang="ts">
86
+ import { ref, computed, watch } from 'vue';
87
+ import { SelectProps } from './types';
88
+ import type { SelectListItem } from '../../types/global';
89
+ import { $u } from '../..';
90
+
91
+ /**
92
+ * select 列选择器
93
+ * @description 此选择器用于单列,多列,多列联动的选择场景。(从1.3.0版本起,不建议使用Picker组件的单列和多列模式,Select组件是专门为列选择而构造的组件,更简单易用。)
94
+ * @tutorial https://uviewpro.cn/zh/components/select.html
95
+ * @property {String} mode 模式选择,"single-column"-单列模式,"mutil-column"-多列模式,"mutil-column-auto"-多列联动模式
96
+ * @property {Array} list 列数据,数组形式,见官网说明
97
+ * @property {Boolean} v-model 布尔值变量,用于控制选择器的弹出与收起
98
+ * @property {Boolean} safe-area-inset-bottom 是否开启底部安全区适配(默认false)
99
+ * @property {String} cancel-color 取消按钮的颜色(默认#606266)
100
+ * @property {String} confirm-color 确认按钮的颜色(默认#2979ff)
101
+ * @property {String} confirm-text 确认按钮的文字
102
+ * @property {String} cancel-text 取消按钮的文字
103
+ * @property {String} default-value 提供的默认选中的下标,见官网说明
104
+ * @property {Boolean} mask-close-able 是否允许通过点击遮罩关闭Picker(默认true)
105
+ * @property {String Number} z-index 弹出时的z-index值(默认10075)
106
+ * @property {String} value-name 自定义list数据的value属性名 1.3.6
107
+ * @property {String} label-name 自定义list数据的label属性名 1.3.6
108
+ * @property {String} child-name 自定义list数据的children属性名,只对多列联动模式有效 1.3.7
109
+ * @event {Function} confirm 点击确定按钮,返回当前选择的值
110
+ * @example <u-select v-model="show" :list="list"></u-select>
111
+ */
112
+
113
+ const props = defineProps(SelectProps);
114
+ const emit = defineEmits(['update:modelValue', 'confirm', 'cancel', 'click']);
115
+ // 用于列改变时,保存当前的索引,下一次变化时比较得出是哪一列发生了变化
116
+
117
+ const defaultSelector = ref<number[]>([0]);
118
+ // picker-view的数据
119
+ const columnData = ref<SelectListItem[][]>([]);
120
+ // 每次队列发生变化时,保存选择的结果
121
+ const selectValue = ref<SelectListItem[]>([]);
122
+ // 上一次列变化时的index
123
+ const lastSelectIndex = ref<number[]>([]);
124
+ // 列数
125
+ const columnNum = ref(0);
126
+ // 列是否还在滑动中,微信小程序如果在滑动中就点确定,结果可能不准确
127
+ const moving = ref(false);
128
+ // 如果用户有传递z-index值,优先使用
129
+ const uZIndex = computed(() => (props.zIndex ? props.zIndex : 10075));
130
+
131
+ const popupValue = computed({
132
+ get: () => props.modelValue,
133
+ set: (val: boolean) => emit('update:modelValue', val)
134
+ });
135
+
136
+ watch(
137
+ () => props.modelValue,
138
+ val => {
139
+ if (val) setTimeout(() => init(), 10);
140
+ },
141
+ { immediate: true }
142
+ );
143
+ // 标识滑动开始,只有微信小程序才有这样的事件
144
+ function pickstart() {
145
+ // #ifdef MP-WEIXIN
146
+ moving.value = true;
147
+ // #endif
148
+ }
149
+ function pickend() {
150
+ // #ifdef MP-WEIXIN
151
+ moving.value = false;
152
+ // #endif
153
+ }
154
+
155
+ function init() {
156
+ setColumnNum();
157
+ setDefaultSelector();
158
+ setColumnData();
159
+ setSelectValue();
160
+ }
161
+
162
+ // 获取默认选中列下标
163
+ function setDefaultSelector() {
164
+ // 如果没有传入默认选中的值,生成长度为columnNum,用0填充的数组
165
+ defaultSelector.value =
166
+ props.defaultValue.length == columnNum.value ? props.defaultValue : Array(columnNum.value).fill(0);
167
+ lastSelectIndex.value = [...defaultSelector.value];
168
+ }
169
+ // 计算列数
170
+ function setColumnNum() {
171
+ // 单列的列数为1
172
+ if (props.mode == 'single-column') columnNum.value = 1;
173
+ // 多列时,this.list数组长度就是列数
174
+ else if (props.mode == 'mutil-column') columnNum.value = props.list.length;
175
+ // 多列联动时,通过历遍this.list的第一个元素,得出有多少列
176
+ else if (props.mode == 'mutil-column-auto') {
177
+ let num = 1;
178
+ let column: any = props.list;
179
+ // 只要有元素并且第一个元素有children属性,继续遍历
180
+ while (Array.isArray(column) && column[0] && typeof column[0] === 'object' && props.childName in column[0]) {
181
+ column = column[0][props.childName];
182
+ num++;
183
+ }
184
+ columnNum.value = num;
185
+ }
186
+ }
187
+
188
+ // 获取需要展示在picker中的列数据
189
+ function setColumnData() {
190
+ let data: SelectListItem[][] = [];
191
+ selectValue.value = [];
192
+ if (props.mode == 'mutil-column-auto') {
193
+ // 获得所有数据中的第一个元素
194
+ let column: any = props.list[defaultSelector.value.length ? defaultSelector.value[0] : 0];
195
+ // 通过循环所有的列数,再根据设定列的数组,得出当前需要渲染的整个列数组
196
+ for (let i = 0; i < columnNum.value; i++) {
197
+ // 第一列默认为整个list数组
198
+ if (i == 0) {
199
+ data[i] = is2DList(props.list) ? props.list[i] || [] : (props.list as SelectListItem[]);
200
+ column = column && typeof column === 'object' ? column[props.childName] : [];
201
+ } else {
202
+ // 大于第一列时,判断是否有默认选中的,如果没有就用该列的第一项
203
+ data[i] = Array.isArray(column) ? column : [];
204
+ column =
205
+ Array.isArray(column) &&
206
+ column[defaultSelector.value[i]] &&
207
+ typeof column[defaultSelector.value[i]] === 'object'
208
+ ? column[defaultSelector.value[i]][props.childName]
209
+ : [];
210
+ }
211
+ }
212
+ } else if (props.mode == 'single-column') {
213
+ data[0] = Array.isArray(props.list) && !is2DList(props.list) ? (props.list as SelectListItem[]) : [];
214
+ } else if (props.mode == 'mutil-column') {
215
+ data = is2DList(props.list) ? props.list : [props.list as SelectListItem[]];
216
+ }
217
+ columnData.value = data;
218
+ }
219
+
220
+ // 获取默认选中的值,如果没有设置defaultValue,就默认选中每列的第一个
221
+ function setSelectValue() {
222
+ for (let i = 0; i < columnNum.value; i++) {
223
+ const tmp = columnData.value[i][defaultSelector.value[i]];
224
+ let data: any = {
225
+ value: tmp ? tmp[props.valueName] : null,
226
+ label: tmp ? tmp[props.labelName] : null
227
+ };
228
+
229
+ // 判断是否存在额外的参数,如果存在,就返回
230
+ if (tmp && tmp.extra !== undefined) data.extra = tmp.extra;
231
+ selectValue.value.push(data);
232
+ }
233
+ }
234
+
235
+ // 列选项
236
+ function columnChange(e: any) {
237
+ let index: number = -1;
238
+ const columnIndex = e.detail.value;
239
+ // 由于后面是需要push进数组的,所以需要先清空数组
240
+ selectValue.value = [];
241
+ defaultSelector.value = columnIndex;
242
+ if (props.mode == 'mutil-column-auto') {
243
+ // 对比前后两个数组,寻找变更的是哪一列,如果某一个元素不同,即可判定该列发生了变化
244
+ lastSelectIndex.value.map((val, idx) => {
245
+ if (val != columnIndex[idx]) index = idx;
246
+ });
247
+ for (let i = index + 1; i < columnNum.value; i++) {
248
+ // 当前变化列的下一列的数据,需要获取上一列的数据,同时需要指定是上一列的第几个的children,再往后的
249
+ // 默认是队列的第一个为默认选项
250
+ const prevCol = columnData.value[i - 1];
251
+ const prevIdx = i - 1 == index ? columnIndex[index] : 0;
252
+ columnData.value[i] =
253
+ Array.isArray(prevCol) && prevCol[prevIdx] && typeof prevCol[prevIdx] === 'object'
254
+ ? prevCol[prevIdx][props.childName]
255
+ : [];
256
+ // 改变的列之后的所有列,默认选中第一个
257
+ defaultSelector.value[i] = 0;
258
+ }
259
+ // 在历遍的过程中,可能由于上一步修改this.columnData,导致产生连锁反应,程序触发columnChange,会有多次调用
260
+ // 只有在最后一次数据稳定后的结果是正确的,此前的历遍中,可能会产生undefined,故需要判断
261
+ columnIndex.map((item: any, idx: number) => {
262
+ let data = columnData.value[idx][columnIndex[idx]];
263
+ let tmp: any = {
264
+ value: data ? data[props.valueName] : null,
265
+ label: data ? data[props.labelName] : null
266
+ };
267
+ // 判断是否有需要额外携带的参数
268
+ if (data && data.extra !== undefined) tmp.extra = data.extra;
269
+ selectValue.value.push(tmp);
270
+ });
271
+ // 保存这一次的结果,用于下次列发生变化时作比较
272
+ lastSelectIndex.value = [...columnIndex];
273
+ } else if (props.mode == 'single-column') {
274
+ let data = columnData.value[0][columnIndex[0]];
275
+ // 初始默认选中值
276
+ let tmp: any = {
277
+ value: data ? data[props.valueName] : null,
278
+ label: data ? data[props.labelName] : null
279
+ };
280
+ // 判断是否有需要额外携带的参数
281
+ if (data && data.extra !== undefined) tmp.extra = data.extra;
282
+ selectValue.value.push(tmp);
283
+ } else if (props.mode == 'mutil-column') {
284
+ // 初始默认选中值
285
+ columnIndex.map((item: any, idx: number) => {
286
+ let data = columnData.value[idx][columnIndex[idx]];
287
+ // 初始默认选中值
288
+ let tmp: any = {
289
+ value: data ? data[props.valueName] : null,
290
+ label: data ? data[props.labelName] : null
291
+ };
292
+ // 判断是否有需要额外携带的参数
293
+ if (data && data.extra !== undefined) tmp.extra = data.extra;
294
+ selectValue.value.push(tmp);
295
+ });
296
+ }
297
+ }
298
+
299
+ function close() {
300
+ emit('update:modelValue', false);
301
+ // 重置default-value默认值
302
+ defaultSelector.value = [0];
303
+ }
304
+ // 点击确定或者取消
305
+ function getResult(event: 'update:modelValue' | 'confirm' | 'cancel' | 'click' | null = null) {
306
+ // #ifdef MP-WEIXIN
307
+ if (moving.value) return;
308
+ // #endif
309
+
310
+ if (event) emit(event, selectValue.value);
311
+ close();
312
+ }
313
+
314
+ function selectHandler() {
315
+ emit('click');
316
+ }
317
+
318
+ // 类型守卫提前声明
319
+ function is2DList(list: SelectListItem[] | SelectListItem[][]): list is SelectListItem[][] {
320
+ return Array.isArray(list) && list.length > 0 && Array.isArray(list[0]);
321
+ }
322
+ </script>
323
+
324
+ <style scoped lang="scss">
325
+ @import '../../libs/css/style.components.scss';
326
+
327
+ .u-select {
328
+ &__action {
329
+ position: relative;
330
+ line-height: $u-form-item-height;
331
+ height: $u-form-item-height;
332
+
333
+ &__icon {
334
+ position: absolute;
335
+ right: 20rpx;
336
+ top: 50%;
337
+ transition: transform 0.4s;
338
+ transform: translateY(-50%);
339
+ z-index: 1;
340
+
341
+ &--reverse {
342
+ transform: rotate(-180deg) translateY(50%);
343
+ }
344
+ }
345
+ }
346
+
347
+ &__hader {
348
+ &__title {
349
+ color: $u-content-color;
350
+ }
351
+ }
352
+
353
+ &--border {
354
+ border-radius: 6rpx;
355
+ border-radius: 4px;
356
+ border: 1px solid $u-form-item-border-color;
357
+ }
358
+
359
+ &__header {
360
+ @include vue-flex;
361
+ align-items: center;
362
+ justify-content: space-between;
363
+ height: 80rpx;
364
+ padding: 0 40rpx;
365
+ }
366
+
367
+ &__body {
368
+ width: 100%;
369
+ height: 500rpx;
370
+ overflow: hidden;
371
+ background-color: #fff;
372
+
373
+ &__picker-view {
374
+ height: 100%;
375
+ box-sizing: border-box;
376
+
377
+ &__item {
378
+ @include vue-flex;
379
+ align-items: center;
380
+ justify-content: center;
381
+ font-size: 32rpx;
382
+ color: $u-main-color;
383
+ padding: 0 8rpx;
384
+ }
385
+ }
386
+ }
387
+ }
388
+ </style>