actionsheet.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <template>
  2. <div class="nut-actionsheet">
  3. <transition :name="isAnimation?'nutFade':''" v-if="isShowMask">
  4. <div class="nut-actionsheet-mask"
  5. @click="clickActionSheetMask"
  6. v-show="isVisible"
  7. ></div>
  8. </transition>
  9. <transition :name="isAnimation?'nutSlideUp':''">
  10. <div class="nut-actionsheet-panel"
  11. v-show="isVisible"
  12. >
  13. <div class="nut-actionsheet-custom" slot-scope>
  14. <slot name="custom" ></slot>
  15. </div>
  16. <dl class="nut-actionsheet-modal" v-if="$slots.title || $slots.subTitle ">
  17. <dt class="nut-actionsheet-title"><slot name="title" slot-scope></slot></dt>
  18. <dd class="nut-actionsheet-sub-title"><slot name="sub-title" slot-scope></slot></dd>
  19. </dl>
  20. <ul class="nut-actionsheet-menu" >
  21. <li class="nut-actionsheet-item"
  22. :class="{'nut-actionsheet-item-active': isHighlight(item), 'nut-actionsheet-item-disabled': item.disable}"
  23. v-for="(item, index) of menuItems"
  24. :key="index"
  25. @click="chooseItem(item, index)"
  26. >{{item[optionTag]}}</li>
  27. </ul>
  28. <div class="nut-actionsheet-cancel"
  29. v-if="cancelTxt"
  30. @click="cancelActionSheet"
  31. >{{cancelTxt}}</div>
  32. </div>
  33. </transition>
  34. </div>
  35. </template>
  36. <script>
  37. export default {
  38. name:'nut-actionsheet',
  39. props: {
  40. isAnimation: {
  41. type: Boolean,
  42. default: true
  43. },
  44. isLockBgScroll: {
  45. type: Boolean,
  46. default: false
  47. },
  48. isVisible: {
  49. type: Boolean,
  50. default: false
  51. },
  52. isShowMask: {
  53. type: Boolean,
  54. default: true
  55. },
  56. isClickChooseClose: {
  57. type: Boolean,
  58. default: true
  59. },
  60. isClickCloseMask: {
  61. type: Boolean,
  62. default: true
  63. },
  64. cancelTxt: {
  65. type: String,
  66. default: ''
  67. },
  68. optionTag: {
  69. type: String,
  70. default: 'name'
  71. },
  72. chooseTagValue: {
  73. type: String,
  74. default: ''
  75. },
  76. menuItems: {
  77. type: Array,
  78. default: () => []
  79. }
  80. },
  81. data() {
  82. return {};
  83. },
  84. watch: {
  85. isVisible (value) {
  86. !!value && this.$emit('open');
  87. if (this.isLockBgScroll) {
  88. if (value) {
  89. document.body.classList.add('nut-overflow-hidden');
  90. } else {
  91. document.body.classList.remove('nut-overflow-hidden');
  92. }
  93. }
  94. }
  95. },
  96. methods: {
  97. isHighlight(item) {
  98. return (this.chooseTagValue && this.chooseTagValue == item[this.optionTag]) || this.chooseTagValue === 0;
  99. },
  100. closeActionSheet() {
  101. this.$emit('close');
  102. },
  103. cancelActionSheet() {
  104. this.closeActionSheet();
  105. this.$emit('cancel');
  106. },
  107. clickActionSheetMask() {
  108. this.isClickCloseMask && this.closeActionSheet();
  109. },
  110. chooseItem(item, index) {
  111. if (!item.disable) {
  112. if (this.isClickChooseClose) {
  113. this.closeActionSheet();
  114. }
  115. this.$emit('choose', item, index);
  116. }
  117. }
  118. }
  119. }
  120. </script>