inputRange.vue 4.15 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>
</template>
<script>
import { limitPrice } from '@/common/format'
export default {
  data() {
    return {
      pickerVisible: false,
      showClose: false,
      userInput: this.displayValue,
      oldVal: {
        startInput: '',
        endInput: ''
      }
    }
  },

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

      this.userInput[0] = limitPrice(event, this.oldVal.startInput, 20, 2)
      this.oldVal.startInput = this.userInput[0]
    },

    handleEndInput(event) {
      // const isNumber = /^\d+$/.test(event)
      // if (!isNumber) {
      //   event = null
      // }
      // if (event > 100) event = 100
      // this.userInput[1] = event

      this.userInput[1] = limitPrice(event, this.oldVal.endInput, 20, 2)
      this.oldVal.endInput = this.userInput[1]
    },
    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() {
      const 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;
  }
  .hide-btn {
    ::v-deep {
      span {
        display: none;
      }
      input {
        padding: 0;
      }
    }
  }
}
</style>