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.

67 lines
2.0 KiB

9 months ago
  1. <template>
  2. <view class="u-swipe-action">
  3. <slot></slot>
  4. </view>
  5. </template>
  6. <script>
  7. import props from './props.js';
  8. /**
  9. * SwipeAction 滑动单元格
  10. * @description 该组件一般用于左滑唤出操作菜单的场景用的最多的是左滑删除操作
  11. * @tutorial https://www.uviewui.com/components/swipeAction.html
  12. * @property {Boolean} autoClose 是否自动关闭其他swipe按钮组
  13. * @event {Function(index)} click 点击组件时触发
  14. * @example <u-swipe-action><u-swipe-action-item :rightOptions="options1" ></u-swipe-action-item></u-swipe-action>
  15. */
  16. export default {
  17. name: 'u-swipe-action',
  18. mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
  19. data() {
  20. return {}
  21. },
  22. provide() {
  23. return {
  24. swipeAction: this
  25. }
  26. },
  27. computed: {
  28. // 这里computed的变量,都是子组件u-swipe-action-item需要用到的,由于头条小程序的兼容性差异,子组件无法实时监听父组件参数的变化
  29. // 所以需要手动通知子组件,这里返回一个parentData变量,供watch监听,在其中去通知每一个子组件重新从父组件(u-swipe-action-item)
  30. // 拉取父组件新的变化后的参数
  31. parentData() {
  32. return [this.autoClose]
  33. }
  34. },
  35. watch: {
  36. // 当父组件需要子组件需要共享的参数发生了变化,手动通知子组件
  37. parentData() {
  38. if (this.children.length) {
  39. this.children.map(child => {
  40. // 判断子组件(u-swipe-action-item)如果有updateParentData方法的话,就就执行(执行的结果是子组件重新从父组件拉取了最新的值)
  41. typeof(child.updateParentData) === 'function' && child.updateParentData()
  42. })
  43. }
  44. },
  45. },
  46. created() {
  47. this.children = []
  48. },
  49. methods: {
  50. closeOther(child) {
  51. if (this.autoClose) {
  52. // 历遍所有的单元格,找出非当前操作中的单元格,进行关闭
  53. this.children.map((item, index) => {
  54. if (child !== item) {
  55. item.closeHandler()
  56. }
  57. })
  58. }
  59. }
  60. }
  61. }
  62. </script>
  63. <style lang="scss" scoped>
  64. </style>