|
| 1 | +<template> |
| 2 | + <input |
| 3 | + v-number="config" |
| 4 | + type="text" |
| 5 | + autocomplete="off" |
| 6 | + :value="maskedValue" |
| 7 | + class="v-number" |
| 8 | + @change="change" |
| 9 | + @input="input" |
| 10 | + @blur="(evt) => $emit('blur', evt)" |
| 11 | + @focus="(evt) => $emit('focus', evt)" |
| 12 | + > |
| 13 | +</template> |
| 14 | + |
| 15 | +<script lang="ts"> |
| 16 | +import Vue from 'vue'; |
| 17 | +import directive from './directive' |
| 18 | +import { cloneDeep, CustomInputEvent, Input } from './core' |
| 19 | +import defaultOptions from './options' |
| 20 | +
|
| 21 | +Vue.directive('number', directive) |
| 22 | +
|
| 23 | +const options = cloneDeep(defaultOptions) |
| 24 | +
|
| 25 | +export default Vue.extend({ |
| 26 | + name: 'VueNumber', |
| 27 | + props: { |
| 28 | + value: { |
| 29 | + required: true, |
| 30 | + type: [Number, String] |
| 31 | + }, |
| 32 | + nullValue: { |
| 33 | + type: [Number, String], |
| 34 | + default: () => options.nullValue |
| 35 | + }, |
| 36 | + masked: { |
| 37 | + type: Boolean, |
| 38 | + default: false |
| 39 | + }, |
| 40 | + reverseFill: { |
| 41 | + type: Boolean, |
| 42 | + default: options.reverseFill |
| 43 | + }, |
| 44 | + prefill: { |
| 45 | + type: Boolean, |
| 46 | + default: options.prefill |
| 47 | + }, |
| 48 | + precision: { |
| 49 | + type: Number, |
| 50 | + default: () => options.precision |
| 51 | + }, |
| 52 | + minimumFractionDigits: { |
| 53 | + type: Number, |
| 54 | + default: () => options.minimumFractionDigits |
| 55 | + }, |
| 56 | + decimal: { |
| 57 | + type: String, |
| 58 | + default: () => options.decimal |
| 59 | + }, |
| 60 | + min: { |
| 61 | + type: Number, |
| 62 | + default: () => options.min |
| 63 | + }, |
| 64 | + max: { |
| 65 | + type: Number, |
| 66 | + default: () => options.max |
| 67 | + }, |
| 68 | + separator: { |
| 69 | + type: String, |
| 70 | + default: () => options.separator |
| 71 | + }, |
| 72 | + prefix: { |
| 73 | + type: String, |
| 74 | + default: () => options.prefix |
| 75 | + }, |
| 76 | + suffix: { |
| 77 | + type: String, |
| 78 | + default: () => options.suffix |
| 79 | + } |
| 80 | + }, |
| 81 | + data() { |
| 82 | + return { |
| 83 | + maskedValue: this.value, |
| 84 | + unmaskedValue: '' as Input | undefined |
| 85 | + } |
| 86 | + }, |
| 87 | + computed: { |
| 88 | + emittedValue() { |
| 89 | + return this.masked ? this.maskedValue : this.unmaskedValue |
| 90 | + }, |
| 91 | + config() { |
| 92 | + return { |
| 93 | + nullValue: this.nullValue, |
| 94 | + masked: this.masked, |
| 95 | + reverseFill: this.reverseFill, |
| 96 | + prefill: this.prefill, |
| 97 | + precision: this.precision, |
| 98 | + minimumFractionDigits: this.minimumFractionDigits, |
| 99 | + decimal: this.decimal, |
| 100 | + min: this.min, |
| 101 | + max: this.max, |
| 102 | + separator: this.separator, |
| 103 | + prefix: this.prefix, |
| 104 | + suffix: this.suffix |
| 105 | + } |
| 106 | + } |
| 107 | + }, |
| 108 | + watch: { |
| 109 | + value(val) { |
| 110 | + if (this.unmaskedValue !== val) { |
| 111 | + this.maskedValue = val |
| 112 | + } |
| 113 | + } |
| 114 | + }, |
| 115 | + methods: { |
| 116 | + input(event: CustomInputEvent) { |
| 117 | + const { target } = event |
| 118 | + this.maskedValue = target.value |
| 119 | + this.unmaskedValue = target.unmaskedValue |
| 120 | + this.$emit('input', this.emittedValue) |
| 121 | + }, |
| 122 | + change() { |
| 123 | + this.$emit('change', this.emittedValue) |
| 124 | + } |
| 125 | + } |
| 126 | +}); |
| 127 | +</script> |
0 commit comments