cl-list-item.uvue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. defineOptions({
  125. name: "cl-list-item"
  126. });
  127. // 定义组件属性
  128. const props = defineProps({
  129. // 透传样式配置
  130. pt: {
  131. type: Object,
  132. default: () => ({})
  133. },
  134. // 图标名称
  135. icon: {
  136. type: String,
  137. default: ""
  138. },
  139. // 图标名称
  140. image: {
  141. type: String,
  142. default: ""
  143. },
  144. // 标签文本
  145. label: {
  146. type: String,
  147. default: ""
  148. },
  149. // 内容对齐方式
  150. justify: {
  151. type: String as PropType<Justify>,
  152. default: "end"
  153. },
  154. // 是否显示箭头
  155. arrow: {
  156. type: Boolean,
  157. default: false
  158. },
  159. // 是否可滑动
  160. swipeable: {
  161. type: Boolean,
  162. default: false
  163. },
  164. // 是否显示点击态
  165. hoverable: {
  166. type: Boolean,
  167. default: false
  168. },
  169. // 是否禁用
  170. disabled: {
  171. type: Boolean,
  172. default: false
  173. },
  174. // 是否显示折叠
  175. collapse: {
  176. type: Boolean,
  177. default: false
  178. }
  179. });
  180. const { proxy } = getCurrentInstance()!;
  181. const slots = useSlots();
  182. // 透传样式类型定义
  183. type PassThrough = {
  184. className?: string; // 根元素类名
  185. wrapper?: PassThroughProps; // 包裹容器样式
  186. inner?: PassThroughProps; // 内部容器样式
  187. label?: PassThroughProps; // 标签文本样式
  188. content?: PassThroughProps; // 内容区域样式
  189. icon?: ClIconProps; // 图标样式
  190. image?: ClImageProps; // 图片样式
  191. collapse?: PassThroughProps; // 折叠内容样式
  192. };
  193. // 计算透传样式
  194. const pt = computed(() => parsePt<PassThrough>(props.pt));
  195. // 滑动状态类型定义
  196. type Swipe = {
  197. width: number; // 滑动区域宽度
  198. maxX: number; // 最大滑动距离
  199. startX: number; // 开始触摸位置
  200. endX: number; // 结束触摸位置
  201. offsetX: number; // 当前偏移量
  202. direction: "right" | "left"; // 滑动方向 - right表示向右滑动显示左侧内容,left表示向左滑动显示右侧内容
  203. moveDirection: "right" | "left"; // 移动方向 - 实时记录手指滑动方向
  204. };
  205. // 滑动状态数据
  206. const swipe = reactive<Swipe>({
  207. width: 0, // 滑动区域宽度,通过查询获取
  208. maxX: 0, // 最大可滑动距离
  209. startX: 0, // 开始触摸的X坐标
  210. endX: 0, // 结束触摸的X坐标
  211. offsetX: 0, // X轴偏移量
  212. direction: "left", // 默认向左滑动
  213. moveDirection: "left" // 默认向左移动
  214. });
  215. /**
  216. * 初始化滑动状态
  217. * 根据插槽判断滑动方向并获取滑动区域宽度
  218. */
  219. function initSwipe() {
  220. if (!props.swipeable) return;
  221. // 根据是否有左侧插槽判断滑动方向
  222. swipe.direction = slots["swipe-left"] != null ? "right" : "left";
  223. // 获取滑动区域宽度
  224. uni.createSelectorQuery()
  225. .in(proxy)
  226. .select(".cl-list-item__swipe")
  227. .boundingClientRect((node) => {
  228. // 获取滑动区域的宽度,如果未获取到则默认为0
  229. swipe.width = (node as NodeInfo).width ?? 0;
  230. // 根据滑动方向(left/right)设置最大可滑动距离,左滑为负,右滑为正
  231. swipe.maxX = swipe.width * (swipe.direction == "left" ? -1 : 1);
  232. })
  233. .exec();
  234. }
  235. /**
  236. * 重置滑动状态
  237. * 将开始和结束位置重置为0
  238. */
  239. function resetSwipe() {
  240. swipe.startX = 0;
  241. swipe.endX = 0;
  242. swipe.offsetX = 0;
  243. }
  244. /**
  245. * 滑动到指定位置
  246. * @param num 目标位置
  247. * 使用requestAnimationFrame实现平滑滑动动画
  248. */
  249. function swipeTo(num: number) {
  250. swipe.offsetX = num;
  251. swipe.endX = num;
  252. }
  253. // 点击态状态
  254. const isHover = ref(false);
  255. /**
  256. * 触摸开始事件处理
  257. * @param e 触摸事件对象
  258. */
  259. function onTouchStart(e: UniTouchEvent) {
  260. isHover.value = true;
  261. // 记录开始触摸位置
  262. if (props.swipeable) {
  263. swipe.startX = (e.touches[0] as UniTouch).pageX;
  264. }
  265. }
  266. /**
  267. * 触摸结束事件处理
  268. * 根据滑动距离判断是否触发完整滑动
  269. */
  270. function onTouchEnd() {
  271. if (isHover.value) {
  272. // 计算滑动阈值 - 取滑动区域一半和50px中的较小值
  273. const threshold = swipe.width / 2 > 50 ? 50 : swipe.width / 2;
  274. // 计算实际滑动距离
  275. const offset = Math.abs(swipe.offsetX - swipe.endX);
  276. // 移除点击效果
  277. isHover.value = false;
  278. // 根据滑动距离判断是否触发滑动
  279. if (offset > threshold) {
  280. // 如果滑动方向与预设方向一致,滑动到最大位置
  281. if (swipe.direction == swipe.moveDirection) {
  282. swipeTo(swipe.maxX);
  283. } else {
  284. // 否则回到起始位置
  285. swipeTo(0);
  286. }
  287. } else {
  288. // 滑动距离不够,回到最近的位置
  289. swipeTo(swipe.endX == 0 ? 0 : swipe.maxX);
  290. }
  291. }
  292. }
  293. /**
  294. * 触摸取消事件处理
  295. */
  296. function onTouchCancel() {
  297. onTouchEnd();
  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() || isAppIOS() ? 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 transition-none;
  363. overflow: visible;
  364. &.is-transition {
  365. @apply duration-200;
  366. transition-property: transform;
  367. }
  368. }
  369. &__inner {
  370. @apply flex flex-row items-center;
  371. padding: 24rpx;
  372. }
  373. &__content {
  374. @apply flex flex-row items-center;
  375. flex: 1;
  376. }
  377. &__swipe {
  378. @apply absolute h-full;
  379. &-left {
  380. @apply left-full;
  381. transform: translateX(1rpx);
  382. }
  383. &-right {
  384. @apply right-full;
  385. }
  386. }
  387. &--disabled {
  388. @apply opacity-50;
  389. }
  390. }
  391. </style>