tui-select.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. <template>
  2. <view @touchmove.stop.prevent="stop">
  3. <view class="tui-select--mask" :class="{'tui-select--mask-show':show}" :style="getStyles" @tap.stop="maskClose">
  4. </view>
  5. <view class="tui-select--wrap" :class="{'tui-select--wrap-show':show}"
  6. :style="{borderTopLeftRadius:radius+'rpx',borderTopRightRadius:radius+'rpx',background:background,zIndex:zIndex}">
  7. <view class="tui-select--header"
  8. :style="{background:background,borderTopLeftRadius:radius+'rpx',borderTopRightRadius:radius+'rpx'}">
  9. <text class="tui-select--header-text"
  10. :style="{fontSize:titleSize+'rpx',color:titleColor,fontWeight:fontWeight}">{{title}}</text>
  11. <view class="tui-select--header-close" @tap.stop="handleClose">
  12. <icon type="clear" color="#ccc" :size="16"></icon>
  13. </view>
  14. <view class="tui-select--header-line" :style="{background:dividerColor}"></view>
  15. </view>
  16. <scroll-view scroll-y class="tui-select--scroll" :show-scrollbar="false" :style="{height:height+'rpx'}">
  17. <view class="tui-select--list">
  18. <view class="tui-select--item" :style="{padding:padding}" @tap="itemClick(index)"
  19. v-for="(model,index) in itemList" :key="index"
  20. :class="{'tui-select--reverse':reverse,'tui-select--item-active':highlight}">
  21. <view class="tui-select--checkbox" :class="{'tui-select--is-checkmark ':isCheckMark}"
  22. :style="{background:model.checked && !isCheckMark ?checkboxColor:'transparent',borderColor:model.checked && !isCheckMark ?checkboxColor:borderColor}">
  23. <view class="tui-select--checkmark"
  24. :style="{borderBottomColor:checkmarkColor,borderRightColor:checkmarkColor}"
  25. v-if="model.checked"></view>
  26. </view>
  27. <view class="tui-select--flex">
  28. <view class="tui-select--icon-box"
  29. :class="{'tui-select--icon-ml':!reverse,'tui-select--icon-mr':reverse}"
  30. :style="{width:iconWidth+'rpx',height:iconWidth+'rpx'}" v-if="model.src">
  31. <image :src="model.src" :style="{width:iconWidth+'rpx',height:iconWidth+'rpx'}"
  32. mode="widthFix"></image>
  33. </view>
  34. <text class="tui-select--item-text"
  35. :class="{'tui-select--text-pl':!reverse,'tui-select--text-pr':reverse}"
  36. :style="{fontSize:size+'rpx',color:color}">{{model.text}}</text>
  37. </view>
  38. <view v-if="dividerLine" class="tui-select--item-line"
  39. :style="{background:dividerColor,left:reverse?0:bottomLeft+'rpx',right:reverse?bottomLeft+'rpx':0}">
  40. </view>
  41. </view>
  42. </view>
  43. </scroll-view>
  44. <view class="tui-select--btn-wrap">
  45. <view class="tui-select--btn" :style="{background:btnBackground}">
  46. <text class="tui-select--btn" :class="['tui-select--btn-text']" :style="{color:btnColor}"
  47. @tap.stop="handleClick">{{btnText}}</text>
  48. </view>
  49. </view>
  50. </view>
  51. </view>
  52. </template>
  53. <script>
  54. export default {
  55. name: "tui-select",
  56. emits: ['confirm', 'close'],
  57. props: {
  58. show: {
  59. type: Boolean,
  60. default: false
  61. },
  62. list: {
  63. type: Array,
  64. default () {
  65. return []
  66. }
  67. },
  68. height: {
  69. type: [Number, String],
  70. default: 600
  71. },
  72. radius: {
  73. type: [Number, String],
  74. default: 24
  75. },
  76. title: {
  77. type: String,
  78. default: '请选择'
  79. },
  80. titleSize: {
  81. type: [Number, String],
  82. default: 32
  83. },
  84. titleColor: {
  85. type: String,
  86. default: '#333'
  87. },
  88. fontWeight: {
  89. type: [Number, String],
  90. default: 400
  91. },
  92. multiple: {
  93. type: Boolean,
  94. default: false
  95. },
  96. background: {
  97. type: String,
  98. default: '#fff'
  99. },
  100. padding: {
  101. type: String,
  102. default: '30rpx'
  103. },
  104. //选择框选中后颜色
  105. checkboxColor: {
  106. type: String,
  107. default: '#5677fc'
  108. },
  109. borderColor: {
  110. type: String,
  111. default: '#ccc'
  112. },
  113. isCheckMark: {
  114. type: Boolean,
  115. default: false
  116. },
  117. checkmarkColor: {
  118. type: String,
  119. default: '#fff'
  120. },
  121. reverse: {
  122. type: Boolean,
  123. default: false
  124. },
  125. dividerLine: {
  126. type: Boolean,
  127. default: true
  128. },
  129. dividerColor: {
  130. type: String,
  131. default: '#EEEEEE'
  132. },
  133. bottomLeft: {
  134. type: [Number, String],
  135. default: 30
  136. },
  137. highlight: {
  138. type: Boolean,
  139. default: true
  140. },
  141. iconWidth: {
  142. type: [Number, String],
  143. default: 48
  144. },
  145. size: {
  146. type: [Number, String],
  147. default: 30
  148. },
  149. color: {
  150. type: String,
  151. default: '#333'
  152. },
  153. btnText: {
  154. type: String,
  155. default: '确定'
  156. },
  157. btnBackground: {
  158. type: String,
  159. default: '#5677fc'
  160. },
  161. btnColor: {
  162. type: String,
  163. default: '#fff'
  164. },
  165. maskBackground: {
  166. type: String,
  167. default: 'rgba(0,0,0,.6)'
  168. },
  169. maskClosable: {
  170. type: [Boolean,String],
  171. default: false
  172. },
  173. zIndex: {
  174. type: [Number, String],
  175. default: 1000
  176. }
  177. },
  178. computed: {
  179. getStyles() {
  180. return `background:${this.maskBackground};z-index:${Number(this.zIndex)-1};`
  181. }
  182. },
  183. watch: {
  184. list(newVal) {
  185. this.initData(newVal)
  186. }
  187. },
  188. data() {
  189. return {
  190. itemList: [],
  191. index: -1
  192. };
  193. },
  194. created() {
  195. this.initData(this.list)
  196. },
  197. methods: {
  198. initData(vals) {
  199. vals = JSON.parse(JSON.stringify(vals))
  200. if (vals && vals.length > 0) {
  201. if (typeof vals[0] !== 'object') {
  202. vals = vals.map(item => {
  203. return {
  204. text: item,
  205. checked: false
  206. }
  207. })
  208. } else {
  209. vals.map((item,index) => {
  210. item.checked = item.checked || false
  211. if(!this.multiple && item.checked){
  212. this.index=index
  213. }
  214. })
  215. }
  216. this.itemList = vals;
  217. }
  218. },
  219. itemClick(index) {
  220. let vals = [...this.itemList]
  221. let item = vals[index]
  222. if (this.multiple) {
  223. item.checked = !item.checked;
  224. } else {
  225. vals.forEach((item, idx) => {
  226. if (index === idx) {
  227. item.checked = true
  228. } else {
  229. item.checked = false
  230. }
  231. })
  232. this.index = index
  233. }
  234. this.itemList = vals;
  235. },
  236. handleClick() {
  237. if (this.multiple) {
  238. let items = []
  239. this.itemList.forEach((item, idx) => {
  240. if (item.checked) {
  241. items.push(this.list[idx])
  242. }
  243. })
  244. this.$emit('confirm', {
  245. options: items
  246. })
  247. } else {
  248. let index = this.index;
  249. this.$emit('confirm', {
  250. index: index,
  251. options: index === -1 ? '' : this.list[index]
  252. })
  253. }
  254. },
  255. maskClose() {
  256. if (!this.maskClosable) return;
  257. this.handleClose()
  258. },
  259. handleClose() {
  260. this.$emit('close', {})
  261. },
  262. stop() {}
  263. }
  264. }
  265. </script>
  266. <style scoped>
  267. .tui-select--mask {
  268. position: fixed;
  269. left: 0;
  270. right: 0;
  271. top: 0;
  272. bottom: 0;
  273. transition: all ease-in-out .3s;
  274. visibility: hidden;
  275. opacity: 0;
  276. }
  277. .tui-select--mask-show {
  278. opacity: 1;
  279. visibility: visible;
  280. }
  281. .tui-select--wrap {
  282. position: fixed;
  283. left: 0;
  284. right: 0;
  285. bottom: 0;
  286. width: 100%;
  287. transform: translate3d(0, 100%, 0);
  288. transition: all 0.3s ease-in-out;
  289. min-height: 20rpx;
  290. opacity: 0;
  291. visibility: hidden;
  292. padding-bottom: constant(safe-area-inset-bottom);
  293. padding-bottom: env(safe-area-inset-bottom);
  294. }
  295. .tui-select--wrap-show {
  296. transform: translate3d(0, 0, 0);
  297. opacity: 1;
  298. visibility: visible;
  299. }
  300. .tui-select--scroll {
  301. width: 100%;
  302. flex: 1;
  303. }
  304. .tui-select--list {
  305. width: 100%;
  306. }
  307. .tui-select--item {
  308. width: 100%;
  309. display: flex;
  310. box-sizing: border-box;
  311. flex: 1;
  312. flex-direction: row;
  313. align-items: center;
  314. position: relative;
  315. /* #ifdef H5 */
  316. cursor: pointer;
  317. /* #endif */
  318. position: relative;
  319. }
  320. .tui-select--item-line {
  321. position: absolute;
  322. bottom: 0;
  323. height: 1px;
  324. -webkit-transform: scaleY(0.5);
  325. transform: scaleY(0.5);
  326. transform-origin: 0 100%;
  327. z-index: 1;
  328. }
  329. .tui-select--item-active:active {
  330. background: rgba(0, 0, 0, .2);
  331. }
  332. .tui-select--flex {
  333. width: 100%;
  334. display: flex;
  335. box-sizing: border-box;
  336. flex: 1;
  337. flex-direction: row;
  338. align-items: center;
  339. }
  340. .tui-select--reverse {
  341. justify-content: space-between;
  342. flex-direction: row-reverse;
  343. }
  344. .tui-select--checkbox {
  345. font-size: 0;
  346. color: rgba(0, 0, 0, 0);
  347. width: 40rpx;
  348. height: 40rpx;
  349. border-width: 1px;
  350. border-style: solid;
  351. display: inline-flex;
  352. box-sizing: border-box;
  353. border-radius: 50%;
  354. vertical-align: top;
  355. flex-shrink: 0;
  356. flex-direction: row;
  357. align-items: center;
  358. justify-content: center;
  359. overflow: hidden;
  360. position: relative;
  361. }
  362. .tui-select--is-checkmark {
  363. border-width: 0 !important;
  364. background: transparent !important;
  365. }
  366. .tui-select--checkmark {
  367. width: 20rpx;
  368. height: 40rpx;
  369. border-bottom-style: solid;
  370. border-bottom-width: 3px;
  371. border-bottom-color: #FFFFFF;
  372. border-right-style: solid;
  373. border-right-width: 3px;
  374. border-right-color: #FFFFFF;
  375. box-sizing: border-box;
  376. transform: rotate(45deg) scale(0.5) translateZ(0);
  377. transform-origin: 54% 48%;
  378. }
  379. .tui-select--item-text {
  380. word-break: break-all;
  381. font-weight: normal;
  382. }
  383. .tui-select--text-pl {
  384. padding-left: 20rpx;
  385. }
  386. .tui-select--text-pr {
  387. padding-right: 20rpx;
  388. }
  389. .tui-select--icon-box {
  390. overflow: hidden;
  391. background-color: #F8F8F8;
  392. flex-shrink: 0;
  393. }
  394. .tui-select--icon-ml {
  395. margin-left: 20rpx;
  396. }
  397. .tui-select--icon-mr {
  398. margin-right: 20rpx;
  399. }
  400. .tui-select--header {
  401. width: 100%;
  402. display: flex;
  403. flex: 1;
  404. height: 98rpx;
  405. flex-direction: row;
  406. align-items: center;
  407. justify-content: center;
  408. position: relative;
  409. }
  410. .tui-select--header-line {
  411. position: absolute;
  412. left: 0;
  413. bottom: 0;
  414. right: 0;
  415. height: 1px;
  416. -webkit-transform: scaleY(0.5);
  417. transform: scaleY(0.5);
  418. transform-origin: 0 100%;
  419. z-index: 1;
  420. }
  421. .tui-select--header-text {
  422. text-align: center;
  423. width: 100%;
  424. white-space: nowrap;
  425. overflow: hidden;
  426. box-sizing: border-box;
  427. text-overflow: ellipsis;
  428. flex: 1;
  429. padding: 0 88rpx;
  430. }
  431. .tui-select--header-close {
  432. width: 50rpx;
  433. height: 50rpx;
  434. position: absolute;
  435. right: 32rpx;
  436. top: 24rpx;
  437. text-align: right;
  438. /* #ifdef H5 */
  439. cursor: pointer;
  440. /* #endif */
  441. }
  442. .tui-select--btn-wrap {
  443. width: 100%;
  444. display: flex;
  445. box-sizing: border-box;
  446. flex: 1;
  447. flex-direction: row;
  448. align-items: center;
  449. justify-content: center;
  450. padding: 32rpx;
  451. }
  452. .tui-select--btn {
  453. width: 100%;
  454. display: flex;
  455. flex: 1;
  456. flex-direction: row;
  457. align-items: center;
  458. justify-content: center;
  459. height: 84rpx;
  460. border-radius: 44rpx;
  461. /* #ifdef H5 */
  462. cursor: pointer;
  463. /* #endif */
  464. font-size: 30rpx;
  465. font-weight: normal;
  466. text-align: center;
  467. }
  468. .tui-select--btn-text:active {
  469. background: rgba(0, 0, 0, .2);
  470. }
  471. </style>