uni-combox.vue 6.7 KB

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