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,218 @@
1
+ <template>
2
+ <view class="u-image" @tap="onClick" :style="[wrapStyle, backgroundStyle]">
3
+ <image
4
+ v-if="!isError"
5
+ :src="src"
6
+ :mode="mode"
7
+ @error="onErrorHandler"
8
+ @load="onLoadHandler"
9
+ :lazy-load="lazyLoad"
10
+ class="u-image__image"
11
+ :show-menu-by-longpress="showMenuByLongpress"
12
+ :style="{ borderRadius: shape === 'circle' ? '50%' : $u.addUnit(borderRadius) }"
13
+ ></image>
14
+ <view v-if="showLoading && loading" class="u-image__loading" :style="{ borderRadius: shape === 'circle' ? '50%' : $u.addUnit(borderRadius), backgroundColor: bgColor }">
15
+ <slot v-if="$slots.loading" name="loading" />
16
+ <u-icon v-else :name="loadingIcon" :width="width" :height="height"></u-icon>
17
+ </view>
18
+ <view v-if="showError && isError && !loading" class="u-image__error" :style="{ borderRadius: shape === 'circle' ? '50%' : $u.addUnit(borderRadius) }">
19
+ <slot v-if="$slots.error" name="error" />
20
+ <u-icon v-else :name="errorIcon" :width="width" :height="height"></u-icon>
21
+ </view>
22
+ </view>
23
+ </template>
24
+
25
+ <script setup lang="ts">
26
+ import { ref, computed, watch, useSlots } from 'vue';
27
+ import { type ImageExpose, ImageProps } from './types';
28
+ import { $u } from '../..';
29
+
30
+ defineOptions({
31
+ name: 'u-image'
32
+ });
33
+
34
+ /**
35
+ * Image 图片
36
+ * @description 此组件为uni-app的image组件的加强版,在继承了原有功能外,还支持淡入动画、加载中、加载失败提示、圆角值和形状等。
37
+ * @tutorial https://uviewui.com/components/image.html
38
+ * @property {String} src 图片地址
39
+ * @property {String} mode 裁剪模式,见官网说明
40
+ * @property {String | Number} width 宽度,单位任意,如果为数值,则为rpx单位(默认100%)
41
+ * @property {String | Number} height 高度,单位任意,如果为数值,则为rpx单位(默认 auto)
42
+ * @property {String} shape 图片形状,circle-圆形,square-方形(默认square)
43
+ * @property {String | Number} border-radius 圆角值,单位任意,如果为数值,则为rpx单位(默认 0)
44
+ * @property {Boolean} lazy-load 是否懒加载,仅微信小程序、App、百度小程序、字节跳动小程序有效(默认 true)
45
+ * @property {Boolean} show-menu-by-longpress 是否开启长按图片显示识别小程序码菜单,仅微信小程序有效(默认 false)
46
+ * @property {String} loading-icon 加载中的图标,或者小图片(默认 photo)
47
+ * @property {String} error-icon 加载失败的图标,或者小图片(默认 error-circle)
48
+ * @property {Boolean} show-loading 是否显示加载中的图标或者自定义的slot(默认 true)
49
+ * @property {Boolean} show-error 是否显示加载错误的图标或者自定义的slot(默认 true)
50
+ * @property {Boolean} fade 是否需要淡入效果(默认 true)
51
+ * @property {Boolean} webp 只支持网络资源,只对微信小程序有效(默认 false)
52
+ * @property {String | Number} duration 搭配fade参数的过渡时间,单位ms(默认 500)
53
+ * @property {String} bg-color 背景颜色,用于深色页面加载图片时,为了和背景色融合(默认 #f3f4f6)
54
+ * @event {Function} click 点击图片时触发
55
+ * @event {Function} error 图片加载失败时触发
56
+ * @event {Function} load 图片加载成功时触发
57
+ * @example <u-image width="100%" height="300rpx" :src="src"></u-image>
58
+ */
59
+
60
+ const emit = defineEmits<{
61
+ (e: 'click'): void;
62
+ (e: 'error', err: any): void;
63
+ (e: 'load'): void;
64
+ }>();
65
+
66
+ const props = defineProps(ImageProps);
67
+
68
+ // 图片是否加载错误,如果是,则显示错误占位图
69
+ const isError = ref(false);
70
+ // 初始化组件时,默认为加载中状态
71
+ const loading = ref(true);
72
+ // 不透明度,为了实现淡入淡出的效果
73
+ const opacity = ref(1);
74
+ // 过渡时间,因为props的值无法修改,故需要一个中间值
75
+ const durationTime = ref(props.duration);
76
+ // 图片加载完成时,去掉背景颜色,因为如果是png图片,就会显示灰色的背景
77
+ const backgroundStyle = ref<Record<string, any>>({});
78
+
79
+ // 监听src变化,处理加载状态
80
+ watch(
81
+ () => props.src,
82
+ n => {
83
+ if (!n) {
84
+ // 如果传入null或者'',或者false,或者undefined,标记为错误状态
85
+ isError.value = true;
86
+ loading.value = false;
87
+ } else {
88
+ isError.value = false;
89
+ loading.value = true;
90
+ }
91
+ },
92
+ { immediate: true }
93
+ );
94
+
95
+ /**
96
+ * 计算图片外层包裹样式
97
+ * @returns {Record<string, any>}
98
+ */
99
+ const wrapStyle = computed(() => {
100
+ let style: Record<string, any> = {};
101
+ // 通过调用addUnit()方法,如果有单位,如百分比,px单位等,直接返回,如果是纯粹的数值,则加上rpx单位
102
+ style.width = $u.addUnit(props.width);
103
+ style.height = $u.addUnit(props.height);
104
+ // 如果是配置了圆形,设置50%的圆角,否则按照默认的配置值
105
+ style.borderRadius = props.shape === 'circle' ? '50%' : $u.addUnit(props.borderRadius);
106
+ // 如果设置圆角,必须要有hidden,否则可能圆角无效
107
+ style.overflow = Number(props.borderRadius) > 0 ? 'hidden' : 'visible';
108
+ if (props.fade) {
109
+ style.opacity = opacity.value;
110
+ style.transition = `opacity ${Number(durationTime.value) / 1000}s ease-in-out`;
111
+ }
112
+ return style;
113
+ });
114
+
115
+ /**
116
+ * 点击图片
117
+ * @emits click
118
+ */
119
+ function onClick() {
120
+ emit('click');
121
+ }
122
+
123
+ /**
124
+ * 图片加载失败
125
+ * @param err 失败事件对象
126
+ * @emits error
127
+ */
128
+ function onErrorHandler(err: any) {
129
+ loading.value = false;
130
+ isError.value = true;
131
+ emit('error', err);
132
+ }
133
+
134
+ /**
135
+ * 图片加载完成,标记loading结束
136
+ * @emits load
137
+ */
138
+ function onLoadHandler() {
139
+ loading.value = false;
140
+ isError.value = false;
141
+ emit('load');
142
+ // 如果不需要动画效果,就不执行下方代码,同时移除加载时的背景颜色
143
+ // 否则无需fade效果时,png图片依然能看到下方的背景色
144
+ if (!props.fade) return removeBgColor();
145
+ // 原来opacity为1(不透明,是为了显示占位图),改成0(透明,意味着该元素显示的是背景颜色,默认的灰色),再改成1,是为了获得过渡效果
146
+ opacity.value = 0;
147
+ // 这里设置为0,是为了图片展示到背景全透明这个过程时间为0,延时之后延时之后重新设置为duration,是为了获得背景透明(灰色)
148
+ // 到图片展示的过程中的淡入效果
149
+ durationTime.value = 0;
150
+ // 延时50ms,否则在浏览器H5,过渡效果无效
151
+ setTimeout(() => {
152
+ durationTime.value = props.duration;
153
+ opacity.value = 1;
154
+ setTimeout(() => {
155
+ removeBgColor();
156
+ }, Number(durationTime.value));
157
+ }, 50);
158
+ }
159
+
160
+ /**
161
+ * 移除图片的背景色
162
+ * 淡入动画过渡完成后,将背景设置为透明色,否则png图片会看到灰色的背景
163
+ */
164
+ function removeBgColor() {
165
+ backgroundStyle.value = {
166
+ backgroundColor: 'transparent'
167
+ };
168
+ }
169
+
170
+ function changeStatus(status: 'loading' | 'error' | 'normal') {
171
+ if (status === 'loading') {
172
+ loading.value = true;
173
+ isError.value = false;
174
+ } else if (status === 'error') {
175
+ loading.value = false;
176
+ isError.value = true;
177
+ } else {
178
+ loading.value = false;
179
+ isError.value = false;
180
+ }
181
+ }
182
+
183
+ // 暴露给模板
184
+ const $slots = useSlots();
185
+
186
+ defineExpose<ImageExpose>({
187
+ changeStatus
188
+ });
189
+ </script>
190
+
191
+ <style scoped lang="scss">
192
+ @import '../../libs/css/style.components.scss';
193
+
194
+ .u-image {
195
+ position: relative;
196
+ transition: opacity 0.5s ease-in-out;
197
+
198
+ &__image {
199
+ width: 100%;
200
+ height: 100%;
201
+ }
202
+
203
+ &__loading,
204
+ &__error {
205
+ position: absolute;
206
+ top: 0;
207
+ left: 0;
208
+ width: 100%;
209
+ height: 100%;
210
+ @include vue-flex;
211
+ align-items: center;
212
+ justify-content: center;
213
+ background-color: $u-bg-color;
214
+ color: $u-tips-color;
215
+ font-size: 46rpx;
216
+ }
217
+ }
218
+ </style>
@@ -0,0 +1,101 @@
1
+ <template>
2
+ <!-- 支付宝小程序使用$u.getRect()获取组件的根元素尺寸,所以在外面套一个"壳" -->
3
+ <view>
4
+ <view class="u-index-anchor-wrapper" :id="$u.guid()" :style="wrapperStyle">
5
+ <view class="u-index-anchor" :class="[active ? 'u-index-anchor--active' : '']" :style="customAnchorStyle">
6
+ <slot v-if="useSlot" />
7
+ <template v-else>
8
+ <text>{{ index }}</text>
9
+ </template>
10
+ </view>
11
+ </view>
12
+ </view>
13
+ </template>
14
+
15
+ <script setup lang="ts">
16
+ import { ref, computed, onMounted, getCurrentInstance } from 'vue';
17
+ import { $u } from '../..';
18
+
19
+ defineOptions({
20
+ name: 'u-index-anchor'
21
+ });
22
+
23
+ /**
24
+ * indexAnchor 索引列表锚点
25
+ * @description 通过折叠面板收纳内容区域,搭配<u-index-anchor>使用
26
+ * @tutorial https://www.uviewui.com/components/indexList.html#indexanchor-props
27
+ * @property {Boolean} use-slot 是否使用自定义内容的插槽(默认false)
28
+ * @property {String|Number} index 索引字符,如果定义了use-slot,此参数自动失效
29
+ * @property {Object} customStyle 自定义样式,对象形式,如"{color: 'red'}"
30
+ * @event {Function} default 锚点位置显示内容,默认为索引字符
31
+ * @example <u-index-anchor :index="item" />
32
+ */
33
+
34
+ const props = defineProps({
35
+ /** 是否使用自定义内容的插槽 */
36
+ useSlot: {
37
+ type: Boolean,
38
+ default: false
39
+ },
40
+ /** 索引字符,如果定义了use-slot,此参数自动失效 */
41
+ index: {
42
+ type: String,
43
+ default: ''
44
+ },
45
+ /** 自定义样式,对象形式 */
46
+ customStyle: {
47
+ type: Object as () => Record<string, any>,
48
+ default: () => ({})
49
+ }
50
+ });
51
+
52
+ // 响应式变量
53
+ const active = ref(false);
54
+ const wrapperStyle = ref<Record<string, any>>({});
55
+ const anchorStyle = ref<Record<string, any>>({});
56
+ let parent: any = null;
57
+
58
+ // 计算属性:合并 anchorStyle 和 customStyle
59
+ const customAnchorStyle = computed(() => {
60
+ return Object.assign({}, anchorStyle.value, props.customStyle);
61
+ });
62
+
63
+ const instance = getCurrentInstance();
64
+
65
+ // 挂载时查找父组件并注册
66
+ onMounted(() => {
67
+ parent = $u.$parent('u-index-list', instance);
68
+ if (parent) {
69
+ parent.exposed?.children.push(instance);
70
+ parent.exposed?.updateData();
71
+ }
72
+ });
73
+ defineExpose({
74
+ active,
75
+ wrapperStyle,
76
+ anchorStyle,
77
+ props
78
+ });
79
+ </script>
80
+
81
+ <style lang="scss" scoped>
82
+ @import '../../libs/css/style.components.scss';
83
+
84
+ .u-index-anchor {
85
+ box-sizing: border-box;
86
+ padding: 14rpx 24rpx;
87
+ color: #606266;
88
+ width: 100%;
89
+ font-weight: 500;
90
+ font-size: 28rpx;
91
+ line-height: 1.2;
92
+ background-color: rgb(245, 245, 245);
93
+ }
94
+
95
+ .u-index-anchor--active {
96
+ right: 0;
97
+ left: 0;
98
+ color: #2979ff;
99
+ background-color: #fff;
100
+ }
101
+ </style>
@@ -0,0 +1,376 @@
1
+ <template>
2
+ <!-- 支付宝小程序使用$u.getRect()获取组件的根元素尺寸,所以在外面套一个"壳" -->
3
+ <view>
4
+ <view class="u-index-bar">
5
+ <slot />
6
+ <view
7
+ v-if="showSidebar"
8
+ class="u-index-bar__sidebar"
9
+ @touchstart.stop.prevent="onTouchMove"
10
+ @touchmove.stop.prevent="onTouchMove"
11
+ @touchend.stop.prevent="onTouchStop"
12
+ @touchcancel.stop.prevent="onTouchStop"
13
+ >
14
+ <view
15
+ v-for="(item, index) in indexList"
16
+ :key="index"
17
+ class="u-index-bar__index"
18
+ :style="{ zIndex: Number(zIndex) + 1, color: activeAnchorIndex === index ? activeColor : '' }"
19
+ :data-index="index"
20
+ >
21
+ {{ item }}
22
+ </view>
23
+ </view>
24
+ <view
25
+ class="u-indexed-list-alert"
26
+ v-if="touchmove && indexList[touchmoveIndex]"
27
+ :style="{
28
+ zIndex: alertZIndex
29
+ }"
30
+ >
31
+ <text>{{ indexList[touchmoveIndex] }}</text>
32
+ </view>
33
+ </view>
34
+ </view>
35
+ </template>
36
+
37
+ <script lang="ts">
38
+ // 索引列表生成函数
39
+ function getIndexList() {
40
+ const indexList: string[] = [];
41
+ const charCodeOfA = 'A'.charCodeAt(0);
42
+ for (let i = 0; i < 26; i++) {
43
+ indexList.push(String.fromCharCode(charCodeOfA + i));
44
+ }
45
+ return indexList;
46
+ }
47
+ </script>
48
+
49
+ <script setup lang="ts">
50
+ import { ref, reactive, computed, watch, onMounted, getCurrentInstance } from 'vue';
51
+ import { $u } from '../..';
52
+
53
+ defineOptions({
54
+ name: 'u-index-list'
55
+ });
56
+
57
+ /**
58
+ * indexList 索引列表
59
+ * @description 通过折叠面板收纳内容区域,搭配<u-index-anchor>使用
60
+ * @tutorial https://www.uviewui.com/components/indexList.html#indexanchor-props
61
+ * @property {Number|String} scroll-top 当前滚动高度,自定义组件无法获得滚动条事件,所以依赖接入方传入
62
+ * @property {Array} index-list 索引字符列表,数组(默认A-Z)
63
+ * @property {Number|String} z-index 锚点吸顶时的层级(默认965)
64
+ * @property {Boolean} sticky 是否开启锚点自动吸顶(默认true)
65
+ * @property {Number|String} offset-top 锚点自动吸顶时与顶部的距离(默认0)
66
+ * @property {String} highlight-color 锚点和右边索引字符高亮颜色(默认#2979ff)
67
+ * @event {Function} select 选中右边索引字符时触发
68
+ * @example <u-index-list :scrollTop="scrollTop"></u-index-list>
69
+ */
70
+ const props = defineProps({
71
+ /** 是否开启锚点自动吸顶 */
72
+ sticky: {
73
+ type: Boolean,
74
+ default: true
75
+ },
76
+ /** 锚点吸顶时的层级 */
77
+ zIndex: {
78
+ type: [Number, String],
79
+ default: ''
80
+ },
81
+ /** 当前滚动高度 */
82
+ scrollTop: {
83
+ type: [Number, String],
84
+ default: 0
85
+ },
86
+ /** 锚点自动吸顶时与顶部的距离 */
87
+ offsetTop: {
88
+ type: [Number, String],
89
+ default: 0
90
+ },
91
+ /** 索引字符列表 */
92
+ indexList: {
93
+ type: Array,
94
+ default: () => getIndexList()
95
+ },
96
+ /** 锚点和右边索引字符高亮颜色 */
97
+ activeColor: {
98
+ type: String,
99
+ default: '#2979ff'
100
+ }
101
+ });
102
+
103
+ const emit = defineEmits(['select']);
104
+ const instance = getCurrentInstance();
105
+
106
+ // 变量定义
107
+ const activeAnchorIndex = ref(0);
108
+ const showSidebar = ref(true);
109
+ const touchmove = ref(false);
110
+ const touchmoveIndex = ref(0);
111
+ // 孩子锚点组件
112
+ const children = reactive<any[]>([]);
113
+ const sidebar = reactive<{ height: number; top: number }>({ height: 0, top: 0 });
114
+ const scrollToAnchorIndex = ref<number | null>(null);
115
+ const timer = ref<any>(null);
116
+ const top = ref(0);
117
+ const height = ref(0);
118
+ const stickyOffsetTop = ref(0);
119
+
120
+ // 计算属性
121
+ // 弹出toast的z-index值
122
+ const alertZIndex = computed(() => $u.zIndex.toast).value;
123
+ // indexList 响应式
124
+ const indexList = computed(() => props.indexList).value;
125
+ const zIndex = computed(() => props.zIndex).value;
126
+ const activeColor = computed(() => props.activeColor).value;
127
+
128
+ // 只能在created生命周期定义children,如果在data定义,会因为循环引用而报错
129
+ children.length = 0;
130
+
131
+ // 兼容 H5/非H5 stickyOffsetTop
132
+ onMounted(() => {
133
+ const offsetTopNum = typeof props.offsetTop === 'string' ? Number(props.offsetTop) : props.offsetTop;
134
+ // #ifdef H5
135
+ stickyOffsetTop.value = offsetTopNum ? uni.upx2px(offsetTopNum) : 44;
136
+ // #endif
137
+ // #ifndef H5
138
+ stickyOffsetTop.value = offsetTopNum ? uni.upx2px(offsetTopNum) : 0;
139
+ // #endif
140
+ });
141
+
142
+ // 监听 scrollTop
143
+ watch(
144
+ () => props.scrollTop,
145
+ () => {
146
+ updateData();
147
+ }
148
+ );
149
+
150
+ function updateData() {
151
+ if (timer.value) clearTimeout(timer.value);
152
+ timer.value = setTimeout(() => {
153
+ showSidebar.value = !!children.length;
154
+ setRect().then(() => {
155
+ onScroll();
156
+ });
157
+ }, 0);
158
+ }
159
+
160
+ /**
161
+ * 获取各区域尺寸
162
+ */
163
+ function setRect() {
164
+ return Promise.all([setAnchorsRect(), setListRect(), setSiderbarRect()]);
165
+ }
166
+
167
+ /**
168
+ * 获取锚点尺寸
169
+ */
170
+ function setAnchorsRect() {
171
+ return Promise.all(
172
+ children.map((anchor, index) => {
173
+ $u.getRect('.u-index-anchor-wrapper', anchor).then((rect: any) => {
174
+ Object.assign(anchor, {
175
+ height: rect.height,
176
+ top: rect.top
177
+ });
178
+ });
179
+ })
180
+ );
181
+ }
182
+
183
+ /**
184
+ * 获取列表尺寸
185
+ */
186
+ function setListRect() {
187
+ return $u.getRect('.u-index-bar', instance).then((rect: any) => {
188
+ height.value = rect.height;
189
+ top.value = rect.top + Number(props.scrollTop);
190
+ });
191
+ }
192
+
193
+ /**
194
+ * 获取侧边栏尺寸
195
+ */
196
+ function setSiderbarRect() {
197
+ return $u.getRect('.u-index-bar__sidebar', instance).then((rect: any) => {
198
+ sidebar.height = rect.height;
199
+ sidebar.top = rect.top;
200
+ });
201
+ }
202
+
203
+ /**
204
+ * 获取当前激活锚点索引
205
+ */
206
+ function getActiveAnchorIndex() {
207
+ const sticky = props.sticky;
208
+ for (let i = children.length - 1; i >= 0; i--) {
209
+ const preAnchorHeight = i > 0 ? children[i - 1].height : 0;
210
+ const reachTop = sticky ? preAnchorHeight : 0;
211
+ if (reachTop >= children[i].top) {
212
+ return i;
213
+ }
214
+ }
215
+ return -1;
216
+ }
217
+
218
+ /**
219
+ * 滚动时处理锚点吸顶和样式
220
+ */
221
+ function onScroll() {
222
+ if (!children.length) return;
223
+ const sticky = props.sticky;
224
+ const scrollTopNum = Number(props.scrollTop);
225
+ const active = getActiveAnchorIndex();
226
+ activeAnchorIndex.value = active;
227
+ if (sticky) {
228
+ let isActiveAnchorSticky = false;
229
+ if (active !== -1) {
230
+ isActiveAnchorSticky = children[active].top <= 0;
231
+ }
232
+ children.forEach((item, index) => {
233
+ if (index === active) {
234
+ let wrapperStyle: any = '';
235
+ let anchorStyle: any = {
236
+ color: `${activeColor}`
237
+ };
238
+ if (isActiveAnchorSticky) {
239
+ wrapperStyle = { height: `${children[index].height}px` };
240
+ anchorStyle = {
241
+ position: 'fixed',
242
+ top: `${stickyOffsetTop.value}px`,
243
+ zIndex: `${zIndex ? zIndex : $u.zIndex.indexListSticky}`,
244
+ color: `${activeColor}`
245
+ };
246
+ }
247
+ item.active = active;
248
+ item.wrapperStyle = wrapperStyle;
249
+ item.anchorStyle = anchorStyle;
250
+ } else if (index === active - 1) {
251
+ const currentAnchor = children[index];
252
+ const currentOffsetTop = currentAnchor.top;
253
+ const targetOffsetTop = index === children.length - 1 ? top.value : children[index + 1].top;
254
+ const parentOffsetHeight = targetOffsetTop - currentOffsetTop;
255
+ const translateY = parentOffsetHeight - currentAnchor.height;
256
+ const anchorStyle = {
257
+ position: 'relative',
258
+ transform: `translate3d(0, ${translateY}px, 0)`,
259
+ zIndex: `${zIndex ? zIndex : $u.zIndex.indexListSticky}`,
260
+ color: `${activeColor}`
261
+ };
262
+ item.active = active;
263
+ item.anchorStyle = anchorStyle;
264
+ } else {
265
+ item.active = false;
266
+ item.anchorStyle = '';
267
+ item.wrapperStyle = '';
268
+ }
269
+ item.exposed.active = item.active;
270
+ item.exposed.anchorStyle = item.anchorStyle;
271
+ item.exposed.wrapperStyle = item.wrapperStyle;
272
+ });
273
+ }
274
+ }
275
+
276
+ /**
277
+ * 侧边栏触摸移动
278
+ */
279
+ function onTouchMove(event: TouchEvent) {
280
+ touchmove.value = true;
281
+ const sidebarLength = children.length;
282
+ const touch = event.touches[0];
283
+ const itemHeight = sidebar.height / sidebarLength;
284
+ let clientY = touch.clientY;
285
+ let index = Math.floor((clientY - sidebar.top) / itemHeight);
286
+ if (index < 0) {
287
+ index = 0;
288
+ } else if (index > sidebarLength - 1) {
289
+ index = sidebarLength - 1;
290
+ }
291
+ touchmoveIndex.value = index;
292
+ scrollToAnchor(index);
293
+ }
294
+
295
+ /**
296
+ * 侧边栏触摸结束
297
+ */
298
+ function onTouchStop() {
299
+ touchmove.value = false;
300
+ scrollToAnchorIndex.value = null;
301
+ }
302
+
303
+ /**
304
+ * 滚动到指定锚点
305
+ */
306
+ function scrollToAnchor(index: number) {
307
+ if (scrollToAnchorIndex.value === index) {
308
+ return;
309
+ }
310
+ scrollToAnchorIndex.value = index;
311
+ const anchor = children.find(item => item.props.index === indexList[index]);
312
+ if (anchor) {
313
+ emit('select', anchor.props.index);
314
+ uni.pageScrollTo({
315
+ duration: 0,
316
+ scrollTop: anchor.top + Number(props.scrollTop)
317
+ });
318
+ }
319
+ }
320
+
321
+ defineExpose({
322
+ updateData,
323
+ setRect,
324
+ onScroll,
325
+ children
326
+ });
327
+ </script>
328
+
329
+ <style lang="scss" scoped>
330
+ @import '../../libs/css/style.components.scss';
331
+
332
+ .u-index-bar {
333
+ position: relative;
334
+ }
335
+
336
+ .u-index-bar__sidebar {
337
+ position: fixed;
338
+ top: 50%;
339
+ right: 0;
340
+ @include vue-flex;
341
+ flex-direction: column;
342
+ text-align: center;
343
+ transform: translateY(-50%);
344
+ user-select: none;
345
+ z-index: 99;
346
+ }
347
+
348
+ .u-index-bar__index {
349
+ font-weight: 500;
350
+ padding: 8rpx 18rpx;
351
+ font-size: 22rpx;
352
+ line-height: 1;
353
+ }
354
+
355
+ .u-indexed-list-alert {
356
+ position: fixed;
357
+ width: 120rpx;
358
+ height: 120rpx;
359
+ right: 90rpx;
360
+ top: 50%;
361
+ margin-top: -60rpx;
362
+ border-radius: 24rpx;
363
+ font-size: 50rpx;
364
+ color: #fff;
365
+ background-color: rgba(0, 0, 0, 0.65);
366
+ @include vue-flex;
367
+ justify-content: center;
368
+ align-items: center;
369
+ padding: 0;
370
+ z-index: 9999999;
371
+ }
372
+
373
+ .u-indexed-list-alert text {
374
+ line-height: 50rpx;
375
+ }
376
+ </style>