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.
- package/changelog.md +6 -0
- package/components/u-action-sheet/u-action-sheet.vue +205 -0
- package/components/u-alert-tips/u-alert-tips.vue +241 -0
- package/components/u-avatar/u-avatar.vue +220 -0
- package/components/u-avatar-cropper/u-avatar-cropper.vue +329 -0
- package/components/u-avatar-cropper/weCropper.d.ts +54 -0
- package/components/u-avatar-cropper/weCropper.js +1267 -0
- package/components/u-avatar-cropper/weCropper.ts +1254 -0
- package/components/u-back-top/u-back-top.vue +156 -0
- package/components/u-badge/u-badge.vue +189 -0
- package/components/u-button/u-button.vue +562 -0
- package/components/u-calendar/u-calendar.vue +725 -0
- package/components/u-car-keyboard/u-car-keyboard.vue +236 -0
- package/components/u-card/u-card.vue +240 -0
- package/components/u-cell-group/u-cell-group.vue +56 -0
- package/components/u-cell-item/u-cell-item.vue +245 -0
- package/components/u-checkbox/u-checkbox.vue +310 -0
- package/components/u-checkbox-group/u-checkbox-group.vue +134 -0
- package/components/u-circle-progress/u-circle-progress.vue +210 -0
- package/components/u-col/u-col.vue +135 -0
- package/components/u-collapse/u-collapse.vue +82 -0
- package/components/u-collapse-item/u-collapse-item.vue +190 -0
- package/components/u-column-notice/u-column-notice.vue +264 -0
- package/components/u-count-down/u-count-down.vue +333 -0
- package/components/u-count-to/u-count-to.vue +297 -0
- package/components/u-divider/u-divider.vue +141 -0
- package/components/u-dropdown/u-dropdown.vue +311 -0
- package/components/u-dropdown-item/u-dropdown-item.vue +135 -0
- package/components/u-empty/u-empty.vue +111 -0
- package/components/u-field/u-field.vue +469 -0
- package/components/u-form/u-form.vue +162 -0
- package/components/u-form-item/u-form-item.vue +476 -0
- package/components/u-full-screen/u-full-screen.vue +80 -0
- package/components/u-gap/u-gap.vue +48 -0
- package/components/u-grid/u-grid.vue +101 -0
- package/components/u-grid-item/u-grid-item.vue +136 -0
- package/components/u-icon/u-icon.vue +389 -0
- package/components/u-image/types.ts +48 -0
- package/components/u-image/u-image.vue +218 -0
- package/components/u-index-anchor/u-index-anchor.vue +101 -0
- package/components/u-index-list/u-index-list.vue +376 -0
- package/components/u-input/u-input.vue +462 -0
- package/components/u-keyboard/u-keyboard.vue +188 -0
- package/components/u-lazy-load/u-lazy-load.vue +288 -0
- package/components/u-line/u-line.vue +71 -0
- package/components/u-line-progress/u-line-progress.vue +128 -0
- package/components/u-link/u-link.vue +87 -0
- package/components/u-loading/u-loading.vue +111 -0
- package/components/u-loadmore/u-loadmore.vue +205 -0
- package/components/u-mask/u-mask.vue +137 -0
- package/components/u-message-input/u-message-input.vue +315 -0
- package/components/u-modal/u-modal.vue +284 -0
- package/components/u-navbar/u-navbar.vue +314 -0
- package/components/u-no-network/image.ts +2 -0
- package/components/u-no-network/u-no-network.vue +311 -0
- package/components/u-notice-bar/u-notice-bar.vue +274 -0
- package/components/u-number-box/u-number-box.vue +344 -0
- package/components/u-number-keyboard/u-number-keyboard.vue +170 -0
- package/components/u-parse/libs/CssHandler.js +100 -0
- package/components/u-parse/libs/MpHtmlParser.js +580 -0
- package/components/u-parse/libs/config.js +80 -0
- package/components/u-parse/libs/handler.wxs +22 -0
- package/components/u-parse/libs/trees.vue +505 -0
- package/components/u-parse/u-parse.vue +645 -0
- package/components/u-picker/u-picker.vue +808 -0
- package/components/u-popup/u-popup.vue +404 -0
- package/components/u-radio/u-radio.vue +272 -0
- package/components/u-radio-group/u-radio-group.vue +116 -0
- package/components/u-rate/u-rate.vue +349 -0
- package/components/u-read-more/u-read-more.vue +199 -0
- package/components/u-row/u-row.vue +95 -0
- package/components/u-row-notice/u-row-notice.vue +273 -0
- package/components/u-search/u-search.vue +298 -0
- package/components/u-section/u-section.vue +175 -0
- package/components/u-select/u-select.vue +387 -0
- package/components/u-skeleton/u-skeleton.vue +230 -0
- package/components/u-slider/u-slider.vue +293 -0
- package/components/u-steps/u-steps.vue +200 -0
- package/components/u-sticky/u-sticky.vue +189 -0
- package/components/u-subsection/u-subsection.vue +388 -0
- package/components/u-swipe-action/u-swipe-action.vue +289 -0
- package/components/u-swiper/u-swiper.vue +305 -0
- package/components/u-switch/u-switch.vue +146 -0
- package/components/u-tabbar/u-tabbar.vue +347 -0
- package/components/u-table/u-table.vue +104 -0
- package/components/u-tabs/u-tabs.vue +322 -0
- package/components/u-tabs-swiper/u-tabs-swiper.vue +426 -0
- package/components/u-tag/u-tag.vue +270 -0
- package/components/u-td/u-td.vue +76 -0
- package/components/u-th/u-th.vue +70 -0
- package/components/u-time-line/u-time-line.vue +39 -0
- package/components/u-time-line-item/u-time-line-item.vue +88 -0
- package/components/u-toast/types.ts +4 -0
- package/components/u-toast/u-toast.vue +238 -0
- package/components/u-top-tips/u-top-tips.vue +118 -0
- package/components/u-tr/u-tr.vue +24 -0
- package/components/u-upload/u-upload.vue +600 -0
- package/components/u-verification-code/u-verification-code.vue +194 -0
- package/components/u-waterfall/u-waterfall.vue +186 -0
- package/iconfont.css +910 -0
- package/index.scss +23 -0
- package/index.ts +166 -0
- package/libs/config/config.ts +26 -0
- package/libs/config/zIndex.ts +37 -0
- package/libs/css/color.scss +155 -0
- package/libs/css/common.scss +176 -0
- package/libs/css/style.components.scss +7 -0
- package/libs/css/style.h5.scss +8 -0
- package/libs/css/style.mp.scss +72 -0
- package/libs/css/style.nvue.scss +3 -0
- package/libs/css/style.vue.scss +175 -0
- package/libs/function/$parent.ts +22 -0
- package/libs/function/addUnit.ts +13 -0
- package/libs/function/color.ts +37 -0
- package/libs/function/colorGradient.ts +123 -0
- package/libs/function/debounce.ts +28 -0
- package/libs/function/deepClone.ts +39 -0
- package/libs/function/deepMerge.ts +34 -0
- package/libs/function/getParent.ts +59 -0
- package/libs/function/getRect.ts +26 -0
- package/libs/function/guid.ts +42 -0
- package/libs/function/md5.ts +397 -0
- package/libs/function/parent.ts +21 -0
- package/libs/function/queryParams.ts +60 -0
- package/libs/function/random.ts +16 -0
- package/libs/function/randomArray.ts +11 -0
- package/libs/function/route.ts +118 -0
- package/libs/function/sys.ts +15 -0
- package/libs/function/test.ts +229 -0
- package/libs/function/throttle.ts +31 -0
- package/libs/function/timeFormat.ts +54 -0
- package/libs/function/timeFrom.ts +48 -0
- package/libs/function/toast.ts +14 -0
- package/libs/function/trim.ts +21 -0
- package/libs/function/type2icon.ts +36 -0
- package/libs/hooks/useEmitter.ts +77 -0
- package/libs/hooks/useParent.ts +29 -0
- package/libs/request/index.ts +237 -0
- package/libs/store/index.ts +88 -0
- package/libs/util/area.ts +1 -0
- package/libs/util/async-validator.js +1356 -0
- package/libs/util/city.ts +1 -0
- package/libs/util/emitter.ts +112 -0
- package/libs/util/mitt.ts +118 -0
- package/libs/util/parent.ts +20 -0
- package/libs/util/province.ts +1 -0
- package/package.json +98 -0
- package/readme.md +165 -0
- package/theme.scss +38 -0
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<view :id="instanceId" class="u-subsection" :style="subsectionStyle">
|
|
3
|
+
<view
|
|
4
|
+
class="u-item u-line-1"
|
|
5
|
+
:style="itemStyle(index)"
|
|
6
|
+
@tap="click(index)"
|
|
7
|
+
:class="[noBorderRight(index), `u-item-${index}`]"
|
|
8
|
+
v-for="(item, index) in listInfo"
|
|
9
|
+
:key="index"
|
|
10
|
+
>
|
|
11
|
+
<view :style="textStyle(index)" class="u-item-text u-line-1">{{ item.name }}</view>
|
|
12
|
+
</view>
|
|
13
|
+
<view class="u-item-bg" :style="itemBarStyle"></view>
|
|
14
|
+
</view>
|
|
15
|
+
</template>
|
|
16
|
+
|
|
17
|
+
<script setup lang="ts">
|
|
18
|
+
import { ref, computed, watch, onMounted, getCurrentInstance } from 'vue';
|
|
19
|
+
import { $u } from '../..';
|
|
20
|
+
|
|
21
|
+
defineOptions({ name: 'u-subsection' });
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* subsection 分段器
|
|
25
|
+
* @description 该分段器一般用于用户从几个选项中选择某一个的场景
|
|
26
|
+
* @tutorial https://www.uviewui.com/components/subsection.html
|
|
27
|
+
* @property {Array} list 选项的数组,形式见上方"基本使用"
|
|
28
|
+
* @property {String | Number} current 初始化时默认选中的选项索引值(默认0)
|
|
29
|
+
* @property {String} activeColor 激活时的颜色,mode为subsection时固定为白色(默认#303133)
|
|
30
|
+
* @property {String} inactiveColor 未激活时字体的颜色,mode为subsection时无效(默认#606266)
|
|
31
|
+
* @property {String} mode 模式选择,见官网"模式选择"说明(默认button)
|
|
32
|
+
* @property {String | Number} fontSize 字体大小,单位rpx(默认28)
|
|
33
|
+
* @property {String | Number} height 组件高度,单位rpx(默认70)
|
|
34
|
+
* @property {Boolean} animation 是否开启动画效果,见上方说明(默认true)
|
|
35
|
+
* @property {Boolean} bold 激活选项的字体是否加粗(默认true)
|
|
36
|
+
* @property {String} bgColor 组件背景颜色,mode为button时有效(默认#eeeeef)
|
|
37
|
+
* @property {String} buttonColor 按钮背景颜色,mode为button时有效(默认#ffffff)
|
|
38
|
+
* @property {Boolean} vibrateShort 在切换分段器的时候,是否让设备震一下(默认false)
|
|
39
|
+
* @event {Function} change 分段器选项发生改变时触发
|
|
40
|
+
* @example <u-subsection active-color="#ff9900"></u-subsection>
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
interface ListItem {
|
|
44
|
+
name: string;
|
|
45
|
+
width?: number;
|
|
46
|
+
[key: string]: any;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const props = defineProps({
|
|
50
|
+
/** tab的数据 */
|
|
51
|
+
list: {
|
|
52
|
+
type: Array as () => Array<string | ListItem>,
|
|
53
|
+
default: () => []
|
|
54
|
+
},
|
|
55
|
+
/** 当前活动的tab的index */
|
|
56
|
+
current: {
|
|
57
|
+
type: [Number, String],
|
|
58
|
+
default: 0
|
|
59
|
+
},
|
|
60
|
+
/** 激活的颜色 */
|
|
61
|
+
activeColor: {
|
|
62
|
+
type: String,
|
|
63
|
+
default: '#303133'
|
|
64
|
+
},
|
|
65
|
+
/** 未激活的颜色 */
|
|
66
|
+
inactiveColor: {
|
|
67
|
+
type: String,
|
|
68
|
+
default: '#606266'
|
|
69
|
+
},
|
|
70
|
+
/** 模式选择,mode=button为按钮形式,mode=subsection时为分段模式 */
|
|
71
|
+
mode: {
|
|
72
|
+
type: String,
|
|
73
|
+
default: 'button'
|
|
74
|
+
},
|
|
75
|
+
/** 字体大小,单位rpx */
|
|
76
|
+
fontSize: {
|
|
77
|
+
type: [Number, String],
|
|
78
|
+
default: 28
|
|
79
|
+
},
|
|
80
|
+
/** 是否开启动画效果 */
|
|
81
|
+
animation: {
|
|
82
|
+
type: Boolean,
|
|
83
|
+
default: true
|
|
84
|
+
},
|
|
85
|
+
/** 组件的高度,单位rpx */
|
|
86
|
+
height: {
|
|
87
|
+
type: [Number, String],
|
|
88
|
+
default: 70
|
|
89
|
+
},
|
|
90
|
+
/** 激活tab的字体是否加粗 */
|
|
91
|
+
bold: {
|
|
92
|
+
type: Boolean,
|
|
93
|
+
default: true
|
|
94
|
+
},
|
|
95
|
+
/** mode=button时,组件背景颜色 */
|
|
96
|
+
bgColor: {
|
|
97
|
+
type: String,
|
|
98
|
+
default: '#eeeeef'
|
|
99
|
+
},
|
|
100
|
+
/** mode = button时,滑块背景颜色 */
|
|
101
|
+
buttonColor: {
|
|
102
|
+
type: String,
|
|
103
|
+
default: '#ffffff'
|
|
104
|
+
},
|
|
105
|
+
/** 在切换分段器的时候,是否让设备震一下 */
|
|
106
|
+
vibrateShort: {
|
|
107
|
+
type: Boolean,
|
|
108
|
+
default: false
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
const emit = defineEmits<{ (e: 'change', index: number): void }>();
|
|
113
|
+
|
|
114
|
+
// 组件内部状态
|
|
115
|
+
const listInfo = ref<ListItem[]>([]);
|
|
116
|
+
const itemBgStyle = ref<{ [key: string]: any }>({
|
|
117
|
+
width: 0,
|
|
118
|
+
left: 0,
|
|
119
|
+
backgroundColor: '#ffffff',
|
|
120
|
+
height: '100%',
|
|
121
|
+
transition: ''
|
|
122
|
+
});
|
|
123
|
+
const currentIndex = ref(Number(props.current));
|
|
124
|
+
const buttonPadding = 3; // mode = button 时,组件的内边距
|
|
125
|
+
const borderRadius = 5; // 圆角值
|
|
126
|
+
const firstTimeVibrateShort = ref(true); // 组件初始化时,会触发current变化,此时不应震动
|
|
127
|
+
const instanceId = $u.guid(); // 组件唯一 id
|
|
128
|
+
const instance = getCurrentInstance();
|
|
129
|
+
|
|
130
|
+
// 监听 current 变化
|
|
131
|
+
watch(
|
|
132
|
+
() => props.current,
|
|
133
|
+
nVal => {
|
|
134
|
+
currentIndex.value = Number(nVal);
|
|
135
|
+
changeSectionStatus(currentIndex.value);
|
|
136
|
+
},
|
|
137
|
+
{ immediate: true }
|
|
138
|
+
);
|
|
139
|
+
|
|
140
|
+
// 初始化 listInfo
|
|
141
|
+
function initListInfo() {
|
|
142
|
+
// 将list的数据,传入listInfo数组,因为不能修改props传递的list值
|
|
143
|
+
// 可以接受直接数组形式,或者数组元素为对象的形式,如:['简介', '评论'],或者[{name: '简介'}, {name: '评论'}]
|
|
144
|
+
listInfo.value = props.list.map(val => {
|
|
145
|
+
if (typeof val !== 'object') {
|
|
146
|
+
return { width: 0, name: val };
|
|
147
|
+
} else {
|
|
148
|
+
return { ...val, width: 0 };
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
initListInfo();
|
|
154
|
+
|
|
155
|
+
onMounted(() => {
|
|
156
|
+
setTimeout(() => {
|
|
157
|
+
getTabsInfo();
|
|
158
|
+
}, 10);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* 设置mode=subsection时,滑块特有的样式
|
|
163
|
+
*/
|
|
164
|
+
const noBorderRight = computed<(index: number) => string>(() => {
|
|
165
|
+
return (index: number) => {
|
|
166
|
+
if (props.mode !== 'subsection') return '';
|
|
167
|
+
let classs = '';
|
|
168
|
+
// 不显示右边的边框
|
|
169
|
+
if (index < props.list.length - 1) classs += ' u-none-border-right';
|
|
170
|
+
// 显示整个组件的左右边圆角
|
|
171
|
+
if (index === 0) classs += ' u-item-first';
|
|
172
|
+
if (index === props.list.length - 1) classs += ' u-item-last';
|
|
173
|
+
return classs;
|
|
174
|
+
};
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* 文字的样式
|
|
179
|
+
*/
|
|
180
|
+
const textStyle = computed<(index: number) => Record<string, any>>(() => {
|
|
181
|
+
return (index: number) => {
|
|
182
|
+
const style: Record<string, any> = {};
|
|
183
|
+
// 设置字体颜色
|
|
184
|
+
if (props.mode === 'subsection') {
|
|
185
|
+
style.color = index === currentIndex.value ? '#ffffff' : props.activeColor;
|
|
186
|
+
} else {
|
|
187
|
+
style.color = index === currentIndex.value ? props.activeColor : props.inactiveColor;
|
|
188
|
+
}
|
|
189
|
+
// 字体加粗
|
|
190
|
+
if (index === currentIndex.value && props.bold) style.fontWeight = 'bold';
|
|
191
|
+
// 文字大小
|
|
192
|
+
style.fontSize = props.fontSize + 'rpx';
|
|
193
|
+
return style;
|
|
194
|
+
};
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* 每个分段器item的样式
|
|
199
|
+
*/
|
|
200
|
+
const itemStyle = computed<(index: number) => Record<string, any>>(() => {
|
|
201
|
+
return (index: number) => {
|
|
202
|
+
const style: Record<string, any> = {};
|
|
203
|
+
if (props.mode === 'subsection') {
|
|
204
|
+
style.borderColor = props.activeColor;
|
|
205
|
+
style.borderWidth = '1px';
|
|
206
|
+
style.borderStyle = 'solid';
|
|
207
|
+
}
|
|
208
|
+
return style;
|
|
209
|
+
};
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* mode=button时,外层view的样式
|
|
214
|
+
*/
|
|
215
|
+
const subsectionStyle = computed<Record<string, any>>(() => {
|
|
216
|
+
const style: Record<string, any> = {};
|
|
217
|
+
style.height = uni.upx2px(Number(props.height)) + 'px';
|
|
218
|
+
if (props.mode === 'button') {
|
|
219
|
+
style.backgroundColor = props.bgColor;
|
|
220
|
+
style.padding = `${buttonPadding}px`;
|
|
221
|
+
style.borderRadius = `${borderRadius}px`;
|
|
222
|
+
}
|
|
223
|
+
return style;
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* 滑块的样式
|
|
228
|
+
*/
|
|
229
|
+
const itemBarStyle = computed<Record<string, any>>(() => {
|
|
230
|
+
const style: Record<string, any> = {};
|
|
231
|
+
style.backgroundColor = props.activeColor;
|
|
232
|
+
style.zIndex = 1;
|
|
233
|
+
if (props.mode === 'button') {
|
|
234
|
+
style.backgroundColor = props.buttonColor;
|
|
235
|
+
style.borderRadius = `${borderRadius}px`;
|
|
236
|
+
style.bottom = `${buttonPadding}px`;
|
|
237
|
+
style.height = uni.upx2px(Number(props.height)) - buttonPadding * 2 + 'px';
|
|
238
|
+
style.zIndex = 0;
|
|
239
|
+
}
|
|
240
|
+
return Object.assign({}, itemBgStyle.value, style);
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* 改变滑块的样式
|
|
245
|
+
* @param nVal 当前选中索引
|
|
246
|
+
*/
|
|
247
|
+
function changeSectionStatus(nVal: number) {
|
|
248
|
+
if (props.mode === 'subsection') {
|
|
249
|
+
// 根据滑块在最左边和最右边时,显示左边和右边的圆角
|
|
250
|
+
if (nVal === props.list.length - 1) {
|
|
251
|
+
itemBgStyle.value.borderRadius = `0 ${buttonPadding}px ${buttonPadding}px 0`;
|
|
252
|
+
}
|
|
253
|
+
if (nVal === 0) {
|
|
254
|
+
itemBgStyle.value.borderRadius = `${buttonPadding}px 0 0 ${buttonPadding}px`;
|
|
255
|
+
}
|
|
256
|
+
if (nVal > 0 && nVal < props.list.length - 1) {
|
|
257
|
+
itemBgStyle.value.borderRadius = '0';
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
// 更新滑块的位置
|
|
261
|
+
setTimeout(() => {
|
|
262
|
+
itemBgLeft();
|
|
263
|
+
}, 10);
|
|
264
|
+
if (props.vibrateShort && !firstTimeVibrateShort.value) {
|
|
265
|
+
// 使手机产生短促震动,微信小程序有效,APP(HX 2.6.8)和H5无效
|
|
266
|
+
// #ifndef H5
|
|
267
|
+
uni.vibrateShort();
|
|
268
|
+
// #endif
|
|
269
|
+
}
|
|
270
|
+
// 第一次过后,设置firstTimeVibrateShort为false,让其下一次可以震动(如果允许震动的话)
|
|
271
|
+
firstTimeVibrateShort.value = false;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* 点击分段器选项
|
|
276
|
+
* @param index 当前点击索引
|
|
277
|
+
*/
|
|
278
|
+
function click(index: number) {
|
|
279
|
+
// 不允许点击当前激活选项
|
|
280
|
+
if (index === currentIndex.value) return;
|
|
281
|
+
currentIndex.value = index;
|
|
282
|
+
changeSectionStatus(index);
|
|
283
|
+
emit('change', index);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* 获取各个tab的节点信息
|
|
288
|
+
*/
|
|
289
|
+
function getTabsInfo() {
|
|
290
|
+
const view = uni.createSelectorQuery().in(instance?.proxy);
|
|
291
|
+
for (let i = 0; i < props.list.length; i++) {
|
|
292
|
+
view.select(`#${instanceId} .u-item-${i}`).boundingClientRect();
|
|
293
|
+
}
|
|
294
|
+
view.exec((res: any[]) => {
|
|
295
|
+
if (!res.length) {
|
|
296
|
+
setTimeout(() => {
|
|
297
|
+
getTabsInfo();
|
|
298
|
+
return;
|
|
299
|
+
}, 10);
|
|
300
|
+
}
|
|
301
|
+
// 将分段器每个item的宽度,放入listInfo数组
|
|
302
|
+
res.map((val, index) => {
|
|
303
|
+
listInfo.value[index].width = val.width;
|
|
304
|
+
});
|
|
305
|
+
// 初始化滑块的宽度
|
|
306
|
+
if (props.mode === 'subsection' || props.mode === 'button') {
|
|
307
|
+
itemBgStyle.value.width = listInfo.value[0]?.width + 'px';
|
|
308
|
+
}
|
|
309
|
+
// 初始化滑块的位置
|
|
310
|
+
itemBgLeft();
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* 计算滑块左侧距离
|
|
316
|
+
*/
|
|
317
|
+
function itemBgLeft() {
|
|
318
|
+
// 根据是否开启动画效果
|
|
319
|
+
if (props.animation) {
|
|
320
|
+
itemBgStyle.value.transition = 'all 0.35s';
|
|
321
|
+
} else {
|
|
322
|
+
itemBgStyle.value.transition = 'all 0s';
|
|
323
|
+
}
|
|
324
|
+
let left = 0;
|
|
325
|
+
// 计算当前活跃item到组件左边的距离
|
|
326
|
+
listInfo.value.forEach((val, index) => {
|
|
327
|
+
if (index < currentIndex.value) left += val.width ?? 0;
|
|
328
|
+
});
|
|
329
|
+
// 根据mode不同模式,计算滑块需要移动的距离
|
|
330
|
+
if (props.mode === 'subsection') {
|
|
331
|
+
itemBgStyle.value.left = left + 'px';
|
|
332
|
+
} else if (props.mode === 'button') {
|
|
333
|
+
itemBgStyle.value.left = left + buttonPadding + 'px';
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
</script>
|
|
337
|
+
|
|
338
|
+
<style lang="scss" scoped>
|
|
339
|
+
@import '../../libs/css/style.components.scss';
|
|
340
|
+
|
|
341
|
+
.u-subsection {
|
|
342
|
+
@include vue-flex;
|
|
343
|
+
align-items: center;
|
|
344
|
+
overflow: hidden;
|
|
345
|
+
position: relative;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
.u-item {
|
|
349
|
+
flex: 1;
|
|
350
|
+
text-align: center;
|
|
351
|
+
font-size: 26rpx;
|
|
352
|
+
height: 100%;
|
|
353
|
+
@include vue-flex;
|
|
354
|
+
align-items: center;
|
|
355
|
+
justify-content: center;
|
|
356
|
+
color: $u-main-color;
|
|
357
|
+
padding: 0 6rpx;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
.u-item-bg {
|
|
361
|
+
background-color: $u-type-primary;
|
|
362
|
+
position: absolute;
|
|
363
|
+
z-index: -1;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
.u-none-border-right {
|
|
367
|
+
border-right: none !important;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
.u-item-first {
|
|
371
|
+
border-top-left-radius: 8rpx;
|
|
372
|
+
border-bottom-left-radius: 8rpx;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
.u-item-last {
|
|
376
|
+
border-top-right-radius: 8rpx;
|
|
377
|
+
border-bottom-right-radius: 8rpx;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
.u-item-text {
|
|
381
|
+
transition: all 0.35s;
|
|
382
|
+
color: $u-main-color;
|
|
383
|
+
@include vue-flex;
|
|
384
|
+
align-items: center;
|
|
385
|
+
position: relative;
|
|
386
|
+
z-index: 3;
|
|
387
|
+
}
|
|
388
|
+
</style>
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<view class="">
|
|
3
|
+
<movable-area class="u-swipe-action" :style="{ backgroundColor: props.bgColor }">
|
|
4
|
+
<movable-view
|
|
5
|
+
class="u-swipe-view"
|
|
6
|
+
@change="change"
|
|
7
|
+
@touchend="touchend"
|
|
8
|
+
@touchstart="touchstart"
|
|
9
|
+
direction="horizontal"
|
|
10
|
+
:disabled="props.disabled"
|
|
11
|
+
:inertia="true"
|
|
12
|
+
:x="moveX"
|
|
13
|
+
:style="{ width: movableViewWidth ? movableViewWidth : '100%' }"
|
|
14
|
+
>
|
|
15
|
+
<view class="u-swipe-content" @tap.stop="contentClick">
|
|
16
|
+
<slot></slot>
|
|
17
|
+
</view>
|
|
18
|
+
<view class="u-swipe-del" v-if="showBtn" @tap.stop="btnClick(index)" :style="btnStyle(item.style)" v-for="(item, index) in props.options" :key="index">
|
|
19
|
+
<view class="u-btn-text">{{ item.text }}</view>
|
|
20
|
+
</view>
|
|
21
|
+
</movable-view>
|
|
22
|
+
</movable-area>
|
|
23
|
+
</view>
|
|
24
|
+
</template>
|
|
25
|
+
|
|
26
|
+
<script lang="ts" setup>
|
|
27
|
+
import { ref, computed, watch, nextTick, onMounted } from 'vue';
|
|
28
|
+
import { $u } from '../..';
|
|
29
|
+
|
|
30
|
+
defineOptions({ name: 'u-swipe-action' });
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* swipeAction 左滑单元格
|
|
34
|
+
* @description 该组件一般用于左滑唤出操作菜单的场景,用的最多的是左滑删除操作。
|
|
35
|
+
* @tutorial https://www.uviewui.com/components/swipeAction.html
|
|
36
|
+
* @property {String} bg-color 整个组件背景颜色(默认#ffffff)
|
|
37
|
+
* @property {Array} options 数组形式,可以配置背景颜色和文字
|
|
38
|
+
* @property {String|Number} index 标识符,点击时候用于区分点击了哪一个,用v-for循环时的index即可
|
|
39
|
+
* @property {String|Number} btn-width 按钮宽度,单位rpx(默认180)
|
|
40
|
+
* @property {Boolean} disabled 是否禁止某个swipeAction滑动(默认false)
|
|
41
|
+
* @property {Boolean} show 打开或者关闭某个组件(默认false)
|
|
42
|
+
* @event {Function} click 点击组件时触发
|
|
43
|
+
* @event {Function} close 组件触发关闭状态时
|
|
44
|
+
* @event {Function} content-click 点击内容时触发
|
|
45
|
+
* @event {Function} open 组件触发打开状态时
|
|
46
|
+
* @example <u-swipe-action btn-text="收藏">...</u-swipe-action>
|
|
47
|
+
*/
|
|
48
|
+
|
|
49
|
+
const props = defineProps({
|
|
50
|
+
/** index值,用于得知点击删除的是哪个按钮 */
|
|
51
|
+
index: {
|
|
52
|
+
type: [Number, String],
|
|
53
|
+
default: ''
|
|
54
|
+
},
|
|
55
|
+
/** 滑动按钮的宽度,单位为rpx */
|
|
56
|
+
btnWidth: {
|
|
57
|
+
type: [String, Number],
|
|
58
|
+
default: 180
|
|
59
|
+
},
|
|
60
|
+
/** 是否禁止某个action滑动 */
|
|
61
|
+
disabled: {
|
|
62
|
+
type: Boolean,
|
|
63
|
+
default: false
|
|
64
|
+
},
|
|
65
|
+
/** 打开或者关闭组件 */
|
|
66
|
+
show: {
|
|
67
|
+
type: Boolean,
|
|
68
|
+
default: false
|
|
69
|
+
},
|
|
70
|
+
/** 组件背景颜色 */
|
|
71
|
+
bgColor: {
|
|
72
|
+
type: String,
|
|
73
|
+
default: '#ffffff'
|
|
74
|
+
},
|
|
75
|
+
/** 是否使手机发生短促震动,目前只在iOS的微信小程序有效(2020-05-06) */
|
|
76
|
+
vibrateShort: {
|
|
77
|
+
type: Boolean,
|
|
78
|
+
default: false
|
|
79
|
+
},
|
|
80
|
+
/** 按钮操作参数 */
|
|
81
|
+
options: {
|
|
82
|
+
type: Array as () => Array<{ text: string; style?: Record<string, any> }>,
|
|
83
|
+
default: () => []
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
const emit = defineEmits(['click', 'close', 'content-click', 'open']);
|
|
88
|
+
|
|
89
|
+
// 组件内部状态
|
|
90
|
+
const moveX = ref(0); // movable-view元素在x轴上需要移动的目标移动距离,用于展开或收起滑动的按钮
|
|
91
|
+
const scrollX = ref(0); // movable-view移动过程中产生的change事件中的x轴移动值
|
|
92
|
+
const status = ref(false); // 滑动的状态,表示当前是展开还是关闭按钮的状态
|
|
93
|
+
const movableAreaWidth = ref(0); // 滑动区域
|
|
94
|
+
const elId = ref($u.guid()); // id,用于通知另外组件关闭时的识别
|
|
95
|
+
const showBtn = ref(false); // 刚开始渲染视图时不显示右边的按钮,避免视图闪动
|
|
96
|
+
|
|
97
|
+
// 计算属性
|
|
98
|
+
const movableViewWidth = computed(() => {
|
|
99
|
+
return movableAreaWidth.value + allBtnWidth.value + 'px';
|
|
100
|
+
});
|
|
101
|
+
const innerBtnWidth = computed(() => {
|
|
102
|
+
// 保证类型安全,btnWidth 转为 number
|
|
103
|
+
return uni.upx2px(Number(props.btnWidth));
|
|
104
|
+
});
|
|
105
|
+
const allBtnWidth = computed(() => {
|
|
106
|
+
return uni.upx2px(Number(props.btnWidth)) * props.options.length;
|
|
107
|
+
});
|
|
108
|
+
const btnStyle = (style: Record<string, any> = {}) => {
|
|
109
|
+
// 按钮样式处理
|
|
110
|
+
style.width = props.btnWidth + 'rpx';
|
|
111
|
+
return style;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
// 监听 show 属性变化,控制展开/收起
|
|
115
|
+
watch(
|
|
116
|
+
() => props.show,
|
|
117
|
+
nVal => {
|
|
118
|
+
if (nVal) {
|
|
119
|
+
open();
|
|
120
|
+
} else {
|
|
121
|
+
close();
|
|
122
|
+
}
|
|
123
|
+
},
|
|
124
|
+
{ immediate: true, deep: true }
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
// 生命周期
|
|
128
|
+
onMounted(() => {
|
|
129
|
+
getActionRect();
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* 点击按钮
|
|
134
|
+
* @param index 当前按钮索引
|
|
135
|
+
*/
|
|
136
|
+
function btnClick(index: number) {
|
|
137
|
+
status.value = false;
|
|
138
|
+
// this.index为点击的几个组件,index为点击某个组件的第几个按钮(options数组的索引)
|
|
139
|
+
emit('click', props.index, index);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* movable-view元素移动事件
|
|
144
|
+
*/
|
|
145
|
+
function change(e: { detail: { x: number } }) {
|
|
146
|
+
scrollX.value = e.detail.x;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* 关闭按钮状态
|
|
151
|
+
*/
|
|
152
|
+
function close() {
|
|
153
|
+
moveX.value = 0;
|
|
154
|
+
status.value = false;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* 打开按钮的状态
|
|
159
|
+
*/
|
|
160
|
+
function open() {
|
|
161
|
+
if (props.disabled) return;
|
|
162
|
+
moveX.value = -allBtnWidth.value;
|
|
163
|
+
status.value = true;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* 用户手指离开movable-view元素,停止触摸
|
|
168
|
+
*/
|
|
169
|
+
function touchend() {
|
|
170
|
+
moveX.value = scrollX.value;
|
|
171
|
+
// 停止触摸时候,判断当前是展开还是关闭状态
|
|
172
|
+
// 关闭状态
|
|
173
|
+
// 这一步很重要,需要先给moveX一个变化的随机值,否则因为前后设置的为同一个值
|
|
174
|
+
// props单向数据流的原因,导致movable-view元素不会发生变化,切记,详见文档:
|
|
175
|
+
// https://uniapp.dcloud.io/use?id=%e5%b8%b8%e8%a7%81%e9%97%ae%e9%a2%98
|
|
176
|
+
// https://uniapp.dcloud.net.cn/tutorial/vue-api.html#componentsolutions
|
|
177
|
+
nextTick(() => {
|
|
178
|
+
if (status.value == false) {
|
|
179
|
+
// 关闭状态左滑,产生的x轴位移为负值,也就是说滑动的距离大于按钮的四分之一宽度,自动展开按钮
|
|
180
|
+
if (scrollX.value <= -allBtnWidth.value / 4) {
|
|
181
|
+
moveX.value = -allBtnWidth.value; // 按钮宽度的负值,即为展开状态movable-view元素左滑的距离
|
|
182
|
+
status.value = true; // 标志当前为展开状态
|
|
183
|
+
emitOpenEvent();
|
|
184
|
+
// 产生震动效果
|
|
185
|
+
if (props.vibrateShort) uni.vibrateShort();
|
|
186
|
+
} else {
|
|
187
|
+
moveX.value = 0; // 如果距离没有按钮宽度的四分之一,自动收起
|
|
188
|
+
status.value = false;
|
|
189
|
+
emitCloseEvent();
|
|
190
|
+
}
|
|
191
|
+
} else {
|
|
192
|
+
// 如果在打开的状态下,右滑动的距离X轴偏移超过按钮的四分之一(负值反过来的四分之三),自动收起按钮
|
|
193
|
+
if (scrollX.value > (-allBtnWidth.value * 3) / 4) {
|
|
194
|
+
moveX.value = 0;
|
|
195
|
+
nextTick(() => {
|
|
196
|
+
moveX.value = 101;
|
|
197
|
+
});
|
|
198
|
+
status.value = false;
|
|
199
|
+
emitCloseEvent();
|
|
200
|
+
} else {
|
|
201
|
+
moveX.value = -allBtnWidth.value;
|
|
202
|
+
status.value = true;
|
|
203
|
+
emitOpenEvent();
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* 触发 open 事件
|
|
211
|
+
*/
|
|
212
|
+
function emitOpenEvent() {
|
|
213
|
+
emit('open', props.index);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* 触发 close 事件
|
|
218
|
+
*/
|
|
219
|
+
function emitCloseEvent() {
|
|
220
|
+
emit('close', props.index);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* 开始触摸
|
|
225
|
+
*/
|
|
226
|
+
function touchstart() {
|
|
227
|
+
// ...可扩展触摸逻辑
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* 获取滑动区域宽度
|
|
232
|
+
*/
|
|
233
|
+
function getActionRect() {
|
|
234
|
+
$u.getRect('.u-swipe-action').then((res: { width: number }) => {
|
|
235
|
+
movableAreaWidth.value = res.width;
|
|
236
|
+
// 等视图更新完后,再显示右边的可滑动按钮,防止这些按钮会"闪一下"
|
|
237
|
+
nextTick(() => {
|
|
238
|
+
showBtn.value = true;
|
|
239
|
+
});
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* 点击内容触发事件
|
|
245
|
+
*/
|
|
246
|
+
function contentClick() {
|
|
247
|
+
// 点击内容时,如果当前为打开状态,收起组件
|
|
248
|
+
if (status.value == true) {
|
|
249
|
+
status.value = false;
|
|
250
|
+
moveX.value = 0;
|
|
251
|
+
}
|
|
252
|
+
emit('content-click', props.index);
|
|
253
|
+
}
|
|
254
|
+
</script>
|
|
255
|
+
|
|
256
|
+
<style scoped lang="scss">
|
|
257
|
+
@import '../../libs/css/style.components.scss';
|
|
258
|
+
|
|
259
|
+
.u-swipe-action {
|
|
260
|
+
width: auto;
|
|
261
|
+
height: initial;
|
|
262
|
+
position: relative;
|
|
263
|
+
overflow: hidden;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
.u-swipe-view {
|
|
267
|
+
@include vue-flex;
|
|
268
|
+
height: initial;
|
|
269
|
+
position: relative;
|
|
270
|
+
/* 这一句很关键,覆盖默认的绝对定位 */
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
.u-swipe-content {
|
|
274
|
+
flex: 1;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
.u-swipe-del {
|
|
278
|
+
position: relative;
|
|
279
|
+
font-size: 30rpx;
|
|
280
|
+
color: #ffffff;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
.u-btn-text {
|
|
284
|
+
position: absolute;
|
|
285
|
+
top: 50%;
|
|
286
|
+
left: 50%;
|
|
287
|
+
transform: translate(-50%, -50%);
|
|
288
|
+
}
|
|
289
|
+
</style>
|