cl-list-item.uvue 8.7 KB

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