uni-combox.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <template>
  2. <view class="uni-combox" :class="border ? '' : 'uni-combox__no-border'">
  3. <view v-if="label" class="uni-combox__label" :style="labelStyle">
  4. <text>{{ label }}</text>
  5. </view>
  6. <view class="uni-combox__input-box">
  7. <input class="uni-combox__input" type="text" :placeholder="placeholder" placeholder-class="uni-combox__input-plac" v-model="inputVal" @input="onInput" @focus="onFocus" @blur="onBlur" />
  8. <uni-icons v-if="!inputVal || !clearAble" :type="showSelector ? 'top' : 'bottom'" size="14" color="#999" @click="toggleSelector"> </uni-icons>
  9. <uni-icons v-if="inputVal && clearAble" type="clear" size="24" color="#999" @click="clean"> </uni-icons>
  10. </view>
  11. <view class="uni-combox__selector" v-if="showSelector" ref="dropdownRef">
  12. <view class="uni-popper__arrow"></view>
  13. <scroll-view scroll-y="true" class="uni-combox__selector-scroll">
  14. <view class="uni-combox__selector-empty" v-if="candidates.length === 0">
  15. <text>{{ emptyTips }}</text>
  16. </view>
  17. <view class="uni-combox__selector-item" v-for="(item, index) in candidates" :key="index" @click="onSelectorClick(item.text, index)">
  18. <view style="width: 100%" class="text-ellipsis">{{ item.text }}</view>
  19. </view>
  20. </scroll-view>
  21. </view>
  22. </view>
  23. </template>
  24. <script>
  25. /**
  26. * Combox 组合输入框
  27. * @description 组合输入框一般用于既可以输入也可以选择的场景
  28. * @tutorial https://ext.dcloud.net.cn/plugin?id=1261
  29. * @property {String} label 左侧文字
  30. * @property {String} labelWidth 左侧内容宽度
  31. * @property {String} placeholder 输入框占位符
  32. * @property {Array} candidates 候选项列表
  33. * @property {String} emptyTips 筛选结果为空时显示的文字
  34. * @property {String} value 组合框的值
  35. */
  36. export default {
  37. name: "uniCombox",
  38. emits: ["input", "update:modelValue", "change"],
  39. props: {
  40. clearAble: {
  41. type: Boolean,
  42. default: false,
  43. },
  44. border: {
  45. type: Boolean,
  46. default: true,
  47. },
  48. label: {
  49. type: String,
  50. default: "",
  51. },
  52. labelWidth: {
  53. type: String,
  54. default: "auto",
  55. },
  56. placeholder: {
  57. type: String,
  58. default: "",
  59. },
  60. candidates: {
  61. type: Array,
  62. default() {
  63. return [];
  64. },
  65. },
  66. emptyTips: {
  67. type: String,
  68. default: "无匹配项",
  69. },
  70. // #ifndef VUE3
  71. value: {
  72. type: [String, Number],
  73. default: "",
  74. },
  75. // #endif
  76. // #ifdef VUE3
  77. modelValue: {
  78. type: [String, Number],
  79. default: "",
  80. },
  81. // #endif
  82. },
  83. data() {
  84. return {
  85. showSelector: false,
  86. inputVal: "",
  87. };
  88. },
  89. computed: {
  90. labelStyle() {
  91. if (this.labelWidth === "auto") {
  92. return "";
  93. }
  94. return `width: ${this.labelWidth}`;
  95. },
  96. filterCandidates() {
  97. return this.candidates.filter((item) => {
  98. return item.toString().indexOf(this.inputVal) > -1;
  99. });
  100. },
  101. filterCandidatesLength() {
  102. return this.filterCandidates.length;
  103. },
  104. },
  105. watch: {
  106. // #ifndef VUE3
  107. value: {
  108. handler(newVal) {
  109. this.inputVal = newVal;
  110. },
  111. immediate: true,
  112. },
  113. // #endif
  114. // #ifdef VUE3
  115. modelValue: {
  116. handler(newVal) {
  117. this.inputVal = newVal;
  118. },
  119. immediate: true,
  120. },
  121. // #endif
  122. },
  123. methods: {
  124. toggleSelector() {
  125. this.showSelector = !this.showSelector;
  126. },
  127. onFocus() {
  128. this.showSelector = true;
  129. },
  130. onBlur(event) {
  131. setTimeout(() => {
  132. const dropdown = this.$refs.dropdownRef;
  133. if (dropdown && !dropdown.contains(event.relatedTarget)) {
  134. this.showSelector = false;
  135. }
  136. }, 154);
  137. },
  138. onSelectorClick(text, index) {
  139. this.inputVal = text;
  140. this.showSelector = false;
  141. this.$emit("input", this.inputVal, true);
  142. this.$emit("update:modelValue", this.inputVal);
  143. this.$emit("change", this.candidates[index]);
  144. },
  145. onInput() {
  146. setTimeout(() => {
  147. this.$emit("input", this.inputVal, false);
  148. this.$emit("update:modelValue", this.inputVal);
  149. });
  150. },
  151. onChange(event) {
  152. this.$emit("change", this.inputValue);
  153. },
  154. clean() {
  155. this.inputVal = "";
  156. this.onInput();
  157. },
  158. },
  159. };
  160. </script>
  161. <style lang="scss" scoped>
  162. .uni-combox {
  163. font-size: 14px;
  164. border: 1px solid #dcdfe6;
  165. padding: 0rpx 20rpx;
  166. position: relative;
  167. /* #ifndef APP-NVUE */
  168. display: flex;
  169. /* #endif */
  170. // height: 40px;
  171. flex-direction: row;
  172. align-items: center;
  173. // border-bottom: solid 1px #DDDDDD;
  174. height: 56rpx;
  175. }
  176. .uni-combox__label {
  177. font-size: 16px;
  178. line-height: 22px;
  179. padding-right: 10px;
  180. color: #999999;
  181. }
  182. .uni-combox__input-box {
  183. position: relative;
  184. /* #ifndef APP-NVUE */
  185. display: flex;
  186. /* #endif */
  187. flex: 1;
  188. flex-direction: row;
  189. align-items: center;
  190. }
  191. .uni-combox__input {
  192. flex: 1;
  193. font-size: 14px;
  194. height: 22px;
  195. line-height: 22px;
  196. }
  197. .uni-combox__input-plac {
  198. font-size: 14px;
  199. color: #999;
  200. }
  201. .uni-combox__selector {
  202. /* #ifndef APP-NVUE */
  203. box-sizing: border-box;
  204. /* #endif */
  205. position: absolute;
  206. bottom: calc(100% + 12px);
  207. left: 0;
  208. width: 100%;
  209. background-color: #ffffff;
  210. border: 1px solid #ebeef5;
  211. border-radius: 6px;
  212. box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  213. z-index: 1000;
  214. padding: 4px 0;
  215. }
  216. .uni-combox__selector-scroll {
  217. /* #ifndef APP-NVUE */
  218. max-height: 100px;
  219. box-sizing: border-box;
  220. /* #endif */
  221. }
  222. .uni-combox__selector-empty,
  223. .uni-combox__selector-item {
  224. /* #ifndef APP-NVUE */
  225. display: flex;
  226. cursor: pointer;
  227. /* #endif */
  228. line-height: 36px;
  229. font-size: 14px;
  230. text-align: center;
  231. // border-bottom: solid 1px #DDDDDD;
  232. padding: 0px 10px;
  233. background-color: #ffffff;
  234. }
  235. .uni-combox__selector-item:hover {
  236. background-color: #f9f9f9;
  237. }
  238. .uni-combox__selector-empty:last-child,
  239. .uni-combox__selector-item:last-child {
  240. /* #ifndef APP-NVUE */
  241. border-bottom: none;
  242. /* #endif */
  243. }
  244. // picker 弹出层通用的指示小三角
  245. .uni-popper__arrow,
  246. .uni-popper__arrow::after {
  247. position: absolute;
  248. display: block;
  249. width: 0;
  250. height: 0;
  251. border-color: transparent;
  252. border-style: solid;
  253. border-width: 6px;
  254. }
  255. .uni-popper__arrow {
  256. filter: drop-shadow(0 2px 12px rgba(0, 0, 0, 0.03));
  257. bottom: -6px; /* 将 top 改为 bottom */
  258. left: 10%;
  259. margin-right: 3px;
  260. border-bottom-width: 0; /* 将 border-top-width 改为 border-bottom-width */
  261. border-top-color: #ebeef5; /* 将 border-bottom-color 改为 border-top-color */
  262. }
  263. .uni-popper__arrow::after {
  264. content: " ";
  265. bottom: 1px; /* 将 top 改为 bottom */
  266. margin-left: -6px;
  267. border-bottom-width: 0; /* 将 border-top-width 改为 border-bottom-width */
  268. border-top-color: #fff; /* 将 border-bottom-color 改为 border-top-color */
  269. }
  270. .uni-combox__no-border {
  271. border: none;
  272. }
  273. </style>