e-select-built-in.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. <template>
  2. <view
  3. class="e-select-box"
  4. :style="{ width: width, minWidth: minWidth }">
  5. <view
  6. class="e-select"
  7. :class="{ 'e-select-disabled': disabled }">
  8. <view
  9. class="e-select-input-box"
  10. @click="toggleSelector">
  11. <!-- 微信小程序input组件在部分安卓机型上会出现文字重影,placeholder抖动问题,2019年时微信小程序就有这个问题,一直没修复,估计短时间内也别指望修复了 -->
  12. <input
  13. class="e-select-input-text"
  14. :placeholder="placeholder"
  15. placeholder-class="e-select-input-placeholder"
  16. v-model="currentData"
  17. @input="filter"
  18. v-if="search && !disabled" />
  19. <view
  20. class="e-select-input-text"
  21. :class="{
  22. 'e-select-input-placeholder': !(currentData || currentData === 0),
  23. }"
  24. v-else>
  25. {{ currentData || currentData === 0 ? currentData : placeholder }}
  26. </view>
  27. <!-- 清空图标,用一个更大的盒子包裹图标,便于点击 -->
  28. <view
  29. class="e-select-icon"
  30. @click.stop="clearVal"
  31. v-if="currentData && clearable && !disabled">
  32. <uni-icons
  33. type="clear"
  34. color="#e1e1e1"
  35. size="16"></uni-icons>
  36. </view>
  37. <!-- 箭头图标,同上 -->
  38. <view
  39. class="e-select-icon"
  40. @click.stop="toggleSelector"
  41. v-else>
  42. <uni-icons
  43. size="16"
  44. color="#6A6A6A"
  45. type="top"
  46. class="arrowAnimation"
  47. :class="showSelector ? 'top' : 'bottom'"></uni-icons>
  48. </view>
  49. </view>
  50. <!-- 全屏遮罩-->
  51. <view
  52. class="e-select--mask"
  53. v-if="showSelector"
  54. @click.stop="toggleSelector" />
  55. <!-- 选项列表 这里用v-show是因为微信小程序会报警告 [Component] slot "" is not found,v-if会导致开发工具不能正确识别到slot -->
  56. <!-- https://developers.weixin.qq.com/community/minihome/doc/000c8295730700d1cd7c81b9656c00 -->
  57. <view
  58. class="e-select-selector"
  59. :style="
  60. position === 'top'
  61. ? 'bottom: calc(0px + 42px)'
  62. : 'top: calc(100% + 12px)'
  63. "
  64. v-show="showSelector">
  65. <!-- 三角小箭头 -->
  66. <view
  67. :class="
  68. position === 'top' ? 'e-popper-arrow-bottom' : 'e-popper-arrow'
  69. "></view>
  70. <scroll-view
  71. scroll-y="true"
  72. :scroll-top="scrollTop"
  73. class="e-select-selector-scroll"
  74. :style="{ maxHeight: maxHeight }"
  75. :scroll-into-view="scrollToId"
  76. :scroll-with-animation="scrollWithAnimation"
  77. v-if="showSelector">
  78. <view
  79. class="e-select-selector-empty"
  80. v-if="currentOptions.length === 0">
  81. <text>{{ emptyTips }}</text>
  82. </view>
  83. <!-- 非空,渲染选项列表 -->
  84. <view
  85. v-else
  86. class="e-select-selector-item"
  87. :class="[
  88. { highlight: currentData == item[props.text] },
  89. {
  90. 'e-select-selector-item-disabled': item[props.disabled],
  91. },
  92. ]"
  93. v-for="(item, index) in currentOptions"
  94. :key="index"
  95. @click="change(item, index)">
  96. <view
  97. id="scrollToId"
  98. v-if="currentData == item[props.text]"></view>
  99. <text>{{ item[props.text] }}</text>
  100. </view>
  101. </scroll-view>
  102. <slot />
  103. </view>
  104. </view>
  105. </view>
  106. </template>
  107. <script>
  108. export default {
  109. name: 'e-select',
  110. data() {
  111. return {
  112. // 是否显示下拉选择列表
  113. showSelector: false,
  114. // 当前选项列表
  115. currentOptions: [],
  116. // 过滤后的选项列表
  117. filterOptions: [],
  118. // 当前值
  119. currentData: '',
  120. // 滚动高度
  121. scrollTop: 0,
  122. // 滚动至的id
  123. scrollToId: 'scrollToId',
  124. // 滚动动画
  125. scrollWithAnimation: false,
  126. };
  127. },
  128. props: {
  129. // vue2 v-model传值方式
  130. value: {
  131. type: [String, Number],
  132. default: '',
  133. },
  134. // vue3 v-model传值方式
  135. modelValue: {
  136. type: [String, Number],
  137. default: '',
  138. },
  139. // 选项列表
  140. options: {
  141. type: Array,
  142. default() {
  143. return [];
  144. },
  145. },
  146. // 选项列表自定义数据格式
  147. props: {
  148. type: Object,
  149. default: () => {
  150. return {
  151. text: 'text',
  152. value: 'value',
  153. disabled: 'disabled',
  154. };
  155. },
  156. },
  157. // 占位文本
  158. placeholder: {
  159. type: String,
  160. default: '请选择',
  161. },
  162. // 输入框宽度
  163. width: {
  164. type: String,
  165. default: '100%',
  166. },
  167. // 输入框最小宽度
  168. minWidth: {
  169. type: String,
  170. default: '120rpx',
  171. },
  172. // 选项列表悬浮框最大高度
  173. maxHeight: {
  174. type: String,
  175. default: '160px',
  176. },
  177. // 选项列表空值占位空值占位
  178. emptyTips: {
  179. type: String,
  180. default: '暂无选项',
  181. },
  182. // 是否可清空
  183. clearable: {
  184. type: Boolean,
  185. default: false,
  186. },
  187. // 是否禁用
  188. disabled: {
  189. type: Boolean,
  190. default: false,
  191. },
  192. // 是否开启搜索
  193. search: {
  194. type: Boolean,
  195. default: true,
  196. },
  197. // 是否开启搜索的滚动动画
  198. animation: {
  199. type: Boolean,
  200. default: true,
  201. },
  202. // 悬浮框位置top/bottom
  203. position: {
  204. type: String,
  205. default: 'bottom',
  206. },
  207. // 分页每页条数
  208. pageSize: {
  209. type: Number,
  210. default: 0,
  211. },
  212. // 分页当前页数
  213. pageIndex: {
  214. type: Number,
  215. default: 1,
  216. },
  217. },
  218. watch: {
  219. options: {
  220. handler(val) {
  221. this.filterOptions = val.slice();
  222. this.initOptions();
  223. this.initData();
  224. },
  225. immediate: true,
  226. deep: true,
  227. },
  228. modelValue: {
  229. handler() {
  230. this.initData();
  231. },
  232. immediate: true,
  233. },
  234. value: {
  235. handler() {
  236. this.initData();
  237. },
  238. immediate: true,
  239. },
  240. pageSize() {
  241. this.initOptions();
  242. },
  243. pageIndex() {
  244. this.initOptions();
  245. },
  246. },
  247. methods: {
  248. /** 处理数据,此函数用于兼容vue2 vue3 */
  249. initData() {
  250. this.currentData = '';
  251. // vue2
  252. if (this.value || this.value === 0) {
  253. for (let i = 0; i < this.options.length; i++) {
  254. const item = this.options[i];
  255. if (item[this.props.value] === this.value) {
  256. this.currentData = item[this.props.text];
  257. this.$emit('getText', this.currentData);
  258. // 如果分页,初始化分页当前页数
  259. if (this.pageSize && this.pageIndex) {
  260. this.$emit('update:pageIndex', Math.floor(i / this.pageSize) + 1);
  261. }
  262. return;
  263. }
  264. }
  265. }
  266. // vue3
  267. else if (this.modelValue || this.modelValue === 0) {
  268. for (let i = 0; i < this.options.length; i++) {
  269. const item = this.options[i];
  270. if (item[this.props.value] === this.modelValue) {
  271. this.currentData = item[this.props.text];
  272. this.$emit('getText', this.currentData);
  273. if (this.pageSize && this.pageIndex) {
  274. this.$emit('update:pageIndex', Math.floor(i / this.pageSize) + 1);
  275. }
  276. return;
  277. }
  278. }
  279. }
  280. },
  281. /** 初始化选项列表 */
  282. initOptions() {
  283. // 设置分页情况下列表
  284. if (this.pageSize && this.pageIndex) {
  285. this.currentOptions = this.filterOptions.slice(
  286. (this.pageIndex - 1) * this.pageSize,
  287. this.pageIndex * this.pageSize
  288. );
  289. } else {
  290. this.currentOptions = this.filterOptions;
  291. }
  292. // scrollTop变化,才能触发滚动顶部,再低如0.01则不能触发,真神奇
  293. this.scrollTop = 0.1;
  294. this.$nextTick(() => {
  295. this.scrollTop = 0;
  296. });
  297. },
  298. /** 过滤选项列表,会自动回到顶部 */
  299. filter() {
  300. // 回到分页第一页
  301. this.$emit('update:pageIndex', 1);
  302. this.$emit('getText', this.currentData);
  303. if (this.currentData) {
  304. this.filterOptions = this.options.filter((item) => {
  305. return item[this.props.text].indexOf(this.currentData) > -1;
  306. });
  307. this.$emit('update:total', this.filterOptions.length);
  308. }
  309. // 等待update:pageIndex事件执行完成
  310. setTimeout(() => {
  311. this.initOptions();
  312. }, 0);
  313. },
  314. /** 改变值 */
  315. change(item, index) {
  316. if (item[this.props.disabled]) return;
  317. const data = {
  318. index,
  319. ...item,
  320. };
  321. this.$emit('change', data);
  322. this.emit(data);
  323. this.toggleSelector();
  324. },
  325. /** 传递父组件值 */
  326. emit(item) {
  327. this.$emit('input', item[this.props.value]);
  328. this.$emit('update:modelValue', item[this.props.value]);
  329. },
  330. /** 清空值 */
  331. clearVal() {
  332. this.$emit('change', 'clear');
  333. this.$emit('input', '');
  334. this.$emit('update:modelValue', '');
  335. },
  336. /** 切换下拉显示 */
  337. toggleSelector() {
  338. if (this.disabled) return;
  339. this.showSelector = !this.showSelector;
  340. if (this.showSelector) {
  341. // 设计理念:只在filter时触发滚动动画,因为每次打开就触发,用户体验不好
  342. if (this.animation) {
  343. setTimeout(() => {
  344. // 开启滚动动画
  345. this.scrollWithAnimation = true;
  346. }, 100);
  347. }
  348. } else {
  349. // 关闭时重新初始化
  350. this.filterOptions = this.options.slice();
  351. this.initData();
  352. this.initOptions();
  353. this.$emit('update:total', this.options.length);
  354. this.scrollWithAnimation = false;
  355. }
  356. },
  357. },
  358. };
  359. </script>
  360. <style lang="scss" scoped>
  361. .e-select-box {
  362. display: flex;
  363. align-items: center;
  364. width: 100%;
  365. box-sizing: border-box;
  366. cursor: pointer;
  367. -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
  368. }
  369. .e-select {
  370. width: 100%;
  371. border-radius: 4px;
  372. box-sizing: border-box;
  373. display: flex;
  374. align-items: center;
  375. user-select: none;
  376. position: relative;
  377. border: 1px solid #dcdfe6;
  378. }
  379. .e-select-disabled {
  380. background-color: #f5f7fa;
  381. cursor: not-allowed;
  382. }
  383. .e-select-input-box {
  384. width: 100%;
  385. padding: 0px 20rpx;
  386. min-height: 34px;
  387. position: relative;
  388. display: flex;
  389. flex: 1;
  390. flex-direction: row;
  391. align-items: center;
  392. .e-select-input-text {
  393. color: #303030;
  394. width: 100%;
  395. color: #333;
  396. white-space: nowrap;
  397. text-overflow: ellipsis;
  398. -o-text-overflow: ellipsis;
  399. overflow: hidden;
  400. font-size: 28rpx;
  401. }
  402. .e-select-input-placeholder {
  403. font-size: 28rpx;
  404. color: #999999;
  405. }
  406. .e-select-icon {
  407. width: 50px;
  408. padding-right: 3px;
  409. height: 100%;
  410. display: flex;
  411. justify-content: flex-end;
  412. align-items: center;
  413. }
  414. .arrowAnimation {
  415. transition: transform 0.3s;
  416. }
  417. .top {
  418. transform: rotateZ(0deg);
  419. }
  420. .bottom {
  421. transform: rotateZ(180deg);
  422. }
  423. }
  424. .e-select--mask {
  425. position: fixed;
  426. top: 0;
  427. bottom: 0;
  428. right: 0;
  429. left: 0;
  430. z-index: 999;
  431. }
  432. .e-select-selector {
  433. box-sizing: border-box;
  434. position: absolute;
  435. left: 0;
  436. width: 100%;
  437. background-color: #ffffff;
  438. border: 1px solid #ebeef5;
  439. border-radius: 6px;
  440. box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  441. z-index: 999;
  442. padding: 4px 2px;
  443. transition: all 1s;
  444. .e-select-selector-scroll {
  445. box-sizing: border-box;
  446. .e-select-selector-empty,
  447. .e-select-selector-item {
  448. display: flex;
  449. cursor: pointer;
  450. line-height: 35rpx;
  451. font-size: 28rpx;
  452. text-align: left;
  453. padding: 15rpx 10px;
  454. }
  455. .e-select-selector-item:hover {
  456. background-color: #f9f9f9;
  457. }
  458. .e-select-selector-empty:last-child,
  459. .e-select-selector-item:last-child {
  460. border-bottom: none;
  461. }
  462. .e-select-selector-item-disabled {
  463. color: #b1b1b1;
  464. cursor: not-allowed;
  465. }
  466. .highlight {
  467. color: #409eff;
  468. font-weight: bold;
  469. background-color: #f5f7fa;
  470. border-radius: 3px;
  471. }
  472. }
  473. }
  474. .e-popper-arrow,
  475. .e-popper-arrow::after,
  476. .e-popper-arrow-bottom,
  477. .e-popper-arrow-bottom::after {
  478. position: absolute;
  479. display: block;
  480. width: 0;
  481. height: 0;
  482. left: 50%;
  483. border-color: transparent;
  484. border-style: solid;
  485. border-width: 6px;
  486. }
  487. .e-popper-arrow {
  488. filter: drop-shadow(0 2px 12px rgba(0, 0, 0, 0.03));
  489. top: -6px;
  490. left: 50%;
  491. transform: translateX(-50%);
  492. margin-right: 3px;
  493. border-top-width: 0;
  494. border-bottom-color: #ebeef5;
  495. }
  496. .e-popper-arrow::after {
  497. content: ' ';
  498. top: 1px;
  499. margin-left: -6px;
  500. border-top-width: 0;
  501. border-bottom-color: #fff;
  502. }
  503. .e-popper-arrow-bottom {
  504. filter: drop-shadow(0 2px 12px rgba(0, 0, 0, 0.03));
  505. bottom: -6px;
  506. left: 50%;
  507. transform: translateX(-50%);
  508. margin-right: 3px;
  509. border-bottom-width: 0;
  510. border-top-color: #ebeef5;
  511. }
  512. .e-popper-arrow-bottom::after {
  513. content: ' ';
  514. bottom: 1px;
  515. margin-left: -6px;
  516. border-bottom-width: 0;
  517. border-top-color: #fff;
  518. }
  519. /* 设置定位元素的位置 */
  520. #scrollToId {
  521. margin-top: -15rpx;
  522. }
  523. </style>