u-select.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. <template>
  2. <view class="u-select" :class="[`u-select--${border}`]" :style="[$u.addStyle(customStyle)]">
  3. <!-- 标签 -->
  4. <view v-if="label" class="u-select__label" :class="[`u-select__label--${align}`]">
  5. <text class="u-select__label-text">{{ label }}</text>
  6. </view>
  7. <!-- 选择框容器 -->
  8. <view class="u-select__container">
  9. <!-- 选择框 -->
  10. <view
  11. class="u-select__input"
  12. :class="[
  13. `u-select__input--${border}`,
  14. `u-select__input--${align}`,
  15. {
  16. 'u-select__input--disabled': disabled,
  17. 'u-select__input--active': showDropdown
  18. }
  19. ]"
  20. :style="[inputStyle]"
  21. @click="onInputClick"
  22. >
  23. <!-- 选择内容 -->
  24. <view class="u-select__content">
  25. <text
  26. v-if="displayText"
  27. class="u-select__text"
  28. :class="{ 'u-select__text--wrap': wrap }"
  29. >
  30. {{ displayText }}
  31. </text>
  32. <text v-else class="u-select__placeholder" :style="[$u.addStyle(placeholderStyle)]">{{ placeholder }}</text>
  33. </view>
  34. <!-- 右侧图标 -->
  35. <view v-if="!showArrow" class="u-select__right">
  36. <!-- 清空按钮 -->
  37. <view
  38. v-if="clearable && !disabled && (innerValue !== '' && innerValue !== null && innerValue !== undefined)"
  39. class="u-select__clear"
  40. @click.stop="onClear"
  41. >
  42. <u-icon
  43. name="close"
  44. size="11"
  45. color="#ffffff"
  46. customStyle="line-height: 12px"
  47. ></u-icon>
  48. </view>
  49. <!-- 箭头图标 -->
  50. <view v-else class="u-select__arrow">
  51. <u-icon
  52. name="arrow-down"
  53. size="12"
  54. color="#c0c4cc"
  55. :class="{ 'u-select__arrow--active': showDropdown }"
  56. ></u-icon>
  57. </view>
  58. </view>
  59. </view>
  60. <!-- 下拉列表 -->
  61. <view
  62. v-if="showDropdown"
  63. class="u-select__dropdown"
  64. :class="[
  65. `u-select__dropdown--${placement}`,
  66. { 'u-select__dropdown--show': showDropdown }
  67. ]"
  68. >
  69. <scroll-view
  70. class="u-select__dropdown-scroll"
  71. scroll-y="true"
  72. :style="{ maxHeight: '200px' }"
  73. >
  74. <!-- 空数据提示 -->
  75. <view v-if="list.length === 0" class="u-select__empty">
  76. <text class="u-select__empty-text">{{ emptyText }}</text>
  77. </view>
  78. <!-- 选项列表 -->
  79. <view v-else class="u-select__options">
  80. <view
  81. v-for="(item, index) in list"
  82. :key="index"
  83. class="u-select__option"
  84. :class="[
  85. {
  86. 'u-select__option--selected': isSelected(item[valueName]),
  87. 'u-select__option--disabled': item.disabled
  88. }
  89. ]"
  90. @click="onOptionClick(item)"
  91. >
  92. <!-- 选项文本 -->
  93. <text class="u-select__option-text">{{ item[keyName] }}</text>
  94. <!-- 选中图标 -->
  95. <view v-if="isSelected(item[valueName])" class="u-select__selected-icon">
  96. <u-icon name="checkmark" size="16" color="#409eff"></u-icon>
  97. </view>
  98. </view>
  99. </view>
  100. </scroll-view>
  101. </view>
  102. </view>
  103. <u-overlay :show="showDropdown" :zIndex="1070" opacity="0" @click="onOverlayClick"></u-overlay>
  104. </view>
  105. </template>
  106. <script>
  107. import props from './props.js';
  108. import mixin from '../../libs/mixin/mixin'
  109. import mpMixin from '../../libs/mixin/mpMixin';
  110. /**
  111. * Select 选择器组件
  112. * @description 基于本地数据的选择器组件,支持单选模式,使用下拉列表形式
  113. * @tutorial https://uview.d3u.cn/components/select.html
  114. *
  115. * @property {String|Number} value 默认值
  116. * @property {Array} list 本地数据,格式 [{text:'',value:''}]
  117. * @property {Boolean} clearable 是否显示清除控件
  118. * @property {String} emptyText 没有数据时显示的文字,本地数据无效
  119. * @property {String} label 左侧标题
  120. * @property {String} placeholder 输入框的提示文字
  121. * @property {String|Object} placeholderStyle 指定placeholder的样式
  122. * @property {String} backgroundColor 背景颜色
  123. * @property {String|Number} round 设置圆角值
  124. * @property {String} borderColor 边框颜色
  125. * @property {Boolean} disabled 是否禁用
  126. * @property {String} disabledColor 禁用状态时的背景色
  127. * @property {Boolean} wrap 是否允许选中文本换行显示
  128. * @property {String} placement 弹出位置
  129. * @property {String} align 选择文字的位置
  130. * @property {Boolean} showArrow 是否隐藏右侧按钮
  131. * @property {String} border 边框类型,surround-四周边框,bottom-底部边框,none-无边框 ( 默认 'surround' )
  132. * @property {Object} customStyle 自定义样式
  133. * @event {Function} change 选中发生变化触发
  134. * @event {Function} open 选择框开启时触发
  135. * @event {Function} close 选择框关闭时触发
  136. * @event {Function} clear 点击清除按钮之后触发
  137. * @example <u-select v-model="value" :list="options" @change="onChange"></u-select>
  138. */
  139. export default {
  140. name: "u-select",
  141. mixins: [mpMixin, mixin, props],
  142. data() {
  143. return {
  144. innerValue: '',
  145. showDropdown: false
  146. }
  147. },
  148. computed: {
  149. // 显示文本
  150. displayText() {
  151. if (!this.innerValue) return ''
  152. const item = this.list.find(item => item[this.valueName] === this.innerValue)
  153. return item ? item[this.keyName] : ''
  154. },
  155. inputStyle() {
  156. let style = {
  157. borderColor: this.borderColor,
  158. backgroundColor: this.backgroundColor,
  159. borderRadius: uni.$u.addUnit(this.round)
  160. }
  161. // 设置背景颜色
  162. if (this.disabled) {
  163. style.backgroundColor = this.disabledColor
  164. style.borderColor = uni.$u.darkenColor(this.disabledColor, 0.1)
  165. }
  166. return style
  167. }
  168. },
  169. watch: {
  170. // #ifdef VUE2
  171. value: {
  172. immediate: true,
  173. handler(newVal) {
  174. this.innerValue = newVal
  175. }
  176. },
  177. // #endif
  178. // #ifdef VUE3
  179. modelValue: {
  180. immediate: true,
  181. handler(newVal) {
  182. this.innerValue = newVal
  183. }
  184. }
  185. // #endif
  186. },
  187. // #ifdef VUE3
  188. emits: ["change", "open", "close", "clear", "update:modelValue"],
  189. // #endif
  190. methods: {
  191. // 判断选项是否被选中
  192. isSelected(value) {
  193. return this.innerValue === value
  194. },
  195. // 点击输入框
  196. onInputClick() {
  197. if (this.disabled) return
  198. if (this.list.length === 0) {
  199. uni.showToast({
  200. title: this.emptyText,
  201. icon: 'none'
  202. })
  203. return
  204. }
  205. this.showDropdown = true
  206. this.$emit('open')
  207. },
  208. // 点击选项
  209. onOptionClick(item) {
  210. if (item.disabled) return
  211. const newValue = item[this.valueName]
  212. this.innerValue = newValue
  213. this.showDropdown = false
  214. this.$emit('close')
  215. this.$emit('change', this.innerValue)
  216. // #ifdef VUE2
  217. this.$emit('input', this.innerValue)
  218. // #endif
  219. // #ifdef VUE3
  220. this.$emit('update:modelValue', this.innerValue)
  221. // #endif
  222. },
  223. // 点击遮罩层
  224. onOverlayClick() {
  225. this.showDropdown = false
  226. this.$emit('close')
  227. },
  228. // 清空选择
  229. onClear() {
  230. if (this.disabled) return
  231. const oldValue = this.innerValue
  232. this.innerValue = ''
  233. this.$emit('change', this.innerValue)
  234. // #ifdef VUE2
  235. this.$emit('input', this.innerValue)
  236. // #endif
  237. // #ifdef VUE3
  238. this.$emit('update:modelValue', this.innerValue)
  239. // #endif
  240. this.$emit('clear', oldValue)
  241. }
  242. }
  243. }
  244. </script>
  245. <style lang="scss" scoped>
  246. @import "../../libs/css/components.scss";
  247. .u-select {
  248. display: flex;
  249. flex-direction: column;
  250. position: relative;
  251. &__label {
  252. margin-bottom: 8px;
  253. &--left {
  254. text-align: left;
  255. }
  256. &--center {
  257. text-align: center;
  258. }
  259. &--right {
  260. text-align: right;
  261. }
  262. &-text {
  263. font-size: 14px;
  264. color: $u-main-color;
  265. line-height: 1.4;
  266. }
  267. }
  268. &__container {
  269. position: relative;
  270. }
  271. &__input {
  272. position: relative;
  273. display: flex;
  274. flex-direction: row;
  275. align-items: center;
  276. padding: 0px 9px;
  277. height: 37px;
  278. z-index: 10;
  279. &--surround {
  280. border: 1px solid $u-border-color;
  281. }
  282. &--bottom {
  283. border: none;
  284. border-bottom: 1px solid $u-border-color;
  285. border-radius: 0;
  286. }
  287. &--none {
  288. border: none;
  289. padding: 0;
  290. }
  291. &--left {
  292. text-align: left;
  293. }
  294. &--center {
  295. text-align: center;
  296. }
  297. &--right {
  298. text-align: right;
  299. }
  300. }
  301. &__content {
  302. flex: 1;
  303. min-width: 0;
  304. }
  305. &__text {
  306. font-size: 14px;
  307. color: $u-main-color;
  308. line-height: 1.4;
  309. &--wrap {
  310. word-break: break-all;
  311. white-space: normal;
  312. }
  313. }
  314. &__placeholder {
  315. font-size: 14px;
  316. color: #c0c4cc;
  317. line-height: 1.4;
  318. }
  319. &__right {
  320. display: flex;
  321. align-items: center;
  322. margin-left: 8px;
  323. }
  324. &__clear {
  325. width: 20px;
  326. height: 20px;
  327. border-radius: 100px;
  328. background-color: #c6c7cb;
  329. @include flex(row);
  330. align-items: center;
  331. justify-content: center;
  332. transform: scale(0.82);
  333. }
  334. &__arrow {
  335. display: flex;
  336. align-items: center;
  337. justify-content: center;
  338. width: 16px;
  339. height: 16px;
  340. transition: transform 0.3s;
  341. &--active {
  342. transform: rotate(180deg);
  343. }
  344. }
  345. &__dropdown {
  346. position: absolute;
  347. top: 100%;
  348. left: 0;
  349. right: 0;
  350. background-color: #ffffff;
  351. border-radius: 4px;
  352. box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  353. z-index: 1080;
  354. margin-top: 4px;
  355. opacity: 0;
  356. pointer-events: none;
  357. &--show {
  358. opacity: 1;
  359. pointer-events: auto;
  360. }
  361. &--top {
  362. top: auto;
  363. bottom: 100%;
  364. margin-top: 0;
  365. margin-bottom: 4px;
  366. }
  367. }
  368. &__dropdown-scroll {
  369. max-height: 200px;
  370. }
  371. &__empty {
  372. padding: 20px;
  373. text-align: center;
  374. &-text {
  375. font-size: 14px;
  376. color: #909399;
  377. }
  378. }
  379. &__options {
  380. padding: 4px 0;
  381. }
  382. &__option {
  383. display: flex;
  384. flex-direction: row;
  385. align-items: center;
  386. padding: 8px 12px;
  387. position: relative;
  388. &:hover {
  389. background-color: #f5f7fa;
  390. }
  391. &--selected {
  392. background-color: #f0f9ff;
  393. color: $u-primary;
  394. font-weight: 500;
  395. &:hover {
  396. background-color: #e6f7ff;
  397. }
  398. }
  399. &--disabled {
  400. color: #c0c4cc;
  401. cursor: not-allowed;
  402. &:hover {
  403. background-color: transparent;
  404. transform: none;
  405. }
  406. &:active {
  407. transform: none;
  408. }
  409. }
  410. }
  411. &__option-text {
  412. flex: 1;
  413. font-size: 14px;
  414. line-height: 1.4;
  415. }
  416. &__selected-icon {
  417. margin-left: 8px;
  418. }
  419. }
  420. </style>