u-swipe-action-item.vue 6.0 KB

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