Skip to content

Commit 505431a

Browse files
committed
fix(ImagePicker): 修复多个报错问题
1. 修复 value 为 null 导致的组件不显示的问题 2. 修复 maxImageSize 为 0 时导致无法添加图片的问题
1 parent d2fc2ec commit 505431a

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

src/image-picker/index.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,11 @@ Component({
4949
},
5050
sizeType: {
5151
// 该写法经测试有效
52-
type: Array|String,
52+
type: Array | String,
5353
value: ['original', 'compressed']
5454
},
5555
// 所选图片最大限制,单位字节
56+
// 0 为无限制
5657
maxImageSize: {
5758
type: Number,
5859
value: 0,
@@ -69,13 +70,20 @@ Component({
6970
type: Boolean,
7071
value: false
7172
},
73+
// 存放图片 url 的数组
74+
// 放在 properties 中是因为引入了 behaviors: ['wx://form-field'],
75+
// wx://form-field 中的 value 为 null,会引起很多报错
76+
// value 放在 data 中没有 properties 优先级高,覆盖不了
77+
// 所以只能放在此处
78+
value: {
79+
type: Array,
80+
value: []
81+
}
7282
},
7383

7484
data: {
7585
// 根据 size 不同,计算的图片显示大小不同
76-
itemSizePercentage: null,
77-
// 存放图片 url 的数组
78-
value: null
86+
itemSizePercentage: null
7987
},
8088

8189
observers: {
@@ -207,7 +215,7 @@ Component({
207215
* @returns {Promise<void>}
208216
*/
209217
async onTapAdd() {
210-
let {value, count, sizeType} = this.data;
218+
let {value, count, sizeType, maxImageSize} = this.data;
211219
const remainCount = count - value.length;
212220
if (value.length >= count || remainCount <= 0) {
213221
return;
@@ -227,7 +235,7 @@ Component({
227235

228236
chooseImageRes.tempFiles.forEach((tempFile) => {
229237
const {path, size} = tempFile;
230-
if (size > this.data.maxImageSize) {
238+
if (size > maxImageSize && maxImageSize > 0) {
231239
oversizeImageUrlArray.push(path);
232240
} else {
233241
addImageUrlArray.push(path);
@@ -255,7 +263,7 @@ Component({
255263
* 供 Form 组件调用的取值方法
256264
* @returns {*}
257265
*/
258-
getValue() {
266+
getValues() {
259267
return this.data.value;
260268
},
261269

0 commit comments

Comments
 (0)