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 ## 0.1.1
- [x] `Switch` and `Case` components - [x] `Switch` and `Case` components
- [x] Fix bug with multiline nested component attributes. - [x] Fix bug with multiline nested component attributes.
- [ ] `Each` with `Map` - [x] `Each` with `Map`
- [ ] `Each` with children in addition to `render`
- [ ] `WhenNotEmpty` with `Map` - [ ] `WhenNotEmpty` with `Map`
## 0.1.0 ## 0.1.0

View File

@ -6,7 +6,7 @@ import org.jetbrains.annotations.Nullable
class Each extends DelegatingWebViewComponent { class Each extends DelegatingWebViewComponent {
private final Collection items private final Object items
private final @Nullable Closure transform private final @Nullable Closure transform
Each(Map attr) { Each(Map attr) {
@ -18,8 +18,16 @@ class Each extends DelegatingWebViewComponent {
protected View getDelegate() { protected View getDelegate() {
return { Writer w -> return { Writer w ->
def cw = new DefaultComponentWriter(w, this.context.renderContext, this.context) def cw = new DefaultComponentWriter(w, this.context.renderContext, this.context)
items.forEach { if (items instanceof Collection) {
cw << (transform ? transform(it) : it) 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>'
)
}
} }