cl-list-item.uvue 8.8 KB

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