Blazor запускает пользовательское событие, когда дочерний компонент меняет значения родительского компонента - PullRequest
0 голосов
/ 08 марта 2020

У меня есть родительский и дочерний компоненты:

Родительский компонент

<IncrementButton Increment="-1" @bind-Attribute="Amount" @onclick="@(() => UpdateBottomPillText())" />
<span>@Amount</span>
<IncrementButton Increment="1" @bind-Attribute="Amount" @onclick="@(() => UpdateBottomPillText())" />
<span>@bottomPillAmount</span>

@code {
    [Parameter] public int Amount { get; set; }
    [Parameter] public EventCallback<int> AmountChanged { get; set; }

    string bottomPillAmount;

    string UpdateBottomPillText()
    {
        double amountAdded = Math.Floor(((double)(Amount - 10.0)) / 2.0);

        bottomPillAmount = amountAdded > 0 ? "+" + amountAdded.ToString() : amountAdded.ToString();

        return Amount.ToString();
    }

    protected override void OnParametersSet()
    {
        UpdateBottomPillText();
    }

}

Дочерний компонент (IncrementButton)

<button type="button" @onclick="OnAttributeChanged">@displayText</button>

@code {
    [Parameter]
    public int Increment { get; set; } = 1;

    [Parameter]
    public int Attribute { get; set; }

    [Parameter]
    public EventCallback<int> AttributeChanged { get; set; }

    [Parameter]
    public EventCallback<MouseEventArgs> OnClick { get; set; }

    string displayText;

    protected override void OnParametersSet()
    {
        displayText = Increment > 0 ? "+" + Increment.ToString() : Increment.ToString();
    }

    private Task OnAttributeChanged()
    {
        Attribute += Increment;
        await OnClick.InvokeAsync(new MouseEventArgs());
        return AttributeChanged.InvokeAsync(Attribute);
    }
}

@bind-Attribute="Amount" работает и обновляет значение при нажатии IncrementButton, но я хотел бы запустить UpdateBottomPillText(), а также обновить значение. Прямо сейчас я делаю это с помощью события onclick, но есть ли способ изменить событие AttributeChanged в родительском компоненте?

1 Ответ

1 голос
/ 08 марта 2020

Здесь необходимо правильно установить двухстороннюю привязку данных между компонентами.

IncrementButton.razor

<button type="button" @onclick="OnAttributeChanged">@displayText</button>

@code {
[Parameter]
public int Increment { get; set; } = 1;

[Parameter]
public int Attribute { get; set; }

[Parameter]
public EventCallback<int> AttributeChanged { get; set; }


string displayText;

protected override void OnParametersSet()
{
    displayText = Increment > 0 ? "+" + Increment.ToString() : 
                                                Increment.ToString();
}

private Task OnAttributeChanged()
{
    Attribute += Increment;
    return AttributeChanged.InvokeAsync(Attribute);
}
}

Использование

<IncrementButton Increment="-1" @bind-Attribute="Amount"/>
<span>@Amount</span>
<IncrementButton Increment="1" @bind-Attribute="Amount"/>
<span>@bottomPillAmount</span>

@code {
  private int amount;
  [Parameter] public int Amount
  {
    get => amount;
    set {
        if(amount != value)
        {
            amount = value;
            UpdateBottomPillText();

        }
    }
 }

 string bottomPillAmount;

string UpdateBottomPillText()
{
    double amountAdded = Math.Floor(((double)(Amount - 10.0)) / 2.0);

    bottomPillAmount = amountAdded > 0 ? "+" + amountAdded.ToString() : amountAdded.ToString();

    return Amount.ToString();
}

protected override void OnParametersSet()
{
    UpdateBottomPillText();

}

}
...