cl-list-item.uvue 8.8 KB

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