Skip to content

Commit f2aaced

Browse files
committed
fix(Counter): 修复数值更新异常问题
close #1106
1 parent 2605410 commit f2aaced

File tree

2 files changed

+51
-72
lines changed

2 files changed

+51
-72
lines changed

src/counter/index.js

Lines changed: 43 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,11 @@ Component({
3333
* 组件的初始数据
3434
*/
3535
data: {
36-
focus: false,
37-
result: 1
36+
focus: false
3837
},
3938

4039
observers: {
41-
'result': function (count) {
40+
'count': function (count) {
4241
eventUtil.emit(this, 'linchange', { count });
4342
},
4443
'count,min,max': function () {
@@ -56,97 +55,75 @@ Component({
5655
},
5756

5857
onCount() {
59-
this.setData({
60-
focus: true
61-
});
58+
this.setData({ focus: true });
6259
},
6360

6461
onBlur(e) {
65-
this.setData({
66-
focus: false
67-
});
6862
let {
6963
value
7064
} = e.detail;
7165
setTimeout(() => {
7266
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' });
7568
});
7669
}, 50);
7770
},
7871

7972
blurCount(value, callback) {
8073
if (value) {
8174
this.valueRange(value);
82-
} else {
83-
this.setData({
84-
result: this.properties.count
85-
});
8675
}
8776
callback && callback();
8877
},
8978

9079
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;
10981

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 });
12698
});
12799
},
128100

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;
135116
}
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
145122
});
146123
},
147124

148-
onInput(e){
149-
eventUtil.emit(this,'lininput',e.detail);
125+
onInput(e) {
126+
eventUtil.emit(this, 'lininput', e.detail);
150127
}
151128
}
152129
});

src/counter/index.wxml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<view class="l-class container-count">
2-
<view class="symbol {{result<=min|| disabled?'disabled l-disabled-class':'abled l-symbol-class'}}"
3-
catchtap="{{result<=min|| disabled?'doNothing':'reduceTap'}}"
2+
<view class="symbol {{count<=min|| disabled?'disabled l-disabled-class':'abled l-symbol-class'}}"
3+
catchtap="{{count<=min|| disabled?'doNothing':'onTapChange'}}"
44
data-type="overflow_min"
5+
data-change-type="reduce"
56
hover-class="{{isHover?'count-hover':''}}">
67
<view class="l-icon l-icon-reduce" style="font-size:{{iconSize}};color:{{iconColor}}"></view>
78
</view>
@@ -10,13 +11,14 @@
1011
disabled="{{disabled}}"
1112
type="number"
1213
focus="{{focus}}"
13-
value="{{result}}"
14+
value="{{count}}"
1415
bind:input="onInput"
1516
bindblur="onBlur"/>
16-
<view wx:else class="l-count-class count" mut-bind:tap="onCount">{{result}}</view>
17-
<view class="l-symbol-class symbol {{result>=max|| disabled?'disabled l-disabled-class':'abled l-symbol-class'}}"
18-
catchtap="{{result>=max|| disabled?'doNothing':'addTap'}}"
17+
<view wx:else class="l-count-class count" mut-bind:tap="onCount">{{count}}</view>
18+
<view class="l-symbol-class symbol {{count>=max|| disabled?'disabled l-disabled-class':'abled l-symbol-class'}}"
19+
catchtap="{{count>=max|| disabled?'doNothing':'onTapChange'}}"
1920
data-type="overflow_max"
21+
data-change-type="add"
2022
hover-class="{{isHover?'count-hover':''}}">
2123
<view class="l-icon l-icon-add" style="font-size:{{iconSize}};color:{{iconColor}}"></view>
2224
</view>

0 commit comments

Comments
 (0)