cl-list-item.uvue 9.0 KB

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