inputRange.vue 4.47 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"
    >
      <template v-if="infinity">
        <el-input-number
          class="el-range-input hide-btn"
          :placeholder="startPlaceholder"
          v-model="userInput[0]"
          @blur="handleBlur(0)"
        ></el-input-number>
        <span class="el-range-separator">至</span>
        <el-input-number
          class="el-range-input hide-btn"
          :placeholder="endPlaceholder"
          v-model="userInput[1]"
          @blur="handleBlur(1)"
        ></el-input-number>
      </template>
      <template v-else>
        <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>
      </template>
      <i
        v-show="showClose"
        @click="handleClickIcon"
        class="el-icon-circle-close"
      ></i>
    </div>
    <div class="errorTips" v-if="tipsShow">最大金额需大于最小金额</div>
  </div>
</template>
<script>
import { limitPrice } from "@/common/format"
export default {
  data() {
    return {
      pickerVisible: false,
      showClose: false,
      userInput: this.displayValue,
      oldVal: {
        startInput: "",
        endInput: ""
      },
      tipsShow: false
    }
  },

  props: {
    displayValue: Array,
    placeholder: String,
    startPlaceholder: String,
    endPlaceholder: String,
    infinity: Boolean,
    regRuleStr: {
      type: String,
      default: "priceReg"
    }
  },
  watch: {
    displayValue(val) {
      if (val) {
        this.userInput = val
        if (val.length == 0) {
          this.tipsShow = false
        }
      }
    }
  },
  methods: {
    handleStartInput(event) {
      this.userInput[0] = limitPrice(
        event,
        this.oldVal.startInput,
        15,
        2,
        "",
        "",
        this.regRuleStr
      )
      this.oldVal.startInput = this.userInput[0]
    },

    handleEndInput(event) {
      this.userInput[1] = limitPrice(
        event,
        this.oldVal.endInput,
        15,
        2,
        "",
        "",
        this.regRuleStr
      )
      this.oldVal.endInput = this.userInput[1]
    },
    handleClickIcon(event) {
      this.userInput[0] = null
      this.userInput[1] = null
      this.tipsShow = false
    },
    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() {
      const val = this.userInput
      if (Array.isArray(val)) {
        if (val[0] == "-") {
          val[0] = null
        }
        if (val[1] == "-") {
          val[1] = null
        }
        if (val[0] && val[1]) {
          if (+val[0] > +val[1]) {
            val[1] = null
            this.tipsShow = true
          } else {
            this.tipsShow = false
          }
        }
      }
    }
  }
}
</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;
  }
  .hide-btn {
    ::v-deep {
      span {
        display: none;
      }
      input {
        padding: 0;
      }
    }
  }
  .errorTips {
    color: red;
    // text-align: center;
    font-size: 12px;
  }
}
</style>