You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

229 lines
6.9 KiB

5 months ago
  1. <template>
  2. <!-- #ifdef MP-ALIPAY -->
  3. <!-- <import-sjs from="./alipay.sjs" name="mysjs" /> -->
  4. <!-- #endif -->
  5. <view class="u-swipe-action-item" ref="u-swipe-action-item">
  6. <view class="u-swipe-action-item__right">
  7. <slot name="button">
  8. <view v-for="(item,index) in options" :key="index" class="u-swipe-action-item__right__button"
  9. :ref="`u-swipe-action-item__right__button-${index}`" :style="[{
  10. alignItems: item.style && item.style.borderRadius ? 'center' : 'stretch'
  11. }]" @tap="buttonClickHandler(item, index)">
  12. <view class="u-swipe-action-item__right__button__wrapper" :style="[{
  13. backgroundColor: item.style && item.style.backgroundColor ? item.style.backgroundColor : '#C7C6CD',
  14. borderRadius: item.style && item.style.borderRadius ? item.style.borderRadius : '0',
  15. padding: item.style && item.style.borderRadius ? '0' : '0 15px',
  16. }, item.style]">
  17. <u-icon v-if="item.icon" :name="item.icon"
  18. :color="item.style && item.style.color ? item.style.color : '#ffffff'"
  19. :size="item.iconSize ? addUnit(item.iconSize) : item.style && item.style.fontSize ? getPx(item.style.fontSize) * 1.2 : 17"
  20. :customStyle="{
  21. marginRight: item.text ? '2px' : 0
  22. }"></u-icon>
  23. <text v-if="item.text" class="u-swipe-action-item__right__button__wrapper__text u-line-1"
  24. :style="[{
  25. color: item.style && item.style.color ? item.style.color : '#ffffff',
  26. fontSize: item.style && item.style.fontSize ? item.style.fontSize : '16px',
  27. lineHeight: item.style && item.style.fontSize ? item.style.fontSize : '16px',
  28. }]">{{ item.text }}</text>
  29. </view>
  30. </view>
  31. </slot>
  32. </view>
  33. <!-- #ifdef APP-VUE || MP-WEIXIN || MP-QQ || H5 -->
  34. <view class="u-swipe-action-item__content" @touchstart="wxs.touchstart" @touchmove="wxs.touchmove"
  35. @touchend="wxs.touchend" :status="status" :change:status="wxs.statusChange" :size="size"
  36. :change:size="wxs.sizeChange">
  37. <slot></slot>
  38. </view>
  39. <!-- #endif -->
  40. <!-- #ifdef MP-ALIPAY || MP-BAIDU || MP-TOUTIAO-->
  41. <view class="u-swipe-action-item__content" @click="clickHandler" @touchstart="touchstart" @touchmove="touchmove"
  42. @touchend="touchend" :style="sliderStyle">
  43. <slot></slot>
  44. </view>
  45. <!-- <view class="u-swipe-action-item__content" @touchstart="mysjs.touchstart" @touchmove="mysjs.touchmove"
  46. @touchend="mysjs.touchend">
  47. <slot></slot>
  48. </view> -->
  49. <!-- #endif -->
  50. <!-- #ifdef APP-NVUE -->
  51. <view class="u-swipe-action-item__content" ref="u-swipe-action-item__content" @panstart="onTouchstart"
  52. @tap="clickHandler">
  53. <slot></slot>
  54. </view>
  55. <!-- #endif -->
  56. </view>
  57. </template>
  58. <!-- #ifdef APP-VUE || MP-WEIXIN || MP-QQ || H5 -->
  59. <script src="./index.wxs" module="wxs" lang="wxs"></script>
  60. <!-- #endif -->
  61. <script>
  62. import { touchMixin } from '../../libs/mixin/touch'
  63. import { props } from './props';
  64. import { mpMixin } from '../../libs/mixin/mpMixin';
  65. import { mixin } from '../../libs/mixin/mixin';
  66. import { addUnit, getPx, sleep } from '../../libs/function/index';
  67. // #ifdef APP-NVUE
  68. import nvue from './nvue.js';
  69. // #endif
  70. // #ifdef APP-VUE || MP-WEIXIN || MP-QQ || H5
  71. import wxs from './wxs.js';
  72. // #endif
  73. // #ifdef MP-ALIPAY || MP-BAIDU || MP-TOUTIAO
  74. import other from './other.js';
  75. // #endif
  76. /**
  77. * SwipeActionItem 滑动单元格子组件
  78. * @description 该组件一般用于左滑唤出操作菜单的场景用的最多的是左滑删除操作
  79. * @tutorial https://ijry.github.io/uview-plus/components/swipeAction.html
  80. * @property {Boolean} show 控制打开或者关闭默认 false
  81. * @property {String | Number} index 标识符如果是v-for可用index索引
  82. * @property {Boolean} disabled 是否禁用默认 false
  83. * @property {Boolean} autoClose 是否自动关闭其他swipe按钮组默认 true
  84. * @property {Number} threshold 滑动距离阈值只有大于此值才被认为是要打开菜单默认 30
  85. * @property {Array} options 右侧按钮内容
  86. * @property {String | Number} duration 动画过渡时间单位ms默认 350
  87. * @event {Function(index)} open 组件打开时触发
  88. * @event {Function(index)} close 组件关闭时触发
  89. * @example <u-swipe-action><u-swipe-action-item :options="options1" ></u-swipe-action-item></u-swipe-action>
  90. */
  91. export default {
  92. name: 'u-swipe-action-item',
  93. emits: ['click'],
  94. mixins: [
  95. mpMixin,
  96. mixin,
  97. // #ifndef APP-NVUE
  98. touchMixin,
  99. // #endif
  100. // #ifdef APP-NVUE
  101. nvue,
  102. // #endif
  103. // #ifdef APP-VUE || MP-WEIXIN || H5 || MP-QQ || H5
  104. wxs,
  105. // #endif
  106. // #ifdef MP-ALIPAY || MP-BAIDU || MP-TOUTIAO
  107. other,
  108. // #endif
  109. props
  110. ],
  111. data() {
  112. return {
  113. // 按钮的尺寸信息
  114. size: {},
  115. // 父组件u-swipe-action的参数
  116. parentData: {
  117. autoClose: true,
  118. },
  119. // 当前状态,open-打开,close-关闭
  120. status: 'close',
  121. sliderStyle: {}
  122. }
  123. },
  124. watch: {
  125. // 由于wxs无法直接读取外部的值,需要在外部值变化时,重新执行赋值逻辑
  126. // #ifndef APP-NVUE
  127. wxsInit(newValue, oldValue) {
  128. this.queryRect()
  129. }
  130. // #endif
  131. },
  132. computed: {
  133. wxsInit() {
  134. return [this.disabled, this.autoClose, this.threshold, this.options, this.duration]
  135. }
  136. },
  137. mounted() {
  138. this.init()
  139. },
  140. methods: {
  141. addUnit,
  142. getPx,
  143. init() {
  144. // 初始化父组件数据
  145. this.updateParentData()
  146. // #ifndef APP-NVUE
  147. sleep().then(() => {
  148. this.queryRect()
  149. })
  150. // #endif
  151. },
  152. updateParentData() {
  153. // 此方法在mixin中
  154. this.getParentData('u-swipe-action')
  155. },
  156. // #ifndef APP-NVUE
  157. // 查询节点
  158. queryRect() {
  159. this.$uGetRect('.u-swipe-action-item__right__button', true).then(buttons => {
  160. this.size = {
  161. buttons,
  162. show: this.show,
  163. disabled: this.disabled,
  164. threshold: this.threshold,
  165. duration: this.duration
  166. }
  167. })
  168. },
  169. // #endif
  170. // 按钮被点击
  171. buttonClickHandler(item, index) {
  172. this.$emit('click', {
  173. index,
  174. name: this.name
  175. })
  176. }
  177. },
  178. }
  179. </script>
  180. <style lang="scss" scoped>
  181. @import "../../libs/css/components.scss";
  182. .u-swipe-action-item {
  183. position: relative;
  184. overflow: hidden;
  185. /* #ifndef APP-NVUE || MP-WEIXIN */
  186. touch-action: pan-y;
  187. /* #endif */
  188. &__content {
  189. transform: translateX(0px); // 修复某些情况下默认右侧按钮是展开的问题
  190. background-color: #FFFFFF;
  191. z-index: 10;
  192. }
  193. &__right {
  194. position: absolute;
  195. top: 0;
  196. bottom: 0;
  197. right: 0;
  198. @include flex;
  199. &__button {
  200. @include flex;
  201. justify-content: center;
  202. overflow: hidden;
  203. align-items: center;
  204. &__wrapper {
  205. @include flex;
  206. align-items: center;
  207. justify-content: center;
  208. padding: 0 15px;
  209. &__text {
  210. @include flex;
  211. align-items: center;
  212. color: #FFFFFF;
  213. font-size: 15px;
  214. text-align: center;
  215. justify-content: center;
  216. }
  217. }
  218. }
  219. }
  220. }
  221. </style>