Each now accepts Maps.

This commit is contained in:
JesseBrault0709 2024-06-12 09:24:58 +02:00
parent 8278cd8662
commit 2c43f9331a
3 changed files with 20 additions and 5 deletions

View File

@ -15,8 +15,7 @@
## 0.1.1
- [x] `Switch` and `Case` components
- [x] Fix bug with multiline nested component attributes.
- [ ] `Each` with `Map`
- [ ] `Each` with children in addition to `render`
- [x] `Each` with `Map`
- [ ] `WhenNotEmpty` with `Map`
## 0.1.0

View File

@ -6,7 +6,7 @@ import org.jetbrains.annotations.Nullable
class Each extends DelegatingWebViewComponent {
private final Collection items
private final Object items
private final @Nullable Closure transform
Each(Map attr) {
@ -18,8 +18,16 @@ class Each extends DelegatingWebViewComponent {
protected View getDelegate() {
return { Writer w ->
def cw = new DefaultComponentWriter(w, this.context.renderContext, this.context)
items.forEach {
cw << (transform ? transform(it) : it)
if (items instanceof Collection) {
items.each {
cw << (transform ? transform(it) : it)
}
} else if (items instanceof Map) {
items.each {
cw << (transform ? transform(it) : it)
}
} else {
throw new IllegalArgumentException("The 'items' attribute of Each may only be a Collection or Map.")
}
}
}

View File

@ -17,4 +17,12 @@ class EachTests extends AbstractWebViewComponentTests {
)
}
@Test
void simpleMap() {
this.doTest(
'<Each items={[a: 0, b: 1]} transform={<p>$it.key: $it.value</p>} />',
'<p>a: 0</p><p>b: 1</p>'
)
}
}