cl-list-item.uvue 8.9 KB

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