u-dropdown-item.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <template>
  2. <view class="u-drawdown-item">
  3. <u-overlay
  4. customStyle="top: 126px"
  5. :show="show"
  6. :closeOnClickOverlay="closeOnClickOverlay"
  7. @click="overlayClick"
  8. ></u-overlay>
  9. <view
  10. class="u-drawdown-item__content"
  11. :style="[style]"
  12. :animation="animationData"
  13. ref="animation"
  14. >
  15. <slot />
  16. </view>
  17. </view>
  18. </template>
  19. <script>
  20. // #ifdef APP-NVUE
  21. const animation = uni.requireNativePlugin('animation')
  22. const dom = uni.requireNativePlugin('dom')
  23. // #endif
  24. import props from './props.js';
  25. /**
  26. * Drawdownitem
  27. * @description
  28. * @tutorial url
  29. * @property {String}
  30. * @event {Function}
  31. * @example
  32. */
  33. export default {
  34. name: 'u-drawdown-item',
  35. mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
  36. data() {
  37. return {
  38. show: false,
  39. top: '126px',
  40. // uni.createAnimation的导出数据
  41. animationData: {},
  42. }
  43. },
  44. mounted() {
  45. this.init()
  46. },
  47. watch: {
  48. // 发生变化时,需要去更新父组件对应的值
  49. dataChange(newValue, oldValue) {
  50. this.updateParentData()
  51. }
  52. },
  53. computed: {
  54. // 监听对应变量的变化
  55. dataChange() {
  56. return [this.title, this.disabled]
  57. },
  58. style() {
  59. const style = {
  60. zIndex: 10071,
  61. position: 'fixed',
  62. display: 'flex',
  63. left: 0,
  64. right: 0
  65. }
  66. style.top = uni.$u.addUnit(this.top)
  67. return style
  68. }
  69. },
  70. methods: {
  71. init() {
  72. this.updateParentData()
  73. },
  74. // 更新父组件所需的数据
  75. updateParentData() {
  76. // 获取父组件u-dropdown
  77. this.getParentData('u-dropdown')
  78. if (!this.parent) uni.$u.error('u-dropdown-item必须配合u-dropdown使用')
  79. // 查找父组件menuList数组中对应的标题数据
  80. const menuIndex = this.parent.menuList.findIndex(item => item.title === this.title)
  81. const menuContent = {
  82. title: this.title,
  83. disabled: this.disabled
  84. }
  85. if (menuIndex >= 0) {
  86. // 如果能找到,则直接修改
  87. this.parent.menuList[menuIndex] = menuContent;
  88. } else {
  89. // 如果无法找到,则为第一次添加,直接push即可
  90. this.parent.menuList.push(menuContent);
  91. }
  92. },
  93. async setContentAnimate(height) {
  94. this.animating = true
  95. // #ifdef APP-NVUE
  96. const ref = this.$refs['animation'].ref
  97. animation.transition(ref, {
  98. styles: {
  99. height: uni.$u.addUnit(height)
  100. },
  101. duration: this.duration,
  102. timingFunction: 'ease-in-out',
  103. }, () => {
  104. this.animating = false
  105. })
  106. // #endif
  107. // #ifndef APP-NVUE
  108. const animation = uni.createAnimation({
  109. timingFunction: 'ease-in-out',
  110. });
  111. animation
  112. .height(height)
  113. .step({
  114. duration: this.duration,
  115. })
  116. .step()
  117. // 导出动画数据给面板的animationData值
  118. this.animationData = animation.export()
  119. // 标识动画结束
  120. uni.$u.sleep(this.duration).then(() => {
  121. this.animating = false
  122. })
  123. // #endif
  124. },
  125. overlayClick() {
  126. this.show = false
  127. this.setContentAnimate(0)
  128. }
  129. },
  130. }
  131. </script>
  132. <style lang="scss" scoped>
  133. @import '../../libs/css/components.scss';
  134. .u-drawdown-item {
  135. &__content {
  136. background-color: #FFFFFF;
  137. overflow: hidden;
  138. height: 0;
  139. }
  140. }
  141. </style>