index.js
6.5 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
var component_1 = require("../common/component");
var validator_1 = require("../common/validator");
var LONG_PRESS_START_TIME = 600;
var LONG_PRESS_INTERVAL = 200;
// add num and avoid float number
function add(num1, num2) {
var cardinal = Math.pow(10, 10);
return Math.round((num1 + num2) * cardinal) / cardinal;
}
function equal(value1, value2) {
return String(value1) === String(value2);
}
(0, component_1.VantComponent)({
field: true,
classes: ['input-class', 'plus-class', 'minus-class'],
props: {
value: {
type: null,
},
integer: {
type: Boolean,
observer: 'check',
},
disabled: Boolean,
inputWidth: String,
buttonSize: String,
asyncChange: Boolean,
disableInput: Boolean,
decimalLength: {
type: Number,
value: null,
observer: 'check',
},
min: {
type: null,
value: 1,
observer: 'check',
},
max: {
type: null,
value: Number.MAX_SAFE_INTEGER,
observer: 'check',
},
step: {
type: null,
value: 1,
},
showPlus: {
type: Boolean,
value: true,
},
showMinus: {
type: Boolean,
value: true,
},
disablePlus: Boolean,
disableMinus: Boolean,
longPress: {
type: Boolean,
value: true,
},
theme: String,
alwaysEmbed: Boolean,
},
data: {
currentValue: '',
},
watch: {
value: function () {
this.observeValue();
},
},
created: function () {
this.setData({
currentValue: this.format(this.data.value).newValue,
});
},
methods: {
observeValue: function () {
var value = this.data.value;
this.setData({ currentValue: this.format(value).newValue });
},
check: function () {
var newValue = this.format(this.data.currentValue).newValue;
if (!equal(newValue, this.data.currentValue)) {
this.setData({ currentValue: newValue });
}
},
isDisabled: function (type) {
var _a = this.data, disabled = _a.disabled, disablePlus = _a.disablePlus, disableMinus = _a.disableMinus, currentValue = _a.currentValue, max = _a.max, min = _a.min;
if (type === 'plus') {
return disabled || disablePlus || +currentValue >= +max;
}
return disabled || disableMinus || +currentValue <= +min;
},
onFocus: function (event) {
this.$emit('focus', event.detail);
},
onBlur: function (event) {
var data = this.format(event.detail.value);
this.setData({ currentValue: data.newValue });
this.emitChange(data);
this.$emit('blur', __assign(__assign({}, event.detail), { value: +data.newValue }));
},
// filter illegal characters
filter: function (value) {
value = String(value).replace(/[^0-9.-]/g, '');
if (this.data.integer && value.indexOf('.') !== -1) {
value = value.split('.')[0];
}
return value;
},
format: function (value) {
// filter illegal characters and format integer
var safeValue = this.filter(value);
// format range
var rangeValue = Math.max(Math.min(this.data.max, +safeValue), this.data.min);
// format decimal
var newValue = (0, validator_1.isDef)(this.data.decimalLength)
? rangeValue.toFixed(this.data.decimalLength)
: String(rangeValue);
return { value: value, newValue: newValue };
},
onInput: function (event) {
var _a = (event.detail || {}).value, value = _a === void 0 ? '' : _a;
// allow input to be empty
if (value === '') {
return;
}
var formatted = this.format(value);
this.emitChange(formatted);
},
emitChange: function (data) {
var value = data.value, newValue = data.newValue;
if (!this.data.asyncChange) {
// fix when input 11. parsed to 11, unable to enter decimal
this.setData({ currentValue: +value === +newValue ? value : newValue });
}
this.$emit('change', +newValue);
},
onChange: function () {
var type = this.type;
if (this.isDisabled(type)) {
this.$emit('overlimit', type);
return;
}
var diff = type === 'minus' ? -this.data.step : +this.data.step;
var value = this.format(String(add(+this.data.currentValue, diff)));
this.emitChange(value);
this.$emit(type);
},
longPressStep: function () {
var _this = this;
this.longPressTimer = setTimeout(function () {
_this.onChange();
_this.longPressStep();
}, LONG_PRESS_INTERVAL);
},
onTap: function (event) {
var type = event.currentTarget.dataset.type;
this.type = type;
this.onChange();
},
onTouchStart: function (event) {
var _this = this;
if (!this.data.longPress) {
return;
}
clearTimeout(this.longPressTimer);
var type = event.currentTarget.dataset.type;
this.type = type;
this.isLongPress = false;
this.longPressTimer = setTimeout(function () {
_this.isLongPress = true;
_this.onChange();
_this.longPressStep();
}, LONG_PRESS_START_TIME);
},
onTouchEnd: function () {
if (!this.data.longPress) {
return;
}
clearTimeout(this.longPressTimer);
},
},
});