Skip to content

Correct way to handle boolean attributes #7

@yohcop

Description

@yohcop

Hi,

Is there a way to handle boolean attributes, such as selected in an <option> ?

I've tried different variations, including typed and non typed, but the best I could end up with it <option selected="false"> which counter-intuitively means that the option is in fact selected.

I see the Attribute enum, with Empty/Missing options, but it seems that typed::elements::option wants the selected attribute to be a String, and not a variant of that enum.

Here are a few things I tried:

  • Boolean - is accepted but false is converted to "false", which is then interpreted by the browser as selected.
    <typed::elements::option selected={false}>abc</typed::elements::option>
  • With an Attribute type, doesn't compile: the value is expected to implement Display.
    <typed::elements::option selected={Attribute::<String>::Missing}>xyz</typed::elements::option>
    <option selected={Attribute::<String>::Missing}>xyz</option>
  • In the non-typed version, I also tried with a pair (String, Option) as it is the type of attributes, but this doesn't parse at all either.
    <option {("selected", None)}>xyz</option>

And yet a few more variations of the above.

Is it doable?

What is needed for handling those attributes, is something that would output either <option selected> / <option selected="..."> or just <option> based on some boolean. I can of course have an outer if/else, but it leads to repeated code for other attributes of the option.

Thanks,

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