u-select.vue 4.6 KB

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