Skip to content

Setting default value for web component attributes #128

@bshankar

Description

@bshankar

Say I have a component with optional fields:

#[component]
fn input<'a>(name: &'a str, value: Option<&'a str>, error: Option<&'a str>, max_length: u8, ...) -> impl Renderable {
  ...
}

It's tedious to enter values of all fields (rsx syntax) each time I use this component.

<Input name="email" value=None error=None max_length=32 ...>

I would like to propose a way to set default values for each attribute. The syntax could look like this:

#[component]
fn input<'a>(
    name: &'a str,
    #[default] value: Option<&'a str>, 
    #[default] error: Option<&'a str>, 
    #[default(32)] max_length: u8,
   ...
) -> impl Renderable {
  ...
}

#[default] uses the Default trait to get the value. You can also specify a value using #[default(value)].
Then you can just use Input component without repetition. You can only specify the attributes you want to override.

<Input name="email" value=(Some("John"))>

What do you think?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions