index.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <template>
  2. <div
  3. ref="reference"
  4. class="el-range-editor el-input__inner"
  5. style="border: 0; padding: 0;border-radius: 0;"
  6. >
  7. <el-input
  8. autocomplete="off"
  9. :placeholder="startPlaceholder"
  10. :value="userInput && userInput[0]"
  11. :disabled="disabled"
  12. v-bind="firstInputId"
  13. :readonly="readonly"
  14. :name="name && name[0]"
  15. type="number"
  16. class="el-range-input"
  17. @input="handleStartInput"
  18. />
  19. <slot name="range-separator">
  20. <span class="el-range-separator">{{ rangeSeparator }}</span>
  21. </slot>
  22. <el-input
  23. autocomplete="off"
  24. :placeholder="endPlaceholder"
  25. :value="userInput && userInput[1]"
  26. :disabled="disabled"
  27. v-bind="secondInputId"
  28. :readonly="readonly"
  29. :name="name && name[1]"
  30. type="number"
  31. class="el-range-input"
  32. @input="handleEndInput"
  33. />
  34. </div>
  35. </template>
  36. <script>
  37. export default {
  38. props: {
  39. value: {
  40. type: [Array, Number, String]
  41. },
  42. disabled: {
  43. type: Boolean,
  44. default: false
  45. },
  46. rangeSeparator: {
  47. type: String,
  48. default: '-'
  49. },
  50. id: {
  51. type: Array
  52. },
  53. name: {
  54. type: Array
  55. },
  56. readonly: {
  57. type: Boolean,
  58. default: false
  59. }
  60. },
  61. data() {
  62. return {
  63. startPlaceholder: '',
  64. endPlaceholder: '',
  65. userInput: []
  66. }
  67. },
  68. computed: {
  69. firstInputId() {
  70. const obj = {}
  71. const id = this.id && this.id[0]
  72. if (id) obj.id = id
  73. return obj
  74. },
  75. secondInputId() {
  76. const obj = {}
  77. const id = this.id && this.id[1]
  78. if (id) obj.id = id
  79. return obj
  80. }
  81. },
  82. watch: {
  83. value(val) {
  84. if (this.$utils.isEmpty(val)) {
  85. this.userInput = [null, null]
  86. } else {
  87. if (Array.isArray(val)) {
  88. this.userInput = val
  89. } else {
  90. this.userInput = [val, null]
  91. }
  92. }
  93. },
  94. userInput: {
  95. handler(val) {
  96. this.$emit('input', val)
  97. },
  98. deep: true
  99. }
  100. },
  101. methods: {
  102. handleStartInput(value) {
  103. if (this.userInput) {
  104. this.userInput = [value, this.userInput[1]]
  105. } else {
  106. this.userInput = [value, null]
  107. }
  108. },
  109. handleEndInput(value) {
  110. if (this.userInput) {
  111. this.userInput = [this.userInput[0], value]
  112. } else {
  113. this.userInput = [null, value]
  114. }
  115. }
  116. }
  117. }
  118. </script>