/
index.js
154 lines (143 loc) · 3.16 KB
/
index.js
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
import hover from '../behaviors/hover';
import eventUtil from '../core/utils/event-util';
Component({
behaviors: [hover],
externalClasses: [
'l-class',
'l-symbol-class',
'l-count-class',
'l-disabled-class'
],
properties: {
count: {
type: Number,
value: 1
},
max: {
type: Number,
value: 9999
},
min: {
type: Number,
value: 1
},
step: {
type: Number,
value: 1
},
disabled: Boolean,
iconSize: String,
iconColor: String
},
/**
* 组件的初始数据
*/
data: {
focus: false,
result: 1
},
observers: {
'result': function (count) {
eventUtil.emit(this, 'linchange', { count });
},
'count,min,max': function () {
this.valueRange(this.data.count, 'parameter');
},
},
/**
* 组件的方法列表
*/
methods: {
doNothing(e) {
const { type } = e.currentTarget.dataset;
eventUtil.emit(this, 'linout', { type, way: 'icon' });
},
onCount() {
this.setData({
focus: true
});
},
onBlur(e) {
this.setData({
focus: false
});
let {
value
} = e.detail;
setTimeout(() => {
this.blurCount(Number(value), () => {
this.data.count = this.data.result;
eventUtil.emit(this, 'lintap', { count: this.data.result, type: 'blur' });
});
}, 50);
},
blurCount(value, callback) {
if (value) {
this.valueRange(value);
} else {
this.setData({
result: this.properties.count
});
}
callback && callback();
},
valueRange(value, way = 'input') {
if (value > this.properties.max) {
this.setData({
result: this.properties.max
}, () => {
eventUtil.emit(this, 'linout', { type: 'overflow_max', way });
});
} else if (value < this.properties.min) {
this.setData({
result: this.properties.min
}, () => {
eventUtil.emit(this, 'linout', { type: 'overflow_min', way });
});
} else {
this.setData({
result: value
});
}
},
reduceTap() {
let distance = this.data.count - this.properties.step;
if (distance <= this.properties.min) {
this.data.count = this.properties.min;
} else {
this.data.count -= this.properties.step;
}
this.setData({
result: this.data.count
});
this.triggerEvent('lintap', {
count: this.data.result,
type: 'reduce'
}, {
bubbles: true,
composed: true
});
},
addTap() {
let distance = this.data.count + this.properties.step;
if (distance >= this.properties.max) {
this.data.count = this.properties.max;
} else {
this.data.count += this.properties.step;
}
this.setData({
result: this.data.count
});
this.triggerEvent('lintap', {
count: this.data.result,
type: 'add'
}, {
bubbles: true,
composed: true
});
},
onInput(e){
eventUtil.emit(this,'lininput',e.detail);
}
}
});