index.vue 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <template>
  2. <view class="jnpf-num-range">
  3. <u-input input-align='right' v-model="min" :placeholder="$t('component.jnpf.numberRange.min')" type="number" @blur="onblur($event,'min')" />
  4. <text class="separator">-</text>
  5. <u-input input-align='right' v-model="max" :placeholder="$t('component.jnpf.numberRange.max')" type="number" @blur="onblur($event,'max')" />
  6. </view>
  7. </template>
  8. <script>
  9. export default {
  10. name: 'jnpf-number-range',
  11. props: {
  12. modelValue: {
  13. type: Array,
  14. default: () => []
  15. },
  16. disabled: {
  17. type: Boolean,
  18. default: false
  19. },
  20. precision: {
  21. type: Number,
  22. default: undefined
  23. },
  24. },
  25. data() {
  26. return {
  27. min: '',
  28. max: ''
  29. }
  30. },
  31. watch: {
  32. modelValue: {
  33. handler(val) {
  34. if (Array.isArray(val) && val.length === 2) {
  35. this.min = val[0]
  36. this.max = val[1]
  37. } else {
  38. this.min = ''
  39. this.max = ''
  40. }
  41. },
  42. immediate: true,
  43. },
  44. min(val) {
  45. this.onChange()
  46. },
  47. max(val) {
  48. this.onChange()
  49. }
  50. },
  51. methods: {
  52. onblur(e, type) {
  53. this[type] = !isNaN(this.precision) && (e == '0' || e) ? Number(e).toFixed(this.precision) : e
  54. },
  55. onChange() {
  56. if ((!this.min && this.min !== 0) && (!this.max && this.max !== 0)) return this.$emit('update:modelValue',
  57. [])
  58. this.$emit('update:modelValue', [this.min, this.max])
  59. }
  60. }
  61. }
  62. </script>
  63. <style lang="scss" scoped>
  64. .jnpf-num-range {
  65. width: 100%;
  66. display: flex;
  67. justify-content: space-between;
  68. align-items: center;
  69. .separator {
  70. margin: 0 20rpx;
  71. flex-shrink: 0;
  72. }
  73. }
  74. </style>