当前位置:   article > 正文

element 年份范围选择器封装

年份范围选择器

element ui 年份范围选择器封装

element 组件库 关于月份范围 以及 日期范围选择器都有相对应的组件 但是年份范围选择器没有给到 因为公司业务需要 我就根据饿了么组件以及其他程序⚪的代码封装了一个年份范围选择器

  • 先看组件效果

默认状态
pp2vTH0.jpg

input获取焦点 下拉框出现
pp2xkCD.md.jpg

选中状态
pp2xnbt.md.jpg

pp2xKVP.jpg

清除icon
pp2x6M9.jpg

相关代码

<template>
  <div class="year-all">
    <transition name="el-zoom-in-top">
      <div
        v-show="showPanel"
        ref="popover"
        class="el-popover"
        placement="bottom"
        popper-class="custom_year_range"
        trigger="manual"
      >
        <div class="_inner float-panel">
          <div class="_inner left-panel">
            <div class="_inner panel-head">
              <i
                class="_inner el-icon-d-arrow-left"
                @click="onClickLeft"
              ></i>
              <span class="year-main">
                {{ leftYearList[0] + '年 ' + '- ' + leftYearList[9] + '年' }}
              </span>
            </div>
            <div class="_inner panel-content">
              <div
                v-for="item in leftYearList"
                :key="item"
                class="year-item"
                :class="{
                  oneSelected: item === startYear && oneSelected,
                  startSelected: item === startYear,
                  endSelected: item === endYear,
                  betweenSelected: item > startYear && item < endYear,
                }"
                @click="onClickItem(item)"
                @mouseover="onHoverItem(item)"
              >
                <a
                  :class="{
                    cell: true,
                    _inner: true,
                    selected: item === startYear || item === endYear,
                  }"
                >
                  {{ item }}
                </a>
              </div>
            </div>
          </div>
          <div class="_inner right-panel">
            <div class="_inner panel-head">
              <i
                class="_inner el-icon-d-arrow-right"
                @click="onClickRight"
              ></i>
              <span class="year-main">{{ rightYearList[0] + '年 ' + '- ' + rightYearList[9] + '年' }}</span>
            </div>
            <div class="_inner panel-content">
              <div
                v-for="item in rightYearList"
                :key="item"
                class="year-item"
                :class="{
                  startSelected: item === startYear,
                  endSelected: item === endYear,
                  betweenSelected: item > startYear && item < endYear,
                }"
              >
                <a
                  :class="{
                    cell: true,
                    _inner: true,
                    selected: item === endYear || item === startYear,
                  }"
                  @click="onClickItem(item)"
                  @mouseover="onHoverItem(item)"
                >
                  {{ item }}
                </a>
              </div>
            </div>
          </div>
        </div>
      </div>
    </transition>
    <div slot="reference">
      <div
        ref="yearPicker"
        style="width: 100%"
        class="el-date-editor el-range-editor el-input__inner el-date-editor--daterange el-range-editor--small"
        @mouseover="onHoverIcon"
        @mouseleave="lossHoverIcon"
      >
        <img
          src="@/assets/image/common/icon_date.png"
          class="el-icon-date"
          @click="clickLeftIcon"
        >
        <input
          ref="inputLeft"
          v-model="startShowYear"
          class="_inner range_input"
          type="text"
          name="yearInput"
          placeholder="开始年份"
          readonly="readonly"
          @focus="onFocus"
          @keyup="handleInput('start')"
        />
        <span class="el-range-separator">{{ sp }}</span>
        <input
          ref="inputRight"
          v-model="endShowYear"
          readonly="readonly"
          class="_inner range_input"
          type="text"
          name="yearInput"
          placeholder="结束年份"
          @focus="onFocus"
          @keyup="handleInput('end')"
        />
        <!-- 删除icon -->
        <i
          v-show="isClearIconShow"
          class="el-input__icon el-range__close-icon el-icon-circle-close"
          @click="clearSelectYear"
        ></i>
      </div>
    </div>
  </div>
</template>

<script>
import DayJs from 'dayjs'
import { SELECT_STATE } from '@/utils/index.js'
export default {
  name: 'YearPicker',
  props: {
    sp: {
      type: String,
      default: '至'
    },
    value: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      isClearIconShow: false,
      itemBg: {},
      startShowYear: null,
      endShowYear: null,
      yearList: [],
      showPanel: false,
      startYear: null,
      endYear: null,
      curYear: 0,
      curSelectedYear: 0,
      curState: SELECT_STATE.unselect,
      isDisplay: false
    }
  },
  computed: {
    oneSelected() {
      return this.curState === SELECT_STATE.selecting && (this.startYear === this.endYear || this.endYear == null)
    },
    leftYearList() {
      return this.yearList.slice(0, 10)
    },
    rightYearList() {
      return this.yearList.slice(10, 20)
    }
  },
  watch: {
    value: {
      handler(val) {
        if (val?.length === 0) return
        const [first, end] = val || []
        this.startShowYear = first
        this.endShowYear = end
      },
      immediate: true,
      deep: true
    },

    startShowYear: {
      handler(val) {
        this.$emit('input', [val, this.endShowYear || ''])
      },
      immediate: true,
      deep: true
    },

    endShowYear: {
      handler(val) {
        this.$emit('input', [this.startShowYear || '', val])
      },
      immediate: true,
      deep: true
    }
  },
  created() {
    const [startYear, endYear] = this.value || []
    if (startYear) {
      this.startYear = Number(startYear)
      this.endYear = Number(endYear)
      this.curState = SELECT_STATE.selected
      this.curYear = startYear
    } else {
      this.curYear = DayJs().year()
    }
    this.updateYearList()
  },

  mounted() {
    window.Vue = this
  },
  methods: {
    clickLeftIcon() {
      this.showPanel = true
    },
    clearSelectYear() {
      this.startShowYear = undefined
      this.endShowYear = undefined
      this.startYear = ''
      this.endYear = ''
    },
    handleInput(type) {
      switch (type) {
        case 'start':
          if (isNaN(this.startShowYear)) {
            this.startShowYear = this.startYear
            return
          }
          this.startYear = this.startShowYear * 1
          break
        case 'end':
          if (isNaN(this.endShowYear)) {
            this.endShowYear = this.endYear
            return
          }
          this.endYear = this.endShowYear * 1
          break
      }
      [this.startYear, this.endYear] = [this.endYear, this.startYear]
      this.startShowYear = this.startYear
      this.endShowYear = this.endYear
    },

    onHoverItem(iYear) {
      if (this.curState === SELECT_STATE.selecting) {
        const tmpStart = this.curSelectedYear
        this.endYear = Math.max(tmpStart, iYear)
        this.startYear = Math.min(tmpStart, iYear)
      }
    },
    onHoverIcon() {
      if (this.startShowYear !== undefined && this.endShowYear !== undefined) {
        this.isClearIconShow = true
      } else {
        this.isClearIconShow = false
      }
    },
    lossHoverIcon() {
      this.isClearIconShow = false
    },
    async onClickItem(selectYear) {
      if (
        this.curState === SELECT_STATE.unselect ||
        this.curState === SELECT_STATE.selected
      ) {
        this.startYear = selectYear
        this.curSelectedYear = selectYear
        this.endYear = null
        this.curState = SELECT_STATE.selecting
      } else if (this.curState === SELECT_STATE.selecting) {
        this.endShowYear = this.endYear || this.startYear
        this.startShowYear = this.startYear
        this.curState = SELECT_STATE.selected
        await this.$nextTick()
        this.showPanel = false
      }
    },

    async onFocus() {
      await this.$nextTick()
      this.showPanel = true
    },
    updateYearList() {
      const startYear = ~~(this.curYear / 10) * 10
      this.yearList = []
      for (let index = 0; index < 20; index++) {
        this.yearList.push(startYear + index)
      }
    },

    onClickLeft() {
      this.curYear = this.curYear * 1 - 10
      this.updateYearList()
    },

    onClickRight() {
      this.curYear = this.curYear * 1 + 10
      this.updateYearList()
    }
  }
}
</script>
<style lang="scss" scoped>
.year-all {
  position: relative;

  .el-popover {
    width: 644px;
    height: 250px;
    top: 44px;

    .float-panel {
      position: relative;
      display: flex;
      justify-content: space-between;

      .left-panel,
      .right-panel {
        width: 50%;
        padding: 0 16px;

        .panel-head {
          font-size: 16px;
          height: 46px;
          display: flex;
          align-items: center;
          justify-content: center;

          .year-main {
            display: flex;
          }

          .year-main:hover {
            color: #2F45FF;
            cursor: pointer;
          }

        }

        .panel-content {
          font-size: 14px;
          display: flex;
          justify-self: start;
          flex-wrap: wrap;

          .year-item {
            display: flex;
            justify-content: center;
            align-items: center;
            width: 64px;
            height: 32px;
            margin-bottom: 12px;
            margin-right: 4px;
          }

          .year-item:hover {
            cursor: pointer;
            background: #F4F4F7;
            border-radius: 2px;
            color: #333;
          }

          .startSelected {
            background: #fff !important;
            border: 1px solid #2F45FF;
            border-radius: 2px;
            color: #2F45FF;
          }

          .endSelected {
            background: #fff !important;
            border: 1px solid #2F45FF;
            border-radius: 2px;
            color: #2F45FF;
          }

          .year-item:active {
            background: #fff;
            border: 1px solid #2F45FF;
            border-radius: 2px;
          }
        }
      }

      .left-panel {
        border-right: 1px solid #ebeef5;
      }

      .el-icon-d-arrow-right {
        position: absolute;
        right: 0;
      }

      .el-icon-d-arrow-right:hover {
        color: #2F45FF;
        cursor: pointer;
      }

      .el-icon-d-arrow-left {
        position: absolute;
        left: 0;
      }

      .el-icon-d-arrow-left:hover {
        color: #2F45FF;
        cursor: pointer;
      }
    }
  }

  .el-icon-date {
    width: 18px;
    height: 18px;
  }

  .el-icon-date:hover {
    cursor: pointer;
  }

  .el-range__close-icon {
    display: flex;
    align-items: center;
  }

  .range_input {
    appearance: none;
    border: none;
    outline: 0;
    padding: 0;
    width: 86px;
    color: #606266;
    line-height: 40px;
    height: 40px;
    margin: 0;
    text-align: center;
    color: #646566;
    font-size: 14px;
  }

  .yearPicker {
    input:first-child {
      text-align: right;
    }

    .labelText {
      position: absolute;
      left: 8px;
    }

    background-color: #fff;

    span {
      padding: 0 8px;
      height: 32px;
      line-height: 32px;
    }

    border: 1px solid #eff1f3;
    height: 34px;
    line-height: 34px;
    border-radius: 4px;
    padding: 0 28px 0 8px;
    box-sizing: border-box;
  }

  input {
    width: 60px;
    border: none;
    height: 32px;
    line-height: 32px;
    box-sizing: border-box;
    background-color: transparent;
  }

  input:focus {
    outline: none;
    background-color: transparent;
  }

  .yearPicker:hover {
    border-color: #3e77fc;
  }

  .dateIcon {
    position: absolute;
    right: 16px;
    top: 9px;
    color: #adb2bc;
  }
}
</style>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
  • 448
  • 449
  • 450
  • 451
  • 452
  • 453
  • 454
  • 455
  • 456
  • 457
  • 458
  • 459
  • 460
  • 461
  • 462
  • 463
  • 464
  • 465
  • 466
  • 467
  • 468
  • 469
  • 470
  • 471
  • 472
  • 473
  • 474
  • 475
  • 476
  • 477
  • 478
  • 479
  • 480
  • 481
  • 482
  • 483
  • 484
  • 485
  • 486
  • 487
  • 488
  • 489
  • 490
  • 491
  • 492
  • 493
  • 494
  • 495
  • 496
  • 497

使用方式

  • 在所需要该组件的页面文件中引入YearPick
    pp2zJJO.md.jpg
  • html中使用
    pp2zNSe.jpg
    *注:使用了element 中的自定义指令clickOutside

pp2zRyj.jpg

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/97585
推荐阅读
相关标签
  

闽ICP备14008679号