@@ -33,12 +33,11 @@ Component({
33
33
* 组件的初始数据
34
34
*/
35
35
data : {
36
- focus : false ,
37
- result : 1
36
+ focus : false
38
37
} ,
39
38
40
39
observers : {
41
- 'result ' : function ( count ) {
40
+ 'count ' : function ( count ) {
42
41
eventUtil . emit ( this , 'linchange' , { count } ) ;
43
42
} ,
44
43
'count,min,max' : function ( ) {
@@ -56,97 +55,75 @@ Component({
56
55
} ,
57
56
58
57
onCount ( ) {
59
- this . setData ( {
60
- focus : true
61
- } ) ;
58
+ this . setData ( { focus : true } ) ;
62
59
} ,
63
60
64
61
onBlur ( e ) {
65
- this . setData ( {
66
- focus : false
67
- } ) ;
68
62
let {
69
63
value
70
64
} = e . detail ;
71
65
setTimeout ( ( ) => {
72
66
this . blurCount ( Number ( value ) , ( ) => {
73
- this . data . count = this . data . result ;
74
- eventUtil . emit ( this , 'lintap' , { count : this . data . result , type : 'blur' } ) ;
67
+ eventUtil . emit ( this , 'lintap' , { count : this . data . count , type : 'blur' } ) ;
75
68
} ) ;
76
69
} , 50 ) ;
77
70
} ,
78
71
79
72
blurCount ( value , callback ) {
80
73
if ( value ) {
81
74
this . valueRange ( value ) ;
82
- } else {
83
- this . setData ( {
84
- result : this . properties . count
85
- } ) ;
86
75
}
87
76
callback && callback ( ) ;
88
77
} ,
89
78
90
79
valueRange ( value , way = 'input' ) {
91
- if ( value > this . properties . max ) {
92
- this . setData ( {
93
- result : this . properties . max
94
- } , ( ) => {
95
- eventUtil . emit ( this , 'linout' , { type : 'overflow_max' , way } ) ;
96
- } ) ;
97
- } else if ( value < this . properties . min ) {
98
- this . setData ( {
99
- result : this . properties . min
100
- } , ( ) => {
101
- eventUtil . emit ( this , 'linout' , { type : 'overflow_min' , way } ) ;
102
- } ) ;
103
- } else {
104
- this . setData ( {
105
- result : value
106
- } ) ;
107
- }
108
- } ,
80
+ const { count, min, max } = this . data ;
109
81
110
- reduceTap ( ) {
111
- let distance = this . data . count - this . properties . step ;
112
- if ( distance <= this . properties . min ) {
113
- this . data . count = this . properties . min ;
114
- } else {
115
- this . data . count -= this . properties . step ;
116
- }
117
- this . setData ( {
118
- result : this . data . count
119
- } ) ;
120
- this . triggerEvent ( 'lintap' , {
121
- count : this . data . result ,
122
- type : 'reduce'
123
- } , {
124
- bubbles : true ,
125
- composed : true
82
+ // 数据错误,显示警告
83
+ way === 'parameter' && value > max && console . error ( `Counter 组件:初始值 ${ count } 大于了最大值 ${ max } ,请注意修正` ) ;
84
+ way === 'parameter' && value < min && console . error ( `Counter 组件:初始值 ${ count } 小于了最小值 ${ min } ,请注意修正` ) ;
85
+
86
+ // 触发相应事件
87
+ value > max && eventUtil . emit ( this , 'linout' , { type : 'overflow_max' , way } ) ;
88
+ value < min && eventUtil . emit ( this , 'linout' , { type : 'overflow_min' , way } ) ;
89
+
90
+ // 如果 value 越界,则修正其值
91
+ value = value > max ? max : value ;
92
+ value = value < min ? min : value ;
93
+
94
+ // 更新页面显示数值
95
+ value === this . data . count && this . setData ( { focus : false } ) ;
96
+ value !== this . data . count && this . setData ( { count : value } , ( ) => {
97
+ this . setData ( { focus : false } ) ;
126
98
} ) ;
127
99
} ,
128
100
129
- addTap ( ) {
130
- let distance = this . data . count + this . properties . step ;
131
- if ( distance >= this . properties . max ) {
132
- this . data . count = this . properties . max ;
133
- } else {
134
- this . data . count += this . properties . step ;
101
+ /**
102
+ * 点击 +/- 改变数值的监听函数
103
+ *
104
+ * @param {Object } e 事件对象
105
+ */
106
+ onTapChange ( e ) {
107
+ const type = e . currentTarget . dataset . changeType ;
108
+ const { count, step, min, max } = this . data ;
109
+ let result ;
110
+
111
+ // 根据 +/- 做不同运算
112
+ if ( type === 'add' ) {
113
+ result = count + step > max ? max : count + step ;
114
+ } else if ( type === 'reduce' ) {
115
+ result = count - step < min ? min : count - step ;
135
116
}
136
- this . setData ( {
137
- result : this . data . count
138
- } ) ;
139
- this . triggerEvent ( 'lintap' , {
140
- count : this . data . result ,
141
- type : 'add'
142
- } , {
143
- bubbles : true ,
144
- composed : true
117
+
118
+ this . setData ( { count : result } ) ;
119
+ eventUtil . emit ( this , 'lintap' , {
120
+ count : this . data . count ,
121
+ type
145
122
} ) ;
146
123
} ,
147
124
148
- onInput ( e ) {
149
- eventUtil . emit ( this , 'lininput' , e . detail ) ;
125
+ onInput ( e ) {
126
+ eventUtil . emit ( this , 'lininput' , e . detail ) ;
150
127
}
151
128
}
152
129
} ) ;
0 commit comments