Skip to content

Commit

Permalink
feat: add default type Option<String> for prop class
Browse files Browse the repository at this point in the history
  • Loading branch information
EqualMa committed Mar 2, 2022
1 parent a39c58c commit 8ad5793
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ folder. You can preview them at [this site](https://frender-rs.github.io/frender

```toml
[dependencies]
frender = "= 1.0.0-alpha.7"
frender = "= 1.0.0-alpha.8"
```

3. Create `index.html` in the project root directory.
Expand Down Expand Up @@ -177,15 +177,28 @@ def_props! {
pub struct MyProps {
// Required prop
name: String,

// Optional prop which defaults to `Default::default()`
age: Option<u8>,
// The following property `age` is optional, and defaults to `None`
age?: Option<u8>,

// The following property `tags` is optional, and defaults to `Vec::default()`
tags?: Vec<String>,

// If the prop type is not specified,
// then frender will infer the type by prop name.
// For example, `class_name` default has type `Option<String>`
// The following property `class_name` is optional, has type Option<String>
class_name?,

// The following property `id` is required, has type Option<String>
id,

// Prop can also have type generics.
// For example, the following is
// the default definition for prop `children`
// the default definition for prop `children`,
// which means it accepts any `Option<TNode>` where TNode implements react::Node,
// and then map the value into `Option<react::Children>` and store it into MyProps.
children<TNode: react::Node>(value: Option<TNode>) -> Option<react::Children> {
value.and_then(react::Node::into_children)
},
Expand Down
2 changes: 1 addition & 1 deletion crates/frender-macros/src/props_to_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ fn infer_field_type_and_builder(
ty,
}
}
"id" | "class_name" => {
"id" | "class_name" | "class" => {
let ty: syn::TypePath = parse_quote_spanned!(span=>
Option<String>);
let ty = syn::Type::Path(ty);
Expand Down

0 comments on commit 8ad5793

Please sign in to comment.