-
Notifications
You must be signed in to change notification settings - Fork 16
/
money.vue
150 lines (149 loc) · 4.71 KB
/
money.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<template>
<div
class="money el-input"
:class="{'is-disabled':disabled,'el-input-group el-input-group--append':!!$slots.append}">
<div class="el-input el-input--suffix">
<input
ref="ipt"
autocomplete="off"
type="text"
rows="2"
:disabled="disabled"
validateevent="true"
class="el-input__inner"
v-model="val">
<span class="el-input__suffix" v-show="showClose">
<span class="el-input__suffix-inner">
<i class="el-input__icon el-icon-circle-close el-input__clear" @click="clear"></i>
</span>
</span>
</div>
<div
v-if="!!$slots.append"
class="el-input-group__append">
<slot name="append"></slot>
</div>
</div>
</template>
<script>
function numToMoney(v, n) {
const fixed = this.fixed || 2
let r
const ipt = this.$refs['ipt']
if (v === '' || v === undefined) {
return v
} else {
let [a, b = ''] = (v + '').split(/(?=\.)/)
let x = a.length % 3
r = a.substr(0, x) +
a.substr(x).replace(/(\d{3})/g, ',$1').substr(+!x)
if (n) r += (+(0 + b)).toFixed(n).substr(1)
else if (b) r += b.substr(0, fixed + 1)
}
if (ipt) {
let st = -1
if (document.activeElement === ipt) {
const val = ipt.value + ''
if (val === '' || /^\.0*$/g.test(val)) {
this.$emit('input', '')
this.$emit('change', '')
return ''
}
const point = val.indexOf('.')
const l0 = val.length
const l1 = r.length
const fix = l1 - l0
const s0 = ipt.selectionStart
const s1 = ipt.selectionEnd
if (point !== -1 && s0 > point) {
st = s0 - fix + (
fix > 0
? s0 === s1
? s0 - point === fix && fix === 1
? 0 : 1
: 2
: -1
)
} else {
if (val.length === 1 && s0 === s1 && fix === fixed + 1) {
st = 1
} else st = s0 + fix
}
}
ipt.value = r
if (st !== -1) ipt.setSelectionRange(st, st)
}
return r
}
export default {
data(){
return {
showClose:false
}
},
props: {
value: {
required: false,
'default': ''
},
disabled: {
type: Boolean,
required: false,
'default': false
},
fixed: {
type: Number,
required: false
},
max: {
type: Number,
required: false
},
min: {
type: Number,
required: false
},
clearable: {
type: Boolean,
required: false,
'default': false
}
},
mounted() {
const {$refs: {ipt}, min = Number.MIN_SAFE_INTEGER} = this;
ipt.addEventListener('blur', ({target: {value}}) => {
this.val = Math.max(value, min) + '';
});
},
methods:{
clear(){
const {ipt} = this.$refs;
ipt.focus();
ipt.value='';
this.$emit('input', '');
this.$emit('change', '');
}
},
computed: {
val: {
get() {
this.showClose = this.clearable&&this.value!=='';
return numToMoney.call(this, this.value, this.fixed)
},
set(v) {
const {fixed = 2, value, max = Number.MAX_SAFE_INTEGER} = this;
const fix1 = v.replace(/[^0-9,.]/g, '')
const fix2 = v.replace(/,/g, '')
const fix3 = parseFloat(Math.min(parseFloat(fix2), max).toFixed(fixed));
const nan = isNaN(fix3)
if (fix1 !== v || nan || value === fix3) {
numToMoney.call(this, value, fixed)
} else if (!nan) {
this.$emit('input', fix3)
this.$emit('change', fix3)
}
}
}
}
}
</script>