w-select.vue 13 KB

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