tn-swipe-action-item.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <template>
  2. <view class="tn-swipe-action-item-class tn-swipe-action-item">
  3. <view class="tn-swipe-action-item__right">
  4. <slot name="button">
  5. <view
  6. v-for="(item, index) in options"
  7. :key="index"
  8. class="tn-swipe-action-item__right__button"
  9. :style="[{
  10. alignItems: item.style && item.style.borderRadius ? 'center' : 'stretch'
  11. }]"
  12. @tap="buttonClickHandler(item, index)"
  13. >
  14. <view
  15. class="tn-swipe-action-item__right__button__wrapper"
  16. :style="[{
  17. backgroundColor: item.style && item.style.backgroundColor ? item.style.backgroundColor : '#AAAAAA',
  18. borderRadius: item.style && item.style.borderRadius ? item.style.borderRadius : '0',
  19. padding: item.style && item.style.borderRadius ? '0' : '0 30rpx'
  20. }, item.style]"
  21. >
  22. <view
  23. v-if="item.icon"
  24. :class="[`tn-icon-${item.icon}`]"
  25. :style="[{
  26. color: item.style && item.style.color ? item.style.color : '#FFFFFF',
  27. fontSize: item.iconSize ? item.iconSize + 'rpx' : item.style && item.style.fontSize ? (item.style.fontsize * 1.2) + 'rpx' : '34rpx',
  28. marginRight: item.text ? '4rpx' : 0
  29. }]"
  30. ></view>
  31. <text
  32. v-if="item.text"
  33. class="tn-swipe-action-item__right__button__text tn-text-ellipsis"
  34. :style="[{
  35. color: item.style && item.style.color ? item.style.color : '#FFFFFF',
  36. fontSize: item.style && item.style.fontSize ? item.style.fontSize + 'rpx' : '32rpx',
  37. lineHeight: item.style && item.style.fontSize ? item.style.fontSize + 'rpx' : '32rpx'
  38. }]"
  39. >{{ item.text }}</text>
  40. </view>
  41. </view>
  42. </slot>
  43. </view>
  44. <view
  45. class="tn-swipe-action-item__content"
  46. :status="status"
  47. :size="size"
  48. :change:status="wxs.statusChange"
  49. :change:size="wxs.sizeChange"
  50. @touchstart="wxs.touchStart"
  51. @touchmove="wxs.touchMove"
  52. @touchend="wxs.touchEnd"
  53. >
  54. <slot></slot>
  55. </view>
  56. </view>
  57. </template>
  58. <!-- #ifdef APP-VUE || MP-WEIXIN || H5 || MP-QQ -->
  59. <script src="./index.wxs" module="wxs" lang="wxs"></script>
  60. <!-- #endif -->
  61. <script>
  62. export default {
  63. name: 'tn-swipe-action-item',
  64. props: {
  65. // 是否显示滑动菜单
  66. show: {
  67. type: Boolean,
  68. default: false
  69. },
  70. // 标识符,如果是v-for可用index的索引值
  71. name: {
  72. type: [String, Number],
  73. default: ''
  74. },
  75. // 右侧按钮内容
  76. options: {
  77. type: Array,
  78. default() {
  79. return []
  80. }
  81. },
  82. // 是否禁用
  83. disabled: {
  84. type: Boolean,
  85. default: false
  86. },
  87. // 是否自动关闭其他swipe按钮组
  88. autoClose: {
  89. type: Boolean,
  90. default: true
  91. },
  92. // 滑动距离阈值,大于此值才会打开菜单
  93. threshold: {
  94. type: Number,
  95. default: 20
  96. },
  97. // 动画过渡时间,单位ms
  98. duration: {
  99. type: Number,
  100. default: 300
  101. }
  102. },
  103. computed: {
  104. // 由于wxs无法直接读取外部的值,需要在外部值变化时,重新执行赋值逻辑
  105. itemData() {
  106. return [this.disabled, this.autoClose, this.threshold, this.options, this.duration]
  107. }
  108. },
  109. data() {
  110. return {
  111. // 按钮尺寸信息
  112. size: {},
  113. // 父组件参数
  114. parentData: {
  115. autoClose: true
  116. },
  117. // 当前状态
  118. status: 'close'
  119. }
  120. },
  121. watch: {
  122. itemData() {
  123. this.queryRect()
  124. }
  125. },
  126. created() {
  127. this.parent = false
  128. this.updateParentData()
  129. this.parent && this.parent.children.push(this)
  130. },
  131. mounted() {
  132. this.$nextTick(() => {
  133. this.init()
  134. })
  135. },
  136. methods: {
  137. init() {
  138. this.queryRect()
  139. },
  140. // 更新父组件信息
  141. updateParentData() {
  142. this.getParentData('tn-swipe-action')
  143. },
  144. // 查询节点
  145. queryRect() {
  146. this._tGetRect('.tn-swipe-action-item__right__button', true).then(res => {
  147. this.size = {
  148. buttons: res,
  149. show: this.show,
  150. disabled: this.disabled,
  151. threshold: this.threshold,
  152. duration: this.duration
  153. }
  154. })
  155. },
  156. // 按钮点击
  157. buttonClickHandler(item, index) {
  158. this.$emit('click', {
  159. type: 'button',
  160. index
  161. })
  162. },
  163. // item点击
  164. handlerItemClick() {
  165. this.$emit('click', {
  166. type: 'item',
  167. name: this.name
  168. })
  169. },
  170. // 关闭时执行
  171. closeHandler() {
  172. this.status = 'close'
  173. },
  174. // 设置状态
  175. setStatus(status) {
  176. this.status = status
  177. },
  178. // 关闭其他单元格
  179. closeOther() {
  180. this.parent && this.parent.closeOther(this)
  181. }
  182. }
  183. }
  184. </script>
  185. <style lang="scss" scoped>
  186. .tn-swipe-action-item {
  187. position: relative;
  188. overflow: hidden;
  189. // touch-action: none;
  190. &__right {
  191. display: flex;
  192. flex-direction: row;
  193. position: absolute;
  194. top: 0;
  195. bottom: 0;
  196. right: 0;
  197. &__button {
  198. display: flex;
  199. flex-direction: row;
  200. justify-content: center;
  201. align-items: center;
  202. overflow: hidden;
  203. &__wrapper {
  204. display: flex;
  205. flex-direction: row;
  206. align-items: center;
  207. justify-content: center;
  208. padding: 0 30rpx;
  209. }
  210. &__text {
  211. display: flex;
  212. flex-direction: row;
  213. align-content: center;
  214. justify-content: center;
  215. text-align: center;
  216. color: #FFFFFF;
  217. font-size: 30rpx;
  218. }
  219. }
  220. }
  221. &__content {
  222. background-color: #FFFFFF;
  223. transform: translateX(0px);
  224. }
  225. }
  226. </style>