Switch, Case, and DefaultCase components.
This commit is contained in:
parent
86fa504c7f
commit
8278cd8662
2
TODO.md
2
TODO.md
@ -13,7 +13,7 @@
|
||||
- [ ] di bug: @Singleton toSelf() causes stack overflow
|
||||
|
||||
## 0.1.1
|
||||
- [ ] `Switch` and `Case` components
|
||||
- [x] `Switch` and `Case` components
|
||||
- [x] Fix bug with multiline nested component attributes.
|
||||
- [ ] `Each` with `Map`
|
||||
- [ ] `Each` with children in addition to `render`
|
||||
|
@ -10,8 +10,11 @@ class DefaultWebViewComponentScope extends DefaultComponentScope implements WebV
|
||||
|
||||
static DefaultWebViewComponentScope getDefaultRootScope() {
|
||||
new DefaultWebViewComponentScope().tap {
|
||||
addWithAttr(Echo)
|
||||
addWithAttr(Case)
|
||||
addWithAttr(DefaultCase)
|
||||
addWithAttr(Each)
|
||||
addWithAttr(Echo)
|
||||
addWithAttr(Switch)
|
||||
addWithAttr(WhenNotEmpty)
|
||||
addWithAttr(WhenNotNull)
|
||||
}
|
||||
|
@ -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 }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -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 }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -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 }
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -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!'
|
||||
)
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user