Skip to content

Commit 386117a

Browse files
committed
refactor: 重构登录 UI 支持键盘
1 parent a324fe4 commit 386117a

File tree

1 file changed

+35
-20
lines changed
  • src/BootstrapBlazor.Server/Components/Samples/Tutorials/LoginAndRegister

1 file changed

+35
-20
lines changed

src/BootstrapBlazor.Server/Components/Samples/Tutorials/LoginAndRegister/Template5.razor

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,12 @@
4949
{
5050
<h3>登录</h3>
5151
<p class="subtitle">使用你的 BootstrapBlazor 帐户。</p>
52-
<BootstrapInput type="email" class="input" placeholder="电子邮件或电话号码" @bind-Value="email"></BootstrapInput>
53-
<div class="error" hidden="@(!showEmailError)">请输入有效的电子邮件地址或电话号码</div>
54-
<Button class="button" Color="Color.Primary" OnClick="OnEmailSubmit">下一步</Button>
52+
<ValidateForm Model="_loginModel" OnValidSubmit="OnEmailSubmit">
53+
<BootstrapInput type="email" class="input" SkipValidate="true" IsAutoFocus="true"
54+
placeholder="电子邮件或电话号码" @bind-Value="@_loginModel.Email"></BootstrapInput>
55+
<div class="error" hidden="@(!showEmailError)">请输入有效的电子邮件地址或电话号码</div>
56+
<Button class="button" Color="Color.Primary" ButtonType="ButtonType.Submit" Text="下一步"></Button>
57+
</ValidateForm>
5558
<div class="links">
5659
<a href="#" @onclick:preventDefault>忘记用户名?</a>
5760
</div>
@@ -62,19 +65,22 @@
6265
else if (!isAuth)
6366
{
6467
<h3>输入你的密码</h3>
65-
<p class="email-display">@email</p>
66-
<BootstrapInput type="password" class="input" placeholder="密码" @bind-Value="password"></BootstrapInput>
67-
<div class="links">
68-
<a href="#" @onclick:preventDefault>忘记了密码?</a>
69-
</div>
70-
<Button class="button" Color="Color.Primary" OnClick="OnPasswordSubmit">下一步</Button>
68+
<p class="email-display">@_loginModel.Email</p>
69+
<ValidateForm Model="_loginModel" OnValidSubmit="OnPasswordSubmit">
70+
<BootstrapInput type="password" class="input" SkipValidate="true" IsAutoFocus="true"
71+
placeholder="密码" @bind-Value="@_loginModel.Password"></BootstrapInput>
72+
<div class="links">
73+
<a href="#" @onclick:preventDefault>忘记了密码?</a>
74+
</div>
75+
<Button class="button" Color="Color.Primary" ButtonType="ButtonType.Submit" Text="下一步"></Button>
76+
</ValidateForm>
7177
<div class="links">
7278
<a href="#" @onclick:preventDefault>其他登录方法</a>
7379
</div>
7480
}
7581
else
7682
{
77-
<div class="email-display2"><span>@email</span></div>
83+
<div class="email-display2"><span>@_loginModel.Email</span></div>
7884
<h5 class="text-center mt-3 mb-0">欢迎,您已成功登录</h5>
7985
<div class="login-video-wrap">
8086
<video class="login-video" autoplay="autoplay" playsinline="playsinline" disablepictureinpicture="">
@@ -86,7 +92,7 @@
8692
</video>
8793
</div>
8894
<p class="text-center text-muted" style="font-size: 0.75rem;">此登录高仿微软登录 UI</p>
89-
<Button class="button" Color="Color.Primary" OnClick="OnEnterSubmit">进入</Button>
95+
<Button class="button" Color="Color.Primary">进入</Button>
9096
}
9197
</div>
9298
</div>
@@ -95,13 +101,16 @@
95101
@code {
96102
private bool isEmailEntered = false;
97103
private bool isAuth = false;
98-
private string email = "[email protected]";
99-
private string password = "123456";
100104
private bool showEmailError = false;
105+
private LoginModel _loginModel = new LoginModel()
106+
{
107+
Email = "[email protected]",
108+
Password = "123456"
109+
};
101110

102-
private void OnEmailSubmit()
111+
private Task OnEmailSubmit(EditContext context)
103112
{
104-
if (string.IsNullOrWhiteSpace(email))
113+
if (string.IsNullOrWhiteSpace(_loginModel.Email))
105114
{
106115
showEmailError = true;
107116
}
@@ -110,22 +119,28 @@
110119
showEmailError = false;
111120
isEmailEntered = true;
112121
}
122+
StateHasChanged();
123+
return Task.CompletedTask;
113124
}
114125

115-
private void OnPasswordSubmit()
126+
private Task OnPasswordSubmit(EditContext context)
116127
{
117128
// 数据库检查密码逻辑可以在这里实现
118129
// 演示代码一律通过
119130
isAuth = true;
131+
StateHasChanged();
132+
return Task.CompletedTask;
120133
}
121134

122-
private void OnEnterSubmit()
135+
private void GoBack()
123136
{
124-
137+
isEmailEntered = false;
125138
}
126139

127-
private void GoBack()
140+
class LoginModel
128141
{
129-
isEmailEntered = false;
142+
public string? Email { get; set; }
143+
144+
public string? Password { get; set; }
130145
}
131146
}

0 commit comments

Comments
 (0)