cl-list-item.uvue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. <template>
  2. <view
  3. class="cl-list-item"
  4. :class="[
  5. {
  6. 'cl-list-item--disabled': disabled
  7. },
  8. pt.className
  9. ]"
  10. @touchstart="onTouchStart"
  11. @touchend="onTouchEnd"
  12. @touchmove="onTouchMove"
  13. @touchcancel="onTouchCancel"
  14. >
  15. <view
  16. class="cl-list-item__wrapper"
  17. :class="[
  18. {
  19. 'is-transition': !isHover,
  20. [isDark ? 'bg-surface-800' : 'bg-white']: true,
  21. [isDark ? '!bg-surface-700' : '!bg-surface-50']: hoverable && isHover
  22. }
  23. ]"
  24. :style="{
  25. transform: `translateX(${swipe.offsetX}px)`
  26. }"
  27. @tap="onTap"
  28. >
  29. <view class="cl-list-item__inner" :class="[pt.inner?.className]">
  30. <slot name="icon">
  31. <cl-icon
  32. v-if="icon != ''"
  33. :name="icon"
  34. :size="pt.icon?.size ?? 36"
  35. :color="pt.icon?.color"
  36. :pt="{
  37. className: `mr-3 ${pt.icon?.className}`
  38. }"
  39. ></cl-icon>
  40. </slot>
  41. <slot name="image">
  42. <cl-image
  43. v-if="image != ''"
  44. :width="pt.image?.width ?? 36"
  45. :height="pt.image?.height ?? 36"
  46. :src="image"
  47. :pt="{
  48. className: `mr-3 rounded-full ${pt.image?.className}`
  49. }"
  50. ></cl-image>
  51. </slot>
  52. <cl-text
  53. v-if="label != ''"
  54. :pt="{
  55. className: parseClass([
  56. 'cl-list-item__label whitespace-nowrap overflow-visible',
  57. [justify == 'start', 'w-24'],
  58. pt.label?.className
  59. ])
  60. }"
  61. >
  62. {{ label }}
  63. </cl-text>
  64. <view
  65. class="cl-list-item__content"
  66. :class="[
  67. {
  68. 'justify-start': justify == 'start',
  69. 'justify-center': justify == 'center',
  70. 'justify-end': justify == 'end'
  71. },
  72. pt.content?.className
  73. ]"
  74. >
  75. <slot></slot>
  76. </view>
  77. <cl-icon
  78. name="arrow-right-s-line"
  79. :size="36"
  80. :pt="{
  81. className: parseClass([
  82. 'text-surface-400 ml-1 duration-200',
  83. {
  84. 'rotate-90': isCollapse
  85. }
  86. ])
  87. }"
  88. v-if="arrow"
  89. ></cl-icon>
  90. </view>
  91. <view
  92. :class="['cl-list-item__swipe', `cl-list-item__swipe-${swipe.direction}`]"
  93. v-if="swipeable"
  94. >
  95. <slot name="swipe-left"></slot>
  96. <slot name="swipe-right"></slot>
  97. </view>
  98. </view>
  99. <cl-collapse
  100. v-model="isCollapse"
  101. :pt="{
  102. className: parseClass(['p-[24rpx]', pt.collapse?.className])
  103. }"
  104. >
  105. <slot name="collapse"></slot>
  106. </cl-collapse>
  107. </view>
  108. </template>
  109. <script lang="ts" setup>
  110. import {
  111. computed,
  112. getCurrentInstance,
  113. onMounted,
  114. reactive,
  115. ref,
  116. useSlots,
  117. type PropType
  118. } from "vue";
  119. import { isAppIOS, isDark, isHarmony, parseClass, parsePt } from "@/cool";
  120. import type { Justify, PassThroughProps } from "../../types";
  121. import type { ClIconProps } from "../cl-icon/props";
  122. import type { ClImageProps } from "../cl-image/props";
  123. defineOptions({
  124. name: "cl-list-item"
  125. });
  126. // 定义组件属性
  127. const props = defineProps({
  128. // 透传样式配置
  129. pt: {
  130. type: Object,
  131. default: () => ({})
  132. },
  133. // 图标名称
  134. icon: {
  135. type: String,
  136. default: ""
  137. },
  138. // 图标名称
  139. image: {
  140. type: String,
  141. default: ""
  142. },
  143. // 标签文本
  144. label: {
  145. type: String,
  146. default: ""
  147. },
  148. // 内容对齐方式
  149. justify: {
  150. type: String as PropType<Justify>,
  151. default: "end"
  152. },
  153. // 是否显示箭头
  154. arrow: {
  155. type: Boolean,
  156. default: false
  157. },
  158. // 是否可滑动
  159. swipeable: {
  160. type: Boolean,
  161. default: false
  162. },
  163. // 是否显示点击态
  164. hoverable: {
  165. type: Boolean,
  166. default: false
  167. },
  168. // 是否禁用
  169. disabled: {
  170. type: Boolean,
  171. default: false
  172. },
  173. // 是否显示折叠
  174. collapse: {
  175. type: Boolean,
  176. default: false
  177. }
  178. });
  179. const { proxy } = getCurrentInstance()!;
  180. const slots = useSlots();
  181. // 透传样式类型定义 - 用于定义可以传入的样式属性类型
  182. type PassThrough = {
  183. className?: string; // 根元素类名
  184. inner?: PassThroughProps; // 内部容器样式
  185. label?: PassThroughProps; // 标签文本样式
  186. content?: PassThroughProps; // 内容区域样式
  187. icon?: ClIconProps; // 图标样式
  188. image?: ClImageProps; // 图片样式
  189. collapse?: PassThroughProps; // 折叠内容样式
  190. };
  191. // 计算透传样式 - 解析传入的样式配置
  192. const pt = computed(() => parsePt<PassThrough>(props.pt));
  193. // 滑动状态类型定义 - 用于管理滑动相关的状态数据
  194. type Swipe = {
  195. width: number; // 滑动区域宽度
  196. maxX: number; // 最大滑动距离
  197. startX: number; // 开始触摸位置
  198. endX: number; // 结束触摸位置
  199. offsetX: number; // 当前偏移量
  200. direction: "right" | "left"; // 滑动方向 - right表示向右滑动显示左侧内容,left表示向左滑动显示右侧内容
  201. moveDirection: "right" | "left"; // 移动方向 - 实时记录手指滑动方向
  202. };
  203. // 滑动状态数据 - 初始化滑动状态
  204. const swipe = reactive<Swipe>({
  205. width: 0, // 滑动区域宽度,通过查询获取
  206. maxX: 0, // 最大可滑动距离
  207. startX: 0, // 开始触摸的X坐标
  208. endX: 0, // 结束触摸的X坐标
  209. offsetX: 0, // X轴偏移量
  210. direction: "left", // 默认向左滑动
  211. moveDirection: "left" // 默认向左移动
  212. });
  213. /**
  214. * 初始化滑动状态
  215. * 根据插槽判断滑动方向并获取滑动区域宽度
  216. */
  217. function initSwipe() {
  218. if (!props.swipeable) return;
  219. // 根据是否有左侧插槽判断滑动方向
  220. swipe.direction = slots["swipe-left"] != null ? "right" : "left";
  221. // 获取滑动区域宽度
  222. uni.createSelectorQuery()
  223. .in(proxy)
  224. .select(".cl-list-item__swipe")
  225. .boundingClientRect((node) => {
  226. // 获取滑动区域的宽度,如果未获取到则默认为0
  227. swipe.width = (node as NodeInfo).width ?? 0;
  228. // 根据滑动方向(left/right)设置最大可滑动距离,左滑为负,右滑为正
  229. swipe.maxX = swipe.width * (swipe.direction == "left" ? -1 : 1);
  230. })
  231. .exec();
  232. }
  233. /**
  234. * 重置滑动状态
  235. * 将开始和结束位置重置为0
  236. */
  237. function resetSwipe() {
  238. swipe.startX = 0;
  239. swipe.endX = 0;
  240. swipe.offsetX = 0;
  241. }
  242. /**
  243. * 滑动到指定位置
  244. * @param num 目标位置
  245. * 使用requestAnimationFrame实现平滑滑动动画
  246. */
  247. function swipeTo(num: number) {
  248. // #ifdef APP
  249. function next() {
  250. requestAnimationFrame(() => {
  251. if (swipe.offsetX != num) {
  252. // 计算每次移动的距离
  253. const step = 2;
  254. const direction = swipe.offsetX < num ? 1 : -1;
  255. // 更新偏移量
  256. swipe.offsetX += step * direction;
  257. // 防止过度滑动
  258. if (direction > 0 ? swipe.offsetX > num : swipe.offsetX < num) {
  259. swipe.offsetX = num;
  260. }
  261. next();
  262. } else {
  263. // 动画结束,更新结束位置
  264. swipe.endX = swipe.offsetX;
  265. }
  266. });
  267. }
  268. next();
  269. // #endif
  270. // #ifdef H5 || MP
  271. swipe.offsetX = num;
  272. swipe.endX = num;
  273. // #endif
  274. }
  275. // 点击态状态 - 用于显示点击效果
  276. const isHover = ref(false);
  277. /**
  278. * 触摸开始事件处理
  279. * @param e 触摸事件对象
  280. */
  281. function onTouchStart(e: UniTouchEvent) {
  282. isHover.value = true;
  283. // 记录开始触摸位置
  284. if (props.swipeable) {
  285. swipe.startX = (e.touches[0] as UniTouch).pageX;
  286. }
  287. }
  288. /**
  289. * 触摸结束事件处理
  290. * 根据滑动距离判断是否触发完整滑动
  291. */
  292. function onTouchEnd() {
  293. if (isHover.value) {
  294. // 计算滑动阈值 - 取滑动区域一半和50px中的较小值
  295. const threshold = swipe.width / 2 > 50 ? 50 : swipe.width / 2;
  296. // 计算实际滑动距离
  297. const offset = Math.abs(swipe.offsetX - swipe.endX);
  298. // 移除点击效果
  299. isHover.value = false;
  300. // 根据滑动距离判断是否触发滑动
  301. if (offset > threshold) {
  302. // 如果滑动方向与预设方向一致,滑动到最大位置
  303. if (swipe.direction == swipe.moveDirection) {
  304. swipeTo(swipe.maxX);
  305. } else {
  306. // 否则回到起始位置
  307. swipeTo(0);
  308. }
  309. } else {
  310. // 滑动距离不够,回到最近的位置
  311. swipeTo(swipe.endX == 0 ? 0 : swipe.maxX);
  312. }
  313. }
  314. }
  315. /**
  316. * 触摸取消事件处理
  317. */
  318. function onTouchCancel() {
  319. onTouchEnd();
  320. isHover.value = false; // 移除点击效果
  321. }
  322. /**
  323. * 触摸移动事件处理
  324. * @param e 触摸事件对象
  325. */
  326. function onTouchMove(e: UniTouchEvent) {
  327. if (isHover.value) {
  328. // 计算滑动偏移量
  329. const offsetX = (e.touches[0] as UniTouch).pageX - swipe.startX;
  330. // 根据偏移量判断滑动方向
  331. swipe.moveDirection = offsetX > 0 ? "right" : "left";
  332. // 计算目标位置
  333. let x = offsetX + swipe.endX;
  334. // 限制滑动范围
  335. if (swipe.direction == "right") {
  336. // 向右滑动时的边界处理
  337. if (x > swipe.maxX) {
  338. x = swipe.maxX;
  339. }
  340. if (x < 0) {
  341. x = 0;
  342. }
  343. }
  344. if (swipe.direction == "left") {
  345. // 向左滑动时的边界处理
  346. if (x < swipe.maxX) {
  347. x = swipe.maxX;
  348. }
  349. if (x > 0) {
  350. x = 0;
  351. }
  352. }
  353. // 更新偏移量
  354. swipe.offsetX = x;
  355. }
  356. }
  357. // 折叠状态
  358. const isCollapse = ref(false);
  359. /**
  360. * 点击事件处理
  361. */
  362. function onTap() {
  363. if (props.collapse) {
  364. isCollapse.value = !isCollapse.value;
  365. }
  366. }
  367. onMounted(() => {
  368. setTimeout(
  369. () => {
  370. initSwipe();
  371. },
  372. isHarmony() || isAppIOS() ? 50 : 0
  373. );
  374. });
  375. defineExpose({
  376. initSwipe,
  377. resetSwipe
  378. });
  379. </script>
  380. <style lang="scss" scoped>
  381. .cl-list-item {
  382. @apply flex flex-col w-full relative;
  383. &__wrapper {
  384. @apply w-full;
  385. overflow: visible;
  386. &.is-transition {
  387. // #ifdef H5 || MP
  388. transition-property: transform;
  389. transition-duration: 0.2s;
  390. // #endif
  391. }
  392. }
  393. &__inner {
  394. @apply flex flex-row items-center;
  395. padding: 24rpx;
  396. }
  397. &__content {
  398. @apply flex flex-row items-center;
  399. flex: 1;
  400. }
  401. &__swipe {
  402. @apply absolute h-full;
  403. &-left {
  404. @apply left-full;
  405. transform: translateX(1rpx);
  406. }
  407. &-right {
  408. @apply right-full;
  409. }
  410. }
  411. &--disabled {
  412. @apply opacity-50;
  413. }
  414. }
  415. </style>