Switch, Case, and DefaultCase components.

This commit is contained in:
JesseBrault0709 2024-06-12 09:20:46 +02:00
parent 86fa504c7f
commit 8278cd8662
6 changed files with 118 additions and 2 deletions

View File

@ -13,7 +13,7 @@
- [ ] di bug: @Singleton toSelf() causes stack overflow - [ ] di bug: @Singleton toSelf() causes stack overflow
## 0.1.1 ## 0.1.1
- [ ] `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` - [ ] `Each` with `Map`
- [ ] `Each` with children in addition to `render` - [ ] `Each` with children in addition to `render`

View File

@ -10,8 +10,11 @@ class DefaultWebViewComponentScope extends DefaultComponentScope implements WebV
static DefaultWebViewComponentScope getDefaultRootScope() { static DefaultWebViewComponentScope getDefaultRootScope() {
new DefaultWebViewComponentScope().tap { new DefaultWebViewComponentScope().tap {
addWithAttr(Echo) addWithAttr(Case)
addWithAttr(DefaultCase)
addWithAttr(Each) addWithAttr(Each)
addWithAttr(Echo)
addWithAttr(Switch)
addWithAttr(WhenNotEmpty) addWithAttr(WhenNotEmpty)
addWithAttr(WhenNotNull) addWithAttr(WhenNotNull)
} }

View File

@ -0,0 +1,26 @@
package groowt.view.component.web.lib
import groowt.view.View
import groowt.view.component.runtime.DefaultComponentWriter
class Case extends DelegatingWebViewComponent {
private final Object match
Case(Map attr) {
match = attr.match
}
@Override
protected View getDelegate() {
return { Writer w ->
def parent = context.getParent(Switch)
if (!parent.doneYet && match == parent.item) {
parent.doneYet = true
def cw = new DefaultComponentWriter(w, context.renderContext, context)
children.each { cw << it }
}
}
}
}

View File

@ -0,0 +1,20 @@
package groowt.view.component.web.lib
import groowt.view.View
import groowt.view.component.runtime.DefaultComponentWriter
class DefaultCase extends DelegatingWebViewComponent {
@Override
protected View getDelegate() {
return { Writer w ->
def parent = context.getParent(Switch)
if (!parent.doneYet) {
parent.doneYet = true
def cw = new DefaultComponentWriter(w, context.renderContext, context)
children.each { cw << it }
}
}
}
}

View File

@ -0,0 +1,23 @@
package groowt.view.component.web.lib
import groowt.view.View
import groowt.view.component.runtime.DefaultComponentWriter
class Switch extends DelegatingWebViewComponent {
final Object item
boolean doneYet
Switch(Map attr) {
item = attr.item
}
@Override
protected View getDelegate() {
return { Writer w ->
def cw = new DefaultComponentWriter(w, context.renderContext, context)
children.each { cw << it }
}
}
}

View File

@ -0,0 +1,44 @@
package groowt.view.component.web.lib
import org.junit.jupiter.api.Test
class SwitchTests extends AbstractWebViewComponentTests {
@Test
void smokeScreen() {
doTest('<Switch></Switch>', '')
}
@Test
void simpleCase() {
doTest(
'<Switch item="Hello"><Case match={"Hello"}>Hello, World!</Case></Switch>',
'Hello, World!'
)
}
@Test
void multipleCases() {
doTest('''
<Switch item='Mars'>
<Case match='Jupiter'>Hello, Jupiter!</Case>
<Case match='Mars'>Hello, Mars!</Case>
</Switch>
'''.trim(),
'Hello, Mars!'
)
}
@Test
void withDefaultCase() {
doTest('''
<Switch item='Not Mercury'>
<Case match='Mercury'>Hello, Mercury!</Case>
<DefaultCase>Hello, Not Mercury!</DefaultCase>
</Switch>
'''.trim(),
'Hello, Not Mercury!'
)
}
}