uni-combox.vue 6.9 KB

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