This PR allows for component parameters to have default values, showcased by the new default example:
use kobold::prelude::*;
#[component(
// Make `name` an optional parameter, default to `"Kobold"`
name?: "Kobold",
// Make `age` an optional parameter, use the `Default` trait
age?,
)]
fn Greeter<'a>(name: &'a str, age: u32) -> impl View + 'a {
let age = (age > 0).then_some(view!(", you are "{ age }" years old"));
view! {
<p> "Hello "{ name }{ age }
}
}
fn main() {
kobold::start(view! {
// Hello Kobold
<Greeter />
// Hello Alice
<Greeter name="Alice" />
// Hello Bob, you are 42 years old
<Greeter name="Bob" age={42} />
});
}
This is a breaking change when it comes to internals (the interaction between #[component] and view! macros), as components now set their fields using methods in a builder pattern. The user facing API remains unaffected, except type hints for component arguments now actually show useful information:
Rust compiler seems to handle this reasonably well as a zero-cost abstraction and doesn't increase the Wasm binary size (at least not in the TodoMVC example). With some fiddling we could make all the methods const if need be.
TODOs:
[x] Add short example in README / main docs.
[x] Document the feature on the #[component] macro.
[x] Document the Undefined type and the Maybe trait.
[x] Update kobold_qr to allow the size of the <canvas> to be set.
This PR allows for component parameters to have default values, showcased by the new
default
example:This is a breaking change when it comes to internals (the interaction between
#[component]
andview!
macros), as components now set their fields using methods in a builder pattern. The user facing API remains unaffected, except type hints for component arguments now actually show useful information:Rust compiler seems to handle this reasonably well as a zero-cost abstraction and doesn't increase the Wasm binary size (at least not in the TodoMVC example). With some fiddling we could make all the methods
const
if need be.TODOs:
#[component]
macro.Undefined
type and theMaybe
trait.kobold_qr
to allow the size of the<canvas>
to be set.