uni-swipe-action-item.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <template>
  2. <!-- 在微信小程序 app vue端 h5 使用wxs 实现-->
  3. <!-- #ifdef APP-VUE || MP-WEIXIN || H5 -->
  4. <view class="uni-swipe">
  5. <!-- #ifdef MP-WEIXIN || VUE3 -->
  6. <view class="uni-swipe_box" :change:prop="wxsswipe.showWatch" :prop="is_show" :data-threshold="threshold"
  7. :data-disabled="disabled" @touchstart="wxsswipe.touchstart" @touchmove="wxsswipe.touchmove"
  8. @touchend="wxsswipe.touchend">
  9. <!-- #endif -->
  10. <!-- #ifndef MP-WEIXIN || VUE3 -->
  11. <view class="uni-swipe_box" :change:prop="renderswipe.showWatch" :prop="is_show" :data-threshold="threshold"
  12. :data-disabled="disabled+''" @touchstart="renderswipe.touchstart" @touchmove="renderswipe.touchmove"
  13. @touchend="renderswipe.touchend">
  14. <!-- #endif -->
  15. <!-- 在微信小程序 app vue端 h5 使用wxs 实现-->
  16. <view class="uni-swipe_button-group button-group--left">
  17. <slot name="left">
  18. <view v-for="(item,index) in leftOptions" :key="index" :style="{
  19. backgroundColor: item.style && item.style.backgroundColor ? item.style.backgroundColor : '#C7C6CD'
  20. }" class="uni-swipe_button button-hock" @touchstart.stop="appTouchStart"
  21. @touchend.stop="appTouchEnd($event,index,item,'left')" @click.stop="onClickForPC(index,item,'left')">
  22. <text class="uni-swipe_button-text"
  23. :style="{color: item.style && item.style.color ? item.style.color : '#FFFFFF',fontSize: item.style && item.style.fontSize ? item.style.fontSize : '16px'}">{{ item.text }}</text>
  24. </view>
  25. </slot>
  26. </view>
  27. <view class="uni-swipe_text--center">
  28. <slot></slot>
  29. </view>
  30. <view class="uni-swipe_button-group button-group--right">
  31. <slot name="right">
  32. <view v-for="(item,index) in rightOptions" :key="index" :style="{
  33. backgroundColor: item.style && item.style.backgroundColor ? item.style.backgroundColor : '#C7C6CD'
  34. }" class="uni-swipe_button button-hock" @touchstart.stop="appTouchStart"
  35. @touchend.stop="appTouchEnd($event,index,item,'right')" @click.stop="onClickForPC(index,item,'right')"><text
  36. class="uni-swipe_button-text"
  37. :style="{color: item.style && item.style.color ? item.style.color : '#FFFFFF',fontSize: item.style && item.style.fontSize ? item.style.fontSize : '16px'}">{{ item.text }}</text>
  38. </view>
  39. </slot>
  40. </view>
  41. </view>
  42. </view>
  43. <!-- #endif -->
  44. <!-- app nvue端 使用 bindingx -->
  45. <!-- #ifdef APP-NVUE -->
  46. <view ref="selector-box--hock" class="uni-swipe" @horizontalpan="touchstart" @touchend="touchend">
  47. <view ref='selector-left-button--hock' class="uni-swipe_button-group button-group--left">
  48. <slot name="left">
  49. <view v-for="(item,index) in leftOptions" :key="index" :style="{
  50. backgroundColor: item.style && item.style.backgroundColor ? item.style.backgroundColor : '#C7C6CD'
  51. }" class="uni-swipe_button button-hock" @click.stop="onClick(index,item,'left')">
  52. <text class="uni-swipe_button-text"
  53. :style="{color: item.style && item.style.color ? item.style.color : '#FFFFFF', fontSize: item.style && item.style.fontSize ? item.style.fontSize : '16px'}">
  54. {{ item.text }}
  55. </text>
  56. </view>
  57. </slot>
  58. </view>
  59. <view ref='selector-right-button--hock' class="uni-swipe_button-group button-group--right">
  60. <slot name="right">
  61. <view v-for="(item,index) in rightOptions" :key="index" :style="{
  62. backgroundColor: item.style && item.style.backgroundColor ? item.style.backgroundColor : '#C7C6CD'
  63. }" class="uni-swipe_button button-hock" @click.stop="onClick(index,item,'right')"><text
  64. class="uni-swipe_button-text"
  65. :style="{color: item.style && item.style.color ? item.style.color : '#FFFFFF',fontSize: item.style && item.style.fontSize ? item.style.fontSize : '16px'}">{{ item.text }}</text>
  66. </view>
  67. </slot>
  68. </view>
  69. <view ref='selector-content--hock' class="uni-swipe_box">
  70. <slot></slot>
  71. </view>
  72. </view>
  73. <!-- #endif -->
  74. <!-- 其他平台使用 js ,长列表性能可能会有影响-->
  75. <!-- #ifdef MP-ALIPAY || MP-BAIDU || MP-TOUTIAO || MP-QQ -->
  76. <view class="uni-swipe">
  77. <view class="uni-swipe_box" @touchstart="touchstart" @touchmove="touchmove" @touchend="touchend"
  78. :style="{transform:moveLeft}" :class="{ani:ani}">
  79. <view class="uni-swipe_button-group button-group--left" :class="[elClass]">
  80. <slot name="left">
  81. <view v-for="(item,index) in leftOptions" :key="index" :style="{
  82. backgroundColor: item.style && item.style.backgroundColor ? item.style.backgroundColor : '#C7C6CD',
  83. fontSize: item.style && item.style.fontSize ? item.style.fontSize : '16px'
  84. }" class="uni-swipe_button button-hock" @touchstart.stop="appTouchStart"
  85. @touchend.stop="appTouchEnd($event,index,item,'left')"><text class="uni-swipe_button-text"
  86. :style="{color: item.style && item.style.color ? item.style.color : '#FFFFFF',}">{{ item.text }}</text>
  87. </view>
  88. </slot>
  89. </view>
  90. <slot></slot>
  91. <view class="uni-swipe_button-group button-group--right" :class="[elClass]">
  92. <slot name="right">
  93. <view v-for="(item,index) in rightOptions" :key="index" :style="{
  94. backgroundColor: item.style && item.style.backgroundColor ? item.style.backgroundColor : '#C7C6CD',
  95. fontSize: item.style && item.style.fontSize ? item.style.fontSize : '16px'
  96. }" @touchstart.stop="appTouchStart" @touchend.stop="appTouchEnd($event,index,item,'right')"
  97. class="uni-swipe_button button-hock"><text class="uni-swipe_button-text"
  98. :style="{color: item.style && item.style.color ? item.style.color : '#FFFFFF',}">{{ item.text }}</text>
  99. </view>
  100. </slot>
  101. </view>
  102. </view>
  103. </view>
  104. <!-- #endif -->
  105. </template>
  106. <script src="./wx.wxs" module="wxsswipe" lang="wxs"></script>
  107. <script module="renderswipe" lang="renderjs">
  108. import render from './render.js'
  109. export default {
  110. mounted(e, ins, owner) {
  111. this.state = {}
  112. },
  113. methods: {
  114. showWatch(newVal, oldVal, ownerInstance, instance) {
  115. render.showWatch(newVal, oldVal, ownerInstance, instance, this)
  116. },
  117. touchstart(e, ownerInstance) {
  118. render.touchstart(e, ownerInstance, this)
  119. },
  120. touchmove(e, ownerInstance) {
  121. render.touchmove(e, ownerInstance, this)
  122. },
  123. touchend(e, ownerInstance) {
  124. render.touchend(e, ownerInstance, this)
  125. }
  126. }
  127. }
  128. </script>
  129. <script>
  130. import mpwxs from './mpwxs'
  131. import bindingx from './bindingx.js'
  132. import mpother from './mpother'
  133. /**
  134. * SwipeActionItem 滑动操作子组件
  135. * @description 通过滑动触发选项的容器
  136. * @tutorial https://ext.dcloud.net.cn/plugin?id=181
  137. * @property {Boolean} show = [left|right|none] 开启关闭组件,auto-close = false 时生效
  138. * @property {Boolean} disabled = [true|false] 是否禁止滑动
  139. * @property {Boolean} autoClose = [true|false] 滑动打开当前组件,是否关闭其他组件
  140. * @property {Number} threshold 滑动缺省值
  141. * @property {Array} leftOptions 左侧选项内容及样式
  142. * @property {Array} rgihtOptions 右侧选项内容及样式
  143. * @event {Function} click 点击选项按钮时触发事件,e = {content,index} ,content(点击内容)、index(下标)
  144. * @event {Function} change 组件打开或关闭时触发,left\right\none
  145. */
  146. export default {
  147. mixins: [mpwxs, bindingx, mpother],
  148. emits: ['click', 'change'],
  149. props: {
  150. // 控制开关
  151. show: {
  152. type: String,
  153. default: 'none'
  154. },
  155. // 禁用
  156. disabled: {
  157. type: Boolean,
  158. default: false
  159. },
  160. // 是否自动关闭
  161. autoClose: {
  162. type: Boolean,
  163. default: true
  164. },
  165. // 滑动缺省距离
  166. threshold: {
  167. type: Number,
  168. default: 20
  169. },
  170. // 左侧按钮内容
  171. leftOptions: {
  172. type: Array,
  173. default () {
  174. return []
  175. }
  176. },
  177. // 右侧按钮内容
  178. rightOptions: {
  179. type: Array,
  180. default () {
  181. return []
  182. }
  183. }
  184. },
  185. // #ifndef VUE3
  186. // TODO vue2
  187. destroyed() {
  188. if (this.__isUnmounted) return
  189. this.uninstall()
  190. },
  191. // #endif
  192. // #ifdef VUE3
  193. // TODO vue3
  194. unmounted() {
  195. this.__isUnmounted = true
  196. this.uninstall()
  197. },
  198. // #endif
  199. methods: {
  200. uninstall() {
  201. if (this.swipeaction) {
  202. this.swipeaction.children.forEach((item, index) => {
  203. if (item === this) {
  204. this.swipeaction.children.splice(index, 1)
  205. }
  206. })
  207. }
  208. },
  209. /**
  210. * 获取父元素实例
  211. */
  212. getSwipeAction(name = 'uniSwipeAction') {
  213. let parent = this.$parent;
  214. let parentName = parent.$options.name;
  215. while (parentName !== name) {
  216. parent = parent.$parent;
  217. if (!parent) return false;
  218. parentName = parent.$options.name;
  219. }
  220. return parent;
  221. }
  222. }
  223. }
  224. </script>
  225. <style lang="scss">
  226. .uni-swipe {
  227. position: relative;
  228. /* #ifndef APP-NVUE */
  229. overflow: hidden;
  230. /* #endif */
  231. }
  232. .uni-swipe_box {
  233. /* #ifndef APP-NVUE */
  234. display: flex;
  235. flex-shrink: 0;
  236. // touch-action: none;
  237. /* #endif */
  238. position: relative;
  239. }
  240. .uni-swipe_content {
  241. // border: 1px red solid;
  242. }
  243. .uni-swipe_text--center {
  244. width: 100%;
  245. /* #ifndef APP-NVUE */
  246. cursor: grab;
  247. /* #endif */
  248. }
  249. .uni-swipe_button-group {
  250. /* #ifndef APP-NVUE */
  251. box-sizing: border-box;
  252. display: flex;
  253. /* #endif */
  254. flex-direction: column;
  255. position: absolute;
  256. top: 0;
  257. bottom: 0;
  258. align-items: center;
  259. justify-content:space-between;
  260. /* #ifdef H5 */
  261. cursor: pointer;
  262. /* #endif */
  263. }
  264. .button-group--left {
  265. left: 0;
  266. transform: translateX(-100%)
  267. }
  268. .button-group--right {
  269. right: 0;
  270. transform: translateX(100%)
  271. }
  272. .uni-swipe_button {
  273. /* #ifdef APP-NVUE */
  274. flex: 1;
  275. /* #endif */
  276. /* #ifndef APP-NVUE */
  277. display: flex;
  278. /* #endif */
  279. flex-direction: row;
  280. justify-content: center;
  281. align-items: center;
  282. padding: 0 9px;
  283. }
  284. .uni-swipe_button-text {
  285. /* #ifndef APP-NVUE */
  286. flex-shrink: 0;
  287. /* #endif */
  288. font-size: 12rpx;
  289. padding-bottom: 2px;
  290. }
  291. .ani {
  292. transition-property: transform;
  293. transition-duration: 0.3s;
  294. transition-timing-function: cubic-bezier(0.165, 0.84, 0.44, 1);
  295. }
  296. /* #ifdef MP-ALIPAY */
  297. .movable-area {
  298. /* width: 100%; */
  299. height: 45px;
  300. }
  301. .movable-view {
  302. display: flex;
  303. /* justify-content: center; */
  304. position: relative;
  305. flex: 1;
  306. height: 45px;
  307. z-index: 2;
  308. }
  309. .movable-view-button {
  310. display: flex;
  311. flex-shrink: 0;
  312. flex-direction: row;
  313. height: 100%;
  314. background: #C0C0C0;
  315. }
  316. /* .transition {
  317. transition: all 0.3s;
  318. } */
  319. .movable-view-box {
  320. flex-shrink: 0;
  321. height: 100%;
  322. background-color: #fff;
  323. }
  324. /* #endif */
  325. </style>