Refactor %for macro handling: improve list rendering logic and key expression evaluation
This commit is contained in:
parent
89bb2ea27d
commit
42aa5a1d75
16
src/main.ts
16
src/main.ts
|
@ -381,7 +381,7 @@ export default (options: ComponentOptions) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle list rendering (%for macro)
|
// Handle list rendering (%for macro)
|
||||||
private _setupListRendering(element: Element, expr: 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) {
|
||||||
|
@ -497,10 +497,10 @@ private _setupListRendering(element: Element, expr: string) {
|
||||||
this._statesListeners[collectionExpr] = () => {
|
this._statesListeners[collectionExpr] = () => {
|
||||||
updateList()
|
updateList()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper method to evaluate key expressions for list items
|
// Helper method to evaluate key expressions for list items
|
||||||
private _evaluateKeyExpression(keyExpr: string, itemData: any, index: number, itemVar: string): any {
|
private _evaluateKeyExpression(keyExpr: string, itemData: any, index: number, itemVar: string): any {
|
||||||
try {
|
try {
|
||||||
// If keyExpr is directly the item property, return it
|
// If keyExpr is directly the item property, return it
|
||||||
if (keyExpr === itemVar) {
|
if (keyExpr === itemVar) {
|
||||||
|
@ -530,10 +530,10 @@ private _evaluateKeyExpression(keyExpr: string, itemData: any, index: number, it
|
||||||
console.error(`Error evaluating key expression: ${keyExpr}`, error)
|
console.error(`Error evaluating key expression: ${keyExpr}`, error)
|
||||||
return index // Fallback to index as key
|
return index // Fallback to index as key
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper method to apply item context to elements
|
// Helper method to apply item context to elements
|
||||||
private _applyItemContext(element: Element, itemContext: Record<string, any>) {
|
private _applyItemContext(element: Element, itemContext: Record<string, any>) {
|
||||||
// Store the item context on the element
|
// Store the item context on the element
|
||||||
(element as any)._itemContext = itemContext
|
(element as any)._itemContext = itemContext
|
||||||
|
|
||||||
|
@ -587,7 +587,7 @@ private _applyItemContext(element: Element, itemContext: Record<string, any>) {
|
||||||
// Also handle event handlers and other bindings if needed
|
// Also handle event handlers and other bindings if needed
|
||||||
// This is more complex and would require extending other methods
|
// This is more complex and would require extending other methods
|
||||||
// to be aware of the item context
|
// to be aware of the item context
|
||||||
}
|
}
|
||||||
|
|
||||||
private _evaluateIfCondition(element: Element, condition: string) {
|
private _evaluateIfCondition(element: Element, condition: string) {
|
||||||
const info = this._conditionalElements.get(element)
|
const info = this._conditionalElements.get(element)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user