-
Notifications
You must be signed in to change notification settings - Fork 9.8k
/
InputCheckbox.cs
47 lines (41 loc) · 2.08 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
40
41
42
43
44
45
46
47
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics.CodeAnalysis;
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>
{
/// <summary>
/// Gets or sets the associated <see cref="ElementReference"/>.
/// <para>
/// May be <see langword="null"/> if accessed before the component is rendered.
/// </para>
/// </summary>
[DisallowNull] public ElementReference? Element { get; protected set; }
/// <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.AddElementReferenceCapture(6, __inputReference => Element = __inputReference);
builder.CloseElement();
}
/// <inheritdoc />
protected override bool TryParseValueFromString(string? value, out bool result, [NotNullWhen(false)] out string? validationErrorMessage)
=> throw new NotSupportedException($"This component does not parse string inputs. Bind to the '{nameof(CurrentValue)}' property, not '{nameof(CurrentValueAsString)}'.");
}