timeDropdown.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. <template>
  2. <div class="select-container" :class="{ 'custom-select-auto-container': !isDefault }">
  3. <el-select ref="selectSelector" v-model="activeLabel" :popper-append-to-body="false"
  4. @visible-change="onVisibleChange" @mouseenter.native="onSelectMouseEnter" @mouseleave.native="onSelectMouseLeave"
  5. :placeholder="placeholder" popper-class="select-custom">
  6. <template v-if="!isDefault" slot="prefix">
  7. <div class="select-prefix">
  8. <span class="select-prefix-value highlight-text" v-if="activeLabel">{{ activeLabel }}</span>
  9. <span class="select-prefix-value" v-else>{{ placeholder }}</span>
  10. <i class="iconfont icon-xiala" :class="{ 'is-reverse': isFocus }"></i>
  11. </div>
  12. </template>
  13. <template slot="empty">
  14. <div class="time-container">
  15. <div class="time-item" :class="{ 'active': item.value === activeValue || (item.disabled && isCustom) }"
  16. v-for="item in options" :key="item.label" :label="item.label" :value="item.value"
  17. @click="handleChange(item)">
  18. <el-popover v-if="item.disabled" class="custom-popover" :append-to-body="false" placement="right-end"
  19. :trigger="popoverTrigger" :offset="12" v-model="showPopover" @show="popShow" @hide="popHide"
  20. ref="customPopover">
  21. <div class="custom-time">
  22. <el-date-picker ref="timePick" class="time-pick" :append-to-body="false" v-model="p_time"
  23. type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
  24. value-format="timestamp" align="center" @change="dateChange">
  25. </el-date-picker>
  26. </div>
  27. <div slot="reference" class="custom-label">
  28. <span>{{ item.label }}</span>
  29. <i class="el-icon-arrow-right"></i>
  30. </div>
  31. </el-popover>
  32. <span v-else>{{ item.label }}</span>
  33. </div>
  34. </div>
  35. </template>
  36. </el-select>
  37. </div>
  38. </template>
  39. <script>
  40. import { Select, Option, Popover, Button, Input, DatePicker } from 'element-ui'
  41. import { dateFormatter } from '@/utils/'
  42. export default {
  43. name: 'timeDropdown',
  44. components: {
  45. [Select.name]: Select,
  46. [Option.name]: Option,
  47. [Popover.name]: Popover,
  48. [Button.name]: Button,
  49. [Input.name]: Input,
  50. [DatePicker.name]: DatePicker
  51. },
  52. props: {
  53. /**
  54. * select 回显的输入框是否使用默认样式
  55. * true:自带输入框
  56. * false: 回显框为根据回显内容自适应宽度,下拉箭头为实心三角箭头
  57. */
  58. isDefault: {
  59. type: Boolean,
  60. default: false
  61. },
  62. /**
  63. * 下拉框展开方式
  64. * hover: 鼠标悬浮 click: 点击
  65. */
  66. trigger: {
  67. type: String,
  68. default: 'click'
  69. },
  70. placeholder: {
  71. type: String,
  72. default: '成立时间'
  73. },
  74. /**
  75. * 自定义popover展开方式
  76. * hover: 鼠标悬浮 click: 点击
  77. */
  78. popoverTrigger: {
  79. type: String,
  80. default: 'hover'
  81. },
  82. value: {
  83. type: String,
  84. default: ''
  85. },
  86. // 下拉数据
  87. selectData: {
  88. type: Array,
  89. default: () => {
  90. return [
  91. {
  92. label: '全部',
  93. value: ''
  94. },
  95. {
  96. label: '近1年内',
  97. value: '-1y'
  98. },
  99. {
  100. label: '1-3年',
  101. value: '1y-3y'
  102. },
  103. {
  104. label: '3-5年',
  105. value: '3y-5y'
  106. },
  107. {
  108. label: '5-10年',
  109. value: '5y-10y'
  110. },
  111. {
  112. label: '10年以上',
  113. value: '10y-'
  114. },
  115. {
  116. value: '0',
  117. label: '自定义',
  118. disabled: true
  119. }
  120. ]
  121. }
  122. }
  123. },
  124. // events => change
  125. // 自定义时间传入参数格式为 开始时间戳-结束时间戳(输出同理,时间戳为毫秒级)
  126. data() {
  127. return {
  128. isFocus: false,
  129. options: this.selectData,
  130. activeValue: this.value,
  131. isCustom: false, // 当前是否是自定义选项
  132. p_time: '',
  133. time: {
  134. start: '',
  135. end: ''
  136. },
  137. showPopover: false,
  138. timer: null
  139. }
  140. },
  141. computed: {
  142. activeLabel() {
  143. const time = this.activeValue
  144. if (time) {
  145. if (this.options.find(v => v.value === time)) {
  146. let label = this.options.find(v => v.value === time).label
  147. if (label === '自定义') {
  148. let start = time.split('-')[0]
  149. let end = time.split('-')[1]
  150. return dateFormatter(Number(start), 'yyyy-MM-dd') + '-' + dateFormatter(Number(end), 'yyyy-MM-dd')
  151. } else {
  152. return label || ''
  153. }
  154. } else {
  155. let start = time.split('-')[0]
  156. let end = time.split('-')[1]
  157. return dateFormatter(Number(start), 'yyyy-MM-dd') + '-' + dateFormatter(Number(end), 'yyyy-MM-dd')
  158. }
  159. } else {
  160. return ''
  161. }
  162. }
  163. },
  164. watch: {
  165. isFocus() {
  166. this.$nextTick(() => {
  167. if (this.showPopover) {
  168. setTimeout(() => {
  169. // popover在下拉框展示时需要重新计算位置,通过先将popover弹框透明度将为0等位置计算完成后再恢复
  170. this.$refs.customPopover[0].updatePopper()
  171. const $popover = this.$root.$el.querySelector('.custom-popover > .el-popover')
  172. $popover.style.opacity = '1'
  173. if (this.$refs.timePick) {
  174. this.$refs.timePick[0].focus()
  175. }
  176. }, 300)
  177. }
  178. })
  179. },
  180. value(val) {
  181. if (val) {
  182. this.setState(val)
  183. }
  184. }
  185. },
  186. methods: {
  187. popShow() {
  188. if (this.$refs.timePick) {
  189. this.$refs.timePick[0].focus()
  190. }
  191. },
  192. popHide() {
  193. // if (this.$refs.timePick) {
  194. // this.$refs.timePick[0].blur()
  195. // }
  196. },
  197. onVisibleChange(flag) {
  198. this.isFocus = flag
  199. if (flag) {
  200. this.setState(this.activeValue)
  201. }
  202. },
  203. onSelectMouseEnter(e) {
  204. if (this.trigger !== 'hover') return
  205. if (!this.timer) {
  206. this.timer = setTimeout(() => {
  207. this.$refs.selectSelector.visible = true
  208. }, 100)
  209. }
  210. },
  211. onSelectMouseLeave(e) {
  212. if (this.trigger !== 'hover') return
  213. clearTimeout(this.timer)
  214. this.timer = null
  215. setTimeout(() => {
  216. this.$refs.selectSelector.blur()
  217. }, 300)
  218. },
  219. dateChange(val) {
  220. if (!val || val.length === 0) return
  221. let start = val[0]
  222. let end = val[1]
  223. this.activeValue = `${start}-${end}`
  224. this.time.start = start
  225. this.time.end = end
  226. this.options.forEach(item => {
  227. if (item.label === '自定义') {
  228. item.value = `${start}-${end}`
  229. }
  230. })
  231. this.isCustom = true
  232. this.$refs.selectSelector.toggleMenu()
  233. this.$refs.customPopover[0].doClose()
  234. this.$emit('change', this.getState())
  235. },
  236. handleChange(item) {
  237. if (item.label !== '自定义') {
  238. this.activeValue = item.value
  239. this.isCustom = false
  240. this.p_time = ''
  241. this.time.start = ''
  242. this.time.end = ''
  243. this.$refs.selectSelector.toggleMenu()
  244. this.$refs.customPopover[0].doClose()
  245. this.$emit('change', this.getState())
  246. } else {
  247. this.isCustom = true
  248. }
  249. },
  250. getState() {
  251. return this.activeValue
  252. },
  253. getAllstate() {
  254. return {
  255. label: this.activeLabel,
  256. value: this.activeValue,
  257. isCustom: this.isCustom
  258. }
  259. },
  260. setState(data) {
  261. this.isCustom = false
  262. if (data) {
  263. const valueArr = this.options.filter(v => !v.disabled).map(t => t.value)
  264. if (valueArr.includes(data)) {
  265. this.activeValue = data
  266. } else {
  267. const timeArr = data.split('-')
  268. const start = Number(timeArr[0])
  269. const end = Number(timeArr[1])
  270. this.isCustom = true
  271. this.time.start = start
  272. this.time.end = end
  273. this.activeValue = data
  274. this.p_time = [start, end]
  275. this.showPopover = true
  276. this.$nextTick(() => {
  277. const $popover = this.$root.$el.querySelector('.custom-popover > .el-popover')
  278. $popover.style.opacity = '0'
  279. })
  280. }
  281. } else {
  282. this.activeValue = data
  283. }
  284. }
  285. }
  286. }
  287. </script>
  288. <style lang="scss" scoped>
  289. .select-container {
  290. .el-date-editor.time-pick {
  291. color: transparent !important;
  292. border-color: transparent !important;
  293. background-color: transparent !important;
  294. }
  295. .time-pick {
  296. ::v-deep {
  297. .el-range-input,
  298. .el-input__icon,
  299. .el-range-separator {
  300. display: none;
  301. }
  302. .el-picker-panel.el-date-range-picker.el-popper {
  303. // position: relative !important;
  304. border: 1px solid $color_main;
  305. top: -48px !important;
  306. }
  307. }
  308. }
  309. ::v-deep {
  310. .popper__arrow {
  311. display: none;
  312. }
  313. .el-input__inner {
  314. color: $color_main;
  315. }
  316. .el-select-dropdown {
  317. border-radius: 5px;
  318. margin-top: 0 !important;
  319. border: 0;
  320. background: transparent !important;
  321. }
  322. }
  323. /* 需要自定义select输入框的样式 */
  324. &.custom-select-auto-container {
  325. .select-prefix {
  326. display: flex;
  327. align-items: center;
  328. width: 100%;
  329. padding: 0 0 0 10px;
  330. height: 28px;
  331. line-height: 28px;
  332. background: #fff;
  333. color: #1d1d1d;
  334. text-overflow: ellipsis;
  335. white-space: nowrap;
  336. overflow: hidden;
  337. text-align: left;
  338. cursor: pointer;
  339. .select-prefix-value {
  340. display: inline-block;
  341. margin-right: 2px;
  342. flex: 1;
  343. text-overflow: ellipsis;
  344. overflow: hidden;
  345. white-space: nowrap;
  346. }
  347. .icon-xiala {
  348. display: inline-block;
  349. font-size: 16px;
  350. flex-shrink: 0;
  351. transform: rotate(0deg);
  352. transition: transform .5s;
  353. &.is-reverse {
  354. transform: rotate(180deg);
  355. }
  356. }
  357. }
  358. ::v-deep {
  359. .el-select {
  360. height: 24px;
  361. text-align: start;
  362. min-width: 50px;
  363. }
  364. .select-custom {
  365. // top: 16px !important;
  366. border-color: $color_main;
  367. }
  368. .el-input {
  369. width: auto;
  370. }
  371. .el-input__prefix {
  372. display: inline-block;
  373. position: relative;
  374. box-sizing: border-box;
  375. color: #606266;
  376. font-size: inherit;
  377. height: 24px;
  378. line-height: normal;
  379. transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
  380. left: 0;
  381. }
  382. .el-input__inner {
  383. position: absolute;
  384. padding: 0;
  385. height: 24px;
  386. line-height: 24px;
  387. }
  388. .el-input__suffix {
  389. display: none;
  390. }
  391. }
  392. }
  393. /* 下拉框自定义样式 */
  394. .time-container {
  395. min-width: 140px;
  396. padding: 8px 0;
  397. border: 1px solid $color_main;
  398. background: #fff;
  399. border-radius: 5px;
  400. margin-top: 2px;
  401. .time-item {
  402. padding: 4px 16px;
  403. font-size: 14px;
  404. line-height: 22px;
  405. color: #1d1d1d;
  406. text-align: left;
  407. cursor: pointer;
  408. &:hover {
  409. background: #ECECEC;
  410. }
  411. &.active {
  412. color: $color_main;
  413. }
  414. span {
  415. display: inline-block;
  416. width: 100%;
  417. }
  418. }
  419. .custom-label {
  420. display: flex;
  421. align-items: center;
  422. justify-content: space-between;
  423. .el-icon-arrow-right {
  424. margin-right: -8px;
  425. }
  426. }
  427. .custom-popover {
  428. .custom-time {
  429. padding: 20px;
  430. margin-left: 4px;
  431. // border: 1px solid $color_main;
  432. // background: #fff;
  433. border-radius: 4px;
  434. position: relative;
  435. &-item {
  436. display: flex;
  437. align-items: center;
  438. margin-bottom: 12px;
  439. }
  440. &-button {
  441. display: flex;
  442. justify-content: flex-end;
  443. .el-button {
  444. width: 60px;
  445. height: 28px;
  446. padding: 0;
  447. }
  448. }
  449. }
  450. ::v-deep {
  451. .el-popover {
  452. margin-left: 16px;
  453. border-color: $color_main;
  454. padding: 0;
  455. border: 0;
  456. background: transparent;
  457. box-shadow: 0px 0px 0px transparent;
  458. }
  459. .time-input {
  460. width: 88px;
  461. height: 24px;
  462. margin: 0 4px;
  463. .el-input__inner {
  464. height: 100%;
  465. padding: 0 8px;
  466. }
  467. }
  468. }
  469. }
  470. }
  471. }
  472. </style>