建材商城系统20241014
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
4.2 KiB

4 months ago
  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.$nextTick(()=>{
  77. this.active = this.parentData.defaultValue.indexOf(newVal) == -1;
  78. })
  79. },
  80. immediate: true
  81. }
  82. },
  83. computed: {
  84. getDownIcon() {
  85. const style = {
  86. size: '30rpx',
  87. color: '#333',
  88. ...this.parentData.extraIcon
  89. }
  90. if (this.active || this.isDroped) {
  91. style.color = this.parentData.extraActiveIcon?.color ? this.parentData.extraActiveIcon?.color : '#3c9cff';
  92. style.size = this.parentData.extraActiveIcon?.size ? this.parentData.extraActiveIcon?.size : '30rpx';
  93. }
  94. if (this.isDroped) {
  95. style.name = this.parentData.extraActiveIcon?.name;
  96. }
  97. return style;
  98. },
  99. getTextStyle() {
  100. const style = {
  101. size: this.parentData.textSize,
  102. color: this.parentData.textColor
  103. };
  104. if (this.active || this.isDroped) {
  105. style.size = this.parentData.textActiveSize;
  106. style.color = this.parentData.textActiveColor;
  107. }
  108. return style;
  109. }
  110. },
  111. created() {
  112. this.init();
  113. },
  114. methods: {
  115. init() {
  116. this.elId = this.$uv.guid();
  117. this.getParentData('uv-drop-down');
  118. if (!this.parent) {
  119. this.$uv.error('uv-drop-down必须搭配uv-drop-down-item组件使用');
  120. }
  121. uni.$on('HANDLE_DROPDOWN_ONE', id => {
  122. if (this.isDroped && this.elId != id) {
  123. this.isDroped = false;
  124. }
  125. })
  126. uni.$on(`${this.parentData.sign}_CLOSEPOPUP`, async () => {
  127. if (this.isDroped) {
  128. this.isDroped = false;
  129. }
  130. })
  131. },
  132. async clickHandler() {
  133. let data = {};
  134. uni.$emit('HANDLE_DROPDOWN_ONE', this.elId);
  135. switch (+this.type) {
  136. case 1:
  137. this.active = !this.active;
  138. data = {
  139. name: this.name,
  140. active: this.active,
  141. type: this.type
  142. };
  143. break;
  144. case 2:
  145. this.isDroped = !this.isDroped;
  146. data = {
  147. name: this.name,
  148. active: this.isDroped,
  149. type: this.type
  150. };
  151. break;
  152. }
  153. this.parentData.clickHandler(data);
  154. this.$emit('click', data);
  155. uni.$emit(`${this.parentData.sign}_CLICKMENU`, {
  156. show: +this.type > 1 && this.isDroped
  157. });
  158. }
  159. }
  160. }
  161. </script>
  162. <style scoped lang="scss">
  163. @import '@/uni_modules/uv-ui-tools/libs/css/components.scss';
  164. .uv-drop-down-item {
  165. @include flex;
  166. align-items: center;
  167. padding: 20rpx;
  168. }
  169. </style>