u-dropdown-item.vue 3.4 KB

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