easy-select.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <template>
  2. <view class="easy-select" @click.stop="trigger" :style="[easySelectSize]">
  3. <input type="text" v-model="value" :placeholder="placeholder" disabled clearable>
  4. <!-- <view class="easy-select-suffix" :style="{border: '1px solid rgba(0,0,0,0)'}" :class="[showSuffix]">
  5. <view class="easy-select-down-tag"></view>
  6. </view> -->
  7. <view class="easy-select-options" v-if="showOptions" :style="{'min-width': boundingClientRect.width + 'px', top: optionsGroupTop, margin: optionsGroupMargin}">
  8. <view class="easy-select-options-item" v-for="item in options" :key="item.code" @click.stop="select(item)" :class="{active: currentSelect.name === item.name}">
  9. <text>{{item.name}}</text>
  10. </view>
  11. </view>
  12. </view>
  13. </template>
  14. <script>
  15. /**
  16. * easy-select
  17. * @author Snoop zhang
  18. * @description Select Component
  19. * */
  20. const COMPONENT_NAME = 'easy-select'
  21. const MAX_OPTIONS_HEIGHT = 137 // 修改务必也修改easy-select-options的css部分
  22. const OPTIONS_ITEM_HEIGHT = 33 // 修改务必也修改easy-select-options-item的css部分
  23. const OPTIONS_MARGIN = 10
  24. const OPTIONS_PADDING = 6 * 2 + 2 // + 2是border
  25. const OPTIONS_OTHER_HEIGHT = OPTIONS_MARGIN + OPTIONS_PADDING
  26. const STORAGE_KEY = '_easyWindowHeight'
  27. const SIZE = {
  28. 'medium': {
  29. width: '150px',
  30. height: '40px'
  31. },
  32. 'small': {
  33. width: '200px',
  34. height: '30px'
  35. },
  36. 'mini': {
  37. width: '160px',
  38. height: '30px'
  39. }
  40. }
  41. export default {
  42. name: COMPONENT_NAME,
  43. props: {
  44. windowHeight: {
  45. type: [Number, String],
  46. default: 0
  47. },
  48. placeholder: {
  49. type: String,
  50. default: '请选择'
  51. },
  52. value: {
  53. type: String,
  54. default: '双皮奶'
  55. },
  56. size: {
  57. type: String,
  58. default: 'medium'
  59. },
  60. options: {
  61. type: Array,
  62. default () {
  63. return []
  64. }
  65. }
  66. },
  67. data() {
  68. return {
  69. showOptions: false,
  70. boundingClientRect: {},
  71. currentSelect: {},
  72. optionsGroupTop: 'auto',
  73. optionsGroupMargin: ''
  74. }
  75. },
  76. computed: {
  77. showSuffix() {
  78. return this.showOptions ? 'showOptions' : 'no-showOptions'
  79. },
  80. easySelectSize() {
  81. let size = this.size.toLowerCase()
  82. if (size in SIZE) {
  83. return {
  84. width: SIZE[size].width,
  85. height: SIZE[size].height
  86. }
  87. } else {
  88. return {}
  89. }
  90. }
  91. },
  92. mounted() {
  93. const elQuery = uni.createSelectorQuery().in(this)
  94. elQuery.select('.easy-select').boundingClientRect(data => {
  95. this.boundingClientRect = data
  96. }).exec();
  97. try {
  98. if (!this.windowHeight) {
  99. const storageHeihgt = uni.getStorageSync(STORAGE_KEY)
  100. if (storageHeihgt) {
  101. this.easyWindowHeight = storageHeihgt
  102. return
  103. }
  104. const res = uni.getSystemInfoSync();
  105. this.easyWindowHeight = res.windowHeight
  106. uni.setStorageSync(STORAGE_KEY, this.easyWindowHeight)
  107. }
  108. } catch (e) {
  109. // error
  110. }
  111. },
  112. methods: {
  113. trigger(e) {
  114. const view = uni.createSelectorQuery().in(this)
  115. view.select('.easy-select').fields({rect: true}, data => {
  116. let { top, bottom } = data
  117. const thresholdHeight = Math.min(MAX_OPTIONS_HEIGHT + OPTIONS_MARGIN, (this.options.length * OPTIONS_ITEM_HEIGHT) +
  118. OPTIONS_OTHER_HEIGHT)
  119. bottom = Number(this.windowHeight || this.easyWindowHeight) - (top + this.boundingClientRect.height) // 距离底部的距离等于视口的高度减上top加select组件的高度
  120. // judge direction
  121. if (bottom < thresholdHeight) {
  122. this.optionsGroupDirection = 'up'
  123. this.optionsGroupTop = -thresholdHeight - 12 + 'px'
  124. this.optionsGroupMargin = '0'
  125. } else {
  126. this.optionsGroupDirection = 'down'
  127. this.optionsGroupTop = 'auto'
  128. this.optionsGroupMargin = '10px 0 0 0'
  129. }
  130. // if (this.scrollTop < )
  131. this.showOptions = !this.showOptions
  132. }).exec();
  133. },
  134. select(options) {
  135. this.showOptions = false
  136. this.currentSelect = options
  137. this.$emit('selectOne', options)
  138. },
  139. hideOptions() {
  140. this.showOptions = false
  141. }
  142. }
  143. }
  144. </script>
  145. <style lang="scss" scoped>
  146. .easy-select {
  147. position: relative;
  148. border-radius: 4px;
  149. color: #606266;
  150. outline: none;
  151. box-sizing: content-box;
  152. padding: 50rpx 0rpx;
  153. height: 30px;
  154. input{
  155. width: 100%;
  156. height: 80rpx;
  157. border: 2rpx solid #999999;
  158. padding-left: 25rpx;
  159. box-shadow:0px 3px 5px 0px rgba(0, 0, 0, 0.27);
  160. border-radius:11rpx;
  161. }
  162. }
  163. .easy-select input {
  164. padding: 0 18rpx;
  165. padding-right: 60rpx;
  166. overflow: hidden;
  167. white-space: nowrap;
  168. text-overflow: ellipsis;
  169. height: 100% !important;
  170. min-height: 100% !important;
  171. }
  172. .easy-select .easy-select-suffix {
  173. position: absolute;
  174. box-sizing: border-box;
  175. height: 100%;
  176. right: 5px;
  177. top: 0;
  178. display: flex;
  179. align-items: center;
  180. transform: rotate(180deg);
  181. transition: all .3s;
  182. transform-origin: center;
  183. }
  184. .easy-select .showOptions {
  185. transform: rotate(0) !important;
  186. }
  187. .easy-select .no-showOptions {
  188. transform: rotate(180deg) !important;
  189. }
  190. .easy-select .easy-select-options {
  191. position: absolute;
  192. padding: 6px 0;
  193. margin-top: 10px;
  194. border: 1px solid #e4e7ed;
  195. border-radius: 4px;
  196. background-color: #fff;
  197. box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .1);
  198. box-sizing: border-box;
  199. transform-origin: center top;
  200. z-index: 2238;
  201. overflow: scroll;
  202. max-height: 274rpx;
  203. }
  204. .easy-select .easy-select-options-item {
  205. padding: 0 20rpx;
  206. position: relative;
  207. white-space: nowrap;
  208. font-size: 14px;
  209. color: #606266;
  210. height: 33px;
  211. line-height: 33px;
  212. box-sizing: border-box;
  213. }
  214. .easy-select .active {
  215. background-color: #F5F7FA
  216. }
  217. </style>