uv-drop-down.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <template>
  2. <uv-sticky :disabled="!isSticky">
  3. <view ref="dropDownRef" class="uv-drop-down" :style="[$uv.addStyle(customStyle)]">
  4. <slot></slot>
  5. </view>
  6. </uv-sticky>
  7. </template>
  8. <script>
  9. import mpMixin from '@/uni_modules/uv-ui-tools/libs/mixin/mpMixin.js';
  10. import mixin from '@/uni_modules/uv-ui-tools/libs/mixin/mixin.js';
  11. // #ifdef APP-NVUE
  12. const dom = uni.requireNativePlugin('dom');
  13. // #endif
  14. /**
  15. * DropDown 下拉框
  16. * @description 下拉筛选
  17. * @tutorial https://ext.dcloud.net.cn/plugin?name=uv-drop-down
  18. * @property {String | Number} sign 组件唯一标识,需要手动传
  19. * @property {Boolean} is-sticky = [true|false] 是否吸顶
  20. * @property {Array} default-value 默认值,表示参数value属于这里面的值,就说明是未选中即是默认展示的值。eg:上面示例中的{label: '全部',value: 'all'} 即是默认值。后续处理逻辑也可以根据是否是其中值进行过滤。
  21. * @property {String} textSize 每项字体大小
  22. * @property {String} textColor 每项文本颜色
  23. * @property {String} textActiveSize 每项选中状态字体大小
  24. * @property {String} textActiveColor 每项选中状态文本颜色
  25. * @property {Object} extraIcon 每项右侧图标
  26. * @property {Object} extraActiveIcon 每项选中后右侧图标
  27. */
  28. export default {
  29. name: 'uv-drop-down',
  30. mixins: [mpMixin, mixin],
  31. emits: ['click'],
  32. props: {
  33. isSticky: {
  34. type: Boolean,
  35. default: true
  36. },
  37. sign: {
  38. type: [String, Number],
  39. default: 'UVDROPDOWN'
  40. },
  41. defaultValue: {
  42. type: Array,
  43. default: () => [0, '0', 'all']
  44. },
  45. textSize: {
  46. type: String,
  47. default: '30rpx'
  48. },
  49. textColor: {
  50. type: String,
  51. default: '#333'
  52. },
  53. textActiveSize: {
  54. type: String,
  55. default: '30rpx'
  56. },
  57. textActiveColor: {
  58. type: String,
  59. default: '#3c9cff'
  60. },
  61. extraIcon: {
  62. type: Object,
  63. default () {
  64. return {
  65. name: 'arrow-down',
  66. size: '30rpx',
  67. color: '#333'
  68. }
  69. }
  70. },
  71. extraActiveIcon: {
  72. type: Object,
  73. default () {
  74. return {
  75. name: 'arrow-up',
  76. size: '30rpx',
  77. color: '#3c9cff'
  78. }
  79. }
  80. }
  81. },
  82. computed: {
  83. parentData() {
  84. return [this.defaultValue, this.textSize, this.textColor, this.textActiveColor, this.textActiveSize, this.extraIcon, this.extraActiveIcon, this.sign, this.clickHandler];
  85. }
  86. },
  87. mounted() {
  88. this.init();
  89. },
  90. methods: {
  91. init() {
  92. uni.$emit(`${this.sign}_CLICKMENU`, {
  93. show: false
  94. });
  95. this.$nextTick(async () => {
  96. const rect = await this.queryRect();
  97. uni.$emit(`${this.sign}_GETRECT`, rect);
  98. })
  99. },
  100. // 查询内容高度
  101. queryRect() {
  102. // #ifndef APP-NVUE
  103. // 组件内部一般用this.$uvGetRect,对外的为getRect,二者功能一致,名称不同
  104. return new Promise(resolve => {
  105. this.$uvGetRect(`.uv-drop-down`).then(size => {
  106. resolve(size)
  107. })
  108. })
  109. // #endif
  110. // #ifdef APP-NVUE
  111. // nvue下,使用dom模块查询元素高度
  112. // 返回一个promise,让调用此方法的主体能使用then回调
  113. return new Promise(resolve => {
  114. dom.getComponentRect(this.$refs.dropDownRef, res => {
  115. res.size.top = res.size.top <= 0 ? 0 : res.size.top;
  116. resolve(res.size)
  117. })
  118. })
  119. // #endif
  120. },
  121. clickHandler(data) {
  122. this.$emit('click', data);
  123. }
  124. }
  125. }
  126. </script>
  127. <style scoped lang="scss">
  128. @import '@/uni_modules/uv-ui-tools/libs/css/components.scss';
  129. .uv-drop-down {
  130. @include flex;
  131. justify-content: space-between;
  132. background-color: #fff;
  133. border-bottom: 1px solid #dadbde;
  134. }
  135. </style>