Skip to content

Commit

Permalink
Merge #525
Browse files Browse the repository at this point in the history
525: Breaking change: add support for required props r=jstarry a=jstarry

#### Problem
Before this change, properties must implement default which means that even if a prop always gets passed in by value to a component, it still needs a default state in props. No longer!

#### Solution
This breaking change allows annotating prop fields as required by using a procedural derive macro.

#### Notes
- If a prop is required, it does not need to implement `Default`.
- If a prop is optional (not required), it must now implement `Default`.
- If a required prop is not passed into a component, your app will not compile.
- Props with lifetime parameters are not supported
- Props with generics are supported!

#### Breaking changes:
1. Properties must now implement the `Properties` trait by adding `#[derive(Properties)]` to their prop structs
2. Non-required prop fields must implement `Default`
3. Callback props created through the closure syntax will no longer return an `Option` type

Depends on #518

Co-authored-by: Justin Starry <[email protected]>
  • Loading branch information
bors[bot] and jstarry committed Jul 24, 2019
2 parents 37da8b4 + 9781884 commit 4c30319
Show file tree
Hide file tree
Showing 33 changed files with 1,009 additions and 352 deletions.
24 changes: 20 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,27 @@ Components live in an Angular-like scopes with **parent-to-child** *(properties)
Properties are also pure Rust types with strict type-checking during the compilation.

```rust
// my_button.rs

#[derive(Properties, PartialEq)]
pub struct Properties {
pub hidden: bool,
#[props(required)]
pub color: Color,
#[props(required)]
pub onclick: Callback<()>,
}

```

```rust
// confirm_dialog.rs

html! {
<nav class="menu">
<MyButton color=Color::Red />
<MyButton onclick=|_| ParentMsg::DoIt />
</nav>
<div class="confirm-dialog">
<MyButton onclick=|_| DialogMsg::Cancel color=Color::Red hidden=true />
<MyButton onclick=|_| DialogMsg::Submit color=Color::Blue />
</div>
}
```

Expand Down
8 changes: 7 additions & 1 deletion ci/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,15 @@ cargo web test --features web_test --target=wasm32-unknown-emscripten
echo "Testing for wasm32-unknown-unknown..."
cargo test --target=wasm32-unknown-unknown

echo "Testing macro..."
echo "Testing html macro..."
cargo test --test macro_test

echo "Testing derive props macro..."
cargo test --test derive_props_test

echo "Testing macro docs..."
(cd crates/macro && cargo test)

check_example() {
echo "Checking example [$2]"
pushd $2 > /dev/null
Expand Down
3 changes: 3 additions & 0 deletions crates/macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ syn = { version = "^0.15.34", features = ["full"] }

[dev-dependencies]
yew = { path = "../.." }

[build-dependencies]
autocfg = "0.1.3"
7 changes: 7 additions & 0 deletions crates/macro/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
extern crate autocfg;

pub fn main() {
if autocfg::new().probe_rustc_version(1, 36) {
println!("cargo:rustc-cfg=has_maybe_uninit");
}
}
Loading

0 comments on commit 4c30319

Please sign in to comment.