Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rn ref 在 wx:for 场景取值问题 #1889

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 29 additions & 20 deletions packages/core/src/platform/builtInMixins/refsMixin.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,38 @@ export default function getRefsMixin () {
})
},
__getRefVal (type, selectorsConf, refFnId) {
if (!this.__refCache[refFnId]) {
this.__refCache[refFnId] = (instance) => {
selectorsConf.forEach((item = []) => {
const [prefix, selectors = ''] = item
if (selectors) {
selectors.trim().split(/\s+/).forEach(selector => {
const refKey = prefix + selector
const refVal = { type, instance, refFnId }
this.__refs[refKey] = this.__refs[refKey] || []
if (instance) { // mount
this.__refs[refKey].push(refVal)
} else { // unmount
const index = this.__refs[refKey].findIndex(item => item.refFnId === refFnId)
if (index > -1) {
this.__refs[refKey].splice(index, 1)
}
const refFn = (instance) => {
selectorsConf.forEach((item = []) => {
const [prefix, selectors = ''] = item
if (selectors) {
selectors.trim().split(/\s+/).forEach(selector => {
const refKey = prefix + selector
const refVal = {
type,
instance,
uid: refFnId || refFn
}
this.__refs[refKey] = this.__refs[refKey] || []
if (instance) { // mount
this.__refs[refKey].push(refVal)
} else { // unmount
const index = this.__refs[refKey].findIndex(item => item.uid === refFnId || item.uid === refFn)
if (index > -1) {
this.__refs[refKey].splice(index, 1)
}
})
}
})
}
})
}
})
}
if (refFnId) {
if (!this.__refCache[refFnId]) {
this.__refCache[refFnId] = refFn
}
return this.__refCache[refFnId]
} else {
return refFn
}
return this.__refCache[refFnId]
},
__selectRef (selector, refType, all = false) {
const splitedSelector = selector.match(/(#|\.)?[^.#]+/g) || []
Expand Down
5 changes: 3 additions & 2 deletions packages/webpack-plugin/lib/template-compiler/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -1811,7 +1811,8 @@ function processRefReact (el, meta) {
const selectors = []

/**
* selectorsConf: [type, [[prefix, selector], [prefix, selector]]]
* refFnId 在 for 循环当中为空,避免 refFn 在运行时被不同 ref 缓存共用,其他场景有唯一值,用作缓存 key
* selectorsConf: [type, [[prefix, selector], [prefix, selector]], refFnId?]
*/
if (!val) {
const rawId = el.attrsMap.id
Expand All @@ -1834,7 +1835,7 @@ function processRefReact (el, meta) {
const selectorsConf = selectors.map(item => `["${item.prefix}", ${item.selector}]`)
addAttrs(el, [{
name: 'ref',
value: `{{ this.__getRefVal('${type}', [${selectorsConf}], 'ref_fn_${++refId}') }}`
value: `{{ this.__getRefVal('${type}', [${selectorsConf}], ${all ? '' : `ref_fn_${++refId}`}) }}`
}])
}

Expand Down