index.vue 2.96 KB
<template>
  <div class="range">
    <div
      :class="[
        ' el-date-editor',
        'el-range-editor',
        'el-input__inner',
        'el-date-editor--daterange',
        'el-range-editor--small',
        pickerVisible ? 'is-active' : ''
      ]"
      @click="handleRangeClick"
      @mouseleave="showClose = false"
      @mousemove="handleMove"
      ref="reference"
    >
      <el-input
        v-model="userInput[0]"
        :placeholder="startPlaceholder"
        @blur="handleBlur(0)"
        @input="handleStartInput"
        class="el-range-input"
      ></el-input>
      <span class="el-range-separator">至</span>
      <el-input
        v-model="userInput[1]"
        :placeholder="endPlaceholder"
        @input="handleEndInput"
        @focus="handleFocus"
        @blur="handleBlur(1)"
        class="el-range-input"
      ></el-input>

      <i
        v-show="showClose"
        @click="handleClickIcon"
        class="el-icon-circle-close"
      ></i>
    </div>
    <!----><!---->
  </div>
</template>
<script>
export default {
  data() {
    return {
      pickerVisible: false,
      showClose: false,
      userInput: this.displayValue
    }
  },

  props: {
    displayValue: Array,
    placeholder: String,
    startPlaceholder: String,
    endPlaceholder: String
  },
  watch: {
    displayValue(val) {
      if (val) {
        this.userInput = val
      }
    }
  },
  methods: {
    handleStartInput(event) {
      let isNumber = /^\d+$/.test(event)
      if (!isNumber) {
        event = null
      }
      if (event > 100) event = 100
      this.userInput[0] = event
    },

    handleEndInput(event) {
      let isNumber = /^\d+$/.test(event)
      if (!isNumber) {
        event = null
      }
      if (event > 100) event = 100
      this.userInput[1] = event
    },
    handleClickIcon(event) {
      this.userInput[0] = null
      this.userInput[1] = null
    },
    valueIsEmpty() {
      const val = this.userInput
      if (Array.isArray(val)) {
        for (let i = 0, len = val.length; i < len; i++) {
          if (val[i]) {
            return false
          }
        }
      }
      return true
    },
    handleRangeClick() {
      this.pickerVisible = true
    },
    handleMove() {
      if (this.valueIsEmpty()) {
        this.showClose = false
      } else {
        this.showClose = true
      }
    },
    handleFocus() {
      if (!this.pickerVisible) {
        this.pickerVisible = true
        this.$emit("focus", this)
      }
    },
    handleBlur(index) {
      this.pickerVisible = false

      this.contrastValue()
    },
    contrastValue() {
      let val = this.userInput
      if (Array.isArray(val)) {
        if (val[0] > val[1]) {
          val[1] = null
        }
      }
    }
  }
}
</script>
<style scoped lang="scss">
.range {
  .el-range-input {
    ::v-deep {
      input {
        border: 0;
        margin-top: -2px;
        height: 28px;
      }
    }
  }
  .el-icon-circle-close {
    color: #dcdfe6;
    margin-left: 10px;
  }
}
</style>