u-select.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <template>
  2. <view class="u-select">
  3. <view :class="['u-select__content', disabled && 'disabled']">
  4. <view class="u-select__label" @click="openSelect">
  5. <slot name="text" :currentLabel="currentLabel">
  6. <text class="u-select__text" v-if="showOptionsLabel">
  7. {{ currentLabel }}
  8. </text>
  9. <text class="u-select__text" v-else>
  10. {{ label }}
  11. </text>
  12. </slot>
  13. <slot name="icon">
  14. <up-icon name="arrow-down" :size="iconSize" :color="iconColor"></up-icon>
  15. </slot>
  16. </view>
  17. <u-overlay :show="isOpen" @click="overlayClick" v-if="overlay" :zIndex="zIndex" :duration="duration + 50"
  18. :customStyle="overlayStyle" :opacity="overlayOpacity" @touchmove.stop.prevent="noop"></u-overlay>
  19. <view class="u-select__options__wrap"
  20. :style="{ overflowY: 'auto', zIndex: zIndex + 1, left: optionsWrapLeft, right: optionsWrapRight, maxHeight: maxHeight}">
  21. <view class="u-select__options" v-if="isOpen">
  22. <slot name="options">
  23. <view class="u-select__options_item" :class="current == item[keyName] ? 'active': ''"
  24. :key="index" v-for="(item, index) in options" @click="selectItem(item)">
  25. <slot name="optionItem" :item="item">
  26. <text class="u-select__item_text" :style="{color: itemColor}">
  27. {{item[labelName]}}
  28. </text>
  29. </slot>
  30. </view>
  31. </slot>
  32. </view>
  33. </view>
  34. </view>
  35. </view>
  36. </template>
  37. <script>
  38. import {
  39. mpMixin
  40. } from '../../libs/mixin/mpMixin';
  41. import {
  42. mixin
  43. } from '../../libs/mixin/mixin';
  44. import {
  45. getWindowInfo
  46. } from '../../libs/function/index';
  47. export default {
  48. name: "up-select",
  49. mixins: [mpMixin, mixin],
  50. emits: ['update:current', 'select'],
  51. props: {
  52. maxHeight: {
  53. type: String,
  54. default: '90vh'
  55. },
  56. overlay: {
  57. type: Boolean,
  58. default: true
  59. },
  60. overlayOpacity: {
  61. type: Number,
  62. default: 0.01
  63. },
  64. overlayStyle: {
  65. type: Object,
  66. default: () => {
  67. return {}
  68. }
  69. },
  70. duration: {
  71. type: Number,
  72. default: 300
  73. },
  74. label: {
  75. type: String,
  76. default: '选项'
  77. },
  78. options: {
  79. type: Array,
  80. default: () => {
  81. return []
  82. }
  83. },
  84. keyName: {
  85. type: String,
  86. default: 'id'
  87. },
  88. labelName: {
  89. type: String,
  90. default: 'name'
  91. },
  92. showOptionsLabel: {
  93. type: Boolean,
  94. default: false
  95. },
  96. current: {
  97. type: [String, Number],
  98. default: ''
  99. },
  100. zIndex: {
  101. type: Number,
  102. default: 11000
  103. },
  104. itemColor: {
  105. type: String,
  106. default: '#333333'
  107. },
  108. iconColor: {
  109. type: String,
  110. default: ''
  111. },
  112. iconSize: {
  113. type: [String],
  114. default: '13px'
  115. },
  116. // 是否禁用
  117. disabled: {
  118. type: Boolean,
  119. default: false
  120. }
  121. },
  122. data() {
  123. return {
  124. isOpen: false,
  125. optionsWrapLeft: 'auto',
  126. optionsWrapRight: 'auto'
  127. }
  128. },
  129. computed: {
  130. currentLabel() {
  131. let name = '';
  132. this.options.forEach((ele) => {
  133. if (ele[this.keyName] === this.current) {
  134. name = ele[this.labelName];
  135. }
  136. });
  137. return name;
  138. }
  139. },
  140. methods: {
  141. openSelect() {
  142. if (this.disabled) return;
  143. this.isOpen = true;
  144. this.$nextTick(() => {
  145. if (this.isOpen) {
  146. this.adjustOptionsWrapPosition();
  147. }
  148. });
  149. },
  150. closeSelect() {
  151. this.isOpen = false;
  152. },
  153. overlayClick() {
  154. this.isOpen = false;
  155. },
  156. selectItem(item) {
  157. this.isOpen = false;
  158. this.$emit('update:current', item[this.keyName]);
  159. this.$emit('select', item);
  160. },
  161. adjustOptionsWrapPosition() {
  162. let wi = getWindowInfo();
  163. let windowWidth = wi.windowWidth;
  164. this.$uGetRect('.u-select__options__wrap').then(rect => {
  165. if (rect.left + rect.width > windowWidth) {
  166. // 如果右侧被遮挡,则调整到左侧
  167. this.optionsWrapLeft = 'auto';
  168. this.optionsWrapRight = `0px`;
  169. }
  170. });
  171. }
  172. }
  173. }
  174. </script>
  175. <style lang="scss" scoped>
  176. .u-select__content {
  177. position: relative;
  178. .u-select__label {
  179. display: flex;
  180. justify-content: space-between;
  181. /* #ifdef H5 */
  182. &:hover {
  183. cursor: pointer;
  184. }
  185. /* #endif */
  186. }
  187. .u-select__text {
  188. margin-right: 2px;
  189. }
  190. &.disabled {
  191. opacity: 0.6;
  192. pointer-events: none;
  193. }
  194. .u-select__options__wrap {
  195. margin-bottom: 46px;
  196. position: absolute;
  197. top: 20px;
  198. left: 0;
  199. }
  200. .u-select__options {
  201. min-width: 100px;
  202. box-sizing: border-box;
  203. border-radius: 4px;
  204. border: 1px solid #f1f1f1;
  205. background-color: #fff;
  206. .u-select__options_item {
  207. padding: 10px 12px;
  208. box-sizing: border-box;
  209. width: 100%;
  210. height: 100%;
  211. &:hover {
  212. background-color: #f7f7f7;
  213. }
  214. /* #ifdef H5 */
  215. &:hover {
  216. cursor: pointer;
  217. }
  218. .u-select__item_text {
  219. &:hover {
  220. cursor: pointer;
  221. }
  222. }
  223. /* #endif */
  224. }
  225. }
  226. }
  227. </style>