Skip to content

Commit 567b4ca

Browse files
committedOct 29, 2019
feat(wx-react): 提供 reactCompHelper方法,方便手动对齐组件
1 parent 8b3b496 commit 567b4ca

File tree

5 files changed

+77
-41
lines changed

5 files changed

+77
-41
lines changed
 

‎packages/wx-react/miniprogram_dist/index.js

+36-19
Original file line numberDiff line numberDiff line change
@@ -199,21 +199,6 @@ function getPropsMethod(wxInst, key) {
199199
}
200200
}
201201

202-
/**
203-
* onX
204-
* @param name
205-
* @returns {boolean}
206-
*/
207-
function isEventProp(name) {
208-
if (!name || name.length <= 3) return false;
209-
const trCode = name.charCodeAt(2);
210-
return name.charCodeAt(0) === 111
211-
&& name.charCodeAt(1) === 110
212-
&& trCode >= 65
213-
&& trCode <= 90;
214-
}
215-
216-
217202
// 外层占位View, 作用是撑满小程序自定义组件生成的View
218203
const DEFAULTCONTAINERSTYLE = '_5_';
219204

@@ -332,6 +317,37 @@ function cleanPageComp(pageComp) {
332317
invokeWillUnmount(allChildren);
333318
}
334319

320+
function reactCompHelper(obj) {
321+
obj.properties = {
322+
...obj.properties,
323+
diuu: null,
324+
};
325+
326+
const rawAttached = obj.attached;
327+
obj.attached = function () {
328+
const rawData = this.data;
329+
Object.defineProperty(this, 'data', {
330+
get: function () {
331+
const compInst = instanceManager.getCompInstByUUID(rawData.diuu);
332+
return {
333+
...rawData,
334+
...compInst.props
335+
}
336+
},
337+
});
338+
rawAttached && rawAttached.call(this);
339+
instanceManager.setWxCompInst(this.data.diuu, this);
340+
};
341+
342+
const rawDetached = obj.detached;
343+
obj.detached = function () {
344+
rawDetached && rawDetached.call(this);
345+
instanceManager.removeUUID(this.data.diuu);
346+
};
347+
348+
return obj
349+
}
350+
335351
/**
336352
* Copyright (c) Areslabs.
337353
*
@@ -1591,7 +1607,7 @@ function updateRNBaseComponent(vnode, parentInst, parentContext, data, oldData,
15911607
} else {
15921608
data[`${diuuKey}onRefreshPassed`] = false;
15931609
}
1594-
} else if (isEventProp(k)) {
1610+
} else if (typeof v === 'function') {
15951611
inst.props[k] = reactEnvWrapper(v);
15961612
} else {
15971613
//避免小程序因为setData undefined报错
@@ -1862,7 +1878,7 @@ function updateBaseView(vnode, parentInst, parentContext, data, oldData, dataPat
18621878

18631879
if (k === 'src') {
18641880
data[`${vnodeDiuu}${k}`] = v.uri || v;
1865-
} else if (isEventProp(k)) {
1881+
} else if (typeof v === 'function') {
18661882
eventProps.push(k);
18671883
} else if (k === 'mode') {
18681884
data[`${vnodeDiuu}${k}`] = resizeMode(v);
@@ -2568,10 +2584,11 @@ var index = {
25682584
instanceManager,
25692585
getPropsMethod,
25702586
unstable_batchedUpdates,
2571-
renderApp
2587+
renderApp,
2588+
reactCompHelper
25722589
};
25732590
const h = createElement;
25742591
const render$1 = deprecated;
25752592

25762593
export default index;
2577-
export { Component, FuncComponent, HocComponent, PureComponent, RNBaseComponent, WxNormalComp, createElement, flattenStyle, getPropsMethod, h, instanceManager, parseElement, render$1 as render, renderApp, renderPage, styleType, tackleWithStyleObj, unstable_batchedUpdates };
2594+
export { Component, FuncComponent, HocComponent, PureComponent, RNBaseComponent, WxNormalComp, createElement, flattenStyle, getPropsMethod, h, instanceManager, parseElement, reactCompHelper, render$1 as render, renderApp, renderPage, styleType, tackleWithStyleObj, unstable_batchedUpdates };

‎packages/wx-react/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@areslabs/wx-react",
3-
"version": "1.0.30",
3+
"version": "1.0.31",
44
"description": "微信版本的React",
55
"files": [
66
"miniprogram_dist",

‎packages/wx-react/src/index.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import WxNormalComp from './WxNormalComp'
1212
import tackleWithStyleObj, {parseElement, flattenStyle} from './tackleWithStyleObj'
1313
import styleType from './styleType'
1414
import instanceManager from './InstanceManager'
15-
import {getPropsMethod} from './util'
15+
import {getPropsMethod, reactCompHelper} from './util'
1616
import {unstable_batchedUpdates, renderPage, renderApp} from './UpdateStrategy'
1717

1818

@@ -40,7 +40,8 @@ export default {
4040
instanceManager,
4141
getPropsMethod,
4242
unstable_batchedUpdates,
43-
renderApp
43+
renderApp,
44+
reactCompHelper
4445
}
4546

4647
export {
@@ -59,7 +60,8 @@ export {
5960
instanceManager,
6061
getPropsMethod,
6162
unstable_batchedUpdates,
62-
renderApp
63+
renderApp,
64+
reactCompHelper
6365
}
6466
export const h = createElement
6567
export const render = deprecated

‎packages/wx-react/src/render.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import instanceManager from './InstanceManager'
1010
import geneUUID from './geneUUID'
1111
import tackleWithStyleObj from './tackleWithStyleObj'
12-
import { DEFAULTCONTAINERSTYLE, filterContext, getCurrentContext, isEventProp, ReactWxEventMap, getRealOc} from './util'
12+
import { DEFAULTCONTAINERSTYLE, filterContext, getCurrentContext, ReactWxEventMap, getRealOc} from './util'
1313
import { CPTComponent, FuncComponent, RNBaseComponent, HocComponent } from './AllComponent'
1414

1515
import {UPDATE_EFFECT, INIT_EFFECT, UpdateState, ForceUpdate, mpRoot} from './constants'
@@ -530,7 +530,7 @@ function updateRNBaseComponent(vnode, parentInst, parentContext, data, oldData,
530530
} else {
531531
data[`${diuuKey}onRefreshPassed`] = false
532532
}
533-
} else if (isEventProp(k)) {
533+
} else if (typeof v === 'function') {
534534
inst.props[k] = reactEnvWrapper(v)
535535
} else {
536536
//避免小程序因为setData undefined报错
@@ -801,7 +801,7 @@ function updateBaseView(vnode, parentInst, parentContext, data, oldData, dataPat
801801

802802
if (k === 'src') {
803803
data[`${vnodeDiuu}${k}`] = v.uri || v
804-
} else if (isEventProp(k)) {
804+
} else if (typeof v === 'function') {
805805
eventProps.push(k)
806806
} else if (k === 'mode') {
807807
data[`${vnodeDiuu}${k}`] = resizeMode(v)

‎packages/wx-react/src/util.js

+32-15
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,6 @@ export function getPropsMethod(wxInst, key) {
1717
}
1818
}
1919

20-
/**
21-
* onX
22-
* @param name
23-
* @returns {boolean}
24-
*/
25-
export function isEventProp(name) {
26-
if (!name || name.length <= 3) return false;
27-
const trCode = name.charCodeAt(2);
28-
return name.charCodeAt(0) === 111
29-
&& name.charCodeAt(1) === 110
30-
&& trCode >= 65
31-
&& trCode <= 90;
32-
}
33-
34-
3520
// 外层占位View, 作用是撑满小程序自定义组件生成的View
3621
export const DEFAULTCONTAINERSTYLE = '_5_'
3722

@@ -150,3 +135,35 @@ export function cleanPageComp(pageComp) {
150135
invokeWillUnmount(allChildren)
151136
}
152137

138+
export function reactCompHelper(obj) {
139+
obj.properties = {
140+
...obj.properties,
141+
diuu: null,
142+
}
143+
144+
const rawAttached = obj.attached
145+
obj.attached = function () {
146+
const rawData = this.data
147+
Object.defineProperty(this, 'data', {
148+
get: function () {
149+
const compInst = instanceManager.getCompInstByUUID(rawData.diuu);
150+
return {
151+
...rawData,
152+
...compInst.props
153+
}
154+
},
155+
})
156+
rawAttached && rawAttached.call(this)
157+
instanceManager.setWxCompInst(this.data.diuu, this)
158+
}
159+
160+
const rawDetached = obj.detached
161+
obj.detached = function () {
162+
rawDetached && rawDetached.call(this)
163+
instanceManager.removeUUID(this.data.diuu)
164+
}
165+
166+
return obj
167+
}
168+
169+

0 commit comments

Comments
 (0)