Refactor %for macro handling: remove key attribute requirement and streamline list rendering logic
This commit is contained in:
parent
7f818b5324
commit
59e5124c14
12
src/main.ts
12
src/main.ts
|
@ -308,13 +308,8 @@ export default (options: ComponentOptions) => {
|
||||||
ifDirectivesToProcess.push({ element: currentElementNode, expr })
|
ifDirectivesToProcess.push({ element: currentElementNode, expr })
|
||||||
else if (macroName === 'for') {
|
else if (macroName === 'for') {
|
||||||
// detect %key="keyName" attribute
|
// detect %key="keyName" attribute
|
||||||
const keyAttr = currentElementNode.getAttribute('%key')
|
this._setupListRendering(currentElementNode, expr)
|
||||||
if (!keyAttr)
|
} else
|
||||||
return console.error(`%for macro requires %key attribute: %for="${expr}"`)
|
|
||||||
this._setupListRendering(currentElementNode, expr, keyAttr)
|
|
||||||
} else if (macroName === 'key') // Ignore %key macro, as it is handled in %for
|
|
||||||
return
|
|
||||||
else
|
|
||||||
console.warn(`Unknown macro: %${macroName}`)
|
console.warn(`Unknown macro: %${macroName}`)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -384,8 +379,9 @@ export default (options: ComponentOptions) => {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Handle list rendering (%for marco)
|
||||||
// Handle list rendering (%for macro)
|
// Handle list rendering (%for macro)
|
||||||
private _setupListRendering(element: Element, expr: string, keyAttr: string) {
|
private _setupListRendering(element: Element, expr: string) {
|
||||||
// Parse the expression (e.g., "item in items" or "(item, index) in items")
|
// Parse the expression (e.g., "item in items" or "(item, index) in items")
|
||||||
const match = expr.match(/(?:\(([^,]+),\s*([^)]+)\)|([^,\s]+))\s+in\s+(.+)/)
|
const match = expr.match(/(?:\(([^,]+),\s*([^)]+)\)|([^,\s]+))\s+in\s+(.+)/)
|
||||||
if (!match) {
|
if (!match) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user