-
Notifications
You must be signed in to change notification settings - Fork 9.8k
/
InputCheckbox.cs
39 lines (35 loc) · 1.79 KB
/
InputCheckbox.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.AspNetCore.Components.Rendering;
namespace Microsoft.AspNetCore.Components.Forms
{
/* This is exactly equivalent to a .razor file containing:
*
* @inherits InputBase<bool>
* <input type="checkbox" @bind="CurrentValue" id="@Id" class="@CssClass" />
*
* The only reason it's not implemented as a .razor file is that we don't presently have the ability to compile those
* files within this project. Developers building their own input components should use Razor syntax.
*/
/// <summary>
/// An input component for editing <see cref="bool"/> values.
/// </summary>
public class InputCheckbox : InputBase<bool>
{
/// <inheritdoc />
protected override void BuildRenderTree(RenderTreeBuilder builder)
{
builder.OpenElement(0, "input");
builder.AddMultipleAttributes(1, AdditionalAttributes);
builder.AddAttribute(2, "type", "checkbox");
builder.AddAttribute(3, "class", CssClass);
builder.AddAttribute(4, "checked", BindConverter.FormatValue(CurrentValue));
builder.AddAttribute(5, "onchange", EventCallback.Factory.CreateBinder<bool>(this, __value => CurrentValue = __value, CurrentValue));
builder.CloseElement();
}
/// <inheritdoc />
protected override bool TryParseValueFromString(string value, out bool result, out string validationErrorMessage)
=> throw new NotImplementedException($"This component does not parse string inputs. Bind to the '{nameof(CurrentValue)}' property, not '{nameof(CurrentValueAsString)}'.");
}
}