select.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. color: #606266;
  149. input{
  150. text-align: right;
  151. }
  152. }
  153. .easy-select input {
  154. overflow: hidden;
  155. white-space: nowrap;
  156. text-overflow: ellipsis;
  157. height: 100% !important;
  158. min-height: 100% !important;
  159. }
  160. .easy-select .easy-select-suffix {
  161. position: absolute;
  162. box-sizing: border-box;
  163. height: 100%;
  164. right: 5px;
  165. top: 0;
  166. display: flex;
  167. align-items: center;
  168. transform: rotate(180deg);
  169. transition: all .3s;
  170. transform-origin: center;
  171. }
  172. .easy-select .showOptions {
  173. transform: rotate(0) !important;
  174. }
  175. .easy-select .no-showOptions {
  176. transform: rotate(180deg) !important;
  177. }
  178. .easy-select .easy-select-options {
  179. position: absolute;
  180. padding: 6px 0;
  181. margin-top: 10px;
  182. border: 1px solid #e4e7ed;
  183. border-radius: 4px;
  184. background-color: #fff;
  185. box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .1);
  186. box-sizing: border-box;
  187. transform-origin: center top;
  188. z-index: 2238;
  189. overflow: scroll;
  190. max-height: 274rpx;
  191. }
  192. .easy-select .easy-select-options-item {
  193. padding: 0 20rpx;
  194. position: relative;
  195. white-space: nowrap;
  196. font-size: 14px;
  197. color: #606266;
  198. height: 33px;
  199. line-height: 33px;
  200. box-sizing: border-box;
  201. }
  202. .easy-select .active {
  203. background-color: #F5F7FA
  204. }
  205. </style>