|
| 1 | +<template> |
| 2 | + <input |
| 3 | + type="text" |
| 4 | + autocomplete="off" |
| 5 | + :value="maskedValue" |
| 6 | + @change="change" |
| 7 | + @input="input" |
| 8 | + v-number="config" |
| 9 | + class="v-number" |
| 10 | + /> |
| 11 | +</template> |
| 12 | + |
| 13 | +<script> |
| 14 | +import directive from "../../../src/directive"; |
| 15 | +import options from "../../../src/options"; |
| 16 | +
|
| 17 | +export default { |
| 18 | + props: { |
| 19 | + modelValue: { |
| 20 | + required: true, |
| 21 | + }, |
| 22 | + nullValue: { |
| 23 | + type: [Number, String], |
| 24 | + default: () => options.nullValue, |
| 25 | + }, |
| 26 | + masked: { |
| 27 | + type: Boolean, |
| 28 | + default: false, |
| 29 | + }, |
| 30 | + reverseFill: { |
| 31 | + type: Boolean, |
| 32 | + default: options.reverseFill, |
| 33 | + }, |
| 34 | + precision: { |
| 35 | + type: Number, |
| 36 | + default: () => options.precision, |
| 37 | + }, |
| 38 | + minimumFractionDigits: { |
| 39 | + type: [Number, Boolean], |
| 40 | + default: () => options.minimumFractionDigits, |
| 41 | + }, |
| 42 | + decimal: { |
| 43 | + type: String, |
| 44 | + default: () => options.decimal, |
| 45 | + }, |
| 46 | + separator: { |
| 47 | + type: String, |
| 48 | + default: () => options.separator, |
| 49 | + }, |
| 50 | + prefix: { |
| 51 | + type: String, |
| 52 | + default: () => options.prefix, |
| 53 | + }, |
| 54 | + suffix: { |
| 55 | + type: String, |
| 56 | + default: () => options.suffix, |
| 57 | + }, |
| 58 | + }, |
| 59 | + directives: { |
| 60 | + number: directive, |
| 61 | + }, |
| 62 | + emits: ["update:modelValue"], |
| 63 | + data() { |
| 64 | + return { |
| 65 | + maskedValue: this.modelValue, |
| 66 | + unmaskedValue: null, |
| 67 | + }; |
| 68 | + }, |
| 69 | + methods: { |
| 70 | + input({ target }) { |
| 71 | + this.maskedValue = target.value; |
| 72 | + this.unmaskedValue = target.unmaskedValue; |
| 73 | + }, |
| 74 | + change() { |
| 75 | + this.$emit("update:modelValue", this.emittedValue); |
| 76 | + }, |
| 77 | + }, |
| 78 | + computed: { |
| 79 | + emittedValue() { |
| 80 | + return this.masked ? this.maskedValue : this.unmaskedValue; |
| 81 | + }, |
| 82 | + config() { |
| 83 | + const config = {}; |
| 84 | + Object.keys(this.$props) |
| 85 | + .filter((item) => item !== "modelValue") |
| 86 | + .forEach((item) => { |
| 87 | + config[item] = this.$props[item]; |
| 88 | + }); |
| 89 | + return config; |
| 90 | + }, |
| 91 | + }, |
| 92 | + watch: { |
| 93 | + modelValue(val) { |
| 94 | + if (this.unmaskedValue !== val) { |
| 95 | + this.maskedValue = val; |
| 96 | + } |
| 97 | + }, |
| 98 | + config: { |
| 99 | + immediate: true, |
| 100 | + handler(val) { |
| 101 | + this.$emit( |
| 102 | + "update:modelValue", |
| 103 | + this.emittedValue || this.unmaskedValue || this.maskedValue |
| 104 | + ); |
| 105 | + }, |
| 106 | + }, |
| 107 | + }, |
| 108 | +}; |
| 109 | +</script> |
0 commit comments