Skip to content

Commit 055f20a

Browse files
committed
fix(alita wx-react wx-react-native): 修复 wxs过滤数组引起的bug
1 parent 7e34f11 commit 055f20a

File tree

8 files changed

+38
-33
lines changed

8 files changed

+38
-33
lines changed

mptemp/commonwxs.wxs

-13
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,12 @@ var lite = function(v) {
4040
return v.constructor === 'Number' || v.constructor === 'String'
4141
}
4242

43-
var isArray = function(v) {
44-
return v.constructor === 'Array'
45-
}
46-
47-
var compact = function(v) {
48-
return v.filter(function(item) {
49-
return item !== null
50-
})
51-
}
5243

5344
module.exports = {
5445
getFinalStyle: getFinalStyle,
5546
lite: lite,
56-
isArray: isArray,
57-
compact: compact,
5847

5948

6049
s: getFinalStyle,
6150
l: lite,
62-
a: isArray,
63-
c: compact
6451
};

packages/wx-react-native/miniprogram_dist/component/WXFlatList/index.wxml

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161

6262
<block wx:if="{{numColumns >= 2}}">
6363
<view style="display: flex; flex-direction: row; justify-content: flex-start; flex-wrap: wrap">
64-
<view wx:for="{{tools.compact(renderItemData)}}" wx:key="key" style="width: {{100 / numColumns}}%">
64+
<view wx:for="{{renderItemData}}" wx:key="key" style="width: {{100 / numColumns}}%">
6565
<renderItemCPT id="id_{{index}}"
6666
diuu="{{item.renderItemDIUU}}"
6767
R="{{item.renderItemDIUUR}}"
@@ -70,7 +70,7 @@
7070
</view>
7171
</block>
7272
<block wx:else>
73-
<block wx:for="{{tools.compact(renderItemData)}}" wx:key="key">
73+
<block wx:for="{{renderItemData}}" wx:key="key">
7474
<view wx:if="{{methods.ArrayContains(stickyHeaderIndices,index)}}" style="{{sti['stickyContainerStyle'+index]}}">
7575
<renderItemCPT id="id_{{index}}"
7676
diuu="{{item.renderItemDIUU}}"

packages/wx-react-native/miniprogram_dist/component/WXPicker/index.wxml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
bindchange="onValueChange">
99
<picker-view-column>
1010
<block
11-
wx:for="{{tools.compact(childrenData)}}"
11+
wx:for="{{childrenData}}"
1212
wx:key="key"
1313
>
1414
<childrenCPT

packages/wx-react-native/miniprogram_dist/component/WXSectionList/index.wxml

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@
3434
style="{{tools.getFinalStyle(ListHeaderComponentDIUUstyle)}}"
3535
/>
3636

37-
<block wx:for="{{tools.compact(sectionsData)}}" wx:key="key">
37+
<block wx:for="{{sectionsData}}" wx:key="key">
3838
<renderSectionHeaderCPT
3939
wx:if="{{item.renderSectionHeaderDIUU}}"
4040
diuu="{{item.renderSectionHeaderDIUU}}"
4141
R="{{item.renderSectionHeaderDIUUR}}"
4242
style="{{tools.getFinalStyle(item.renderSectionHeaderDIUUstyle)}}"
4343
/>
4444

45-
<block wx:for="{{tools.compact(item.renderItemData)}}" wx:key="key">
45+
<block wx:for="{{item.renderItemData}}" wx:key="key">
4646
<renderItemCPT
4747
diuu="{{item.renderItemDIUU}}"
4848
R="{{item.renderItemDIUUR}}"

packages/wx-react/miniprogram_dist/getChangePath.js

+21-12
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
*
77
*/
88

9+
function hasMoreKeys(okeys, nkeys) {
10+
const nkeysSet = new Set(nkeys)
11+
12+
return okeys.some(key => !nkeysSet.has(key))
13+
}
14+
915
function getObjectPathInner(v, prefix, result) {
1016
const tv = typeof v
1117

@@ -54,17 +60,17 @@ function getChangePathInner(newR, oldR, prefix, result) {
5460
) {
5561
getObjectPathInner(newR, prefix, result)
5662
} else if (Array.isArray(newR)) {
63+
// 由于小程序 setData 设置为 undefined 会出问题。 所以这种情况直接设置对象
64+
if (newR.length < oldR.length) {
65+
result[prefix] = newR
66+
return
67+
}
68+
5769
for (let i = 0; i < newR.length; i++) {
5870
const v = newR[i]
5971
const ov = oldR[i]
6072
getChangePathInner(v, ov, `${prefix}[${i}]`, result)
6173
}
62-
63-
if (newR.length < oldR.length) {
64-
for (let i = newR.length; i < oldR.length; i ++) {
65-
result[`${prefix}[${i}]`] = null
66-
}
67-
}
6874
} else if (tn === 'object' && tn !== null) {
6975
if (newR.__isAnimation__) {
7076
result[prefix] = newR
@@ -73,18 +79,21 @@ function getChangePathInner(newR, oldR, prefix, result) {
7379

7480

7581
const nkeys = Object.keys(newR)
82+
const okeys = Object.keys(oldR)
83+
84+
// 由于小程序 setData 设置为 undefined 会出问题。 所以这种情况直接设置对象
85+
// TODO 这种情况下, 是否依然可以减少数据的传递呢??
86+
if (hasMoreKeys(okeys, nkeys)) {
87+
result[prefix] = newR
88+
return
89+
}
90+
7691
for (let i = 0; i < nkeys.length; i++) {
7792
const k = nkeys[i]
7893
const v = newR[k]
7994
const ov = oldR[k]
8095
getChangePathInner(v, ov, `${prefix}.${k}`, result)
8196
}
82-
83-
const okeys = Object.keys(oldR)
84-
const onlyNkeys = okeys.filter(k => newR[k] === undefined)
85-
for(let i = 0; i < onlyNkeys.length; i ++) {
86-
result[`${prefix}.${onlyNkeys[i]}`] = null
87-
}
8897
} else {
8998
result[prefix] = newR
9099
}

src/tran/childrenToTemplate.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export default function childrenToTemplate(ast, info) {
117117
[
118118
t.jsxAttribute(t.jsxIdentifier('datakey'), t.stringLiteral(datakey)),
119119
t.jsxAttribute(t.jsxIdentifier('tempVnode'), ele),
120-
t.jsxAttribute(t.jsxIdentifier('wx:if'), t.stringLiteral(`{{${datakey}}}`)),
120+
t.jsxAttribute(t.jsxIdentifier('wx:if'), t.stringLiteral(`{{${datakey}}} !== undefined`)),
121121
t.jsxAttribute(t.jsxIdentifier('is'), t.stringLiteral(tempName)),
122122
t.jsxAttribute(t.jsxIdentifier('data'), t.stringLiteral(`{{d: ${datakey}}}`))
123123
]

src/tran/geneWxml.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,16 @@ export default function(info) {
6666
const name = childTemplates[i];
6767
// 如果只使用一个child 小程序会报递归, 然后就不渲染了
6868
const subT = `
69-
<template name="${name}"><block wx:if="{{t.a(d)}}"><block wx:for="{{t.c(d)}}" wx:key="key"><block wx:if="{{t.l(item)}}">{{item}}</block><template wx:else is="{{item.tempName}}" data="{{...item}}"></template></block></block><template wx:elif="{{d.tempName}}" is="{{d.tempName}}" data="{{...d}}"></template></template>
69+
<template name="${name}">
70+
<block wx:if="{{t.l(d)}}">{{d}}</block>
71+
<template wx:elif="{{d.tempName}}" is="{{d.tempName}}" data="{{...d}}"/>
72+
<block wx:else>
73+
<block wx:for="{{d}}" wx:key="key">
74+
<block wx:if="{{t.l(item)}}">{{item}}</block>
75+
<template wx:else is="{{item.tempName}}" data="{{...item}}"/>
76+
</block>
77+
</block>
78+
</template>
7079
`;
7180

7281
templateWxml = subT + templateWxml;

src/tran/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export default function (ast, filepath, isFuncComp, entryFilePath, isPageComp) {
6666

6767
geneReactJS(ast, info)
6868

69-
ast = literalTemplate(ast, info)
69+
//ast = literalTemplate(ast, info)
7070

7171
ast = addEventHandler(ast, info)
7272

0 commit comments

Comments
 (0)