uv-drop-down-item.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <template>
  2. <view class="uv-drop-down-item" @click="clickHandler">
  3. <uv-text :text="label" :size="getTextStyle.size" :color="getTextStyle.color" lines="1" :custom-style="{marginRight: '10rpx',maxWidth:'200rpx'}"></uv-text>
  4. <uv-icon :name="getDownIcon.name" :size="getDownIcon.size" :color="getDownIcon.color" v-if="[1,'1'].indexOf(type)==-1"></uv-icon>
  5. </view>
  6. </template>
  7. <script>
  8. import mpMixin from '@/uni_modules/uv-ui-tools/libs/mixin/mpMixin.js';
  9. import mixin from '@/uni_modules/uv-ui-tools/libs/mixin/mixin.js';
  10. /**
  11. * DropDown 下拉框
  12. * @description 下拉筛选
  13. * @tutorial https://ext.dcloud.net.cn/plugin?name=uv-drop-down
  14. * @property {String | Number} name 字段标识
  15. * @property {String | Number} type 类型 1 没有筛选项,直接进行选中和不选中 2 有多个选项
  16. * @property {String | Number} label 筛选项的文本
  17. * @property {Boolean} isDrop 该项是否打开
  18. */
  19. export default {
  20. name: 'uv-drop-down-item',
  21. mixins: [mpMixin, mixin],
  22. emits: ['click'],
  23. props: {
  24. name: {
  25. type: [String, Number],
  26. default: ''
  27. },
  28. // 类型 1 没有筛选项,直接进行选中和不选中 2 有多个选项
  29. type: {
  30. type: [String, Number],
  31. default: '2'
  32. },
  33. // 筛选的文本
  34. label: {
  35. type: [String],
  36. default: ''
  37. },
  38. // 筛选值
  39. value: {
  40. type: [String, Number, null],
  41. default: ''
  42. },
  43. // 是否下拉菜单打开
  44. isDrop: {
  45. type: Boolean,
  46. default: false
  47. }
  48. },
  49. data() {
  50. return {
  51. parentData: {
  52. defaultValue: [0, '0', 'all'],
  53. textSize: '30rpx',
  54. textColor: '#333',
  55. textActiveSize: '30rpx',
  56. textActiveColor: '#3c9cff',
  57. extraIcon: {},
  58. extraActiveIcon: {},
  59. sign: '',
  60. clickHandler: Function
  61. },
  62. active: false,
  63. isDroped: false,
  64. elId: ''
  65. }
  66. },
  67. watch: {
  68. isDrop: {
  69. handler(newVal) {
  70. this.isDroped = newVal;
  71. },
  72. immediate: true
  73. },
  74. value: {
  75. handler(newVal) {
  76. this.active = this.parentData.defaultValue.indexOf(newVal) == -1;
  77. },
  78. immediate: true
  79. }
  80. },
  81. computed: {
  82. getDownIcon() {
  83. const style = {
  84. size: '30rpx',
  85. color: '#333',
  86. ...this.parentData.extraIcon
  87. }
  88. if (this.active || this.isDroped) {
  89. style.color = this.parentData.extraActiveIcon?.color ? this.parentData.extraActiveIcon?.color : '#3c9cff';
  90. style.size = this.parentData.extraActiveIcon?.size ? this.parentData.extraActiveIcon?.size : '30rpx';
  91. }
  92. if (this.isDroped) {
  93. style.name = this.parentData.extraActiveIcon?.name;
  94. }
  95. return style;
  96. },
  97. getTextStyle() {
  98. const style = {
  99. size: this.parentData.textSize,
  100. color: this.parentData.textColor
  101. };
  102. if (this.active || this.isDroped) {
  103. style.size = this.parentData.textActiveSize;
  104. style.color = this.parentData.textActiveColor;
  105. }
  106. return style;
  107. }
  108. },
  109. created() {
  110. this.init();
  111. },
  112. methods: {
  113. init() {
  114. this.elId = this.$uv.guid();
  115. this.getParentData('uv-drop-down');
  116. if (!this.parent) {
  117. this.$uv.error('uv-drop-down必须搭配uv-drop-down-item组件使用');
  118. }
  119. uni.$on('HANDLE_DROPDOWN_ONE', id => {
  120. if (this.isDroped && this.elId != id) {
  121. this.isDroped = false;
  122. }
  123. })
  124. uni.$on(`${this.parentData.sign}_CLOSEPOPUP`, async () => {
  125. if (this.isDroped) {
  126. this.isDroped = false;
  127. }
  128. })
  129. },
  130. async clickHandler() {
  131. let data = {};
  132. uni.$emit('HANDLE_DROPDOWN_ONE', this.elId);
  133. switch (+this.type) {
  134. case 1:
  135. this.active = !this.active;
  136. data = {
  137. name: this.name,
  138. active: this.active,
  139. type: this.type
  140. };
  141. break;
  142. case 2:
  143. this.isDroped = !this.isDroped;
  144. data = {
  145. name: this.name,
  146. active: this.isDroped,
  147. type: this.type
  148. };
  149. break;
  150. }
  151. this.parentData.clickHandler(data);
  152. this.$emit('click', data);
  153. uni.$emit(`${this.parentData.sign}_CLICKMENU`, {
  154. show: +this.type > 1 && this.isDroped
  155. });
  156. }
  157. }
  158. }
  159. </script>
  160. <style scoped lang="scss">
  161. @import '@/uni_modules/uv-ui-tools/libs/css/components.scss';
  162. .uv-drop-down-item {
  163. @include flex;
  164. align-items: center;
  165. padding: 20rpx;
  166. }
  167. </style>