@nstc-business/imes 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,294 @@
1
+ <template>
2
+ <div
3
+ class="n20-num-w"
4
+ @mouseenter="isHove = true"
5
+ @mouseleave="isHove = false"
6
+ >
7
+ <el-input
8
+ ref="input"
9
+ v-model="valueStr"
10
+ v-bind="$attrs"
11
+ class="n20-stc"
12
+ :disabled="disabled"
13
+ :placeholder="phd"
14
+ :clearable="isClearable"
15
+ :validate-event="false"
16
+ :maxlength="maxlength > 16 ? 16 : maxlength"
17
+ @focus="focusFn"
18
+ @input="inputFn"
19
+ @blur="blurFn"
20
+ @clear="clearFn"
21
+ @change="changeFn"
22
+ @keydown.native="stepFn"
23
+ >
24
+ <template slot="suffix">
25
+ <slot name="suffix"></slot>
26
+ </template>
27
+ <template slot="prefix">
28
+ <slot name="prefix"></slot>
29
+ </template>
30
+ <template v-if="type === 'rate'">
31
+ <span v-if="suffixV" slot="suffix" class="el-input__icon">
32
+ {{ suffix }}
33
+ </span>
34
+ </template>
35
+ </el-input>
36
+ </div>
37
+ </template>
38
+
39
+ <script>
40
+ import { N } from 'n20-common-lib'
41
+ import XEUtils from 'xe-utils'
42
+
43
+ export default {
44
+ name: 'InputNumber',
45
+ props: {
46
+ value: {
47
+ type: [Number, String],
48
+ default: undefined
49
+ },
50
+ type: {
51
+ type: String,
52
+ default: 'money',
53
+ validator(v) {
54
+ return ['money', 'rate'].includes(v)
55
+ }
56
+ },
57
+ min: {
58
+ type: Number,
59
+ default: -9999999999999.99
60
+ },
61
+ max: {
62
+ type: Number,
63
+ default: 9999999999999.99
64
+ },
65
+ step: {
66
+ type: Number,
67
+ default: 1
68
+ },
69
+ stepStrictly: {
70
+ type: Boolean,
71
+ default: false
72
+ },
73
+ disabled: {
74
+ type: Boolean,
75
+ default: undefined
76
+ },
77
+ isClearable: {
78
+ type: Boolean,
79
+ default: false
80
+ },
81
+ placeholder: {
82
+ type: String,
83
+ default: undefined
84
+ },
85
+ dNum: {
86
+ type: Number,
87
+ default: undefined
88
+ },
89
+ format: {
90
+ type: String,
91
+ default: undefined
92
+ },
93
+ suffix: {
94
+ type: String,
95
+ default: '%'
96
+ },
97
+ rangeAuto: {
98
+ type: Boolean,
99
+ default: false
100
+ },
101
+ maxlength: {
102
+ type: Number,
103
+ default: 16
104
+ }
105
+ },
106
+ data() {
107
+ this.listeners = Object.assign({}, this.$listeners, {
108
+ input: () => {}
109
+ })
110
+ return {
111
+ valueStr: '',
112
+ showStr: true,
113
+ isHove: false,
114
+ isFocus: false
115
+ }
116
+ },
117
+ computed: {
118
+ suffixV() {
119
+ if (!this.isClearable) return true
120
+ if (!this.valueStr) return true
121
+ if (!(this.isHove || this.isFocus)) return true
122
+ return false
123
+ },
124
+ phd() {
125
+ if (this.placeholder) {
126
+ return this.placeholder
127
+ } else {
128
+ return '请输入'
129
+ }
130
+ },
131
+ fNum() {
132
+ if (this.format) {
133
+ let i_f = this.format.indexOf('.')
134
+ return i_f === -1 ? 0 : this.format.length - i_f - 1
135
+ }
136
+ if (this.dNum || this.dNum === 0) {
137
+ return this.dNum
138
+ }
139
+ if (this.type === 'rate') {
140
+ return 4
141
+ }
142
+ if (this.type === 'money') {
143
+ return 2
144
+ }
145
+ return 2
146
+ }
147
+ },
148
+ watch: {
149
+ value: {
150
+ handler(val) {
151
+ if (this.rangeAuto) {
152
+ if (val || val === 0) {
153
+ let nF = N.toString(val).split('.')[1] || ''
154
+ if (nF.length < this.fNum) {
155
+ return (this.valueStr = N.addThousands(
156
+ this.subFixed(val, this.fNum)
157
+ ))
158
+ }
159
+ }
160
+ this.valueStr = N.addThousands(val)
161
+ } else {
162
+ this.valueStr = N.addThousands(this.subFixed(val, this.fNum))
163
+ }
164
+ },
165
+ immediate: true
166
+ }
167
+ },
168
+ methods: {
169
+ focusFn() {
170
+ this.isFocus = true
171
+ if (!this.disabled && this.valueStr) {
172
+ this.valueStr = this.valueStr.replace(/,/g, '')
173
+ this.preValue = this.valueStr
174
+ }
175
+ },
176
+ blurFn() {
177
+ this.isFocus = false
178
+ this.changeFn(this.valueStr)
179
+ this.$emit('blur')
180
+ this.dispatch('ElFormItem', 'el.form.blur', [this.value])
181
+ },
182
+ clearFn() {
183
+ this.$emit('clear')
184
+ },
185
+ stepFn(ev) {
186
+ if (
187
+ (ev.code === 'ArrowUp' || ev.code === 'ArrowDown') &&
188
+ !this.disabled
189
+ ) {
190
+ let val = N(this.valueStr)
191
+ if (!isNaN(val)) {
192
+ ev.preventDefault()
193
+ val = ev.code === 'ArrowUp' ? val + this.step : val - this.step
194
+ if (val < this.min) {
195
+ val = this.min
196
+ } else if (val > this.max) {
197
+ val = this.max
198
+ }
199
+ if (this.rangeAuto) {
200
+ this.valueStr = N.toString(val)
201
+ } else {
202
+ this.valueStr = this.subFixed(val, this.fNum)
203
+ }
204
+ }
205
+ }
206
+ },
207
+ inputFn(valStr) {
208
+ if (valStr !== '-' && isNaN(valStr.replace(/,/g, ''))) {
209
+ this.valueStr = this.preValue
210
+ } else {
211
+ this.preValue = valStr
212
+ }
213
+ },
214
+ changeFn(valStr) {
215
+ if (this.changeIng) return
216
+ this.changeIng = true
217
+ setTimeout(() => {
218
+ this.changeIng = false
219
+ })
220
+
221
+ let val = N(valStr)
222
+ if (isNaN(val)) {
223
+ this.valueStr = ''
224
+ let oVal = this.value
225
+ if (oVal || oVal === 0) {
226
+ this.$emit('input', undefined)
227
+ this.$emit('change', undefined)
228
+ this.dispatch('ElFormItem', 'el.form.change', [undefined])
229
+ }
230
+ return
231
+ }
232
+
233
+ let nStr
234
+ if (this.rangeAuto) {
235
+ nStr = N.toString(val)
236
+ this.valueStr = N.addThousands(valStr)
237
+ } else {
238
+ if (this.stepStrictly) {
239
+ let N = val / this.step
240
+ if (val % this.step !== 0) {
241
+ val = Math.round(N) * this.step
242
+ }
243
+ }
244
+ if (val < this.min) {
245
+ val = this.min
246
+ } else if (val > this.max) {
247
+ val = this.max
248
+ }
249
+ nStr = this.subFixed(val, this.fNum)
250
+ this.valueStr = N.addThousands(nStr)
251
+ }
252
+
253
+ this.$nextTick(() => {
254
+ let nVar = Number(nStr)
255
+ let oVal = this.value
256
+ if (oVal !== nVar) {
257
+ this.$emit('input', nVar)
258
+ this.$emit('change', nVar)
259
+ this.dispatch('ElFormItem', 'el.form.change', [nVar])
260
+ }
261
+ })
262
+ },
263
+ dispatch(componentName, eventName, params) {
264
+ var parent = this.$parent || this.$root
265
+ var name = parent.$options.componentName
266
+
267
+ while (parent && (!name || name !== componentName)) {
268
+ parent = parent.$parent
269
+
270
+ if (parent) {
271
+ name = parent.$options.componentName
272
+ }
273
+ }
274
+ if (parent) {
275
+ parent.$emit.apply(parent, [eventName].concat(params))
276
+ }
277
+ },
278
+ subFixed(n, decimal = 6) {
279
+ if (n === undefined || n === null || n === '') {
280
+ return ''
281
+ } else {
282
+ const value = n.toString().split('.')
283
+ // 整数或者小数位小于设置的小数位,用toFixed自动补零
284
+ if (value[1] === undefined || value[1].length < decimal) {
285
+ return XEUtils.toFixed(n, decimal)
286
+ } else {
287
+ // 小数位超过设置的小数位不四舍五入
288
+ return Number(value[0] + '.' + value[1].substring(0, decimal))
289
+ }
290
+ }
291
+ }
292
+ }
293
+ }
294
+ </script>