fui-dropdown-menu.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. <template>
  2. <view class="fui-dropdown__menu">
  3. <slot></slot>
  4. <view class="fui-dropdown__menu-list"
  5. :class="{'fui-ddm__down':direction!=='up','fui-ddm__up':direction==='up','fui-ddm__down-show':isShow && direction!=='up','fui-ddm__up-show':isShow && direction==='up'}"
  6. :style="getStyles">
  7. <scroll-view class="fui-ddm__scroll" scroll-y :style="{maxHeight:maxHeight+'rpx',minWidth:minWidth+'rpx'}">
  8. <view class="fui-dropdown__menu-item" :style="{background:background,padding:padding}"
  9. :class="{'fui-ddm__reverse':isReverse,'fui-ddm__item-line':splitLine && itemList.length-1!==index}"
  10. v-for="(model,index) in itemList" :key="index" @tap.stop="itemClick(index)">
  11. <view class="fui-ddm__checkbox"
  12. :class="{'fui-is__checkmark':isCheckMark,'fui-ddm__checkbox-color':(!checkboxColor || checkboxColor=='true') && model.checked && !isCheckMark}"
  13. :style="{background:model.checked && !isCheckMark ?checkboxColor:'transparent',borderColor:model.checked && !isCheckMark ?checkboxColor:borderColor}"
  14. v-if="isCheckbox">
  15. <view class="fui-ddm__checkmark"
  16. :style="{borderBottomColor:checkmarkColor,borderRightColor:checkmarkColor}"
  17. v-if="model.checked"></view>
  18. </view>
  19. <view class="fui-ddm__flex">
  20. <view class="fui-ddm__icon-box"
  21. :class="{'fui-ddm__icon-ml':!isReverse && isCheckbox,'fui-ddm__icon-mr':isReverse}"
  22. :style="{width:iconWidth+'rpx',height:iconWidth+'rpx'}" v-if="model.src">
  23. <image src="/static/images/common/logo.png"
  24. :style="{width:iconWidth+'rpx',height:iconWidth+'rpx'}"></image>
  25. </view>
  26. <text class="fui-ddm__item-text"
  27. :class="{'fui-ddm__text-pl':!isReverse && (isCheckbox || model.src),'fui-ddm__text-pr':isReverse && (isCheckbox || model.src)}"
  28. :style="{fontSize:size+'rpx',color:selectedColor && model.checked?selectedColor:color}">{{model.text}}</text>
  29. </view>
  30. </view>
  31. </scroll-view>
  32. </view>
  33. <view class="fui-ddm__mask" :style="{backgrround:maskBackground}" v-if="isShow && isMask" @tap="close(1)">
  34. </view>
  35. </view>
  36. </template>
  37. <script>
  38. /*!
  39. * 下拉菜单
  40. * 由于weex android上overflow仅支持hidden,所以该组件不支持Nvue端
  41. * Nvue端使用组件fui-dropdown-list或fui-select组件替代该组件
  42. */
  43. export default {
  44. name: "fui-dropdown-menu",
  45. emits: ['click', 'close'],
  46. props: {
  47. options: {
  48. type: Array,
  49. default () {
  50. return []
  51. }
  52. },
  53. maxHeight: {
  54. type: [Number, String],
  55. default: 400
  56. },
  57. minWidth: {
  58. type: [Number, String],
  59. default: 0
  60. },
  61. left: {
  62. type: [Number, String],
  63. default: 0
  64. },
  65. //right大于等于0时生效,left失效
  66. right: {
  67. type: [Number, String],
  68. default: -1
  69. },
  70. background: {
  71. type: String,
  72. default: '#fff'
  73. },
  74. radius: {
  75. type: [Number, String],
  76. default: 0
  77. },
  78. padding: {
  79. type: String,
  80. default: '32rpx'
  81. },
  82. isCheckbox: {
  83. type: Boolean,
  84. default: true
  85. },
  86. //选择框选中后颜色
  87. checkboxColor: {
  88. type: String,
  89. // #ifdef APP-NVUE
  90. default: '#465CFF'
  91. // #endif
  92. // #ifndef APP-NVUE
  93. default: ''
  94. // #endif
  95. },
  96. //选择框未选中时边框颜色
  97. borderColor: {
  98. type: String,
  99. default: '#ccc'
  100. },
  101. //是否只展示对号,无边框背景
  102. isCheckMark: {
  103. type: Boolean,
  104. default: false
  105. },
  106. //对号颜色
  107. checkmarkColor: {
  108. type: String,
  109. default: '#fff'
  110. },
  111. //选择框与内容是否颠倒排列
  112. isReverse: {
  113. type: Boolean,
  114. default: false
  115. },
  116. //是否需要分割线条
  117. splitLine: {
  118. type: Boolean,
  119. default: false
  120. },
  121. iconWidth: {
  122. type: [Number, String],
  123. default: 48
  124. },
  125. size: {
  126. type: [Number, String],
  127. default: 32
  128. },
  129. color: {
  130. type: String,
  131. default: '#181818'
  132. },
  133. selectedColor: {
  134. type: String,
  135. default: ''
  136. },
  137. isMask: {
  138. type: Boolean,
  139. default: true
  140. },
  141. maskBackground: {
  142. type: String,
  143. default: 'transparent'
  144. },
  145. //down/up
  146. direction: {
  147. type: String,
  148. default: 'down'
  149. }
  150. },
  151. watch: {
  152. options(newVal) {
  153. this.initData(newVal)
  154. }
  155. },
  156. data() {
  157. return {
  158. itemList: [],
  159. isShow: false
  160. };
  161. },
  162. computed: {
  163. getStyles() {
  164. let styles = `border-radius:${this.radius}rpx;background:${this.background};`
  165. let right = Number(this.right || 0)
  166. if (right >= 0) {
  167. styles += 'right:0;'
  168. } else {
  169. styles += 'left:0;'
  170. }
  171. return styles
  172. }
  173. },
  174. created() {
  175. this.initData(this.options)
  176. },
  177. methods: {
  178. initData(vals) {
  179. if (vals && vals.length > 0) {
  180. if (typeof vals[0] !== 'object') {
  181. vals = vals.map(item => {
  182. return {
  183. text: item,
  184. checked: false
  185. }
  186. })
  187. } else {
  188. vals.map(item => {
  189. item.checked = item.checked || false
  190. })
  191. }
  192. this.itemList = vals;
  193. }
  194. },
  195. itemClick(index) {
  196. let item = this.itemList[index]
  197. let vals = [...this.itemList]
  198. vals.forEach((item, idx) => {
  199. if (index === idx) {
  200. item.checked = true
  201. } else {
  202. item.checked = false
  203. }
  204. })
  205. this.itemList = vals;
  206. this.$emit('click', {
  207. index: index,
  208. ...item
  209. })
  210. this.close(2)
  211. },
  212. close(type) {
  213. this.isShow = false;
  214. if (type === 1) {
  215. this.$emit('close', {})
  216. }
  217. },
  218. show() {
  219. this.isShow = true;
  220. }
  221. }
  222. }
  223. </script>
  224. <style scoped>
  225. .fui-dropdown__menu {
  226. flex: 1;
  227. position: relative;
  228. }
  229. .fui-ddm__scroll {
  230. /* #ifndef APP-NVUE */
  231. width: auto;
  232. /* #endif */
  233. }
  234. .fui-dropdown__menu-list {
  235. position: absolute;
  236. box-shadow: 0 0 10rpx rgba(2, 4, 38, 0.05);
  237. overflow: hidden;
  238. z-index: 992;
  239. opacity: 0;
  240. /* #ifndef APP-NVUE */
  241. visibility: hidden;
  242. transition: all 0.3s ease-in-out;
  243. /* #endif */
  244. }
  245. .fui-ddm__down {
  246. transform-origin: 0 0;
  247. bottom: 0;
  248. /* #ifndef APP-NVUE */
  249. transform: translate3d(0, 100%, 0) scaleY(0);
  250. /* #endif */
  251. }
  252. .fui-ddm__down-show {
  253. /* #ifndef APP-NVUE */
  254. transform: translate3d(0, 100%, 0) scaleY(1);
  255. visibility: visible;
  256. /* #endif */
  257. opacity: 1;
  258. }
  259. .fui-ddm__up {
  260. transform-origin: 0 100%;
  261. top: 0;
  262. /* #ifndef APP-NVUE */
  263. transform: translate3d(0, -100%, 0) scaleY(0);
  264. /* #endif */
  265. }
  266. .fui-ddm__up-show {
  267. /* #ifndef APP-NVUE */
  268. transform: translate3d(0, -100%, 0) scaleY(1);
  269. visibility: visible;
  270. /* #endif */
  271. opacity: 1;
  272. }
  273. .fui-ddm__mask {
  274. position: fixed;
  275. top: 0;
  276. left: 0;
  277. right: 0;
  278. bottom: 0;
  279. z-index: 990;
  280. }
  281. .fui-dropdown__menu-item {
  282. /* #ifndef APP-NVUE */
  283. width: 100%;
  284. display: flex;
  285. box-sizing: border-box;
  286. transform: scale(1) translateZ(0);
  287. /* #endif */
  288. flex: 1;
  289. flex-direction: row;
  290. align-items: center;
  291. background-color: #FFFFFF;
  292. position: relative;
  293. /* #ifdef H5 */
  294. cursor: pointer;
  295. /* #endif */
  296. }
  297. .fui-ddm__flex {
  298. /* #ifndef APP-NVUE */
  299. width: 100%;
  300. display: flex;
  301. box-sizing: border-box;
  302. /* #endif */
  303. flex: 1;
  304. flex-direction: row;
  305. align-items: center;
  306. }
  307. /* #ifndef APP-NVUE */
  308. .fui-ddm__item-line {
  309. position: relative;
  310. }
  311. .fui-ddm__item-line::after {
  312. content: '';
  313. position: absolute;
  314. border-bottom: 1px solid var(--fui-color-border, #EEEEEE);
  315. /* #ifdef H5 */
  316. transform: scaleY(0.5);
  317. /* #endif */
  318. /* #ifndef H5 */
  319. transform: scaleY(0.5) translateZ(0);
  320. /* #endif */
  321. transform-origin: 0 100%;
  322. bottom: 0;
  323. right: 0;
  324. left: 32rpx;
  325. pointer-events: none;
  326. }
  327. /* #endif */
  328. .fui-dropdown__menu-item:active {
  329. /* #ifdef APP-NVUE */
  330. background-color: rgba(0, 0, 0, .2) !important;
  331. /* #endif */
  332. /* #ifndef APP-NVUE */
  333. background-color: var(--fui-bg-color-hover, rgba(0, 0, 0, .2)) !important;
  334. /* #endif */
  335. }
  336. .fui-ddm__reverse {
  337. justify-content: space-between;
  338. flex-direction: row-reverse;
  339. }
  340. .fui-ddm__checkbox {
  341. font-size: 0;
  342. color: rgba(0, 0, 0, 0);
  343. width: 40rpx;
  344. height: 40rpx;
  345. border-width: 1px;
  346. border-style: solid;
  347. /* #ifdef APP-NVUE */
  348. border-radius: 40rpx;
  349. /* #endif */
  350. /* #ifndef APP-NVUE */
  351. display: inline-flex;
  352. box-sizing: border-box;
  353. border-radius: 50%;
  354. vertical-align: top;
  355. flex-shrink: 0;
  356. /* #endif */
  357. flex-direction: row;
  358. align-items: center;
  359. justify-content: center;
  360. overflow: hidden;
  361. position: relative;
  362. }
  363. /* #ifndef APP-NVUE */
  364. .fui-ddm__checkbox-color {
  365. background: var(--fui-color-primary, #465CFF) !important;
  366. border-color: var(--fui-color-primary, #465CFF) !important;
  367. }
  368. /* #endif */
  369. .fui-is__checkmark {
  370. border-width: 0 !important;
  371. background: transparent !important;
  372. }
  373. .fui-ddm__checkmark {
  374. width: 20rpx;
  375. height: 40rpx;
  376. border-bottom-style: solid;
  377. border-bottom-width: 3px;
  378. border-bottom-color: #FFFFFF;
  379. border-right-style: solid;
  380. border-right-width: 3px;
  381. border-right-color: #FFFFFF;
  382. /* #ifndef APP-NVUE */
  383. box-sizing: border-box;
  384. transform: rotate(45deg) scale(0.5) translateZ(0);
  385. /* #endif */
  386. /* #ifdef APP-NVUE */
  387. transform: rotate(45deg) scale(0.5);
  388. /* #endif */
  389. transform-origin: 54% 48%;
  390. }
  391. .fui-ddm__item-text {
  392. /* #ifndef APP-NVUE */
  393. word-break: break-all;
  394. /* #endif */
  395. font-weight: normal;
  396. }
  397. .fui-ddm__text-pl {
  398. padding-left: 24rpx;
  399. }
  400. .fui-ddm__text-pr {
  401. padding-right: 24rpx;
  402. }
  403. .fui-ddm__icon-box {
  404. overflow: hidden;
  405. background-color: #F1F4FA;
  406. /* #ifndef APP-NVUE */
  407. flex-shrink: 0;
  408. /* #endif */
  409. }
  410. .fui-ddm__icon-ml {
  411. margin-left: 24rpx;
  412. }
  413. .fui-ddm__icon-mr {
  414. margin-right: 24rpx;
  415. }
  416. </style>