u-select.vue 4.5 KB

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