Skip to content

Make @bind-value easier to setup in non-razor syntax #747

@linkdotnet

Description

@linkdotnet

Motivation

Components, especially form-based controls like InputText, InputDate, ... are often used like this:

<InputText @bind-Value="_myparam"></InputText>

For a test we have to setup the following 3 properties to make it work:

  • Value
  • ValueChanged
  • ValueExpression

If a user uses the razor syntax it will work out of the box like this: Render(@<InputText @bind-Value="myProp"></InputText>); but it will not work with "plain old csharp syntax". We have to do the following:

string value = "";
RenderComponent<InputText>(p => p
    .Add(s => s.Value, value)
    .Add(s => s.ValueChanged, s => value = s)
    .Add(s => s.ValueExpression, () => value));

Possible solution

So we will introduce a new helper function which will setup all the 3 calls in one operation:

string value = "";
RenderComponent(p => p.AddTwoWayBinding(value));

There is no selector as we only support Value as valid binding property name (see more under out of scope).

There might be a Value but no ValueExpression and or ValueChanged hence we throw an exception. The new API is really limited to the default use of @bind-Value.

Remarks

With that @bind is included as well as is a simplification of @bind-Value. What is out of scope is to support other events like this:

@bind-value="userName" @bind-value:event="onblur"

Out of scope

Instead of the change event the blur event is taken. As this is anyway the minority of cases the user has to fallback to setting up all of the 3 required properties/methods him or herself (see the motivation in the beginning).

Also more abstract you can have bind-{SomeName} which translates to {SomeName}, {SomeName}Changed and {SomeName} Expression but to be honest I would not address that.

Another point would be "native" HTML controls which support "bind-value" with lowercase v in value.This is anyway something we don't have to support. The user can't just render native HTML elements.

Metadata

Metadata

Assignees

Labels

input neededWhen an issue requires input or suggestionsneeds designMore design is needed before this issue should result in a feature being implemented.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions