-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCounterOnHooksDeferredPersisted.razor
86 lines (71 loc) · 2.73 KB
/
CounterOnHooksDeferredPersisted.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
@implements IDisposable
@inherits PersistedHookedComponentBase<PersistedCounterState>
@{ var (count, setCount) = UseState(s => s.Count); }
@{ var (age, setAge) = UseState(s => s.Age); }
@{ var (gender, setGender) = UseState(s => s.Gender); }
@{ var (user, setUser) = UseState(s => s.User); }
<h1>Defer state persistance</h1>
@if (showNotification)
{
<div class="alert alert-success" role="alert">
Changes saved!
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
}
<p>Current count: @count</p>
<div class="form-group">
<button class="btn btn-primary" @onclick="() => setCount(count + 1)">+1</button>
<button class="btn btn-primary" @onclick="() => setCount(count - 1)">-1</button>
<button class="btn btn-danger" @onclick="() => setCount(0)">Reset</button>
</div>
<p>Age: @age</p>
<div class="form-group">
<button class="btn btn-primary" @onclick="() => setAge(18)">18</button>
<button class="btn btn-primary" @onclick="() => setAge(21)">21</button>
<button class="btn btn-primary" @onclick="() => setAge(50)">50</button>
</div>
<p>User: @user.FirstName @user.LastName</p>
<p>Gender: @gender</p>
<p>Address: @user.Address</p>
<div class="form-group">
<label for="firstName">First Name</label>
<input id="firstName" class="form-control" value="@user.FirstName" @onchange="s => setUser(user.WithFirstName(s.Value.ToString()))" />
</div>
<div class="form-group">
<label for="lastName">Last Name</label>
<input id="lastName" class="form-control" value="@user.LastName" @onchange="s => setUser(user.WithLastName(s.Value.ToString()))" />
</div>
<div class="form-group">
<label for="address">Address</label>
<input id="address" class="form-control" value="@user.Address" @onchange="s => setUser(user.WithAddress(s.Value.ToString()))" />
</div>
<div class="form-group">
<label for="gender">Gender</label>
<select id="gender" class="form-control" value="@gender" @onchange="a => OnGenderChange(a, setGender)">
<option value="Empty"></option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
</div>
<button class="btn btn-success" @onclick="Save">Save</button>
@code {
private bool showNotification = false;
[Parameter]
public PersistedCounterState Props { get; set; }
protected void OnGenderChange(ChangeEventArgs args, Action<string> setGender)
{
setGender(args.Value.ToString());
}
protected override void OnInitialized()
{
DeferStatePersistans();
base.OnInitialized();
}
private void Save(MouseEventArgs args)
{
Persist();
showNotification = true;
}
}