Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binding radio buttons #310

Open
g105b opened this issue Jan 14, 2022 · 2 comments
Open

Binding radio buttons #310

g105b opened this issue Jan 14, 2022 · 2 comments
Assignees
Milestone

Comments

@g105b
Copy link
Member

g105b commented Jan 14, 2022

This is something that seems currently unimplemented. If there is a bunch of radio buttons, and a known value, how to bind the corresponding radio's checked attribute?

@g105b
Copy link
Member Author

g105b commented Jan 17, 2023

A possible solution:

Using the = character to bind a bool, depending on whether the "sex" bind property matches the given value.

<input type="radio" name="sex" value="male" data-bind:checked="sex=male" />
<input type="radio" name="sex" value="female" data-bind:checked="sex=female" />

which could be further simplified to the following (due to the "value" attribute containing the value that we want to match to)

<input type="radio" name="sex" value="male" data-bind:checked="sex=@value" />
<input type="radio" name="sex" value="female" data-bind:checked="sex=@value" />

and I suppose this could be even further simplified to

<input type="radio" name="sex" value="male" data-bind:checked="@name=@value" />
<input type="radio" name="sex" value="female" data-bind:checked="@name=@value" />

but there's something funky about that syntax
$binder->bindData($person);

class Person {
  public function __construct(
    public readonly string $id,
    public readonly string $name,
    public readonly DateTime $dateOfBirth,
    public readonly Sex $sex,
  } {}
}

Enum Sex {
  case male;
  case female;
}

The idea here being that we already have this syntax:

<span id="name" data-bind:text="@id">Your name</span>

The @ character is a "bind key modifier", which expands to the value of the named attribute.

If this makes sense, I'll implement this feature as a "boolean value modifier" and document on the Modifier characters wiki page.

@g105b g105b self-assigned this Jan 24, 2023
@g105b g105b added this to the v4 milestone Feb 20, 2023
@g105b
Copy link
Member Author

g105b commented Jan 8, 2024

Note that this will also be really useful with data-bind:remove usage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant