uv-drop-down-item.vue 4.4 KB

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