Skip to content

Commit baa101f

Browse files
committed
feat: evaluate :group attr on load
1 parent f45eb52 commit baa101f

File tree

4 files changed

+41
-11
lines changed

4 files changed

+41
-11
lines changed

index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1471,7 +1471,7 @@ <h3 class="font-bold font-mono">Multi Select:</h3>
14711471
grid-template-rows: 0fr 1fr;
14721472
}
14731473
</style>
1474-
<div class="accordion" :group>
1474+
<div class="accordion" :group="activeSection = 'about'">
14751475
<section
14761476
class="grid transition-all border-gray-300 border border-b-0 rounded hover:bg-gray-100"
14771477
:class="group.activeSection == 'about' ? 'active' : ''"

lib/entity.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export class Entity {
1414
this.dynamicScripts = dynamicScripts
1515

1616
this.variables = []
17+
this.groupVariables = []
1718
this.id = this.generateEntityUUID()
1819

1920
this.state = {}
@@ -23,10 +24,13 @@ export class Entity {
2324

2425
if (this.base.debug) this.element.dataset.entityId = this.id
2526

26-
this.attributes.evaluateGroup()
27+
this.setAsGroup()
2728
}
2829

2930
setAsGroup() {
31+
if (!this.element.hasAttribute(':group')) return
32+
if (this.isGroup()) return
33+
3034
this.uuid = this.id
3135
this.element.dataset['mini.uuid'] = this.uuid
3236
}
@@ -117,7 +121,8 @@ export class Entity {
117121
return !(variable in this.state)
118122
})
119123

120-
this.variables.push(...identifiers)
124+
if (name === ':group') this.groupVariables.push(...identifiers)
125+
else this.variables.push(...identifiers)
121126
})
122127
}
123128

lib/entity/attributes.js

+32-8
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,15 @@ export class Attributes {
6464
const ids = {
6565
$: 'document-querySelector',
6666
el: `proxyWindow['${this.base.id}${State.DISABLE_RE_RENDER_KEY}']`,
67+
group: this.base.group
68+
? `proxyWindow['${this.base.group.id}${
69+
!options.isGroup ? State.DISABLE_RE_RENDER_KEY : ''
70+
}']`
71+
: undefined,
72+
...(options.ids || {}),
6773
// "this" is set under the interpreter as bind context
6874
}
6975

70-
if (this.base.group)
71-
ids.group = `proxyWindow['${this.base.group.id}${State.DISABLE_RE_RENDER_KEY}']`
72-
7376
engine.replace(ids)
7477

7578
// window variables are used instead of proxy window
@@ -93,7 +96,7 @@ export class Attributes {
9396

9497
async evaluateAttribute(attr) {
9598
if (!Attributes.isValidAttribute(attr, this.base.element)) return
96-
if (attr === ':group') this.evaluateGroup()
99+
if (attr === ':group') await this.evaluateGroup()
97100
else if (attr === ':class') await this.evaluateClass()
98101
else if (attr === ':text') await this.evaluateText()
99102
else if (attr === ':value') await this.evaluateValue()
@@ -131,10 +134,31 @@ export class Attributes {
131134
})
132135
}
133136

134-
evaluateGroup() {
135-
if (!this.base.element.hasAttribute(':group')) return
136-
if (this.base.isGroup()) return
137-
this.base.setAsGroup()
137+
/*
138+
:group is a special attribute that acts as an :load event
139+
when it has a given expr. Unlike other attributes, state updates
140+
inside :group will trigger re-renders.
141+
142+
NOTE: This should NOT be used in this.evaluate() because it will
143+
trigger an infinite loop.
144+
*/
145+
async evaluateGroup() {
146+
if (!this.base.isGroup()) return
147+
148+
const expr = this.base.element.getAttribute(':group')
149+
if (!expr) return
150+
151+
const ids = {}
152+
153+
this.base.groupVariables.forEach((variable) => {
154+
ids[variable] = `proxyWindow['${this.base.id}'].${variable}`
155+
})
156+
157+
try {
158+
await this._interpret(expr, { isGroup: true, ids })
159+
} catch (error) {
160+
this._handleError(':group', expr, error)
161+
}
138162
}
139163

140164
async evaluateClass() {

lib/state.js

+1
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ export class State {
196196

197197
evaluate() {
198198
Array.from(this.entities.values()).forEach((entity) => {
199+
entity.attributes.evaluateAttribute(':group')
199200
entity.attributes.evaluate()
200201
})
201202

0 commit comments

Comments
 (0)