-
Notifications
You must be signed in to change notification settings - Fork 0
/
Index.razor
109 lines (85 loc) · 2.62 KB
/
Index.razor
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
@page "/"
@using Microsoft.Extensions.Logging;
@inject ILogger<Index> Logger
@code {
public Date DateComponent1 { get; set; }
DateTime? _dateValue1 = DateTime.Now;
public DateTime? DateValue1
{
get => _dateValue1;
set
{
Logger.LogWarning("Set property DateValue1 to {value}", value);
_dateValue1 = value;
}
}
public Date DateComponent2 { get; set; }
DateTime? _dateValue2 = DateTime.Now;
public DateTime? DateValue2
{
get => _dateValue2;
set
{
Logger.LogWarning("Set property DateValue2 to {value}", value);
_dateValue2 = value;
}
}
protected override void OnInitialized()
{
Logger.LogWarning("Render Index ('/') page");
}
private DateTime RandomDate()
{
var rnd = new Random();
return new DateTime(rnd.Next(1900, 2100), rnd.Next(1, 12), rnd.Next(1, 27));
}
}
<h3>Description</h3>
<blockquote>
I have a problem with a Razor component resetting values when the component is wrapped in another component.
The component works fine when used stand-alone.<br />
<br />
The component is a date input control. <br />
<br />
To simulate the problem:<br />
<ol>
<li>Enter a invalid date value into input #1. Eg. the value 'a'</li>
<li>You will see the border turn red and the title-attribute will show the parse error</li>
<li>Enter the same value into input #2</li>
<li>You will see the value will reset to the original value, due to a redraw of the parent component</li>
</ol>
</blockquote>
<hr />
<h3>#1. Input</h3>
<div class="sample">
<!-- Stand alone Component -->
<Date @ref="DateComponent1"
@bind-Value="DateValue1" />
<!-- Button to test binding -->
<button class="btn btn-primary"
@onclick="()=>DateValue1=RandomDate()">
Set random date
</button>
<div class="pt-2">
<!-- Display the bound value -->
DateValue1: <span class="text-danger">@DateValue1</span>
</div>
</div>
<hr />
<h3>#2. Input with parent</h3>
<div class="sample">
<!-- Component is wrapped in a Label component -->
<Label>
<Date @ref="DateComponent2"
@bind-Value="DateValue2" />
<!-- Button to test binding -->
<button class="btn btn-primary"
@onclick="()=>DateValue2=RandomDate()">
Set random date
</button>
</Label>
<div class="pt-2">
<!-- Display the bound value -->
DateValue2: <span class="text-danger">@DateValue2</span>
</div>
</div>