This repository has been archived by the owner on Aug 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changed Component class into ElementCreator typealias (#397)
Co-authored-by: Ian Clarke <[email protected]>
- Loading branch information
1 parent
99e9269
commit eb6dc0d
Showing
3 changed files
with
88 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,135 +1,122 @@ | ||
package kweb.docs | ||
|
||
import kweb.ElementCreator | ||
import kweb.* | ||
import kweb.InputType.text | ||
import kweb.docs.BulmaInput.BulmaColor | ||
import kweb.docs.BulmaInput.BulmaColor.SUCCESS | ||
import kweb.docs.BulmaInput.BulmaColor.WARNING | ||
import kweb.state.* | ||
import kweb.util.json | ||
|
||
// ANCHOR: simple_component | ||
class SimpleComponent( | ||
val prompt: String = "Enter Your Name", | ||
val name: KVar<String> | ||
) : Component { | ||
override fun render(elementCreator: ElementCreator<Element>) { | ||
with(elementCreator) { | ||
div { | ||
h1().text(prompt) | ||
input(type = text).value = name | ||
} | ||
div { | ||
span().text(name.map { "Hello, $it" }) | ||
} | ||
} | ||
fun Component.simple( | ||
prompt: String = "Enter Your Name", | ||
name: KVar<String> | ||
) { | ||
div { | ||
h1().text(prompt) | ||
input(type = text).value = name | ||
} | ||
div { | ||
span().text(name.map { "Hello, $it" }) | ||
} | ||
} | ||
// ANCHOR_END: simple_component | ||
|
||
fun simple_component() { | ||
// ANCHOR: component_usage | ||
Kweb(port = 16097) { | ||
doc.body { | ||
render(SimpleComponent(name = kvar("World"))) | ||
Kweb(port = 16097) { | ||
doc.body { | ||
simple(name = kvar("World")) | ||
} | ||
} | ||
} | ||
// ANCHOR_END: component_usage | ||
} | ||
|
||
|
||
// ANCHOR: bulma_component_example | ||
class BulmaInput( | ||
val type : InputType, | ||
val color: KVal<BulmaColor>? = null, | ||
val size: KVal<BulmaSize>? = null, | ||
val style: KVal<BulmaStyle>? = null, | ||
val state: KVal<BulmaState>? = null, | ||
val disabled: KVal<Boolean>? = null, | ||
val value: KVar<String> | ||
) : Component { | ||
|
||
override fun render(elementCreator: ElementCreator<*>) { | ||
with(elementCreator) { | ||
input(type = type) { inputElement -> | ||
var inputClassList: KVal<List<String>> = kval(listOf("input")) | ||
with(inputElement) { | ||
|
||
if (color != null) { | ||
inputClassList += color.map { listOf(it.cssClassName) } | ||
} | ||
if (size != null) { | ||
inputClassList += size.map { listOf(it.cssClassName) } | ||
} | ||
if (this@BulmaInput.style != null) { | ||
inputClassList += this@BulmaInput.style.map { listOf(it.cssClassName) } | ||
} | ||
if (state != null) { | ||
inputClassList += state.map { listOf(it.cssClassName) } | ||
} | ||
|
||
if (disabled != null) { | ||
this["disabled"] = disabled.map { it.json } | ||
} | ||
|
||
classes(inputClassList.map { it.joinToString(" ") }) | ||
|
||
this.value = this@BulmaInput.value | ||
|
||
} | ||
enum class BulmaColor(val cssClassName: String) { | ||
PRIMARY("is-primary"), | ||
LINK("is-link"), | ||
INFO("is-info"), | ||
SUCCESS("is-success"), | ||
WARNING("is-warning"), | ||
DANGER("is-danger") | ||
} | ||
|
||
enum class BulmaSize(val cssClassName: String) { | ||
SMALL("is-small"), | ||
NORMAL("is-normal"), | ||
MEDIUM("is-medium"), | ||
LARGE("is-large") | ||
} | ||
|
||
enum class BulmaStyle(val cssClassName: String) { | ||
ROUNDED("is-rounded"), | ||
FOCUSED("is-focused") | ||
} | ||
|
||
enum class BulmaState(val cssClassName: String) { | ||
NORMAL("is-normal"), | ||
HOVER("is-hovered"), | ||
FOCUS("is-focused"), | ||
LOADING("is-loading"), | ||
} | ||
|
||
fun Component.bulmaInput( | ||
type: InputType, | ||
color: KVal<BulmaColor>? = null, | ||
size: KVal<BulmaSize>? = null, | ||
style: KVal<BulmaStyle>? = null, | ||
state: KVal<BulmaState>? = null, | ||
disabled: KVal<Boolean>? = null, | ||
value: KVar<String> | ||
) { | ||
input(type = type) { inputElement -> | ||
var inputClassList: KVal<List<String>> = kval(listOf("input")) | ||
with(inputElement) { | ||
|
||
if (color != null) { | ||
inputClassList += color.map { listOf(it.cssClassName) } | ||
} | ||
if (size != null) { | ||
inputClassList += size.map { listOf(it.cssClassName) } | ||
} | ||
if (style != null) { | ||
inputClassList += style.map { listOf(it.cssClassName) } | ||
} | ||
if (state != null) { | ||
inputClassList += state.map { listOf(it.cssClassName) } | ||
} | ||
} | ||
} | ||
|
||
enum class BulmaColor(val cssClassName: String) { | ||
PRIMARY("is-primary"), | ||
LINK("is-link"), | ||
INFO("is-info"), | ||
SUCCESS("is-success"), | ||
WARNING("is-warning"), | ||
DANGER("is-danger") | ||
} | ||
if (disabled != null) { | ||
this["disabled"] = disabled.map { it.json } | ||
} | ||
|
||
enum class BulmaSize(val cssClassName: String) { | ||
SMALL("is-small"), | ||
NORMAL("is-normal"), | ||
MEDIUM("is-medium"), | ||
LARGE("is-large") | ||
} | ||
classes(inputClassList.map { it.joinToString(" ") }) | ||
|
||
enum class BulmaStyle(val cssClassName: String) { | ||
ROUNDED("is-rounded"), | ||
FOCUSED("is-focused") | ||
} | ||
this.value = value | ||
|
||
enum class BulmaState(val cssClassName: String) { | ||
NORMAL("is-normal"), | ||
HOVER("is-hovered"), | ||
FOCUS("is-focused"), | ||
LOADING("is-loading"), | ||
} | ||
} | ||
} | ||
// ANCHOR_END: bulma_component_example | ||
|
||
fun bulmaComponentUsageExample() { | ||
// ANCHOR: bulma_component_usage | ||
Kweb(port = 12354) { | ||
doc.head { | ||
element("link", | ||
attributes = mapOf( | ||
"rel" to "stylesheet".json, | ||
"href" to "https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.1/css/bulma.min.css".json | ||
Kweb(port = 12354) { | ||
doc.head { | ||
element( | ||
"link", | ||
attributes = mapOf( | ||
"rel" to "stylesheet".json, | ||
"href" to "https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.1/css/bulma.min.css".json | ||
) | ||
) | ||
) | ||
} | ||
} | ||
|
||
doc.body { | ||
val username = kvar("") | ||
val color = username.map { if (it.length < 5) { WARNING } else { SUCCESS } | ||
doc.body { | ||
val username = kvar("") | ||
val color = username.map { if (it.length < 5) BulmaColor.WARNING else BulmaColor.SUCCESS } | ||
bulmaInput(type = text, value = username, color = color) | ||
} | ||
render(BulmaInput(type = text, value = username, color = color)) | ||
} | ||
} | ||
// ANCHOR_END: bulma_component_usage | ||
} |