w-select.vue 13 KB

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