w-select.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. <template>
  2. <view
  3. class="w-select"
  4. id="wSelect"
  5. :style="{
  6. '--select-wrap-width': width,
  7. '--select-wrap-height': height,
  8. '--select-bg-color': bgColor
  9. }"
  10. >
  11. <view :class="isShow ? 'select-wrap-active' : ''" class="select-wrap" @click="changeShow" style="height: 35px;">
  12. <view v-if="multiple" class="select-content">
  13. <view class="select-content-item-default" v-if="multiSelectList.length === 0 && !filterable">
  14. {{ defaultValue }}
  15. </view>
  16. <view class="select-content-item" v-if="multiSelectList.length > 0">
  17. {{ multiSelectList[0][valueName] }}
  18. </view>
  19. <view class="select-content-item" v-if="multiSelectList.length > 1">
  20. {{ multiLength }}
  21. </view>
  22. </view>
  23. <input
  24. v-if="!multiple || filterable"
  25. type="text"
  26. @input="inputChange"
  27. @blur="blurChange"
  28. :placeholder="multiple ? multiSelectList.length === 0 ? defaultValue : '' : defaultValue"
  29. :disabled="!filterable"
  30. :style="!filterable ? 'pointer-events: none' : 'font-size:'+fontSize"
  31. :value="inputData"
  32. style="font-size: 14px;font-weight: bold;"
  33. >
  34. <!-- #ifdef VUE2 -->
  35. <view
  36. @click.stop="refreshValue"
  37. class="close-icon"
  38. v-if="showClose && (multiple ? value.length > 0 : value)"
  39. >
  40. <image :src="refreshUrl" mode="" />
  41. </view>
  42. <view
  43. v-if="value.length <= 0 || !showClose"
  44. :class="isShow ? 'w-select-arrow-up' : ''"
  45. class="w-select-arrow "
  46. />
  47. <!-- #endif -->
  48. <!-- #ifdef VUE3 -->
  49. <view
  50. @click.stop="refreshValue"
  51. class="close-icon"
  52. v-if="showClose && (multiple ? modelValue.length > 0 : modelValue)"
  53. >
  54. <image :src="refreshUrl" mode="" />
  55. </view>
  56. <view
  57. v-if="modelValue.length <= 0 || !showClose"
  58. :class="isShow ? 'w-select-arrow-up' : ''"
  59. class="w-select-arrow "
  60. />
  61. <!-- #endif -->
  62. <scroll-view
  63. scroll-y
  64. v-show="optionsShow"
  65. :class="[
  66. isShow
  67. ? showPosition === 'bottom'
  68. ? 'animation-bottom-in'
  69. : 'animation-top-in'
  70. : showPosition === 'bottom'
  71. ? 'animation-bottom-out'
  72. : 'animation-top-out',
  73. showPosition === 'bottom'
  74. ? 'position-bottom'
  75. : 'position-top'
  76. ]"
  77. class="select-options"
  78. >
  79. <!-- #ifdef VUE2 -->
  80. <view
  81. @click.stop="handleClickItem(item)"
  82. :class="
  83. multiple &&
  84. multiSelectList.find(
  85. res => res[keyName] === item[keyName]
  86. )
  87. ? 'item-active'
  88. : value === item[keyName]
  89. ? 'item-active'
  90. : ''
  91. "
  92. v-for="item in filterList"
  93. :key="item[keyName]"
  94. class="select-option-item"
  95. >
  96. {{ item[valueName] }}
  97. </view>
  98. <!-- #endif -->
  99. <!-- #ifdef VUE3 -->
  100. <view
  101. @click.stop="handleClickItem(item)"
  102. :class="
  103. multiple &&
  104. multiSelectList.find(
  105. res => res[keyName] === item[keyName]
  106. )
  107. ? 'item-active'
  108. : modelValue === item[keyName]
  109. ? 'item-active'
  110. : ''
  111. "
  112. v-for="item in filterList"
  113. :key="item[keyName]"
  114. class="select-option-item"
  115. :style="{fontSize:fontSize}"
  116. >
  117. {{ item[valueName] }}
  118. </view>
  119. <!-- #endif -->
  120. <view class="options-no-data" v-if="filterList.length < 1" :style="{fontSize:fontSize}">
  121. 无匹配数据~
  122. </view>
  123. </scroll-view>
  124. </view>
  125. <view v-if="isShow" @click="closeContentSelect" class="contentMask" />
  126. </view>
  127. </template>
  128. <script>
  129. export default {
  130. props: {
  131. width: {
  132. type: String,
  133. default: '100%'
  134. },
  135. height: {
  136. type: String,
  137. default: '30px'
  138. },
  139. bgColor: {
  140. type: String,
  141. default: '#fff'
  142. },
  143. // 是否多选
  144. multiple: {
  145. type: Boolean,
  146. default: false
  147. },
  148. // 是否可搜索
  149. filterable: {
  150. type: Boolean,
  151. default: false
  152. },
  153. // 是否显示关闭按钮
  154. showClose: {
  155. type: Boolean,
  156. default: false
  157. },
  158. // 渲染列表
  159. list: {
  160. type: Array,
  161. default: () => []
  162. },
  163. // #ifdef VUE3
  164. // 双向绑定的值
  165. modelValue: {
  166. type: [Array, String, Number],
  167. default: ''
  168. },
  169. // #endif
  170. // #ifdef VUE2
  171. // 双向绑定的值
  172. value: {
  173. type: [Array, String, Number],
  174. default: ''
  175. },
  176. // #endif
  177. // 默认显示的内容
  178. defaultValue: {
  179. type: String,
  180. default: '请输入所在公司名称,至少四个字'
  181. },
  182. // 显示的内容
  183. valueName: {
  184. type: String,
  185. default: 'label'
  186. },
  187. // 绑定的内容
  188. keyName: {
  189. type: String,
  190. default: 'value'
  191. },
  192. fontSize:{
  193. type: String,
  194. default:'14px'
  195. }
  196. },
  197. // #ifdef VUE3
  198. emits: ['update:modelValue', 'change'],
  199. // #endif
  200. watch: {
  201. list: {
  202. immediate: true,
  203. deep: true,
  204. handler (news) {
  205. this.filterList = news
  206. const findItem = news.find(item => {
  207. let isItem = ''
  208. // #ifdef VUE3
  209. if (item[this.keyName] === this.modelValue) {
  210. isItem = true
  211. } else {
  212. isItem = false
  213. }
  214. // #endif
  215. // #ifdef VUE2
  216. if (item[this.keyName] === this.value) {
  217. isItem = true
  218. } else {
  219. isItem = false
  220. }
  221. // #endif
  222. return isItem
  223. })
  224. if (findItem) {
  225. this.inputData = findItem[this.valueName]
  226. }
  227. }
  228. }
  229. },
  230. computed: {
  231. multiLength () {
  232. const length = this.multiSelectList.length - 1
  233. return '+' + length
  234. },
  235. bottomDistance () {
  236. return (
  237. this.windowHeight - this.distanceTop - this.curHeight
  238. ) // 当前元素距离可视屏幕底部的距离
  239. }
  240. },
  241. data () {
  242. return {
  243. inputData: '',
  244. // #ifdef VUE3
  245. multiSelectList: this.multiple ? this.modelValue : [],
  246. // #endif
  247. // #ifdef VUE2
  248. multiSelectList: this.multiple ? this.value : [],
  249. // #endif
  250. isShow: false,
  251. optionsShow: false,
  252. windowHeight: null,
  253. curHeight: null,
  254. distanceTop: null,
  255. showPosition: 'bottom',
  256. filterList: [],
  257. refreshUrl: 'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz48c3ZnIHdpZHRoPSIyNCIgaGVpZ2h0PSIyNCIgdmlld0JveD0iMCAwIDQ4IDQ4IiBmaWxsPSJub25lIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjxyZWN0IHdpZHRoPSI0OCIgaGVpZ2h0PSI0OCIgZmlsbD0id2hpdGUiIGZpbGwtb3BhY2l0eT0iMC4wMSIvPjxwYXRoIGQ9Ik0yNCA0NEMzNS4wNDU3IDQ0IDQ0IDM1LjA0NTcgNDQgMjRDNDQgMTIuOTU0MyAzNS4wNDU3IDQgMjQgNEMxMi45NTQzIDQgNCAxMi45NTQzIDQgMjRDNCAzNS4wNDU3IDEyLjk1NDMgNDQgMjQgNDRaIiBmaWxsPSJub25lIiBzdHJva2U9IiM3YzZlNmUiIHN0cm9rZS13aWR0aD0iNCIgc3Ryb2tlLWxpbmVqb2luPSJyb3VuZCIvPjxwYXRoIGQ9Ik0yOS42NTY5IDE4LjM0MzFMMTguMzQzMiAyOS42NTY4IiBzdHJva2U9IiM3YzZlNmUiIHN0cm9rZS13aWR0aD0iNCIgc3Ryb2tlLWxpbmVjYXA9InJvdW5kIiBzdHJva2UtbGluZWpvaW49InJvdW5kIi8+PHBhdGggZD0iTTE4LjM0MzIgMTguMzQzMUwyOS42NTY5IDI5LjY1NjgiIHN0cm9rZT0iIzdjNmU2ZSIgc3Ryb2tlLXdpZHRoPSI0IiBzdHJva2UtbGluZWNhcD0icm91bmQiIHN0cm9rZS1saW5lam9pbj0icm91bmQiLz48L3N2Zz4='
  258. }
  259. },
  260. mounted () {
  261. this.$nextTick(() => {
  262. const res = uni.getSystemInfoSync()
  263. this.windowHeight = res.windowHeight // 当前设备屏幕高度
  264. uni
  265. .createSelectorQuery()
  266. .in(this)
  267. .select('#wSelect')
  268. .boundingClientRect(data => {
  269. this.distanceTop = data.top // 当前元素距离顶部的距离
  270. this.curHeight = data.height
  271. })
  272. .exec()
  273. })
  274. },
  275. methods: {
  276. showPositon () {
  277. this.showPosition = 'bottom'
  278. if (this.bottomDistance < this.windowHeight / 3) {
  279. this.showPosition = 'top'
  280. }
  281. },
  282. changeShow () {
  283. this.isShow = !this.isShow
  284. if (this.isShow === false) {
  285. this.filterList = this.list
  286. setTimeout(() => {
  287. this.optionsShow = false
  288. }, 200)
  289. } else {
  290. this.showPositon()
  291. this.optionsShow = this.isShow
  292. }
  293. },
  294. closeContentSelect () {
  295. this.isShow = false
  296. setTimeout(() => {
  297. this.optionsShow = false
  298. }, 200)
  299. },
  300. setValue (value = '') {
  301. // #ifdef VUE3
  302. this.$emit('update:modelValue', value)
  303. // #endif
  304. // #ifdef VUE2
  305. this.$emit('input', value)
  306. // #endif
  307. },
  308. inputChange (e) {
  309. const value = e.detail.value
  310. if(this.multiple && this.filterable) {
  311. this.inputData = value
  312. }else {
  313. this.setValue(value)
  314. this.inputData = value
  315. }
  316. this.filterList = this.list.filter(item =>
  317. item[this.valueName].includes(value)
  318. )
  319. },
  320. blurChange(e) {
  321. const value = e.detail.value
  322. if(this.multiple && this.filterable && value) {
  323. let curValue ={
  324. [this.keyName]:value,
  325. [this.valueName]:value
  326. }
  327. this.multiSelect(curValue)
  328. }
  329. },
  330. refreshValue () {
  331. this.setValue('')
  332. this.inputData = ''
  333. this.$emit('change', '')
  334. this.filterList = this.list
  335. if (this.multiple) {
  336. this.multiSelectList = []
  337. }
  338. },
  339. handleClickItem (e) {
  340. if (this.multiple) {
  341. this.multiSelect(e)
  342. } else {
  343. this.setValue(e[this.keyName])
  344. this.inputData = e[this.valueName]
  345. this.$emit('change', e)
  346. this.changeShow()
  347. }
  348. },
  349. multiSelect (item) {
  350. const index = this.multiSelectList.findIndex(
  351. res => res[this.valueName] === item[this.valueName]
  352. )
  353. if (index > -1) {
  354. this.multiSelectList.splice(index, 1)
  355. } else {
  356. this.multiSelectList.push(item)
  357. }
  358. this.inputData = ''
  359. this.filterList = this.list
  360. this.setValue(this.multiSelectList)
  361. this.$emit('change', item)
  362. }
  363. }
  364. }
  365. </script>
  366. <style lang="scss" scoped>
  367. .w-select {
  368. --select-wrap-width: 100%;
  369. --select-wrap-height: 30px;
  370. --select-border-radius: 4px;
  371. --select-border: 1px solid #dcdfe6;
  372. --select-active-border: 1px solid #409eff;
  373. --select-options-max-height: 70vh;
  374. --select-option-item-font-size: 14px;
  375. --select-input-font-size: 14px;
  376. --no-data-default-color: #999999;
  377. --select-options-box-shadow: 0px 0px 12px rgb(0 0 0 / 12%);
  378. --select-bg-color: #ffffff;
  379. .select-wrap {
  380. position: relative;
  381. display: flex;
  382. justify-content: space-between;
  383. align-items: center;
  384. width: var(--select-wrap-width);
  385. height: var(--select-wrap-height);
  386. border: var(--select-border);
  387. border-radius: var(--select-border-radius);
  388. background-color: var(--select-bg-color);
  389. transition: all 0.2s;
  390. input {
  391. padding: 0 10px;
  392. width: 100%;
  393. min-width: 0;
  394. height: 100%;
  395. font-size: var(--select-input-font-size);
  396. flex: 1;
  397. }
  398. .select-content {
  399. display: flex;
  400. align-items: center;
  401. font-size: var(--select-option-item-font-size);
  402. .select-content-item {
  403. margin-left: 5px;
  404. padding: 2px 6px;
  405. border-radius: var(--select-border-radius);
  406. color: #aa93b1;
  407. background-color: #f4f4f5;
  408. }
  409. .select-content-item-default {
  410. margin-left: 5px;
  411. color: var(--no-data-default-color);
  412. }
  413. }
  414. .close-icon {
  415. position: absolute;
  416. top: 50%;
  417. right: 7px;
  418. z-index: 1000;
  419. width: 15px;
  420. height: 15px;
  421. transform: translateY(-50%);
  422. image {
  423. width: 100%;
  424. height: 100%;
  425. }
  426. }
  427. .position-bottom {
  428. top: calc(var(--select-wrap-height) + 10px);
  429. }
  430. .position-top {
  431. bottom: calc(var(--select-wrap-height) + 10px);
  432. }
  433. .select-options {
  434. position: absolute;
  435. right: 0;
  436. left: 0;
  437. z-index: 9999;
  438. overflow: scroll;
  439. padding: 10px 0 10px 0px;
  440. max-height: var(--select-options-max-height);
  441. border-radius: var(--select-border-radius);
  442. background-color: var(--select-bg-color);
  443. box-shadow: var(--select-options-box-shadow);
  444. .select-option-item {
  445. margin-bottom: 5px;
  446. padding: 10px;
  447. font-size: var(--select-option-item-font-size);
  448. transition: background-color 0.2s;
  449. }
  450. .item-active {
  451. font-weight: 700;
  452. color: #409eff;
  453. background-color: #f5f7fa;
  454. }
  455. .options-no-data {
  456. font-size: var(--select-option-item-font-size);
  457. text-align: center;
  458. color: var(--no-data-default-color);
  459. }
  460. }
  461. .w-select-arrow {
  462. display: inline-block;
  463. margin: 3px 10px 0;
  464. width: 8px;
  465. height: 8px;
  466. border-top: 1px solid transparent;
  467. border-right: 1px solid transparent;
  468. border-bottom: 1px solid #999999;
  469. border-left: 1px solid #999999;
  470. transition: all 0.3s;
  471. transform: translateY(-50%) rotate(-45deg);
  472. }
  473. .w-select-arrow-up {
  474. transform: rotate(-225deg);
  475. }
  476. }
  477. .select-wrap-active {
  478. border: var(--select-active-border);
  479. }
  480. .animation-bottom-in {
  481. animation-name: bottom-in;
  482. animation-duration: 0.4s;
  483. animation-timing-function: ease-out;
  484. animation-fill-mode: both;
  485. }
  486. .animation-bottom-out {
  487. animation-name: bottom-out;
  488. animation-duration: 0.2s;
  489. animation-timing-function: ease-out;
  490. animation-fill-mode: both;
  491. }
  492. .animation-top-in {
  493. animation-name: top-in;
  494. animation-duration: 0.4s;
  495. animation-timing-function: ease-out;
  496. animation-fill-mode: both;
  497. }
  498. .animation-top-out {
  499. animation-name: top-out;
  500. animation-duration: 0.2s;
  501. animation-timing-function: ease-out;
  502. animation-fill-mode: both;
  503. }
  504. @keyframes bottom-in {
  505. 0% {
  506. opacity: 0;
  507. transform: translateY(-15%);
  508. }
  509. 100% {
  510. opacity: 1;
  511. transform: translateY(0);
  512. }
  513. }
  514. @keyframes bottom-out {
  515. 0% {
  516. opacity: 1;
  517. transform: translateY(0);
  518. }
  519. 100% {
  520. opacity: 0;
  521. transform: translateY(-20%);
  522. }
  523. }
  524. @keyframes top-in {
  525. 0% {
  526. opacity: 0;
  527. transform: translateY(15%);
  528. }
  529. 100% {
  530. opacity: 1;
  531. transform: translateY(0);
  532. }
  533. }
  534. @keyframes top-out {
  535. 0% {
  536. opacity: 1;
  537. transform: translateY(0);
  538. }
  539. 100% {
  540. opacity: 0;
  541. transform: translateY(20%);
  542. }
  543. }
  544. .contentMask {
  545. position: fixed;
  546. top: 0;
  547. right: 0;
  548. bottom: 0;
  549. left: 0;
  550. z-index: 998;
  551. width: 100%;
  552. height: 100%;
  553. }
  554. }
  555. </style>