u-dropdown.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <template>
  2. <view class="u-drawdown">
  3. <view
  4. class="u-dropdown__menu"
  5. :style="{
  6. height: $u.addUnit(height)
  7. }"
  8. ref="u-dropdown__menu"
  9. >
  10. <view
  11. class="u-dropdown__menu__item"
  12. v-for="(item, index) in menuList"
  13. :key="index"
  14. @tap.stop="clickHandler(item, index)"
  15. >
  16. <view class="u-dropdown__menu__item__content">
  17. <text
  18. class="u-dropdown__menu__item__content__text"
  19. :style="[index === current ? activeStyle : inactiveStyle]"
  20. >{{item.title}}</text>
  21. <view
  22. class="u-dropdown__menu__item__content__arrow"
  23. :class="[index === current && 'u-dropdown__menu__item__content__arrow--rotate']"
  24. >
  25. <u-icon
  26. :name="menuIcon"
  27. :size="$u.addUnit(menuIconSize)"
  28. ></u-icon>
  29. </view>
  30. </view>
  31. </view>
  32. </view>
  33. <view class="u-dropdown__content">
  34. <slot />
  35. </view>
  36. </view>
  37. </template>
  38. <script>
  39. import props from './props.js';
  40. import mpMixin from '../../libs/mixin/mpMixin.js';
  41. import mixin from '../../libs/mixin/mixin.js';
  42. /**
  43. * Dropdown
  44. * @description
  45. * @tutorial url
  46. * @property {String}
  47. * @event {Function}
  48. * @example
  49. */
  50. export default {
  51. name: 'u-dropdown',
  52. mixins: [mixin, props],
  53. data() {
  54. return {
  55. // �˵�����
  56. menuList: [],
  57. current: 0
  58. }
  59. },
  60. computed: {
  61. },
  62. created() {
  63. // �������������(u-dropdown-item)��this��������data������������������΢��С��������ѭ�����ö�����
  64. this.children = [];
  65. },
  66. methods: {
  67. clickHandler(item, index) {
  68. this.children.map(child => {
  69. if(child.title === item.title) {
  70. // this.queryRect('u-dropdown__menu').then(size => {
  71. child.$emit('click')
  72. child.setContentAnimate(child.show ? 0 : 300)
  73. child.show = !child.show
  74. // })
  75. } else {
  76. child.show = false
  77. child.setContentAnimate(0)
  78. }
  79. })
  80. },
  81. // ��ȡ��ǩ�ijߴ�λ��
  82. queryRect(el) {
  83. // #ifndef APP-NVUE
  84. // $uGetRectΪuView�Դ��Ľڵ��ѯ�򻯷���https://ijry.github.io/uview-plus/.uviewui.com/js/getRect.html
  85. // ����ڲ�һ����this.$uGetRect�������Ϊthis.$u.getRect�����߹���һ�£����Ʋ�ͬ
  86. return new Promise(resolve => {
  87. this.$uGetRect(`.${el}`).then(size => {
  88. resolve(size)
  89. })
  90. })
  91. // #endif
  92. // #ifdef APP-NVUE
  93. // nvue�£�ʹ��domģ���ѯԪ�ظ߶�
  94. // ����һ��promise���õ��ô˷�����������ʹ��then�ص�
  95. return new Promise(resolve => {
  96. dom.getComponentRect(this.$refs[el], res => {
  97. resolve(res.size)
  98. })
  99. })
  100. // #endif
  101. },
  102. },
  103. }
  104. </script>
  105. <style lang="scss">
  106. @import '../../libs/css/components.scss';
  107. .u-dropdown {
  108. &__menu {
  109. @include flex;
  110. &__item {
  111. flex: 1;
  112. @include flex;
  113. justify-content: center;
  114. &__content {
  115. @include flex;
  116. align-items: center;
  117. }
  118. }
  119. }
  120. }
  121. </style>