修复没有动态绑定时, ref不生效的bug

pull/1/head 1.5.4
yutent 2023-03-20 11:29:19 +08:00
parent 58d4277964
commit 0eecaa22dc
2 changed files with 42 additions and 36 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@bd/core", "name": "@bd/core",
"version": "1.5.3", "version": "1.5.4",
"type": "module", "type": "module",
"description": "百搭UI组件库的核心", "description": "百搭UI组件库的核心",
"main": "dist/index.js", "main": "dist/index.js",

View File

@ -301,42 +301,51 @@ class TemplateInstance {
let nodeIndex = 0 let nodeIndex = 0
let partIndex = 0 let partIndex = 0
let templatePart = parts[0] let templatePart = parts[0]
// 没有动态绑定时, 查一遍是否有ref属性
while (templatePart !== void 0) { if (templatePart === void 0) {
if (nodeIndex === templatePart.index) { do {
let part
if (templatePart.type === CHILD_PART) {
part = new ChildPart(node, node.nextSibling, this, options)
} else if (templatePart.type === ATTRIBUTE_PART) {
part = new templatePart.ctor(
node,
templatePart.name,
templatePart.strings,
this,
options
)
} else if (templatePart.type === ELEMENT_PART) {
part = new ElementPart(node, this, options)
}
// console.log(node)
if (node.nodeType === 1 && node.getAttribute('ref')) { if (node.nodeType === 1 && node.getAttribute('ref')) {
if (options.host.$refs) { options.host.$refs[node.getAttribute('ref')] = node
options.host.$refs[node.getAttribute('ref')] = node node.removeAttribute('ref')
} }
} while ((node = walker.nextNode()) !== null)
} else {
while (templatePart !== void 0) {
if (nodeIndex === templatePart.index) {
let part
if (templatePart.type === CHILD_PART) {
part = new ChildPart(node, node.nextSibling, this, options)
} else if (templatePart.type === ATTRIBUTE_PART) {
part = new templatePart.ctor(
node,
templatePart.name,
templatePart.strings,
this,
options
)
} else if (templatePart.type === ELEMENT_PART) {
part = new ElementPart(node, this, options)
}
if (node.nodeType === 1 && node.getAttribute('ref')) {
if (options.host.$refs) {
options.host.$refs[node.getAttribute('ref')] = node
}
}
this._parts.push(part)
templatePart = parts[++partIndex]
}
if (
nodeIndex !==
(templatePart === null || templatePart === void 0
? void 0
: templatePart.index)
) {
node = walker.nextNode()
nodeIndex++
} }
this._parts.push(part)
templatePart = parts[++partIndex]
}
if (
nodeIndex !==
(templatePart === null || templatePart === void 0
? void 0
: templatePart.index)
) {
node = walker.nextNode()
nodeIndex++
} }
} }
return fragment return fragment
} }
_update(values) { _update(values) {
@ -410,7 +419,6 @@ class ChildPart {
_commitNode(value) { _commitNode(value) {
if (this._$committedValue !== value) { if (this._$committedValue !== value) {
this.#clear() this.#clear()
this._$committedValue = this._insert(value) this._$committedValue = this._insert(value)
} }
} }
@ -669,8 +677,6 @@ export function render(value, container, options = {}) {
} }
part._$setValue(value) part._$setValue(value)
// console.log(container, options.host, options.host.$refs)
return part return part
} }