select.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <template>
  2. <view class="u-select" @click="close" :class="{ disabled: disabled }">
  3. <view class="u-select__content">
  4. <view class="u-select__label" @click.stop="openSelect">
  5. <slot name="text">
  6. <trans class="u-select__text" :_t="label" />
  7. </slot>
  8. <slot name="icon" v-if="!icon">
  9. <u-icon
  10. name="arrow-down"
  11. :size="iconSize"
  12. :color="iconColor"
  13. ></u-icon>
  14. </slot>
  15. </view>
  16. <view class="u-select__options" :style="{ zIndex: zIndex }" v-if="isOpen">
  17. <slot name="options">
  18. <view class="options" v-if="options.length">
  19. <view
  20. class="u-select__options_item"
  21. :class="current == index ? 'active' : ''"
  22. :key="index"
  23. v-for="(item, index) in options"
  24. @click.stop="selectItem(item, index)"
  25. >
  26. <slot name="optionItem" :item="item">
  27. <trans
  28. class="u-select__item_text"
  29. :style="{ color: itemColor }"
  30. :_t="item[keyName]"
  31. v-if="!isTran"
  32. />
  33. <text
  34. class="u-select__item_text"
  35. :style="{ color: itemColor }"
  36. v-else
  37. >
  38. {{ item[keyName] }}
  39. </text>
  40. </slot>
  41. </view>
  42. </view>
  43. <view class="_no" v-else>
  44. <trans class="no_text" _t="无数据" />
  45. </view>
  46. </slot>
  47. </view>
  48. </view>
  49. </view>
  50. </template>
  51. <script>
  52. export default {
  53. name: "up-select",
  54. emits: ["update:current", "select", "selectClick"],
  55. props: {
  56. label: {
  57. type: String,
  58. default: "选项",
  59. },
  60. options: {
  61. type: Array,
  62. default: () => {
  63. return [];
  64. },
  65. },
  66. keyName: {
  67. type: String,
  68. default: "name",
  69. },
  70. current: {
  71. type: [String, Number],
  72. default: "",
  73. },
  74. zIndex: {
  75. type: Number,
  76. default: 10,
  77. },
  78. itemColor: {
  79. type: String,
  80. default: "#333333",
  81. },
  82. iconColor: {
  83. type: String,
  84. default: "",
  85. },
  86. iconSize: {
  87. type: [String],
  88. default: "13px",
  89. },
  90. icon: Boolean,
  91. isTran: Boolean,
  92. disabled: {
  93. // 新增的禁用属性
  94. type: Boolean,
  95. default: false,
  96. },
  97. },
  98. data() {
  99. return {
  100. isOpen: false,
  101. };
  102. },
  103. methods: {
  104. openSelect() {
  105. this.isOpen = !this.isOpen;
  106. this.$emit("selectClick");
  107. },
  108. close() {
  109. this.isOpen = false;
  110. },
  111. selectItem(item, index) {
  112. this.isOpen = false;
  113. this.$emit("update:current", index);
  114. this.$emit("select", item);
  115. },
  116. },
  117. };
  118. </script>
  119. <style lang="scss" scoped>
  120. .u-select__content {
  121. position: relative;
  122. .u-select__label {
  123. display: flex;
  124. font-size: 28rpx;
  125. font-weight: 500;
  126. line-height: 60rpx;
  127. margin: 0 8rpx;
  128. color: var(--text-02);
  129. /* #ifdef H5 */
  130. &:hover {
  131. cursor: pointer;
  132. }
  133. /* #endif */
  134. }
  135. .u-select__text {
  136. margin-right: 2px;
  137. }
  138. .u-select__options {
  139. min-width: 300rpx;
  140. box-sizing: border-box;
  141. border-radius: 4px;
  142. border: 1px solid #f1f1f1;
  143. background-color: #fff;
  144. position: absolute;
  145. top: auto;
  146. left: -10px;
  147. padding: 10rpx 0;
  148. box-shadow: 0px 0px 12px #0000001f;
  149. margin-top: 18rpx;
  150. overflow: hidden scroll;
  151. .options {
  152. max-height: 274px;
  153. overflow: hidden scroll;
  154. }
  155. .u-select__options_item {
  156. box-sizing: border-box;
  157. width: 100%;
  158. min-width: 300rpx;
  159. &:hover {
  160. background-color: #f7f7f7;
  161. }
  162. /* #ifdef H5 */
  163. &:hover {
  164. cursor: pointer;
  165. }
  166. .u-select__item_text {
  167. white-space: nowrap;
  168. overflow: hidden;
  169. text-overflow: ellipsis;
  170. &:hover {
  171. cursor: pointer;
  172. }
  173. }
  174. /* #endif */
  175. }
  176. }
  177. .u-select__options::after {
  178. content: "";
  179. position: absolute;
  180. left: 60rpx;
  181. top: -12rpx;
  182. // transform: translateX(-30%);
  183. border-left: 14rpx solid transparent;
  184. border-right: 14rpx solid transparent;
  185. border-bottom: 14rpx solid var(--light);
  186. z-index: 10;
  187. }
  188. }
  189. ._no {
  190. text-align: center;
  191. .no_text {
  192. padding: 20rpx 0;
  193. text-align: center;
  194. font-size: 24rpx;
  195. color: #909399;
  196. }
  197. }
  198. .disabled {
  199. pointer-events: none;
  200. border: 2rpx solid #dadbde !important;
  201. background-color: rgb(245, 247, 250) !important;
  202. box-shadow: 0 0 0 1px var(--borderColor) inset;
  203. color: #606266;
  204. }
  205. </style>